2020use std:: sync:: Arc ;
2121
2222use arrow:: datatypes:: { DataType , Field , Schema } ;
23- use datafusion_common:: config:: ConfigOptions ;
2423use datafusion_common:: Result ;
2524use datafusion_common:: ScalarValue ;
25+ use datafusion_common:: config:: ConfigOptions ;
26+ use datafusion_expr:: Operator ;
2627use datafusion_expr:: { WindowFrame , WindowFrameBound , WindowFrameUnits } ;
2728use datafusion_functions_window:: row_number:: row_number_udwf;
2829use datafusion_physical_expr:: expressions:: { BinaryExpr , Column , col, lit} ;
2930use datafusion_physical_expr:: window:: StandardWindowExpr ;
3031use datafusion_physical_expr_common:: sort_expr:: { LexOrdering , PhysicalSortExpr } ;
31- use datafusion_physical_optimizer:: window_topn:: WindowTopN ;
3232use datafusion_physical_optimizer:: PhysicalOptimizerRule ;
33+ use datafusion_physical_optimizer:: window_topn:: WindowTopN ;
3334use datafusion_physical_plan:: displayable;
3435use datafusion_physical_plan:: filter:: FilterExec ;
3536use datafusion_physical_plan:: placeholder_row:: PlaceholderRowExec ;
3637use datafusion_physical_plan:: projection:: ProjectionExec ;
3738use datafusion_physical_plan:: sorts:: sort:: SortExec ;
3839use datafusion_physical_plan:: windows:: { BoundedWindowAggExec , create_udwf_window_expr} ;
3940use datafusion_physical_plan:: { ExecutionPlan , InputOrderMode } ;
40- use datafusion_expr:: Operator ;
4141use insta:: assert_snapshot;
4242
4343fn schema ( ) -> Arc < Schema > {
@@ -68,18 +68,17 @@ fn build_window_topn_plan(
6868 op : Operator ,
6969) -> Result < Arc < dyn ExecutionPlan > > {
7070 let s = schema ( ) ;
71- let input: Arc < dyn ExecutionPlan > =
72- Arc :: new ( PlaceholderRowExec :: new ( Arc :: clone ( & s) ) ) ;
71+ let input: Arc < dyn ExecutionPlan > = Arc :: new ( PlaceholderRowExec :: new ( Arc :: clone ( & s) ) ) ;
7372
7473 // Sort by pk ASC, val ASC
7574 let ordering = LexOrdering :: new ( vec ! [
7675 PhysicalSortExpr :: new_default( col( "pk" , & s) ?) . asc( ) ,
7776 PhysicalSortExpr :: new_default( col( "val" , & s) ?) . asc( ) ,
78- ] ) . unwrap ( ) ;
77+ ] )
78+ . unwrap ( ) ;
7979
80- let sort: Arc < dyn ExecutionPlan > = Arc :: new (
81- SortExec :: new ( ordering. clone ( ) , input) . with_preserve_partitioning ( true ) ,
82- ) ;
80+ let sort: Arc < dyn ExecutionPlan > =
81+ Arc :: new ( SortExec :: new ( ordering. clone ( ) , input) . with_preserve_partitioning ( true ) ) ;
8382
8483 // ROW_NUMBER() OVER (PARTITION BY pk ORDER BY val)
8584 let partition_by = vec ! [ col( "pk" , & s) ?] ;
@@ -123,17 +122,15 @@ fn build_window_topn_plan(
123122/// Build a plan with no partition-by: ROW_NUMBER() OVER (ORDER BY val)
124123fn build_window_topn_no_partition ( limit_value : i64 ) -> Result < Arc < dyn ExecutionPlan > > {
125124 let s = schema ( ) ;
126- let input: Arc < dyn ExecutionPlan > =
127- Arc :: new ( PlaceholderRowExec :: new ( Arc :: clone ( & s) ) ) ;
125+ let input: Arc < dyn ExecutionPlan > = Arc :: new ( PlaceholderRowExec :: new ( Arc :: clone ( & s) ) ) ;
128126
129127 // Sort by val ASC only (no partition key)
130- let ordering = LexOrdering :: new ( vec ! [
131- PhysicalSortExpr :: new_default( col( "val" , & s) ?) . asc( ) ,
132- ] ) . unwrap ( ) ;
128+ let ordering =
129+ LexOrdering :: new ( vec ! [ PhysicalSortExpr :: new_default( col( "val" , & s) ?) . asc( ) ] )
130+ . unwrap ( ) ;
133131
134- let sort: Arc < dyn ExecutionPlan > = Arc :: new (
135- SortExec :: new ( ordering. clone ( ) , input) . with_preserve_partitioning ( true ) ,
136- ) ;
132+ let sort: Arc < dyn ExecutionPlan > =
133+ Arc :: new ( SortExec :: new ( ordering. clone ( ) , input) . with_preserve_partitioning ( true ) ) ;
137134
138135 // ROW_NUMBER() OVER (ORDER BY val) — no partition by
139136 let order_by = vec ! [ PhysicalSortExpr :: new_default( col( "val" , & s) ?) . asc( ) ] ;
@@ -146,7 +143,7 @@ fn build_window_topn_no_partition(limit_value: i64) -> Result<Arc<dyn ExecutionP
146143 "row_number" . to_string ( ) ,
147144 false ,
148145 ) ?,
149- & [ ] , // empty partition_by
146+ & [ ] , // empty partition_by
150147 & order_by,
151148 Arc :: new ( WindowFrame :: new_bounds (
152149 WindowFrameUnits :: Rows ,
@@ -174,17 +171,16 @@ fn build_window_topn_no_partition(limit_value: i64) -> Result<Arc<dyn ExecutionP
174171/// Build a plan where filter is on a data column (not window output)
175172fn build_non_window_filter_plan ( ) -> Result < Arc < dyn ExecutionPlan > > {
176173 let s = schema ( ) ;
177- let input: Arc < dyn ExecutionPlan > =
178- Arc :: new ( PlaceholderRowExec :: new ( Arc :: clone ( & s) ) ) ;
174+ let input: Arc < dyn ExecutionPlan > = Arc :: new ( PlaceholderRowExec :: new ( Arc :: clone ( & s) ) ) ;
179175
180176 let ordering = LexOrdering :: new ( vec ! [
181177 PhysicalSortExpr :: new_default( col( "pk" , & s) ?) . asc( ) ,
182178 PhysicalSortExpr :: new_default( col( "val" , & s) ?) . asc( ) ,
183- ] ) . unwrap ( ) ;
179+ ] )
180+ . unwrap ( ) ;
184181
185- let sort: Arc < dyn ExecutionPlan > = Arc :: new (
186- SortExec :: new ( ordering. clone ( ) , input) . with_preserve_partitioning ( true ) ,
187- ) ;
182+ let sort: Arc < dyn ExecutionPlan > =
183+ Arc :: new ( SortExec :: new ( ordering. clone ( ) , input) . with_preserve_partitioning ( true ) ) ;
188184
189185 let partition_by = vec ! [ col( "pk" , & s) ?] ;
190186 let order_by = vec ! [ PhysicalSortExpr :: new_default( col( "val" , & s) ?) . asc( ) ] ;
@@ -257,7 +253,8 @@ fn flipped_3_gteq_rn() -> Result<()> {
257253 let ordering = LexOrdering :: new ( vec ! [
258254 PhysicalSortExpr :: new_default( col( "pk" , & s) ?) . asc( ) ,
259255 PhysicalSortExpr :: new_default( col( "val" , & s) ?) . asc( ) ,
260- ] ) . unwrap ( ) ;
256+ ] )
257+ . unwrap ( ) ;
261258
262259 let sort: Arc < dyn ExecutionPlan > = Arc :: new (
263260 SortExec :: new ( ordering. clone ( ) , input) . with_preserve_partitioning ( true ) ,
@@ -292,7 +289,8 @@ fn flipped_3_gteq_rn() -> Result<()> {
292289
293290 // Flipped: 3 >= rn (Literal GtEq Column)
294291 let rn_col = Arc :: new ( Column :: new ( "row_number" , 2 ) ) ;
295- let limit_lit = lit ( ScalarValue :: UInt64 ( Some ( 3 ) ) ) ; let predicate = Arc :: new ( BinaryExpr :: new ( limit_lit, Operator :: GtEq , rn_col) ) ;
292+ let limit_lit = lit ( ScalarValue :: UInt64 ( Some ( 3 ) ) ) ;
293+ let predicate = Arc :: new ( BinaryExpr :: new ( limit_lit, Operator :: GtEq , rn_col) ) ;
296294 let filter: Arc < dyn ExecutionPlan > =
297295 Arc :: new ( FilterExec :: try_new ( predicate, window) ?) ;
298296 filter
@@ -313,7 +311,10 @@ fn non_window_column_filter_no_change() -> Result<()> {
313311 let before = plan_str ( plan. as_ref ( ) ) ;
314312 let optimized = optimize ( plan) ?;
315313 let after = plan_str ( optimized. as_ref ( ) ) ;
316- assert_eq ! ( before, after, "Plan should not change when filter is on data column" ) ;
314+ assert_eq ! (
315+ before, after,
316+ "Plan should not change when filter is on data column"
317+ ) ;
317318 Ok ( ( ) )
318319}
319320
@@ -323,7 +324,10 @@ fn config_disabled_no_change() -> Result<()> {
323324 let before = plan_str ( plan. as_ref ( ) ) ;
324325 let optimized = optimize_disabled ( plan) ?;
325326 let after = plan_str ( optimized. as_ref ( ) ) ;
326- assert_eq ! ( before, after, "Plan should not change when config is disabled" ) ;
327+ assert_eq ! (
328+ before, after,
329+ "Plan should not change when config is disabled"
330+ ) ;
327331 Ok ( ( ) )
328332}
329333
@@ -345,17 +349,16 @@ fn no_partition_by_no_change() -> Result<()> {
345349#[ test]
346350fn with_projection_between ( ) -> Result < ( ) > {
347351 let s = schema ( ) ;
348- let input: Arc < dyn ExecutionPlan > =
349- Arc :: new ( PlaceholderRowExec :: new ( Arc :: clone ( & s) ) ) ;
352+ let input: Arc < dyn ExecutionPlan > = Arc :: new ( PlaceholderRowExec :: new ( Arc :: clone ( & s) ) ) ;
350353
351354 let ordering = LexOrdering :: new ( vec ! [
352355 PhysicalSortExpr :: new_default( col( "pk" , & s) ?) . asc( ) ,
353356 PhysicalSortExpr :: new_default( col( "val" , & s) ?) . asc( ) ,
354- ] ) . unwrap ( ) ;
357+ ] )
358+ . unwrap ( ) ;
355359
356- let sort: Arc < dyn ExecutionPlan > = Arc :: new (
357- SortExec :: new ( ordering. clone ( ) , input) . with_preserve_partitioning ( true ) ,
358- ) ;
360+ let sort: Arc < dyn ExecutionPlan > =
361+ Arc :: new ( SortExec :: new ( ordering. clone ( ) , input) . with_preserve_partitioning ( true ) ) ;
359362
360363 let partition_by = vec ! [ col( "pk" , & s) ?] ;
361364 let order_by = vec ! [ PhysicalSortExpr :: new_default( col( "val" , & s) ?) . asc( ) ] ;
@@ -418,4 +421,4 @@ fn with_projection_between() -> Result<()> {
418421 PlaceholderRowExec
419422 "# ) ;
420423 Ok ( ( ) )
421- }
424+ }
0 commit comments