Repository navigation
feat: push window predicates on expression PARTITION BY keys #25865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alamb
merged 8 commits into
apache:main
from
zhuqi-lucas:window-expr-partition-key-pushdown
Oct 9, 2026
+356
−51
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c6be450
fix: keep a volatile predicate above a window function
zhuqi-lucas 4dc3f4c
feat: push window predicates on expression PARTITION BY keys
zhuqi-lucas 5acfd50
test: cover expression partition keys in window filter pushdown
zhuqi-lucas 3f6a0ec
Merge branch 'main' into window-expr-partition-key-pushdown
zhuqi-lucas eb77997
fix: keep a subquery predicate above the window
zhuqi-lucas adb0705
test: cover window filter pushdown over an expression key end to end
zhuqi-lucas 8a2066f
Merge branch 'main' into window-expr-partition-key-pushdown
zhuqi-lucas 2909958
Address review: keys by reference, comments with examples, coverage i…
zhuqi-lucas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1135,8 +1135,8 @@ impl OptimizerRule for PushDownFilter { | |
| // multiple window functions, each with potentially different partition keys. | ||
| // Therefore, we need to ensure that any potential partition key returned is used in | ||
| // ALL window functions. Otherwise, filters cannot be pushed by through that column. | ||
| fn extract_partition_keys(func: &WindowFunction) -> HashSet<Column> { | ||
| expr_columns(&func.params.partition_by) | ||
| fn extract_partition_keys(func: &WindowFunction) -> HashSet<&Expr> { | ||
| func.params.partition_by.iter().collect() | ||
| } | ||
|
|
||
| let potential_partition_keys = window | ||
|
|
@@ -1172,21 +1172,23 @@ impl OptimizerRule for PushDownFilter { | |
| let mut keep_predicates = vec![]; | ||
| let mut push_predicates = vec![]; | ||
| for expr in predicates { | ||
| let cols = expr.column_refs(); | ||
| if cols.iter().all(|c| potential_partition_keys.contains(c)) { | ||
| // A volatile predicate has to stay above the window: pushing it | ||
| // changes which rows the window function sees. | ||
| if !expr.is_volatile() | ||
| && reads_only_partition_keys(&expr, &potential_partition_keys)? | ||
| { | ||
| push_predicates.push(expr); | ||
| } else { | ||
| keep_predicates.push(expr); | ||
| } | ||
| } | ||
|
|
||
| // Unlike with aggregations, there are no cases where we have to replace, e.g., | ||
| // `a+b` with Column(a)+Column(b). This is because partition expressions are not | ||
| // available as standalone columns to the user. For example, while an aggregation on | ||
| // `a+b` becomes Column(a + b), in a window partition it becomes | ||
| // `func() PARTITION BY [a + b] ...`. Thus, filters on expressions always remain in | ||
| // place, so we can use `push_predicates` directly. This is consistent with other | ||
| // optimizers, such as the one used by Postgres. | ||
| // `a+b` with Column(a+b). This is because partition expressions are not available | ||
| // as standalone columns to the user: while an aggregation on `a+b` becomes | ||
| // Column(a + b), in a window partition it stays `func() PARTITION BY [a + b] ...`. | ||
| // That is why the predicate is matched against the key expressions themselves and | ||
| // can be pushed unchanged. | ||
|
|
||
| // If we have a filter to push, we push it down to the input of the aggregate | ||
| let result = if let Some(predicate) = conjunction(push_predicates) { | ||
|
|
@@ -1585,6 +1587,54 @@ fn with_filters(predicates: Vec<Expr>, plan: LogicalPlan) -> LogicalPlan { | |
| } | ||
| } | ||
|
|
||
| /// Can `expr` be evaluated below a window with these `PARTITION BY` keys? | ||
| /// | ||
| /// A predicate that reads only the partition keys is constant within each | ||
| /// partition, so filtering before the window drops whole partitions and leaves | ||
| /// the surviving rows' window values unchanged. "Reads only the keys" is checked | ||
| /// structurally: every column reference must sit inside a subtree that is equal | ||
| /// to one of the keys. Given `PARTITION BY a, b + c`: | ||
| /// | ||
| /// * `a < 5`, `b + c = 4` and `(b + c) + 1 > 10` can be pushed down | ||
| /// * `d < 5` and `b < 5` cannot (`b` on its own is not a key), and neither can | ||
| /// `c + b = 4` (the match is structural, `c + b` is not `b + c`) | ||
| /// | ||
| /// A predicate containing a subquery is never pushed: what the subquery reads is | ||
| /// not visible from the expression tree. | ||
| fn reads_only_partition_keys( | ||
| expr: &Expr, | ||
| partition_keys: &HashSet<&Expr>, | ||
| ) -> Result<bool> { | ||
| let mut reads_something_else = false; | ||
| expr.apply(|node| { | ||
| Ok(if partition_keys.contains(&node) { | ||
| // the whole key was matched, so whatever it reads is accounted for | ||
| TreeNodeRecursion::Jump | ||
| } else if reads_beyond_this_node(node) { | ||
| reads_something_else = true; | ||
| TreeNodeRecursion::Stop | ||
| } else { | ||
| TreeNodeRecursion::Continue | ||
| }) | ||
| })?; | ||
| Ok(!reads_something_else) | ||
| } | ||
|
|
||
| /// Does this node read data that walking its children cannot account for? A | ||
| /// column reads itself; a subquery reads whatever its plan reads, which | ||
| /// `Expr::apply` does not visit. | ||
| fn reads_beyond_this_node(node: &Expr) -> bool { | ||
| matches!( | ||
| node, | ||
| Expr::Column(_) | ||
| | Expr::OuterReferenceColumn(..) | ||
| | Expr::ScalarSubquery(_) | ||
| | Expr::Exists(_) | ||
| | Expr::InSubquery(_) | ||
| | Expr::SetComparison(_) | ||
| ) | ||
| } | ||
|
|
||
| fn expr_columns(exprs: &[Expr]) -> HashSet<Column> { | ||
| exprs | ||
| .iter() | ||
|
|
@@ -1956,10 +2006,12 @@ mod tests { | |
| ) | ||
| } | ||
|
|
||
| /// verifies that filters on partition expressions are not pushed, as the single expression | ||
| /// column is not available to the user, unlike with aggregations | ||
| /// verifies that a filter on an expression partition key is pushed; the | ||
| /// remaining shapes (mixed keys, operand order, subqueries, volatile | ||
| /// predicates, several windows) are covered in | ||
| /// `push_down_filter_regression.slt` | ||
| #[test] | ||
| fn filter_expression_keep_window() -> Result<()> { | ||
| fn filter_expression_move_window() -> Result<()> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the filter is moved! |
||
| let table_scan = test_table_scan()?; | ||
|
|
||
| let window = Expr::from(WindowFunction::new( | ||
|
|
@@ -1975,49 +2027,14 @@ mod tests { | |
|
|
||
| let plan = LogicalPlanBuilder::from(table_scan) | ||
| .window(vec![window])? | ||
| // unlike with aggregations, single partition column "test.a + test.b" is not available | ||
| // to the plan, so we use multiple columns when filtering | ||
| .filter(add(col("a"), col("b")).gt(lit(10i64)))? | ||
| .build()?; | ||
|
|
||
| assert_optimized_plan_equal!( | ||
| plan, | ||
| @r" | ||
| Filter: test.a + test.b > Int64(10) | ||
| WindowAggr: windowExpr=[[rank() PARTITION BY [test.a + test.b] ORDER BY [test.c ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]] | ||
| TableScan: test | ||
| " | ||
| ) | ||
| } | ||
|
|
||
| /// verifies that filters are not pushed on order by columns (that are not used in partitioning) | ||
| #[test] | ||
| fn filter_order_keep_window() -> Result<()> { | ||
| let table_scan = test_table_scan()?; | ||
|
|
||
| let window = Expr::from(WindowFunction::new( | ||
| WindowFunctionDefinition::WindowUDF( | ||
| datafusion_functions_window::rank::rank_udwf(), | ||
| ), | ||
| vec![], | ||
| )) | ||
| .partition_by(vec![col("a")]) | ||
| .order_by(vec![col("c").sort(true, true)]) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let plan = LogicalPlanBuilder::from(table_scan) | ||
| .window(vec![window])? | ||
| .filter(col("c").gt(lit(10i64)))? | ||
| .filter(add(col("a"), col("b")).gt(lit(10i64)))? // a + b > 10 | ||
| .build()?; | ||
| assert_plan_not_transformed!(plan.clone()); | ||
|
|
||
| assert_optimized_plan_equal!( | ||
| plan, | ||
| @r" | ||
| Filter: test.c > Int64(10) | ||
| WindowAggr: windowExpr=[[rank() PARTITION BY [test.a] ORDER BY [test.c ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]] | ||
| TableScan: test | ||
| WindowAggr: windowExpr=[[rank() PARTITION BY [test.a + test.b] ORDER BY [test.c ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]] | ||
| TableScan: test, full_filters=[test.a + test.b > Int64(10)] | ||
| " | ||
| ) | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍