You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Which issue does this PR close?
- Closes#17360.
## Rationale for this change
in LogicalPlan::Filter unparsing,
if there's a window expr, it should be converted to quailify.
postgres must has an alias for derived table. otherwise it will
complain:
```
ERROR: subquery in FROM must have an alias.
```
fixed this issue at the same time.
## What changes are included in this PR?
If window expr is found, convert filter to quailify.
## Are these changes tested?
UT
## Are there any user-facing changes?
No
---------
Co-authored-by: Jeffrey Vo <jeffrey.vo.australia@gmail.com>
let table = table_scan(Some("test"),&schema,Some(vec![0,1]))?.build()?;
2551
+
let plan = LogicalPlanBuilder::window_plan(table,vec![window_expr.clone()])?;
2552
+
2553
+
let name = plan.schema().fields().last().unwrap().name().clone();
2554
+
let plan = LogicalPlanBuilder::from(plan)
2555
+
.filter(col(name.clone()).eq(lit(1i64)))?
2556
+
.project(vec![col("k"), col("v"), col(name)])?
2557
+
.build()?;
2558
+
2559
+
let unparser = Unparser::new(&UnparserPostgreSqlDialect{});
2560
+
let sql = unparser.plan_to_sql(&plan)?;
2561
+
assert_snapshot!(
2562
+
sql,
2563
+
@r#"SELECT "test"."k", "test"."v", "rank() PARTITION BY [test.k] ORDER BY [test.v ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING" FROM (SELECT "test"."k" AS "k", "test"."v" AS "v", rank() OVER (PARTITION BY "test"."k" ORDER BY "test"."v" ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "rank() PARTITION BY [test.k] ORDER BY [test.v ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING" FROM "test") AS "test" WHERE ("rank() PARTITION BY [test.k] ORDER BY [test.v ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING" = 1)"#
2564
+
);
2565
+
2566
+
let unparser = Unparser::new(&UnparserMySqlDialect{});
2567
+
let sql = unparser.plan_to_sql(&plan)?;
2568
+
assert_snapshot!(
2569
+
sql,
2570
+
@r#"SELECT `test`.`k`, `test`.`v`, `rank() PARTITION BY [test.k] ORDER BY [test.v ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING` FROM (SELECT `test`.`k` AS `k`, `test`.`v` AS `v`, rank() OVER (PARTITION BY `test`.`k` ORDER BY `test`.`v` ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS `rank() PARTITION BY [test.k] ORDER BY [test.v ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING` FROM `test`) AS `test` WHERE (`rank() PARTITION BY [test.k] ORDER BY [test.v ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING` = 1)"#
2571
+
);
2572
+
2573
+
let unparser = Unparser::new(&SqliteDialect{});
2574
+
let sql = unparser.plan_to_sql(&plan)?;
2575
+
assert_snapshot!(
2576
+
sql,
2577
+
@r#"SELECT `test`.`k`, `test`.`v`, `rank() PARTITION BY [test.k] ORDER BY [test.v ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING` FROM (SELECT `test`.`k` AS `k`, `test`.`v` AS `v`, rank() OVER (PARTITION BY `test`.`k` ORDER BY `test`.`v` ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS `rank() PARTITION BY [test.k] ORDER BY [test.v ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING` FROM `test`) AS `test` WHERE (`rank() PARTITION BY [test.k] ORDER BY [test.v ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING` = 1)"#
2578
+
);
2579
+
2580
+
let unparser = Unparser::new(&DefaultDialect{});
2581
+
let sql = unparser.plan_to_sql(&plan)?;
2582
+
assert_snapshot!(
2583
+
sql,
2584
+
@r#"SELECT test.k, test.v, rank() OVER (PARTITION BY test.k ORDER BY test.v ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM test QUALIFY (rank() OVER (PARTITION BY test.k ORDER BY test.v ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) = 1)"#
2585
+
);
2586
+
2587
+
// without table qualifier
2588
+
let table = table_scan(Some("test"),&schema,Some(vec![0,1]))?.build()?;
let plan = LogicalPlanBuilder::window_plan(table,vec![window_expr])?;
2593
+
2594
+
let name = plan.schema().fields().last().unwrap().name().clone();
2595
+
let plan = LogicalPlanBuilder::from(plan)
2596
+
.filter(col(name.clone()).eq(lit(1i64)))?
2597
+
.project(vec![col("k"), col("v"), col(name)])?
2598
+
.build()?;
2599
+
2600
+
let unparser = Unparser::new(&UnparserPostgreSqlDialect{});
2601
+
let sql = unparser.plan_to_sql(&plan)?;
2602
+
assert_snapshot!(
2603
+
sql,
2604
+
@r#"SELECT "k", "v", "rank() PARTITION BY [k] ORDER BY [v ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING" FROM (SELECT "k" AS "k", "v" AS "v", rank() OVER (PARTITION BY "k" ORDER BY "v" ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "rank() PARTITION BY [k] ORDER BY [v ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING" FROM (SELECT "test"."k" AS "k", "test"."v" AS "v" FROM "test") AS "derived_projection") AS "__qualify_subquery" WHERE ("rank() PARTITION BY [k] ORDER BY [v ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING" = 1)"#
0 commit comments