@@ -247,7 +247,7 @@ def from_pycapsule(func: ScalarUDFExportable) -> ScalarUDF:
247247 This function will instantiate a Scalar UDF that uses a DataFusion
248248 ScalarUDF that is exported via the FFI bindings.
249249 """
250- name = str (udf .__class__ )
250+ name = str (func .__class__ )
251251 return ScalarUDF (
252252 name = name ,
253253 func = func ,
@@ -409,9 +409,9 @@ def udf4() -> Summarize:
409409 accum: The accumulator python function. Only needed when calling as a
410410 function. Skip this argument when using ``udaf`` as a decorator.
411411 If you have a Rust backed AggregateUDF within a PyCapsule, you can
412- pass this parameter and ignore the rest. They will be determined directly
413- from the underlying function. See the online documentation for more
414- information.
412+ pass this parameter and ignore the rest. They will be determined
413+ directly from the underlying function. See the online documentation
414+ for more information.
415415 input_types: The data types of the arguments to ``accum``.
416416 return_type: The data type of the return value.
417417 state_type: The data types of the intermediate accumulation.
@@ -486,7 +486,7 @@ def from_pycapsule(func: AggregateUDFExportable) -> AggregateUDF:
486486 This function will instantiate a Aggregate UDF that uses a DataFusion
487487 AggregateUDF that is exported via the FFI bindings.
488488 """
489- name = str (udf .__class__ )
489+ name = str (func .__class__ )
490490 return AggregateUDF (
491491 name = name ,
492492 accumulator = func ,
@@ -656,6 +656,12 @@ def include_rank(self) -> bool:
656656 return False
657657
658658
659+ class WindowUDFExportable (Protocol ):
660+ """Type hint for object that has __datafusion_window_udf__ PyCapsule."""
661+
662+ def __datafusion_window_udf__ (self ) -> object : ... # noqa: D105
663+
664+
659665class WindowUDF :
660666 """Class for performing window user-defined functions (UDF).
661667
@@ -676,6 +682,9 @@ def __init__(
676682 See :py:func:`udwf` for a convenience function and argument
677683 descriptions.
678684 """
685+ if hasattr (func , "__datafusion_window_udf__" ):
686+ self ._udwf = df_internal .WindowUDF .from_pycapsule (func )
687+ return
679688 self ._udwf = df_internal .WindowUDF (
680689 name , func , input_types , return_type , str (volatility )
681690 )
@@ -751,7 +760,10 @@ def biased_numbers() -> BiasedNumbers:
751760
752761 Args:
753762 func: Only needed when calling as a function. Skip this argument when
754- using ``udwf`` as a decorator.
763+ using ``udwf`` as a decorator. If you have a Rust backed WindowUDF
764+ within a PyCapsule, you can pass this parameter and ignore the rest.
765+ They will be determined directly from the underlying function. See
766+ the online documentation for more information.
755767 input_types: The data types of the arguments.
756768 return_type: The data type of the return value.
757769 volatility: See :py:class:`Volatility` for allowed values.
@@ -760,6 +772,9 @@ def biased_numbers() -> BiasedNumbers:
760772 Returns:
761773 A user-defined window function that can be used in window function calls.
762774 """
775+ if hasattr (args [0 ], "__datafusion_window_udf__" ):
776+ return WindowUDF .from_pycapsule (args [0 ])
777+
763778 if args and callable (args [0 ]):
764779 # Case 1: Used as a function, require the first parameter to be callable
765780 return WindowUDF ._create_window_udf (* args , ** kwargs )
@@ -827,6 +842,22 @@ def wrapper(*args: Any, **kwargs: Any) -> Expr:
827842
828843 return decorator
829844
845+ @staticmethod
846+ def from_pycapsule (func : WindowUDFExportable ) -> WindowUDF :
847+ """Create a Window UDF from WindowUDF PyCapsule object.
848+
849+ This function will instantiate a Window UDF that uses a DataFusion
850+ WindowUDF that is exported via the FFI bindings.
851+ """
852+ name = str (func .__class__ )
853+ return WindowUDF (
854+ name = name ,
855+ func = func ,
856+ input_types = None ,
857+ return_type = None ,
858+ volatility = None ,
859+ )
860+
830861
831862class TableFunction :
832863 """Class for performing user-defined table functions (UDTF).
0 commit comments