chenkovsky commented on code in PR #17367:
URL: https://github.com/apache/datafusion/pull/17367#discussion_r2366210894
##########
datafusion/sql/tests/cases/plan_to_sql.rs:
##########
@@ -2521,6 +2523,69 @@ fn
test_unparse_left_semi_join_with_table_scan_projection() -> Result<()> {
Ok(())
}
+#[test]
+fn test_unparse_window() -> Result<()> {
+ // SubqueryAlias: t
+ // Projection: t.k, t.v, rank() PARTITION BY [t.k] ORDER BY [t.v ASC NULLS
LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS r
+ // Filter: rank() PARTITION BY [t.k] ORDER BY [t.v ASC NULLS LAST]
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW = UInt64(1)
+ // WindowAggr: windowExpr=[[rank() PARTITION BY [t.k] ORDER BY [t.v
ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+ // TableScan: t projection=[k, v]
+
+ let schema = Schema::new(vec![
+ Field::new("k", DataType::Int32, false),
+ Field::new("v", DataType::Int32, false),
+ ]);
+ let window_expr = Expr::WindowFunction(Box::new(WindowFunction {
+ fun: WindowFunctionDefinition::WindowUDF(rank_udwf()),
+ params: WindowFunctionParams {
+ args: vec![],
+ partition_by: vec![col("k")],
+ order_by: vec![col("v").sort(true, true)],
+ window_frame: WindowFrame::new(None),
+ null_treatment: None,
+ distinct: false,
+ filter: None,
+ },
+ }));
+ let table_scan = table_scan(Some("test"), &schema, Some(vec![0,
1]))?.build()?;
+ let plan = LogicalPlanBuilder::window_plan(table_scan, vec![window_expr])?;
+
+ let name = plan.schema().fields().last().unwrap().name().clone();
+ let plan = LogicalPlanBuilder::from(plan)
+ .filter(col(name.clone()).eq(lit(1i64)))?
+ .project(vec![col("k"), col("v"), col(name)])?
+ .build()?;
+
+ let unparser = Unparser::new(&UnparserPostgreSqlDialect {});
+ let sql = unparser.plan_to_sql(&plan)?;
+ assert_snapshot!(
+ sql,
+ @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)"#
Review Comment:
column name is chunky. but the test is essential. because postgres and
datafusion have different default column names for window function. I added a
default alias to make sure unparsed sql can be queried in postgres.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]