Skip to content

Commit 5964e47

Browse files
committed
Revert "feat: add ensure_expr_list function to flatten and validate nested expressions"
This reverts commit 5598f90.
1 parent 646d0ab commit 5964e47

File tree

3 files changed

+12
-37
lines changed

3 files changed

+12
-37
lines changed

python/datafusion/dataframe.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
Expr,
4646
SortKey,
4747
ensure_expr,
48-
ensure_expr_list,
4948
expr_list_to_raw_expr_list,
5049
sort_list_to_raw_sort_list,
5150
)
@@ -489,7 +488,17 @@ def with_columns(
489488
Returns:
490489
DataFrame with the new columns added.
491490
"""
492-
expressions = ensure_expr_list(exprs)
491+
492+
def _iter_exprs(items: Iterable[Expr | Iterable[Expr]]) -> Iterable[Expr | str]:
493+
for expr in items:
494+
if isinstance(expr, str):
495+
yield expr
496+
elif isinstance(expr, Iterable) and not isinstance(expr, Expr):
497+
yield from _iter_exprs(expr)
498+
else:
499+
yield expr
500+
501+
expressions = [ensure_expr(e) for e in _iter_exprs(exprs)]
493502
for alias, expr in named_exprs.items():
494503
ensure_expr(expr)
495504
expressions.append(expr.alias(alias).expr)

python/datafusion/expr.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from __future__ import annotations
2424

25-
from typing import TYPE_CHECKING, Any, ClassVar, Iterable, Optional, Sequence
25+
from typing import TYPE_CHECKING, Any, ClassVar, Optional, Sequence
2626

2727
import pyarrow as pa
2828

@@ -219,7 +219,6 @@
219219
"WindowFrame",
220220
"WindowFrameBound",
221221
"ensure_expr",
222-
"ensure_expr_list",
223222
]
224223

225224

@@ -244,31 +243,6 @@ def ensure_expr(value: Expr | Any) -> expr_internal.Expr:
244243
return value.expr
245244

246245

247-
def ensure_expr_list(
248-
exprs: Iterable[Expr | Iterable[Expr]],
249-
) -> list[expr_internal.Expr]:
250-
"""Flatten an iterable of expressions, validating each via ``ensure_expr``.
251-
252-
Args:
253-
exprs: Possibly nested iterable containing expressions.
254-
255-
Returns:
256-
A flat list of raw expressions.
257-
258-
Raises:
259-
TypeError: If any item is not an instance of :class:`Expr`.
260-
"""
261-
262-
def _iter(items: Iterable[Expr | Iterable[Expr]]) -> Iterable[expr_internal.Expr]:
263-
for expr in items:
264-
if isinstance(expr, Iterable) and not isinstance(expr, Expr):
265-
yield from _iter(expr)
266-
else:
267-
yield ensure_expr(expr)
268-
269-
return list(_iter(exprs))
270-
271-
272246
def _to_raw_expr(value: Expr | str) -> expr_internal.Expr:
273247
"""Convert a Python expression or column name to its raw variant.
274248

python/tests/test_dataframe.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,6 @@ def test_with_columns_invalid_expr(df):
436436
TypeError, match=r"Use col\(\)/column\(\) or lit\(\)/literal\(\)"
437437
):
438438
df.with_columns(c="a")
439-
with pytest.raises(
440-
TypeError, match=r"Use col\(\)/column\(\) or lit\(\)/literal\(\)"
441-
):
442-
df.with_columns(["a"])
443-
with pytest.raises(
444-
TypeError, match=r"Use col\(\)/column\(\) or lit\(\)/literal\(\)"
445-
):
446-
df.with_columns(c=["a"])
447439

448440

449441
def test_cast(df):

0 commit comments

Comments
 (0)