fix: Reject non-numeric string literals in SUM, AVG and other numeric aggregators #20845
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
FrankChen021
Follow-up of #18010 and #20782.
Description
As discussed in #18010,
SUM('abc')andAVG('abc')return null instead of failing. They declare a NUMERIC operand, so Calcite's implicit coercion casts the string literal to DECIMAL, and'abc'becomesnull:DECIMAL(38, 19)before Druid sees it. The check added in #20782 inAggregations.toDruidExpressionForNumericAggregatornever sees the literal for this reason.Reject non-numeric string literals before the implicit cast
DruidTypeCoercionnow overridesbuiltinFunctionCoercion. 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 throwsInvalidSqlInputwith the same message as the quantile aggregators:Some notes about the scope:
ABS('abc')go through the same method, but I left them out of this PR.SUM('abc' || 'x')is not checked.SUM(?)with'abc'is rejected the same way.ARRAY_AGG(dim1, 'abc')keeps its "parametermaxBytesmust be a numeric literal" error (testArrayAggWithStringMaxBytesand friends).SUM(CAST('abc' AS DOUBLE))are not checked, same as in #20782.builtinFunctionCoercionhas nothrowOnFailureflag, so the error is thrown directly. ForOperandTypes.or(...)checkers, Calcite first tries all rules without coercion, so a rule that accepts the string literal as is still wins (for exampleAPPROX_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 forSUM('abc')andAVG('abc'). I also added SUM and AVG cases for a numeric column,__time,NULL,CAST(NULL AS DOUBLE), BOOLEAN, ARRAY, a complex column and explicitCAST('abc' AS DOUBLE), plusSUM('abc') OVER (),SUM(DISTINCT 'abc'),SUM('abc') FILTER (...),SUM(''),SUM('1e3')andSUM('1.5')withOVER ()andFILTER (...). ForBIT_AND/BIT_OR/BIT_XORthere are'abc',__timeandCAST(NULL AS BIGINT)cases, plusBIT_OR('3')andBIT_XOR('3'). There is no positiveBIT_ANDliteral case becauseBIT_ANDreturns 0 here even forBIT_AND(1), which doesn't go through the new check. That looks like a separate issue.APPROX_COUNT_DISTINCT('abc')andAPPROX_COUNT_DISTINCT_BUILTIN('abc')are added to cover theOperandTypes.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.VarianceSqlAggregatorTestgets tests forVARIANCE,VAR_POP,VAR_SAMP,STDDEV,STDDEV_POPandSTDDEV_SAMPwith'm1'(error),'1.5', a STRING column,__timeandCAST(NULL AS DOUBLE).Bitmap64ExactCountSqlAggregatorTestgets a test forBITMAP64_EXACT_COUNT('abc'), andCalciteParameterQueryTestgetsSUM(?)with'abc'and'1.5'.Release note
SUM,AVG,BIT_AND,BIT_OR,BIT_XOR,VARIANCE,VAR_POP,VAR_SAMP,STDDEV,STDDEV_POPandSTDDEV_SAMPnow 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 asSUM('abc')orSUM(''). Before, these queries did not fail (SUM,AVGand theBIT_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 asSUM(?)with'abc', and to extension aggregators that declare a NUMERIC first operand with Calcite's checkers, such asBITMAP64_EXACT_COUNT. Numeric string literals such asSUM('1.5'), STRING columns and explicit casts work as before.<hr>
##### Key changed/added classes in this PR
DruidTypeCoercionnumeric_agg_string_args.iqVarianceSqlAggregatorTestBitmap64ExactCountSqlAggregatorTestCalciteParameterQueryTest<hr>
This PR has: