@@ -244,11 +244,13 @@ where
244244/// # use std::sync::LazyLock;
245245/// # use arrow::datatypes::{DataType, Field, FieldRef};
246246/// # use datafusion_common::{DataFusionError, plan_err, Result};
247- /// # use datafusion_expr::{col, Signature, Volatility, PartitionEvaluator, WindowFrame, ExprFunctionExt, Documentation};
247+ /// # use datafusion_expr::{col, Signature, Volatility, PartitionEvaluator, WindowFrame, ExprFunctionExt, Documentation, LimitEffect };
248248/// # use datafusion_expr::{WindowUDFImpl, WindowUDF};
249249/// # use datafusion_functions_window_common::field::WindowUDFFieldArgs;
250250/// # use datafusion_functions_window_common::partition::PartitionEvaluatorArgs;
251251/// # use datafusion_expr::window_doc_sections::DOC_SECTION_ANALYTICAL;
252+ /// # use datafusion_physical_expr_common::physical_expr;
253+ /// # use std::sync::Arc;
252254///
253255/// #[derive(Debug, Clone, PartialEq, Eq, Hash)]
254256/// struct SmoothIt {
@@ -295,6 +297,9 @@ where
295297/// fn documentation(&self) -> Option<&Documentation> {
296298/// Some(get_doc())
297299/// }
300+ /// fn limit_effect(&self, _args: &[Arc<dyn physical_expr::PhysicalExpr>]) -> LimitEffect {
301+ /// LimitEffect::Unknown
302+ /// }
298303/// }
299304///
300305/// // Create a new WindowUDF from the implementation
@@ -414,6 +419,23 @@ pub trait WindowUDFImpl: Debug + DynEq + DynHash + Send + Sync {
414419 fn documentation ( & self ) -> Option < & Documentation > {
415420 None
416421 }
422+
423+ /// If not causal, returns the effect this function will have on the window
424+ fn limit_effect ( & self , _args : & [ Arc < dyn PhysicalExpr > ] ) -> LimitEffect {
425+ LimitEffect :: Unknown
426+ }
427+ }
428+
429+ /// the effect this function will have on the limit pushdown
430+ pub enum LimitEffect {
431+ /// Does not affect the limit (i.e. this is causal)
432+ None ,
433+ /// Either undeclared, or dynamic (only evaluatable at run time)
434+ Unknown ,
435+ /// Grow the limit by N rows
436+ Relative ( usize ) ,
437+ /// Limit needs to be at least N rows
438+ Absolute ( usize ) ,
417439}
418440
419441pub enum ReversedUDWF {
@@ -522,6 +544,10 @@ impl WindowUDFImpl for AliasedWindowUDFImpl {
522544 fn documentation ( & self ) -> Option < & Documentation > {
523545 self . inner . documentation ( )
524546 }
547+
548+ fn limit_effect ( & self , args : & [ Arc < dyn PhysicalExpr > ] ) -> LimitEffect {
549+ self . inner . limit_effect ( args)
550+ }
525551}
526552
527553// Window UDF doc sections for use in public documentation
@@ -557,15 +583,17 @@ pub mod window_doc_sections {
557583
558584#[ cfg( test) ]
559585mod test {
560- use crate :: { PartitionEvaluator , WindowUDF , WindowUDFImpl } ;
586+ use crate :: { LimitEffect , PartitionEvaluator , WindowUDF , WindowUDFImpl } ;
561587 use arrow:: datatypes:: { DataType , FieldRef } ;
562588 use datafusion_common:: Result ;
563589 use datafusion_expr_common:: signature:: { Signature , Volatility } ;
564590 use datafusion_functions_window_common:: field:: WindowUDFFieldArgs ;
565591 use datafusion_functions_window_common:: partition:: PartitionEvaluatorArgs ;
592+ use datafusion_physical_expr_common:: physical_expr:: PhysicalExpr ;
566593 use std:: any:: Any ;
567594 use std:: cmp:: Ordering ;
568595 use std:: hash:: { DefaultHasher , Hash , Hasher } ;
596+ use std:: sync:: Arc ;
569597
570598 #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
571599 struct AWindowUDF {
@@ -604,6 +632,10 @@ mod test {
604632 fn field ( & self , _field_args : WindowUDFFieldArgs ) -> Result < FieldRef > {
605633 unimplemented ! ( )
606634 }
635+
636+ fn limit_effect ( & self , _args : & [ Arc < dyn PhysicalExpr > ] ) -> LimitEffect {
637+ LimitEffect :: Unknown
638+ }
607639 }
608640
609641 #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
@@ -643,6 +675,10 @@ mod test {
643675 fn field ( & self , _field_args : WindowUDFFieldArgs ) -> Result < FieldRef > {
644676 unimplemented ! ( )
645677 }
678+
679+ fn limit_effect ( & self , _args : & [ Arc < dyn PhysicalExpr > ] ) -> LimitEffect {
680+ LimitEffect :: Unknown
681+ }
646682 }
647683
648684 #[ test]
0 commit comments