fix: Enforce scalar subquery cardinality guard #27711
All Vouch review confidence checks passed
Residual risk 0.35 = Change Risk (0.59) × (1 − Review Confidence (0.41)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.59 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 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.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #27711"
general
weight: 0.35
pramodsatya
Summary
Fixes prestodb/presto#27709.
Native execution could incorrectly suppress
Scalar sub-query has returned multiple rowsfor correlated scalar subqueries when the scalar-subquery cardinality guard was combined with another filter predicate into a singleANDexpression, or when predicates were pushed below the guard allowing rows to be eliminated before the guard evaluates.Bug
TransformCorrelatedScalarSubqueryplans the scalar-subquery cardinality check as a filter directly aboveMarkDistinct. The filter predicate is a guard like:This guard must run before any later predicate is allowed to discard the row. Otherwise, a row whose correlated scalar subquery produced multiple rows can disappear without raising the required error.
Multiple optimizer passes can fold this guard together with other predicates into a single
AND:That is unsafe for native execution. Native lowers the merged filter into Velox
FilterProjectexpression evaluation. Velox applies top-level conjuncts stage-wise overSelectivityVectors:ConjunctExpr::evalSpecialFormevaluates each input overactiveRows,ConjunctExpr::updateResultremoves rows whoseANDresult is already false, andfinalizeErrorsclears errors for rows that are no longer active.Additionally,
PredicatePushDown.visitMarkDistinctcan push predicates below MarkDistinct and the join, eliminating rows before they reach the guard. This prevents the guard from ever seeing the non-distinct rows it needs to reject.For a query such as:
the comparison predicate can reject the duplicate rows before the scalar-subquery guard's
fail(...)is preserved, so native returns an empty result instead of failing.Fix
Three changes:
1.
FilterNode.doNotMergeflag: New boolean field onFilterNode. The filter created byTransformCorrelatedScalarSubquery(the cardinality guard) sets this totrue.2.
MergeFilters: Skips merging when the child filter hasdoNotMerge=true.3.
PredicatePushDown:-
visitFilter: When a FilterNode hasdoNotMerge=true, does not combine its predicate with inherited predicates. Instead, rewrites the source withTRUEand places any inherited predicates as a separate filter above.-
visitMarkDistinct(native execution only): When the non-pushable set contains the guard, does not push any predicates below MarkDistinct. Creates two separate FilterNodes above: the guard in its own filter (so Velox can't mask its error), and remaining predicates in another filter above it.The
doNotMergeflag is a generic fix (not gated on native execution) since Java can also exhibit incorrect behavior when the guard is combined with other predicates. ThevisitMarkDistinctchange is native-specific since Velox requires the guard to be in its own filter operator to prevent error masking across operator boundaries.This preserves:
instead of:
Validation
TestMergeFilters: verifies filters skip merging when child hasdoNotMerge=true, and merge normally otherwise.TestPredicatePushdown,TestSubqueries,TestTransformCorrelatedScalarSubquery: all pass.testCorrelatedScalarSubqueryMultipleRowsGuardinTestSubqueries: Java-side test verifying the error is raised.testSubqueriesinAbstractTestNativeGeneralQueries: native-side tests verifying the error is raised against the standalone Velox runner.Release Notes
Summary by Sourcery
Ensure scalar subquery multiple-rows guards are preserved correctly during native Velox expression conversion for AND predicates.
Bug Fixes:
Enhancements:
Summary by Sourcery
Prevent merging scalar subquery cardinality guard filters with other predicates when they depend on MarkDistinct marker variables, and add tests to cover the behavior in both planner rules and native execution.
Bug Fixes:
Tests:
Summary by Sourcery
Preserve scalar subquery cardinality guard behavior for native execution by preventing unsafe filter merging above MarkDistinct and adding coverage tests.
Bug Fixes:
Tests: