33import operator
44from functools import reduce
55from itertools import chain
6- from typing import TYPE_CHECKING , Callable
6+ from typing import TYPE_CHECKING
77
88import duckdb
99from duckdb import CoalesceOperator , Expression
@@ -49,30 +49,6 @@ def _expr(self) -> type[DuckDBExpr]:
4949 def _lazyframe (self ) -> type [DuckDBLazyFrame ]:
5050 return DuckDBLazyFrame
5151
52- def _expr_from_elementwise (
53- self , func : Callable [[Iterable [Expression ]], Expression ], * exprs : DuckDBExpr
54- ) -> DuckDBExpr :
55- def call (df : DuckDBLazyFrame ) -> list [Expression ]:
56- cols = (col for _expr in exprs for col in _expr (df ))
57- return [func (cols )]
58-
59- def window_function (
60- df : DuckDBLazyFrame , window_inputs : DuckDBWindowInputs
61- ) -> list [Expression ]:
62- cols = (
63- col for _expr in exprs for col in _expr .window_function (df , window_inputs )
64- )
65- return [func (cols )]
66-
67- return self ._expr (
68- call = call ,
69- window_function = window_function ,
70- evaluate_output_names = combine_evaluate_output_names (* exprs ),
71- alias_output_names = combine_alias_output_names (* exprs ),
72- backend_version = self ._backend_version ,
73- version = self ._version ,
74- )
75-
7652 def concat (
7753 self , items : Iterable [DuckDBLazyFrame ], * , how : ConcatMethod
7854 ) -> DuckDBLazyFrame :
@@ -124,7 +100,7 @@ def func(cols: Iterable[Expression]) -> Expression:
124100 )
125101 return reduce (operator .and_ , it )
126102
127- return self ._expr_from_elementwise (func , * exprs )
103+ return self ._expr . _from_elementwise_horizontal_op (func , * exprs )
128104
129105 def any_horizontal (self , * exprs : DuckDBExpr , ignore_nulls : bool ) -> DuckDBExpr :
130106 def func (cols : Iterable [Expression ]) -> Expression :
@@ -135,25 +111,25 @@ def func(cols: Iterable[Expression]) -> Expression:
135111 )
136112 return reduce (operator .or_ , it )
137113
138- return self ._expr_from_elementwise (func , * exprs )
114+ return self ._expr . _from_elementwise_horizontal_op (func , * exprs )
139115
140116 def max_horizontal (self , * exprs : DuckDBExpr ) -> DuckDBExpr :
141117 def func (cols : Iterable [Expression ]) -> Expression :
142118 return F ("greatest" , * cols )
143119
144- return self ._expr_from_elementwise (func , * exprs )
120+ return self ._expr . _from_elementwise_horizontal_op (func , * exprs )
145121
146122 def min_horizontal (self , * exprs : DuckDBExpr ) -> DuckDBExpr :
147123 def func (cols : Iterable [Expression ]) -> Expression :
148124 return F ("least" , * cols )
149125
150- return self ._expr_from_elementwise (func , * exprs )
126+ return self ._expr . _from_elementwise_horizontal_op (func , * exprs )
151127
152128 def sum_horizontal (self , * exprs : DuckDBExpr ) -> DuckDBExpr :
153129 def func (cols : Iterable [Expression ]) -> Expression :
154130 return reduce (operator .add , (CoalesceOperator (col , lit (0 )) for col in cols ))
155131
156- return self ._expr_from_elementwise (func , * exprs )
132+ return self ._expr . _from_elementwise_horizontal_op (func , * exprs )
157133
158134 def mean_horizontal (self , * exprs : DuckDBExpr ) -> DuckDBExpr :
159135 def func (cols : Iterable [Expression ]) -> Expression :
@@ -162,7 +138,7 @@ def func(cols: Iterable[Expression]) -> Expression:
162138 operator .add , (CoalesceOperator (col , lit (0 )) for col in cols )
163139 ) / reduce (operator .add , (col .isnotnull ().cast (BIGINT ) for col in cols ))
164140
165- return self ._expr_from_elementwise (func , * exprs )
141+ return self ._expr . _from_elementwise_horizontal_op (func , * exprs )
166142
167143 def when (self , predicate : DuckDBExpr ) -> DuckDBWhen :
168144 return DuckDBWhen .from_expr (predicate , context = self )
0 commit comments