Skip to content

Commit f54dc25

Browse files
authored
chore: simplify broadcast for spark-like and ibis (#2595)
* chore: simplify `broadcast` for spark-like and ibis * duckdb too
1 parent 5d16cc0 commit f54dc25

File tree

3 files changed

+5
-39
lines changed

3 files changed

+5
-39
lines changed

narwhals/_duckdb/expr.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,8 @@ def broadcast(self, kind: Literal[ExprKind.AGGREGATION, ExprKind.LITERAL]) -> Se
162162
if self._backend_version < (1, 3):
163163
msg = "At least version 1.3 of DuckDB is required for binary operations between aggregates and columns."
164164
raise NotImplementedError(msg)
165-
166165
template = "{expr} over ()"
167-
168-
def func(df: DuckDBLazyFrame) -> Sequence[Expression]:
169-
return [SQLExpression(template.format(expr=expr)) for expr in self(df)]
170-
171-
return self.__class__(
172-
func,
173-
evaluate_output_names=self._evaluate_output_names,
174-
alias_output_names=self._alias_output_names,
175-
backend_version=self._backend_version,
176-
version=self._version,
177-
)
166+
return self._with_callable(lambda expr: SQLExpression(template.format(expr=expr)))
178167

179168
@classmethod
180169
def from_column_names(

narwhals/_ibis/expr.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from ibis import _ as col
88

99
from narwhals._compliant import LazyExpr
10-
from narwhals._expression_parsing import ExprKind
1110
from narwhals._ibis.expr_dt import IbisExprDateTimeNamespace
1211
from narwhals._ibis.expr_list import IbisExprListNamespace
1312
from narwhals._ibis.expr_str import IbisExprStringNamespace
@@ -20,7 +19,7 @@
2019
from typing_extensions import Self
2120

2221
from narwhals._compliant.typing import AliasNames, EvalNames, EvalSeries
23-
from narwhals._expression_parsing import ExprMetadata
22+
from narwhals._expression_parsing import ExprKind, ExprMetadata
2423
from narwhals._ibis.dataframe import IbisLazyFrame
2524
from narwhals._ibis.namespace import IbisNamespace
2625
from narwhals._ibis.typing import ExprT, WindowFunction
@@ -149,19 +148,8 @@ def func(window_inputs: WindowInputs) -> ir.Value:
149148
return func
150149

151150
def broadcast(self, kind: Literal[ExprKind.AGGREGATION, ExprKind.LITERAL]) -> Self:
152-
if kind is ExprKind.LITERAL:
153-
return self
154-
155-
def func(df: IbisLazyFrame) -> Sequence[ir.Value]:
156-
return [expr.over() for expr in self(df)]
157-
158-
return self.__class__(
159-
func,
160-
evaluate_output_names=self._evaluate_output_names,
161-
alias_output_names=self._alias_output_names,
162-
backend_version=self._backend_version,
163-
version=self._version,
164-
)
151+
# Ibis does its own broadcasting.
152+
return self
165153

166154
@classmethod
167155
def from_column_names(

narwhals/_spark_like/expr.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,7 @@ def __call__(self, df: SparkLikeLazyFrame) -> Sequence[Column]:
106106
def broadcast(self, kind: Literal[ExprKind.AGGREGATION, ExprKind.LITERAL]) -> Self:
107107
if kind is ExprKind.LITERAL:
108108
return self
109-
110-
def func(df: SparkLikeLazyFrame) -> Sequence[Column]:
111-
return [result.over(self.partition_by()) for result in self(df)]
112-
113-
return self.__class__(
114-
func,
115-
evaluate_output_names=self._evaluate_output_names,
116-
alias_output_names=self._alias_output_names,
117-
backend_version=self._backend_version,
118-
version=self._version,
119-
implementation=self._implementation,
120-
)
109+
return self._with_callable(lambda expr: expr.over(self.partition_by()))
121110

122111
@property
123112
def _F(self): # type: ignore[no-untyped-def] # noqa: ANN202, N802

0 commit comments

Comments
 (0)