← Back to Pull requests

fix: Reject non-numeric string literals in SUM, AVG and other numeric aggregators #20845

Closed
unknown closed this pull request without merging on Nov 06, 2026 07:34 UTC
View on GitHub
unknown unknown commented on Nov 05, 2026 04:38 UTC
Author

Follow-up of #18010 and #20782.

Description

As discussed in #18010, SUM('abc') and AVG('abc') return null instead of failing. They declare a NUMERIC operand, so Calcite's implicit coercion casts the string literal to DECIMAL, and 'abc' becomes null:DECIMAL(38, 19) before Druid sees it. The check added in #20782 in Aggregations.toDruidExpressionForNumericAggregator never sees the literal for this reason.

Reject non-numeric string literals before the implicit cast

DruidTypeCoercion now overrides builtinFunctionCoercion. If the operator is an aggregate function, its first operand is a string literal, the operand would be cast to a numeric type and the value can't be parsed as a number (Calcites.isNumericString), it throws InvalidSqlInput with the same message as the quantile aggregators:

Function [SUM] argument [1] must be numeric, got string literal ['abc']. If you meant to refer to a column, use double quotes.

Some notes about the scope:

  • Only aggregate functions. This is the follow-up for SUM/AVG suggested in #18010. Scalar functions like ABS('abc') go through the same method, but I left them out of this PR.

  • Only string literals. A constant expression like SUM('abc' || 'x') is not checked.

  • Dynamic parameters are bound as literals before validation, so a VARCHAR parameter like SUM(?) with 'abc' is rejected the same way.

  • Only the first operand. Other aggregator operands are already validated by the aggregators themselves, for example ARRAY_AGG(dim1, 'abc') keeps its "parameter maxBytes must be a numeric literal" error (testArrayAggWithStringMaxBytes and friends).

  • Explicit casts like SUM(CAST('abc' AS DOUBLE)) are not checked, same as in #20782.

  • builtinFunctionCoercion has no throwOnFailure flag, so the error is thrown directly. For OperandTypes.or(...) checkers, Calcite first tries all rules without coercion, so a rule that accepts the string literal as is still wins (for example APPROX_COUNT_DISTINCT('abc') still returns 1). The error can only hide a later rule that needs its own coercion of the string literal. I couldn't find such an aggregator.
  • Tests

    numeric_agg_string_args.iq (added in #20782) now expects errors for SUM('abc') and AVG('abc'). I also added SUM and AVG cases for a numeric column, __time, NULL, CAST(NULL AS DOUBLE), BOOLEAN, ARRAY, a complex column and explicit CAST('abc' AS DOUBLE), plus SUM('abc') OVER (), SUM(DISTINCT 'abc'), SUM('abc') FILTER (...), SUM(''), SUM('1e3') and SUM('1.5') with OVER () and FILTER (...). For BIT_AND/BIT_OR/BIT_XOR there are 'abc', __time and CAST(NULL AS BIGINT) cases, plus BIT_OR('3') and BIT_XOR('3'). There is no positive BIT_AND literal case because BIT_AND returns 0 here even for BIT_AND(1), which doesn't go through the new check. That looks like a separate issue. APPROX_COUNT_DISTINCT('abc') and APPROX_COUNT_DISTINCT_BUILTIN('abc') are added to cover the OperandTypes.or(...) case above. Only the non-numeric literal cases have different results from master. SUM(DISTINCT 'abc') already failed on master, with the "DISTINCT is not supported when useApproximateCountDistinct is enabled" error, and now fails with the string literal error first. VarianceSqlAggregatorTest gets tests for VARIANCE, VAR_POP, VAR_SAMP, STDDEV, STDDEV_POP and STDDEV_SAMP with 'm1' (error), '1.5', a STRING column, __time and CAST(NULL AS DOUBLE). Bitmap64ExactCountSqlAggregatorTest gets a test for BITMAP64_EXACT_COUNT('abc'), and CalciteParameterQueryTest gets SUM(?) with 'abc' and '1.5'.

    Release note

    SUM, AVG, BIT_AND, BIT_OR, BIT_XOR, VARIANCE, VAR_POP, VAR_SAMP, STDDEV, STDDEV_POP and STDDEV_SAMP now fail at planning time with an invalid input error when their input is a string literal that can't be parsed as a number, such as SUM('abc') or SUM(''). Before, these queries did not fail (SUM, AVG and the BIT_ functions returned null). SUM('abc') OVER () failed with a Calcite assertion error before and now returns the same invalid input error. The same applies to a non-numeric VARCHAR dynamic parameter, such as SUM(?) with 'abc', and to extension aggregators that declare a NUMERIC first operand with Calcite's checkers, such as BITMAP64_EXACT_COUNT. Numeric string literals such as SUM('1.5'), STRING columns and explicit casts work as before.

    <hr>

    ##### Key changed/added classes in this PR
    DruidTypeCoercion
    numeric_agg_string_args.iq
    VarianceSqlAggregatorTest
    Bitmap64ExactCountSqlAggregatorTest
    CalciteParameterQueryTest

    <hr>

    This PR has:

  • been self-reviewed.

  • a release note entry in the PR description.

  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.

  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • Diff: 637 lines (+477 / -160) across 7 files Reviewer: FrankChen021 Review duration: 1615m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.50) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 637 lines diff across 7 files
    0.50
    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.386
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #20845"
    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: 50.4% (MEDIUM) · Higher = larger blast radius
    50.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: 21.8% · Safe to merge
    21.8%
    Repository