Repository navigation
fix: avoid stack overflow on deep logical filters - #7510
Merged
wjones127 merged 1 commit intoJun 30, 2026
Merged
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
wjones127
approved these changes
Jun 30, 2026
wjones127
left a comment
Contributor
There was a problem hiding this comment.
This looks like a good scoped fix. There are some things that it doesn't fix, like a AND b OR c AND d OR ..., but I think you are right now of this should be pushed into a follow up at the DataFusion level.
wjones127
deleted the
xuanwo/oss-1232-segfault-on-recursive-andor-query-processing
branch
June 30, 2026 19:54
Xuanwo
added a commit
that referenced
this pull request
Nov 11, 2026
## Summary `Planner::parse_filter` rewrites `array_any_match` lambdas before planning (#10947). Both rewrite passes use sqlparser's recursive visitors, but a filter such as `a AND b AND ...` parses as a chain as deep as its number of operands. The visitors therefore overflowed the stack in `planner::tests::test_parse_deep_logical_filter`, aborting the test process on current main (`67b2821`) and on #10716's integrated head, before #7510's iterative `flatten_logical_binary_exprs` / `balanced_binary_expr` protection could run. This change balances each `AND`/`OR` chain in the parsed SQL AST before those visitors traverse it. Balancing uses an explicit stack and rebuilds a chain only with the same operator, so `AND` and `OR` are never mixed. `binary_expr` already 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::element` using the existing `any_element` fixture: - a 1000-operand `OR` chain of real `array_any_match` calls below `NOT`, evaluated against independent expected rows; - a lambda body with 1000 `OR` predicates, covering the body clone/check/format path. Added the far-end-lambda AST regression adapted from #10953 (thanks @sarwarbhuiyan): a 1000-condition `AND`/`OR` chain with a lambda at the end is walked by its leaves with an explicit stack, checking that the last leaf is the rewritten `any_element` call 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`/`OR` `test_parse_deep_logical_filter` is 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 -- --check` - `cargo clippy --profile ci --all --tests --benches -- -D warnings` ## Context 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. --------- 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.
This fixes OSS-1232 by preventing deeply chained
AND/ORSQL filters from overflowing the Rust stack during Lance's SQL filter planning.The immediate root cause is in Lance's side of the SQL planning path. We first cloned deeply nested sqlparser AST expressions, and then recursively converted left-deep
AND/ORtrees into DataFusion expressions. A user-supplied flat logical filter could therefore abort the process before Lance returned an error.This PR is intentionally a Lance-side workaround: it moves parsed SQL expressions out of the AST instead of recursively cloning them, then flattens same-operator logical chains and rebuilds a balanced DataFusion expression tree. That protects the known vulnerable path without waiting for upstream DataFusion/sqlparser behavior to change.
A more systematic follow-up should add shared expression complexity/depth limits across SQL, Substrait, DataFusion-expression inputs, merge-insert predicates, and any later optimization / planning passes, so Lance rejects pathological expressions consistently instead of fixing individual recursive call sites one by one.