Skip to content

Commit 708cd4d

Browse files
timsaucerclaude
andcommitted
fix: guard with_metadata against empty dict and empty keys
Empty `metadata` dict now returns the input expression unchanged (previously bubbled an opaque DataFusion error about minimum arg count). Empty keys raise `ValueError` to match the docstring contract. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 398b388 commit 708cd4d

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

python/datafusion/functions.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,6 +3058,9 @@ def with_metadata(expr: Expr, metadata: dict[str, str]) -> Expr:
30583058
input field is preserved; new keys overwrite on collision. Keys must be
30593059
non-empty strings; empty values are allowed.
30603060
3061+
An empty ``metadata`` dict is a no-op and returns the input expression
3062+
unchanged. Empty keys raise :py:class:`ValueError`.
3063+
30613064
Examples:
30623065
>>> ctx = dfn.SessionContext()
30633066
>>> df = ctx.from_pydict({"a": [1]})
@@ -3071,11 +3074,16 @@ def with_metadata(expr: Expr, metadata: dict[str, str]) -> Expr:
30713074
... ).collect_column("u")[0].as_py()
30723075
'ms'
30733076
"""
3074-
args = [expr]
3077+
if not metadata:
3078+
return expr
3079+
args = [expr.expr]
30753080
for k, v in metadata.items():
3076-
args.append(Expr.string_literal(k))
3077-
args.append(Expr.string_literal(v))
3078-
return Expr(f.with_metadata(*(a.expr for a in args)))
3081+
if not k:
3082+
msg = "with_metadata keys must be non-empty strings"
3083+
raise ValueError(msg)
3084+
args.append(Expr.string_literal(k).expr)
3085+
args.append(Expr.string_literal(v).expr)
3086+
return Expr(f.with_metadata(*args))
30793087

30803088

30813089
def get_field(expr: Expr, *names: Expr | str) -> Expr:

0 commit comments

Comments
 (0)