@@ -1687,23 +1687,54 @@ async fn simple_window_function() -> Result<()> {
16871687#[ tokio:: test]
16881688async fn stacked_windows_with_same_default_name_via_builder ( ) -> Result < ( ) > {
16891689 // Substrait projections are positional and drop DataFusion's aliases, so the
1690- // inherited `rn1` column and the new window both come back under the default
1691- // name `row_number() ROWS BETWEEN ...` . The consumer must keep them apart.
1690+ // inherited `rn1` column and the new window both come back under the same
1691+ // default name . The consumer must keep them apart.
16921692 // See https://github.com/apache/datafusion/issues/23007
1693+ use datafusion:: arrow:: util:: pretty:: pretty_format_batches;
16931694 use datafusion:: functions_window:: expr_fn:: row_number;
1695+ use datafusion:: logical_expr:: ExprFunctionExt ;
16941696
16951697 let ctx = create_context ( ) . await ?;
16961698 let scan = ctx. table ( "data" ) . await ?. into_optimized_plan ( ) ?;
1699+ let row_number_by_a = || {
1700+ row_number ( )
1701+ . order_by ( vec ! [ col( "a" ) . sort( true , false ) ] )
1702+ . build ( )
1703+ } ;
1704+ // The filter between the two windows makes `rn1` and `rn2` differ for the
1705+ // surviving row. A consumer that resolved `rn2` to the inherited window
1706+ // column would then return wrong values instead of passing unnoticed.
16971707 let plan = LogicalPlanBuilder :: from ( scan)
1698- . window ( vec ! [ row_number( ) . alias( "rn1" ) ] ) ?
1699- . window ( vec ! [ row_number( ) . alias( "rn2" ) ] ) ?
1708+ . window ( vec ! [ row_number_by_a( ) ?. alias( "rn1" ) ] ) ?
1709+ . filter ( col ( "a" ) . gt ( lit ( 1i64 ) ) ) ?
1710+ . window ( vec ! [ row_number_by_a( ) ?. alias( "rn2" ) ] ) ?
17001711 . build ( ) ?;
17011712
17021713 let plan2 = substrait_roundtrip ( & plan, & ctx) . await ?;
17031714 // Compare output fields; qualifiers and functional dependencies
17041715 // can differ after a Substrait round trip.
17051716 assert_eq ! ( plan. schema( ) . as_arrow( ) , plan2. schema( ) . as_arrow( ) ) ;
1706- DataFrame :: new ( ctx. state ( ) , plan2) . show ( ) . await ?;
1717+
1718+ let expected = DataFrame :: new ( ctx. state ( ) , plan)
1719+ . select_columns ( & [ "a" , "rn1" , "rn2" ] ) ?
1720+ . collect ( )
1721+ . await ?;
1722+ let actual = DataFrame :: new ( ctx. state ( ) , plan2)
1723+ . select_columns ( & [ "a" , "rn1" , "rn2" ] ) ?
1724+ . collect ( )
1725+ . await ?;
1726+ let actual = pretty_format_batches ( & actual) ?. to_string ( ) ;
1727+ assert_eq ! ( pretty_format_batches( & expected) ?. to_string( ) , actual) ;
1728+ assert_snapshot ! (
1729+ actual,
1730+ @r"
1731+ +---+-----+-----+
1732+ | a | rn1 | rn2 |
1733+ +---+-----+-----+
1734+ | 3 | 2 | 1 |
1735+ +---+-----+-----+
1736+ "
1737+ ) ;
17071738 Ok ( ( ) )
17081739}
17091740
0 commit comments