@@ -29,6 +29,7 @@ use crate::utils::expr_to_columns;
2929use crate :: Volatility ;
3030use crate :: { udaf, ExprSchemable , Operator , Signature , WindowFrame , WindowUDF } ;
3131
32+ use crate :: function:: WindowFunctionSimplification ;
3233use arrow:: datatypes:: { DataType , FieldRef } ;
3334use datafusion_common:: cse:: { HashNode , NormalizeEq , Normalizeable } ;
3435use datafusion_common:: tree_node:: {
@@ -297,7 +298,7 @@ pub enum Expr {
297298 /// [`ExprFunctionExt`]: crate::expr_fn::ExprFunctionExt
298299 AggregateFunction ( AggregateFunction ) ,
299300 /// Represents the call of a window function with arguments.
300- WindowFunction ( WindowFunction ) ,
301+ WindowFunction ( Box < WindowFunction > ) , // Boxed as it is large (272 bytes)
301302 /// Returns whether the list contains the expr value.
302303 InList ( InList ) ,
303304 /// EXISTS subquery
@@ -341,6 +342,13 @@ impl From<Column> for Expr {
341342 }
342343}
343344
345+ /// Create an [`Expr`] from a [`WindowFunction`]
346+ impl From < WindowFunction > for Expr {
347+ fn from ( value : WindowFunction ) -> Self {
348+ Expr :: WindowFunction ( Box :: new ( value) )
349+ }
350+ }
351+
344352/// Create an [`Expr`] from an optional qualifier and a [`FieldRef`]. This is
345353/// useful for creating [`Expr`] from a [`DFSchema`].
346354///
@@ -774,6 +782,16 @@ impl WindowFunctionDefinition {
774782 WindowFunctionDefinition :: AggregateUDF ( fun) => fun. name ( ) ,
775783 }
776784 }
785+
786+ /// Return the the inner window simplification function, if any
787+ ///
788+ /// See [`WindowFunctionSimplification`] for more information
789+ pub fn simplify ( & self ) -> Option < WindowFunctionSimplification > {
790+ match self {
791+ WindowFunctionDefinition :: AggregateUDF ( _) => None ,
792+ WindowFunctionDefinition :: WindowUDF ( udwf) => udwf. simplify ( ) ,
793+ }
794+ }
777795}
778796
779797impl Display for WindowFunctionDefinition {
@@ -838,6 +856,23 @@ impl WindowFunction {
838856 null_treatment : None ,
839857 }
840858 }
859+
860+ /// return the partition by expressions
861+ pub fn partition_by ( & self ) -> & Vec < Expr > {
862+ & self . partition_by
863+ }
864+
865+ /// return the order by expressions
866+ pub fn order_by ( & self ) -> & Vec < Sort > {
867+ & self . order_by
868+ }
869+
870+ /// Return the the inner window simplification function, if any
871+ ///
872+ /// See [`WindowFunctionSimplification`] for more information
873+ pub fn simplify ( & self ) -> Option < WindowFunctionSimplification > {
874+ self . fun . simplify ( )
875+ }
841876}
842877
843878/// EXISTS expression
@@ -1893,24 +1928,24 @@ impl NormalizeEq for Expr {
18931928 _ => false ,
18941929 }
18951930 }
1896- (
1897- Expr :: WindowFunction ( WindowFunction {
1931+ ( Expr :: WindowFunction ( left ) , Expr :: WindowFunction ( right ) ) => {
1932+ let WindowFunction {
18981933 fun : self_fun,
18991934 args : self_args,
19001935 partition_by : self_partition_by,
19011936 order_by : self_order_by,
19021937 window_frame : self_window_frame,
19031938 null_treatment : self_null_treatment,
1904- } ) ,
1905- Expr :: WindowFunction ( WindowFunction {
1939+ } = left . as_ref ( ) ;
1940+ let WindowFunction {
19061941 fun : other_fun,
19071942 args : other_args,
19081943 partition_by : other_partition_by,
19091944 order_by : other_order_by,
19101945 window_frame : other_window_frame,
19111946 null_treatment : other_null_treatment,
1912- } ) ,
1913- ) => {
1947+ } = right . as_ref ( ) ;
1948+
19141949 self_fun. name ( ) == other_fun. name ( )
19151950 && self_window_frame == other_window_frame
19161951 && self_null_treatment == other_null_treatment
@@ -2150,14 +2185,15 @@ impl HashNode for Expr {
21502185 distinct. hash ( state) ;
21512186 null_treatment. hash ( state) ;
21522187 }
2153- Expr :: WindowFunction ( WindowFunction {
2154- fun,
2155- args : _args,
2156- partition_by : _partition_by,
2157- order_by : _order_by,
2158- window_frame,
2159- null_treatment,
2160- } ) => {
2188+ Expr :: WindowFunction ( window_func) => {
2189+ let WindowFunction {
2190+ fun,
2191+ args : _args,
2192+ partition_by : _partition_by,
2193+ order_by : _order_by,
2194+ window_frame,
2195+ null_treatment,
2196+ } = window_func. as_ref ( ) ;
21612197 fun. hash ( state) ;
21622198 window_frame. hash ( state) ;
21632199 null_treatment. hash ( state) ;
@@ -2458,14 +2494,15 @@ impl Display for SchemaDisplay<'_> {
24582494
24592495 Ok ( ( ) )
24602496 }
2461- Expr :: WindowFunction ( WindowFunction {
2462- fun,
2463- args,
2464- partition_by,
2465- order_by,
2466- window_frame,
2467- null_treatment,
2468- } ) => {
2497+ Expr :: WindowFunction ( window_func) => {
2498+ let WindowFunction {
2499+ fun,
2500+ args,
2501+ partition_by,
2502+ order_by,
2503+ window_frame,
2504+ null_treatment,
2505+ } = window_func. as_ref ( ) ;
24692506 write ! (
24702507 f,
24712508 "{}({})" ,
@@ -2612,14 +2649,16 @@ impl Display for Expr {
26122649 // Expr::ScalarFunction(ScalarFunction { func, args }) => {
26132650 // write!(f, "{}", func.display_name(args).unwrap())
26142651 // }
2615- Expr :: WindowFunction ( WindowFunction {
2616- fun,
2617- args,
2618- partition_by,
2619- order_by,
2620- window_frame,
2621- null_treatment,
2622- } ) => {
2652+ Expr :: WindowFunction ( window_func) => {
2653+ let WindowFunction {
2654+ fun,
2655+ args,
2656+ partition_by,
2657+ order_by,
2658+ window_frame,
2659+ null_treatment,
2660+ } = window_func. as_ref ( ) ;
2661+
26232662 fmt_function ( f, & fun. to_string ( ) , false , args, true ) ?;
26242663
26252664 if let Some ( nt) = null_treatment {
@@ -3076,6 +3115,10 @@ mod test {
30763115 // If this test fails when you change `Expr`, please try
30773116 // `Box`ing the fields to make `Expr` smaller
30783117 // See https://github.com/apache/datafusion/issues/14256 for details
3079- assert_eq ! ( size_of:: <Expr >( ) , 272 ) ;
3118+ assert_eq ! ( size_of:: <Expr >( ) , 112 ) ;
3119+ assert_eq ! ( size_of:: <ScalarValue >( ) , 64 ) ;
3120+ assert_eq ! ( size_of:: <DataType >( ) , 24 ) ; // 3 ptrs
3121+ assert_eq ! ( size_of:: <Vec <Expr >>( ) , 24 ) ;
3122+ assert_eq ! ( size_of:: <Arc <Expr >>( ) , 8 ) ;
30803123 }
30813124}
0 commit comments