fix: SanityCheckPlan error with window functions and NVL filter #20231
+130
−11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Closes #20194
Rationale for this change
A query with
ROW_NUMBER() OVER (... ORDER BY CASE WHEN col='0' THEN 1 ELSE 0 END)combined with a filternvl(t2.value_2_3,'0')='0'fails with aSanityCheckPlanerror. This worked in 50.3.0 but broke in 52.1.0.What changes are included in this PR?
Root cause:
collect_columns_from_predicate_innerwas extracting equality pairs where neither side was aColumn(e.g.nvl(col, '0') = '0'), creating equivalence classes between complex expressions and literals.normalize_expr's deep traversal would then replace the literal'0'inside unrelated sort/window CASE WHEN expressions with the complex NVL expression, corrupting the sort ordering and causing a mismatch betweenSortExec's reported output ordering andBoundedWindowAggExec's expected ordering.Fix (two changes in
filter.rs):collect_columns_from_predicate_inner: Only extract equality pairs where at least one side is aColumnreference. This matches the function's documented intent ("Column-Pairs") and prevents complex-expression-to-literal equivalence classes from being created.extend_constants: RecognizeLiteralexpressions as inherently constant (previously only checkedis_expr_constanton the input's equivalence properties, which doesn't know about literals). This ensures constant propagation still works forcomplex_expr = literalpredicates — e.g.nvl(col, '0')is properly marked as constant after the filter.How was this tested?
test_collect_columns_skips_non_column_pairsverifying the filtering logicTest plan
collect_columns_from_predicate_innercolumn filtering