Skip to content

Commit fda500a

Browse files
Avoid Aliased Window Expr Enter Unreachable Code (#14109)
1 parent 722307f commit fda500a

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

datafusion/optimizer/src/push_down_filter.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use datafusion_common::tree_node::{
2929
use datafusion_common::{
3030
internal_err, plan_err, qualified_name, Column, DFSchema, Result,
3131
};
32+
use datafusion_expr::expr::WindowFunction;
3233
use datafusion_expr::expr_rewriter::replace_col;
3334
use datafusion_expr::logical_plan::{Join, JoinType, LogicalPlan, TableScan, Union};
3435
use datafusion_expr::utils::{
@@ -1001,23 +1002,34 @@ impl OptimizerRule for PushDownFilter {
10011002
// multiple window functions, each with potentially different partition keys.
10021003
// Therefore, we need to ensure that any potential partition key returned is used in
10031004
// ALL window functions. Otherwise, filters cannot be pushed by through that column.
1005+
let extract_partition_keys = |func: &WindowFunction| {
1006+
func.partition_by
1007+
.iter()
1008+
.map(|c| Column::from_qualified_name(c.schema_name().to_string()))
1009+
.collect::<HashSet<_>>()
1010+
};
10041011
let potential_partition_keys = window
10051012
.window_expr
10061013
.iter()
10071014
.map(|e| {
1008-
if let Expr::WindowFunction(window_expression) = e {
1009-
window_expression
1010-
.partition_by
1011-
.iter()
1012-
.map(|c| {
1013-
Column::from_qualified_name(
1014-
c.schema_name().to_string(),
1015-
)
1016-
})
1017-
.collect::<HashSet<_>>()
1018-
} else {
1019-
// window functions expressions are only Expr::WindowFunction
1020-
unreachable!()
1015+
match e {
1016+
Expr::WindowFunction(window_func) => {
1017+
extract_partition_keys(window_func)
1018+
}
1019+
Expr::Alias(alias) => {
1020+
if let Expr::WindowFunction(window_func) =
1021+
alias.expr.as_ref()
1022+
{
1023+
extract_partition_keys(window_func)
1024+
} else {
1025+
// window functions expressions are only Expr::WindowFunction
1026+
unreachable!()
1027+
}
1028+
}
1029+
_ => {
1030+
// window functions expressions are only Expr::WindowFunction
1031+
unreachable!()
1032+
}
10211033
}
10221034
})
10231035
// performs the set intersection of the partition keys of all window functions,

datafusion/sqllogictest/test_files/subquery.slt

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,6 @@ select struct(1, 'b')
13061306
----
13071307
{c0: 1, c1: b}
13081308

1309-
13101309
query T
13111310
select (select struct(1, 'b')['c1']);
13121311
----
@@ -1330,7 +1329,6 @@ WHERE 1+2 = 3 AND column1 IN (SELECT struct(1, 'b')['c0']);
13301329
----
13311330
1
13321331

1333-
13341332
query I
13351333
SELECT * FROM foo
13361334
WHERE EXISTS (SELECT * FROM (values (1)) WHERE column1 = foo.x AND struct(1, 'b')['c0'] = 1);
@@ -1344,6 +1342,47 @@ WHERE 1+2 = 3 AND EXISTS (SELECT * FROM (values (1)) WHERE column1 = foo.x AND s
13441342
----
13451343
1
13461344

1347-
13481345
statement ok
13491346
drop table foo;
1347+
1348+
1349+
# Test for window alias in subquery
1350+
1351+
# Setup source table
1352+
statement ok
1353+
CREATE TABLE source_table (
1354+
column1 TEXT,
1355+
column2 TIMESTAMP,
1356+
column3 FLOAT
1357+
);
1358+
1359+
statement ok
1360+
INSERT INTO source_table VALUES
1361+
('item1', TIMESTAMP '1970-01-01 00:00:01', 50.0),
1362+
('item2', TIMESTAMP '1970-01-01 00:00:02', 30.0),
1363+
('item1', TIMESTAMP '1970-01-01 00:00:03', 25.0);
1364+
1365+
# Execute the query
1366+
query TPR
1367+
WITH SubQuery AS (
1368+
SELECT
1369+
a.column1,
1370+
a.column2 AS ts_column,
1371+
a.column3,
1372+
SUM(a.column3) OVER (
1373+
PARTITION BY a.column1
1374+
ORDER BY a.column2 RANGE BETWEEN INTERVAL '10 minutes' PRECEDING AND CURRENT ROW
1375+
) AS moving_sum
1376+
FROM source_table a
1377+
)
1378+
SELECT
1379+
column1,
1380+
ts_column,
1381+
moving_sum
1382+
FROM SubQuery
1383+
WHERE moving_sum > 60;
1384+
----
1385+
item1 1970-01-01T00:00:03 75
1386+
1387+
statement ok
1388+
drop table source_table;

0 commit comments

Comments
 (0)