← Back to Pull requests

perf(sql): reduce SQL planning allocations for large literal arrays #20367

Closed
unknown closed this pull request without merging on Oct 17, 2026 13:11 UTC
View on GitHub
unknown unknown commented on Sep 17, 2026 03:05 UTC
Author

Related to #20326.

Description

Large literal arrays already contain constant Calcite values, but the existing planning path converts them into Druid expressions, formats them as text, parses that text, evaluates it, and rebuilds Calcite literals:

DruidRexExecutor.reduce()
  Expressions.toDruidExpression(...)
    ArrayConstructorOperatorConversion
      DirectOperatorConversion
        Convert each literal into a child DruidExpression
        DruidExpression.ofFunctionCall(..., "array", operands)
          Creates the text-generator callback

  druidExpression.getExpression()
    Formats "array('a','b',...)"

  Parser.parse(...)
    Builds a new Druid expression tree

  expr.eval(...)
    Produces the array value

  rexBuilder.makeLiteral(...)
    Rebuilds the Calcite array and its elements

This round trip allocates per-element expression objects, strings, parser objects, evaluated values, and replacement Calcite nodes.

This change skips that work for already-normalized string and integer literal arrays. Expressions requiring conversion or evaluation retain the existing path.

Benchmark results

| Type | Elements | Before MB/op | After MB/op | Diff MB/op | Reduction | Before ms/op | After ms/op |
|---|---:|---:|---:|---:|---:|---:|---:|
| LONG | 100,000 | 355.87 | 226.23 | 129.63 | 36.43% | 215.95 | 153.58 |
| LONG | 1,000,000 | 3535.11 | 2216.22 | 1318.89 | 37.31% | 2899.52 | 2324.95 |
| STRING | 100,000 | 1541.08 | 1231.01 | 310.06 | 20.12% | 788.72 | 580.71 |
| STRING | 1,000,000 | 15561.88 | 12307.44 | 3254.44 | 20.91% | 8106.12 | 6150.46 |

MB is decimal; allocation means total bytes allocated per planning operation, not peak or retained heap.

Both variants use Calcite 1.42.0 with only the CALCITE-7782 fix. The benchmark isolates this Druid change using identical dependencies and harness settings.

JDK 25.0.4.1; two forks; two warmup and three measurement iterations; GC profiler. Queries are planned without execution or EXPLAIN serialization. Timing results are preliminary.

Tests

Tests cover identity reuse, NULLs, integer boundaries, type mismatches, charset/collation differences, computed operands, and SQL IN/NOT IN semantics.

  • 261 tests passed with the declared Calcite dependency.

  • 186 targeted tests passed with patched Calcite. The broader patched run encounters an unrelated exact-version assertion.
  • Release note

    Reduced SQL planning allocations for large literal string and integer arrays, including arrays generated from large IN lists.

    Diff: 317 lines (+237 / -80) across 3 files Reviewer: GWphua Review duration: 43805m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.40) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 317 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.345
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #20367"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 40.4% (MEDIUM) · Higher = larger blast radius
    40.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.5% · Safe to merge
    17.5%
    Repository