Repository navigation
fix: walk AND/OR chains iteratively in the lambda filter rewrite - #10953
sarwarbhuiyan wants to merge 1 commit into
Conversation
The rewrite visited the whole parsed filter recursively, so a filter with a thousand conditions overflowed the stack (planner::tests::test_parse_deep_logical_filter). Walk AND/OR chains with an explicit stack, as the planner does, and visit each operand. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| stack.push(right.as_mut()); | ||
| stack.push(left.as_mut()); | ||
| } | ||
| operand => rewrite_operand(operand, &mut rewriter)?, |
There was a problem hiding this comment.
Long chains inside parentheses still overflow the stack: Nested reaches rewrite_operand, which recursively visits the entire chain. On this head, (chain), NOT (chain), and coalesce((chain), false) each aborted with 1,000 true conditions, while the unwrapped AND/OR versions passed. This is unchanged from the base revision, so no change is requested here. #10952 separately targets these remaining cases by balancing chains before the recursive visitors.
Reproducer
Add this integration test as rust/lance-datafusion/tests/deep_filter.rs and run cargo test -p lance-datafusion --test deep_filter parenthesized -- --exact:
use std::sync::Arc;
use arrow_schema::Schema;
use lance_datafusion::planner::Planner;
#[test]
fn parenthesized() {
let planner = Planner::new(Arc::new(Schema::empty()));
let chain = std::iter::repeat_n("true", 1000)
.collect::<Vec<_>>()
.join(" AND ");
planner.parse_filter(&format!("({chain})")).unwrap();
}Expected: parsing succeeds. Observed: stack overflow and process abort (exit 134). The executed experiment also optimized the result and covered the other wrappers above.
|
Closing in favor of #10952, which fixes the same regression and also covers parenthesized chains, |
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]>
## 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]>
#10947 merged while its CI was still running. Its lambda rewrite visits the whole parsed filter recursively, so a filter with a thousand conditions overflows the stack. That fails
planner::tests::test_parse_deep_logical_filteron main (linux-buildandlinux-armon #10947):This walks AND/OR chains with an explicit stack, the way the planner already does (#7510), and visits each operand recursively as before.
Tests
test_rewrite_deep_logical_filter(AND and OR): a lambda at the end of a 1,000-condition chain is still rewritten, with no overflow.lance-datafusionlib tests pass, includingtest_parse_deep_logical_filter.Lint:
cargo fmt --all, andcargo clippy --no-deps -p lance-datafusion --tests -- -D warnings.🤖 Generated with Claude Code