Skip to content

Apache DataFusion SQL Query Engine

feat(optimizer): Enable filter pushdown on window functions (#14026)

nuno-fariaad5a04f47f67
diff --git a/datafusion/optimizer/Cargo.toml b/datafusion/optimizer/Cargo.toml
index ba0dedc576..3f5ec9b0da 100644
--- a/datafusion/optimizer/Cargo.toml
+++ b/datafusion/optimizer/Cargo.toml
@@ -55,6 +55,7 @@ regex-syntax = "0.8.0"
async-trait = { workspace = true }
ctor = { workspace = true }
datafusion-functions-aggregate = { workspace = true }
+datafusion-functions-window = { workspace = true }
datafusion-functions-window-common = { workspace = true }
datafusion-sql = { workspace = true }
env_logger = { workspace = true }
diff --git a/datafusion/optimizer/src/push_down_filter.rs b/datafusion/optimizer/src/push_down_filter.rs
index 0b328ad39f..534209c4e1 100644
--- a/datafusion/optimizer/src/push_down_filter.rs
+++ b/datafusion/optimizer/src/push_down_filter.rs
@@ -985,6 +985,87 @@ impl OptimizerRule for PushDownFilter {
}
})
}
+ // Tries to push filters based on the partition key(s) of the window function(s) used.
+ // Example:
+ // Before:
+ // Filter: (a > 1) and (b > 1) and (c > 1)
+ // Window: func() PARTITION BY [a] ...
+ // ---
+ // After:
+ // Filter: (b > 1) and (c > 1)
+ // Window: func() PARTITION BY [a] ...
+ // Filter: (a > 1)
+ LogicalPlan::Window(window) => {
+ // Retrieve the set of potential partition keys where we can push filters by.
+ // Unlike aggregations, where there is only one statement per SELECT, there can be
+ // 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.
+ let potential_partition_keys = window
+ .window_expr
+ .iter()
+ .map(|e| {
+ if let Expr::WindowFunction(window_expression) = e {
+ window_expression
+ .partition_by
+ .iter()
+ .map(|c| {
+ Column::from_qualified_name(
+ c.schema_name().to_string(),
+ )
+ })
+ .collect::<HashSet<_>>()
+ } else {
+ // window functions expressions are only Expr::WindowFunction
+ unreachable!()
+ }
+ })
+ // performs the set intersection of the partition keys of all window functions,
+ // returning only the common ones
+ .reduce(|a, b| &a & &b)
+ .unwrap_or_default();
+
+ let predicates = split_conjunction_owned(filter.predicate);
+ 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)) {
+ 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.
+
+ let window_input = Arc::clone(&window.input);
+ Transformed::yes(LogicalPlan::Window(window))
+ .transform_data(|new_plan| {
+ // If we have a filter to push, we push it down to the input of the window
+ if let Some(predicate) = conjunction(push_predicates) {
+ let new_filter = make_filter(predicate, window_input)?;
+ insert_below(new_plan, new_filter)
+ } else {
+ Ok(Transformed::no(new_plan))
+ }
+ })?
+ .map_data(|child_plan| {
+ // if there are any remaining predicates we can't push, add them
+ // back as a filter
+ if let Some(predicate) = conjunction(keep_predicates) {
+ make_filter(predicate, Arc::new(child_plan))
+ } else {
+ Ok(child_plan)
+ }
+ })
+ }
LogicalPlan::Join(join) => push_down_join(join, Some(&filter.predicate)),
LogicalPlan::TableScan(scan) => {
let filter_predicates = split_conjunction(&filter.predicate);
@@ -1289,12 +1370,12 @@ mod tests {
use async_trait::async_trait;
use datafusion_common::{DFSchemaRef, ScalarValue};
- use datafusion_expr::expr::ScalarFunction;
+ use datafusion_expr::expr::{ScalarFunction, WindowFunction};
use datafusion_expr::logical_plan::table_scan;
use datafusion_expr::{
- col, in_list, in_subquery, lit, ColumnarValue, Extension, LogicalPlanBuilder,
- ScalarUDF, ScalarUDFImpl, Signature, TableSource, TableType,
- UserDefinedLogicalNodeCore, Volatility,
+ col, in_list, in_subquery, lit, ColumnarValue, ExprFunctionExt, Extension,
+ LogicalPlanBuilder, ScalarUDF, ScalarUDFImpl, Signature, TableSource, TableType,
+ UserDefinedLogicalNodeCore, Volatility, WindowFunctionDefinition,
};
use crate::optimizer::Optimizer;
@@ -1442,6 +1523,227 @@ mod tests {
assert_optimized_plan_eq(plan, expected)
}
+ /// verifies that when partitioning by 'a' and 'b', and filtering by 'b', 'b' is pushed
+ #[test]
+ fn filter_move_window() -> Result<()> {
+ let table_scan = test_table_scan()?;
+
+ let window = Expr::WindowFunction(WindowFunction::new(
+ WindowFunctionDefinition::WindowUDF(
+ datafusion_functions_window::rank::rank_udwf(),
+ ),
+ vec![],
+ ))
+ .partition_by(vec![col("a"), col("b")])
+ .order_by(vec![col("c").sort(true, true)])
+ .build()
+ .unwrap();
+
+ let plan = LogicalPlanBuilder::from(table_scan)
+ .window(vec![window])?
+ .filter(col("b").gt(lit(10i64)))?
+ .build()?;
+
+ let expected = "\
+ WindowAggr: windowExpr=[[rank() PARTITION BY [test.a, test.b] ORDER BY [test.c ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]\
+ \n TableScan: test, full_filters=[test.b > Int64(10)]";
+ assert_optimized_plan_eq(plan, expected)
+ }
+
+ /// verifies that when partitioning by 'a' and 'b', and filtering by 'a' and 'b', both 'a' and
+ /// 'b' are pushed
+ #[test]
+ fn filter_move_complex_window() -> Result<()> {
+ let table_scan = test_table_scan()?;
+
+ let window = Expr::WindowFunction(WindowFunction::new(
+ WindowFunctionDefinition::WindowUDF(
+ datafusion_functions_window::rank::rank_udwf(),
+ ),
+ vec![],
+ ))
+ .partition_by(vec![col("a"), col("b")])
+ .order_by(vec![col("c").sort(true, true)])
+ .build()
+ .unwrap();
+
+ let plan = LogicalPlanBuilder::from(table_scan)
+ .window(vec![window])?
+ .filter(and(col("a").gt(lit(10i64)), col("b").eq(lit(1i64))))?
+ .build()?;
+
+ let expected = "\
+ WindowAggr: windowExpr=[[rank() PARTITION BY [test.a, test.b] ORDER BY [test.c ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]\
+ \n TableScan: test, full_filters=[test.a > Int64(10), test.b = Int64(1)]";
+ assert_optimized_plan_eq(plan, expected)
+ }
+
+ /// verifies that when partitioning by 'a' and filtering by 'a' and 'b', only 'a' is pushed
+ #[test]
+ fn filter_move_partial_window() -> Result<()> {
+ let table_scan = test_table_scan()?;
+
+ let window = Expr::WindowFunction(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(and(col("a").gt(lit(10i64)), col("b").eq(lit(1i64))))?
+ .build()?;
+
+ let expected = "\
+ Filter: test.b = Int64(1)\
+ \n WindowAggr: windowExpr=[[rank() PARTITION BY [test.a] ORDER BY [test.c ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]\
+ \n TableScan: test, full_filters=[test.a > Int64(10)]";
+ assert_optimized_plan_eq(plan, expected)
+ }
+
+ /// verifies that filters on partition expressions are not pushed, as the single expression
+ /// column is not available to the user, unlike with aggregations
+ #[test]
+ fn filter_expression_keep_window() -> Result<()> {
+ let table_scan = test_table_scan()?;
+
+ let window = Expr::WindowFunction(WindowFunction::new(
+ WindowFunctionDefinition::WindowUDF(
+ datafusion_functions_window::rank::rank_udwf(),
+ ),
+ vec![],
+ ))
+ .partition_by(vec![add(col("a"), col("b"))]) // PARTITION BY a + b
+ .order_by(vec![col("c").sort(true, true)])
+ .build()
+ .unwrap();
+
+ 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()?;
+
+ let expected = "\
+ Filter: test.a + test.b > Int64(10)\
+ \n WindowAggr: windowExpr=[[rank() PARTITION BY [test.a + test.b] ORDER BY [test.c ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]\
+ \n TableScan: test";
+ assert_optimized_plan_eq(plan, expected)
+ }
+
+ /// 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::WindowFunction(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)))?
+ .build()?;
+
+ let expected = "\
+ Filter: test.c > Int64(10)\
+ \n WindowAggr: windowExpr=[[rank() PARTITION BY [test.a] ORDER BY [test.c ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]\
+ \n TableScan: test";
+ assert_optimized_plan_eq(plan, expected)
+ }
+
+ /// verifies that when we use multiple window functions with a common partition key, the filter
+ /// on that key is pushed
+ #[test]
+ fn filter_multiple_windows_common_partitions() -> Result<()> {
+ let table_scan = test_table_scan()?;
+
+ let window1 = Expr::WindowFunction(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 window2 = Expr::WindowFunction(WindowFunction::new(
+ WindowFunctionDefinition::WindowUDF(
+ datafusion_functions_window::rank::rank_udwf(),
+ ),
+ vec![],
+ ))
+ .partition_by(vec![col("b"), col("a")])
+ .order_by(vec![col("c").sort(true, true)])
+ .build()
+ .unwrap();
+
+ let plan = LogicalPlanBuilder::from(table_scan)
+ .window(vec![window1, window2])?
+ .filter(col("a").gt(lit(10i64)))? // a appears in both window functions
+ .build()?;
+
+ let expected = "\
+ WindowAggr: windowExpr=[[rank() PARTITION BY [test.a] ORDER BY [test.c ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, rank() PARTITION BY [test.b, test.a] ORDER BY [test.c ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]\
+ \n TableScan: test, full_filters=[test.a > Int64(10)]";
+ assert_optimized_plan_eq(plan, expected)
+ }
+
+ /// verifies that when we use multiple window functions with different partitions keys, the
+ /// filter cannot be pushed
+ #[test]
+ fn filter_multiple_windows_disjoint_partitions() -> Result<()> {
+ let table_scan = test_table_scan()?;
+
+ let window1 = Expr::WindowFunction(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 window2 = Expr::WindowFunction(WindowFunction::new(
+ WindowFunctionDefinition::WindowUDF(
+ datafusion_functions_window::rank::rank_udwf(),
+ ),
+ vec![],
+ ))
+ .partition_by(vec![col("b"), col("a")])
+ .order_by(vec![col("c").sort(true, true)])
+ .build()
+ .unwrap();
+
+ let plan = LogicalPlanBuilder::from(table_scan)
+ .window(vec![window1, window2])?
+ .filter(col("b").gt(lit(10i64)))? // b only appears in one window function
+ .build()?;
+
+ let expected = "\
+ Filter: test.b > Int64(10)\
+ \n WindowAggr: windowExpr=[[rank() PARTITION BY [test.a] ORDER BY [test.c ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, rank() PARTITION BY [test.b, test.a] ORDER BY [test.c ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]\
+ \n TableScan: test";
+ assert_optimized_plan_eq(plan, expected)
+ }
+
/// verifies that a filter is pushed to before a projection, the filter expression is correctly re-written
#[test]
fn alias() -> Result<()> {
diff --git a/datafusion/sqllogictest/test_files/window.slt b/datafusion/sqllogictest/test_files/window.slt
index 56f088dfd1..3c6f0f6deb 100644
--- a/datafusion/sqllogictest/test_files/window.slt
+++ b/datafusion/sqllogictest/test_files/window.slt
@@ -5127,3 +5127,296 @@ order by id;
statement ok
drop table t1;
+
+# test filters that will be pushed into windows
+statement count 0
+create table t1 (c1 int, c2 int) as values
+ (1, 1),
+ (1, 10),
+ (2, 1),
+ (2, 10),
+ (3, 1),
+ (3, 10);
+
+# base query
+query III
+select c1, c2, rank() over (partition by c1 order by c2) as rank
+from t1
+order by c1;
+----
+1 1 1
+1 10 2
+2 1 1
+2 10 2
+3 1 1
+3 10 2
+
+# push filter since it uses a partition column
+query TT
+explain select c1, c2, rank
+from (
+ select c1, c2, rank() over (partition by c1 order by c2) as rank
+ from t1
+)
+where c1 = 2 or c1 = 3
+order by c1, c2, rank;
+----
+logical_plan
+01)Sort: t1.c1 ASC NULLS LAST, t1.c2 ASC NULLS LAST, rank ASC NULLS LAST
+02)--Projection: t1.c1, t1.c2, rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS rank
+03)----WindowAggr: windowExpr=[[rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+04)------Filter: t1.c1 = Int32(2) OR t1.c1 = Int32(3)
+05)--------TableScan: t1 projection=[c1, c2]
+physical_plan
+01)SortPreservingMergeExec: [c1@0 ASC NULLS LAST, c2@1 ASC NULLS LAST, rank@2 ASC NULLS LAST]
+02)--ProjectionExec: expr=[c1@0 as c1, c2@1 as c2, rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as rank]
+03)----BoundedWindowAggExec: wdw=[rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Ok(Field { name: "rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW", data_type: UInt64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(Int32(NULL)), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]
+04)------SortExec: expr=[c1@0 ASC NULLS LAST, c2@1 ASC NULLS LAST], preserve_partitioning=[true]
+05)--------CoalesceBatchesExec: target_batch_size=1
+06)----------RepartitionExec: partitioning=Hash([c1@0], 2), input_partitions=2
+07)------------CoalesceBatchesExec: target_batch_size=1
+08)--------------FilterExec: c1@0 = 2 OR c1@0 = 3
+09)----------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+10)------------------MemoryExec: partitions=1, partition_sizes=[1]
+
+query III
+select c1, c2, rank
+from (
+ select c1, c2, rank() over (partition by c1 order by c2) as rank
+ from t1
+)
+where c1 = 2 or c1 = 3
+order by c1, c2, rank;
+----
+2 1 1
+2 10 2
+3 1 1
+3 10 2
+
+# do not push filter since it does not use a partition column
+query TT
+explain select c1, c2, rank
+from (
+ select c1, c2, rank() over (partition by c1 order by c2) as rank
+ from t1
+)
+where c2 >= 10
+order by c1, c2, rank;
+----
+logical_plan
+01)Sort: t1.c1 ASC NULLS LAST, t1.c2 ASC NULLS LAST, rank ASC NULLS LAST
+02)--Projection: t1.c1, t1.c2, rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS rank
+03)----Filter: t1.c2 >= Int32(10)
+04)------WindowAggr: windowExpr=[[rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+05)--------TableScan: t1 projection=[c1, c2]
+physical_plan
+01)SortPreservingMergeExec: [c1@0 ASC NULLS LAST, c2@1 ASC NULLS LAST, rank@2 ASC NULLS LAST]
+02)--ProjectionExec: expr=[c1@0 as c1, c2@1 as c2, rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as rank]
+03)----CoalesceBatchesExec: target_batch_size=1
+04)------FilterExec: c2@1 >= 10
+05)--------BoundedWindowAggExec: wdw=[rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Ok(Field { name: "rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW", data_type: UInt64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(Int32(NULL)), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]
+06)----------SortExec: expr=[c1@0 ASC NULLS LAST, c2@1 ASC NULLS LAST], preserve_partitioning=[true]
+07)------------CoalesceBatchesExec: target_batch_size=1
+08)--------------RepartitionExec: partitioning=Hash([c1@0], 2), input_partitions=2
+09)----------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+10)------------------MemoryExec: partitions=1, partition_sizes=[1]
+
+query III
+select c1, c2, rank
+from (
+ select c1, c2, rank() over (partition by c1 order by c2) as rank
+ from t1
+)
+where c2 >= 10
+order by c1, c2, rank;
+----
+1 10 2
+2 10 2
+3 10 2
+
+# push partial multi column filter (conjunction)
+query TT
+explain select c1, c2, rank
+from (
+ select c1, c2, rank() over (partition by c1 order by c2) as rank
+ from t1
+)
+where c1 = 1 and c2 = 10
+order by c1, c2, rank;
+----
+logical_plan
+01)Sort: t1.c1 ASC NULLS LAST, t1.c2 ASC NULLS LAST, rank ASC NULLS LAST
+02)--Projection: t1.c1, t1.c2, rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS rank
+03)----Filter: t1.c2 = Int32(10)
+04)------WindowAggr: windowExpr=[[rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+05)--------Filter: t1.c1 = Int32(1)
+06)----------TableScan: t1 projection=[c1, c2]
+physical_plan
+01)SortPreservingMergeExec: [c1@0 ASC NULLS LAST, c2@1 ASC NULLS LAST, rank@2 ASC NULLS LAST]
+02)--ProjectionExec: expr=[c1@0 as c1, c2@1 as c2, rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as rank]
+03)----CoalesceBatchesExec: target_batch_size=1
+04)------FilterExec: c2@1 = 10
+05)--------BoundedWindowAggExec: wdw=[rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Ok(Field { name: "rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW", data_type: UInt64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(Int32(NULL)), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]
+06)----------SortExec: expr=[c2@1 ASC NULLS LAST], preserve_partitioning=[true]
+07)------------CoalesceBatchesExec: target_batch_size=1
+08)--------------RepartitionExec: partitioning=Hash([c1@0], 2), input_partitions=2
+09)----------------CoalesceBatchesExec: target_batch_size=1
+10)------------------FilterExec: c1@0 = 1
+11)--------------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+12)----------------------MemoryExec: partitions=1, partition_sizes=[1]
+
+query III
+select c1, c2, rank
+from (
+ select c1, c2, rank() over (partition by c1 order by c2) as rank
+ from t1
+)
+where c1 = 1 and c2 = 10
+order by c1, c2, rank;
+----
+1 10 2
+
+# do not push multi column filter (disjunction)
+query TT
+explain select c1, c2, rank
+from (
+ select c1, c2, rank() over (partition by c1 order by c2) as rank
+ from t1
+)
+where c1 = 1 or c2 = 10
+order by c1, c2, rank;
+----
+logical_plan
+01)Sort: t1.c1 ASC NULLS LAST, t1.c2 ASC NULLS LAST, rank ASC NULLS LAST
+02)--Projection: t1.c1, t1.c2, rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS rank
+03)----Filter: t1.c1 = Int32(1) OR t1.c2 = Int32(10)
+04)------WindowAggr: windowExpr=[[rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+05)--------TableScan: t1 projection=[c1, c2]
+physical_plan
+01)SortPreservingMergeExec: [c1@0 ASC NULLS LAST, c2@1 ASC NULLS LAST, rank@2 ASC NULLS LAST]
+02)--ProjectionExec: expr=[c1@0 as c1, c2@1 as c2, rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as rank]
+03)----CoalesceBatchesExec: target_batch_size=1
+04)------FilterExec: c1@0 = 1 OR c2@1 = 10
+05)--------BoundedWindowAggExec: wdw=[rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Ok(Field { name: "rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW", data_type: UInt64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(Int32(NULL)), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]
+06)----------SortExec: expr=[c1@0 ASC NULLS LAST, c2@1 ASC NULLS LAST], preserve_partitioning=[true]
+07)------------CoalesceBatchesExec: target_batch_size=1
+08)--------------RepartitionExec: partitioning=Hash([c1@0], 2), input_partitions=2
+09)----------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+10)------------------MemoryExec: partitions=1, partition_sizes=[1]
+
+query III
+select c1, c2, rank
+from (
+ select c1, c2, rank() over (partition by c1 order by c2) as rank
+ from t1
+)
+where c1 = 1 or c2 = 10
+order by c1, c2, rank;
+----
+1 1 1
+1 10 2
+2 10 2
+3 10 2
+
+# multiple window functions with a filter in a common key -> filter will be pushed past both windows
+query TT
+explain select c1, c2, rank1, rank2
+from (
+ select c1, c2, rank() over (partition by c1 order by c2) as rank1,
+ rank() over (partition by c2, c1 order by c1) as rank2
+ from t1
+)
+where c1 > 1
+order by c1, c2, rank1, rank2;
+----
+logical_plan
+01)Sort: t1.c1 ASC NULLS LAST, t1.c2 ASC NULLS LAST, rank1 ASC NULLS LAST, rank2 ASC NULLS LAST
+02)--Projection: t1.c1, t1.c2, rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS rank1, rank() PARTITION BY [t1.c2, t1.c1] ORDER BY [t1.c1 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS rank2
+03)----WindowAggr: windowExpr=[[rank() PARTITION BY [t1.c2, t1.c1] ORDER BY [t1.c1 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+04)------WindowAggr: windowExpr=[[rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+05)--------Filter: t1.c1 > Int32(1)
+06)----------TableScan: t1 projection=[c1, c2]
+physical_plan
+01)SortPreservingMergeExec: [c1@0 ASC NULLS LAST, c2@1 ASC NULLS LAST, rank1@2 ASC NULLS LAST, rank2@3 ASC NULLS LAST]
+02)--SortExec: expr=[c1@0 ASC NULLS LAST, c2@1 ASC NULLS LAST, rank1@2 ASC NULLS LAST, rank2@3 ASC NULLS LAST], preserve_partitioning=[true]
+03)----ProjectionExec: expr=[c1@0 as c1, c2@1 as c2, rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as rank1, rank() PARTITION BY [t1.c2, t1.c1] ORDER BY [t1.c1 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@3 as rank2]
+04)------BoundedWindowAggExec: wdw=[rank() PARTITION BY [t1.c2, t1.c1] ORDER BY [t1.c1 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Ok(Field { name: "rank() PARTITION BY [t1.c2, t1.c1] ORDER BY [t1.c1 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW", data_type: UInt64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(Int32(NULL)), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]
+05)--------SortExec: expr=[c2@1 ASC NULLS LAST, c1@0 ASC NULLS LAST], preserve_partitioning=[true]
+06)----------CoalesceBatchesExec: target_batch_size=1
+07)------------RepartitionExec: partitioning=Hash([c2@1, c1@0], 2), input_partitions=2
+08)--------------BoundedWindowAggExec: wdw=[rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Ok(Field { name: "rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW", data_type: UInt64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(Int32(NULL)), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]
+09)----------------SortExec: expr=[c1@0 ASC NULLS LAST, c2@1 ASC NULLS LAST], preserve_partitioning=[true]
+10)------------------CoalesceBatchesExec: target_batch_size=1
+11)--------------------RepartitionExec: partitioning=Hash([c1@0], 2), input_partitions=2
+12)----------------------CoalesceBatchesExec: target_batch_size=1
+13)------------------------FilterExec: c1@0 > 1
+14)--------------------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+15)----------------------------MemoryExec: partitions=1, partition_sizes=[1]
+
+query IIII
+select c1, c2, rank1, rank2
+from (
+ select c1, c2, rank() over (partition by c1 order by c2) as rank1,
+ rank() over (partition by c2, c1 order by c1) as rank2
+ from t1
+)
+where c1 > 1
+order by c1, c2, rank1, rank2;
+----
+2 1 1 1
+2 10 2 1
+3 1 1 1
+3 10 2 1
+
+# multiple window functions with a filter whose key only appears in one window -> filter will be
+# pushed past only one function (in this case, since that window appears above in the plan)
+query TT
+explain select c1, c2, rank1, rank2
+from (
+ select c1, c2, rank() over (partition by c1 order by c2) as rank1,
+ rank() over (partition by c2, c1 order by c1) as rank2
+ from t1
+)
+where c2 > 1
+order by c1, c2, rank1, rank2;
+----
+logical_plan
+01)Sort: t1.c1 ASC NULLS LAST, t1.c2 ASC NULLS LAST, rank1 ASC NULLS LAST, rank2 ASC NULLS LAST
+02)--Projection: t1.c1, t1.c2, rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS rank1, rank() PARTITION BY [t1.c2, t1.c1] ORDER BY [t1.c1 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS rank2
+03)----WindowAggr: windowExpr=[[rank() PARTITION BY [t1.c2, t1.c1] ORDER BY [t1.c1 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+04)------Filter: t1.c2 > Int32(1)
+05)--------WindowAggr: windowExpr=[[rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+06)----------TableScan: t1 projection=[c1, c2]
+physical_plan
+01)SortPreservingMergeExec: [c1@0 ASC NULLS LAST, c2@1 ASC NULLS LAST, rank1@2 ASC NULLS LAST, rank2@3 ASC NULLS LAST]
+02)--SortExec: expr=[c1@0 ASC NULLS LAST, c2@1 ASC NULLS LAST, rank1@2 ASC NULLS LAST, rank2@3 ASC NULLS LAST], preserve_partitioning=[true]
+03)----ProjectionExec: expr=[c1@0 as c1, c2@1 as c2, rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as rank1, rank() PARTITION BY [t1.c2, t1.c1] ORDER BY [t1.c1 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@3 as rank2]
+04)------BoundedWindowAggExec: wdw=[rank() PARTITION BY [t1.c2, t1.c1] ORDER BY [t1.c1 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Ok(Field { name: "rank() PARTITION BY [t1.c2, t1.c1] ORDER BY [t1.c1 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW", data_type: UInt64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(Int32(NULL)), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]
+05)--------SortExec: expr=[c2@1 ASC NULLS LAST, c1@0 ASC NULLS LAST], preserve_partitioning=[true]
+06)----------CoalesceBatchesExec: target_batch_size=1
+07)------------RepartitionExec: partitioning=Hash([c2@1, c1@0], 2), input_partitions=2
+08)--------------CoalesceBatchesExec: target_batch_size=1
+09)----------------FilterExec: c2@1 > 1
+10)------------------BoundedWindowAggExec: wdw=[rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Ok(Field { name: "rank() PARTITION BY [t1.c1] ORDER BY [t1.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW", data_type: UInt64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(Int32(NULL)), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]
+11)--------------------SortExec: expr=[c1@0 ASC NULLS LAST, c2@1 ASC NULLS LAST], preserve_partitioning=[true]
+12)----------------------CoalesceBatchesExec: target_batch_size=1
+13)------------------------RepartitionExec: partitioning=Hash([c1@0], 2), input_partitions=2
+14)--------------------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+15)----------------------------MemoryExec: partitions=1, partition_sizes=[1]
+
+query IIII
+select c1, c2, rank1, rank2
+from (
+ select c1, c2, rank() over (partition by c1 order by c2) as rank1,
+ rank() over (partition by c2, c1 order by c1) as rank2
+ from t1
+)
+where c2 > 1
+order by c1, c2, rank1, rank2;
+----
+1 10 2 1
+2 10 2 1
+3 10 2 1
+
+statement ok
+drop table t1;