MCPcopy Create free account
hub / github.com/apache/datafusion / test_unparse_window

Function test_unparse_window

datafusion/sql/tests/cases/plan_to_sql.rs:2769–2850  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2767
2768#[test]
2769fn test_unparse_window() -> Result<()> {
2770 // SubqueryAlias: t
2771 // 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
2772 // Filter: rank() PARTITION BY [t.k] ORDER BY [t.v ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW = UInt64(1)
2773 // WindowAggr: windowExpr=[[rank() PARTITION BY [t.k] ORDER BY [t.v ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
2774 // TableScan: t projection=[k, v]
2775
2776 let schema = Schema::new(vec![
2777 Field::new("k", DataType::Int32, false),
2778 Field::new("v", DataType::Int32, false),
2779 ]);
2780 let window_expr = Expr::WindowFunction(Box::new(WindowFunction {
2781 fun: WindowFunctionDefinition::WindowUDF(rank_udwf()),
2782 params: WindowFunctionParams {
2783 args: vec![],
2784 partition_by: vec![col("k")],
2785 order_by: vec![col("v").sort(true, true)],
2786 window_frame: WindowFrame::new(None),
2787 null_treatment: None,
2788 distinct: false,
2789 filter: None,
2790 },
2791 }));
2792 let table = table_scan(Some("test"), &schema, Some(vec![0, 1]))?.build()?;
2793 let plan = LogicalPlanBuilder::window_plan(table, vec![window_expr.clone()])?;
2794
2795 let name = plan.schema().fields().last().unwrap().name().clone();
2796 let plan = LogicalPlanBuilder::from(plan)
2797 .filter(col(name.clone()).eq(lit(1i64)))?
2798 .project(vec![col("k"), col("v"), col(name)])?
2799 .build()?;
2800
2801 let unparser = Unparser::new(&UnparserPostgreSqlDialect {});
2802 let sql = unparser.plan_to_sql(&plan)?;
2803 assert_snapshot!(
2804 sql,
2805 @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)"#
2806 );
2807
2808 let unparser = Unparser::new(&UnparserMySqlDialect {});
2809 let sql = unparser.plan_to_sql(&plan)?;
2810 assert_snapshot!(
2811 sql,
2812 @"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)"
2813 );
2814
2815 let unparser = Unparser::new(&SqliteDialect {});
2816 let sql = unparser.plan_to_sql(&plan)?;
2817 assert_snapshot!(
2818 sql,
2819 @"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)"
2820 );
2821
2822 let unparser = Unparser::new(&DefaultDialect {});
2823 let sql = unparser.plan_to_sql(&plan)?;
2824 assert_snapshot!(
2825 sql,
2826 @"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)"

Callers

nothing calls this directly

Calls 15

newFunction · 0.85
WindowUDFClass · 0.85
table_scanFunction · 0.85
lastMethod · 0.80
plan_to_sqlMethod · 0.80
WindowFunctionClass · 0.50
colFunction · 0.50
litFunction · 0.50
buildMethod · 0.45
cloneMethod · 0.45
nameMethod · 0.45
fieldsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…