Skip to content

Commit 8f15e11

Browse files
committed
refactor: add _ensure_expr function for improved expression validation in DataFrame methods
1 parent f3a1afe commit 8f15e11

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

python/datafusion/dataframe.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,23 @@ def __init__(
292292
self.bloom_filter_ndv = bloom_filter_ndv
293293

294294

295+
def _ensure_expr(value: Expr) -> expr_internal.Expr:
296+
"""Return the internal expression or raise ``TypeError`` if invalid.
297+
298+
Args:
299+
value: Candidate expression.
300+
301+
Returns:
302+
The internal expression representation.
303+
304+
Raises:
305+
TypeError: If ``value`` is not an instance of :class:`Expr`.
306+
"""
307+
if not isinstance(value, Expr):
308+
raise TypeError(EXPR_TYPE_ERROR)
309+
return value.expr
310+
311+
295312
class DataFrame:
296313
"""Two dimensional table representation of data.
297314
@@ -430,9 +447,7 @@ def filter(self, *predicates: Expr) -> DataFrame:
430447
"""
431448
df = self.df
432449
for p in predicates:
433-
if not isinstance(p, Expr):
434-
raise TypeError(EXPR_TYPE_ERROR)
435-
df = df.filter(p.expr)
450+
df = df.filter(_ensure_expr(p))
436451
return DataFrame(df)
437452

438453
def with_column(self, name: str, expr: Expr) -> DataFrame:
@@ -445,9 +460,7 @@ def with_column(self, name: str, expr: Expr) -> DataFrame:
445460
Returns:
446461
DataFrame with the new column.
447462
"""
448-
if not isinstance(expr, Expr):
449-
raise TypeError(EXPR_TYPE_ERROR)
450-
return DataFrame(self.df.with_column(name, expr.expr))
463+
return DataFrame(self.df.with_column(name, _ensure_expr(expr)))
451464

452465
def with_columns(
453466
self, *exprs: Expr | Iterable[Expr], **named_exprs: Expr
@@ -778,11 +791,7 @@ def join_on(
778791
Returns:
779792
DataFrame after join.
780793
"""
781-
exprs = []
782-
for expr in on_exprs:
783-
if not isinstance(expr, Expr):
784-
raise TypeError(EXPR_TYPE_ERROR)
785-
exprs.append(expr.expr)
794+
exprs = [_ensure_expr(expr) for expr in on_exprs]
786795
return DataFrame(self.df.join_on(right.df, exprs, how))
787796

788797
def explain(self, verbose: bool = False, analyze: bool = False) -> None:

0 commit comments

Comments
 (0)