Repository navigation
fix(datafusion): balance deep filters before the lambda rewrite - #10952
Merged
Merged
Conversation
`Planner::parse_filter` rewrites lambda calls through recursive sqlparser visitors. A filter such as `a AND b AND ...` parses as a chain as deep as its number of operands, so those visitors overflowed the stack on the deep logical filter regression (#7510) before the existing iterative flattening in `binary_expr` could run. That made #10947 break accepted filters. Balance each `AND`/`OR` chain in the raw AST before the visitors traverse it. `binary_expr` rebuilds the same chains balanced after parsing, so the parsed expression and its evaluation are unchanged. The balance pass also makes a deep lambda body shallow before it is cloned, checked and printed. Add regressions with real `array_any_match` calls in a deep chain below `NOT` and with a deep lambda body, checking the evaluated results on the element fixture. They abort with a stack overflow before this change.
There was a problem hiding this comment.
Claude Code Review
This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.
Tip: disable this comment in your organization's Code Review settings.
Contributor
There was a problem hiding this comment.
✅ Gate recommendation: approve.
The pre-pass restores the deep-filter protection from #7510 before the lambda visitors run, preserving logical precedence and NULL behavior. No merge-blocking issues found.
Restore a non-logical node unchanged if the pre-pass meets it, and assign the balanced result only when one expression remains, instead of relying on `unreachable!` and `expect` in library code. The invariants remain checked with a debug assertion. Also document that `rewrite_element_lambdas` regroups each same-operator logical chain before its recursive visitors run, so callers know the AST is rebalanced and the visitor depth is bounded.
BubbleCal
approved these changes
Nov 11, 2026
Adapt #10953's far-end-lambda regression to the balanced AST: walk the top-level AND/OR chain's leaves with an explicit stack and check that the last leaf is the rewritten any_element call. Also assert that no lambda remains, so a skipped or partial rewrite fails the test. Parameterized over AND and OR with 1000 ordinary conditions ahead of the lambda. Co-authored-by: Sarwar Bhuiyan <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Planner::parse_filterrewritesarray_any_matchlambdas before planning (#10947). Both rewrite passes use sqlparser's recursive visitors, but a filter such asa AND b AND ...parses as a chain as deep as its number of operands. The visitors therefore overflowed the stack inplanner::tests::test_parse_deep_logical_filter, aborting the test process on current main (67b2821) and on #10716's integrated head, before #7510's iterativeflatten_logical_binary_exprs/balanced_binary_exprprotection could run.This change balances each
AND/ORchain in the parsed SQL AST before those visitors traverse it. Balancing uses an explicit stack and rebuilds a chain only with the same operator, soANDandORare never mixed.binary_expralready rebuilds the same chains balanced after parsing, so parsed expressions and evaluation are unchanged. The pre-pass also makes a deep lambda body shallow before it is cloned, checked and printed. The function documentation now states this regrouping and why it bounds visitor depth.Tests
Added regressions in
udf::elementusing the existingany_elementfixture:ORchain of realarray_any_matchcalls belowNOT, evaluated against independent expected rows;ORpredicates, covering the body clone/check/format path.Added the far-end-lambda AST regression adapted from #10953 (thanks @sarwarbhuiyan): a 1000-condition
AND/ORchain with a lambda at the end is walked by its leaves with an explicit stack, checking that the last leaf is the rewrittenany_elementcall and that no lambda remains. The walk does not assume the parsed left-deep shape, which this change rebalances.All three abort with SIGABRT on unfixed main and pass with this change. The existing 1000-operand
AND/ORtest_parse_deep_logical_filteris unchanged in depth and assertions.Commands run on this head:
cargo test --profile ci -p lance-datafusion --lib(381 passed)cargo test --profile ci -p lance-datafusion --all-features --lib(431 passed)cargo test --profile ci -p lance --lib -- dataset::tests::dataset_scanner::test_any_element(13 passed)cargo test --profile ci -p lance --lib -- dataset::tests::dataset_scanner::test_lambda_filter_rejects_unsupported_forms(2 passed)cargo fmt --all -- --checkcargo clippy --profile ci --all --tests --benches -- -D warningsContext
Regression introduced by #10947 over the deep-filter protection added for #7510. This PR repairs that base failure and does not close either issue. No performance measurement is claimed: the regrouping cost is only derived statically as O(n log n) per logical node.