Skip to content

Commit 04979ea

Browse files
timsaucerclaude
andcommitted
refactor: collapse try_cast_to_type into cast_to_type kwarg
The previous commit exposed cast_to_type and try_cast_to_type as two separate pyo3 bindings and unified them in the Python wrapper via a try_cast kwarg. That left try_cast_to_type in datafusion._internal without a matching public Python name, breaking test_datafusion_missing_exports. Move the dispatch into the rust binding: cast_to_type now takes a try_cast kwarg and selects between functions::expr_fn::cast_to_type and try_cast_to_type internally. Only one pyo3 binding is registered, so the wrapper-coverage check passes and the Python entrypoint is unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 152ef81 commit 04979ea

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

crates/core/src/functions.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,15 @@ expr_fn!(arrow_typeof, arg_1);
609609
expr_fn!(arrow_cast, arg_1 datatype);
610610
expr_fn!(arrow_try_cast, arg_1 datatype);
611611
expr_fn!(arrow_field, arg_1);
612-
expr_fn!(cast_to_type, arg_1 reference);
613-
expr_fn!(try_cast_to_type, arg_1 reference);
612+
#[pyfunction]
613+
#[pyo3(signature = (arg_1, reference, *, try_cast = false))]
614+
fn cast_to_type(arg_1: PyExpr, reference: PyExpr, try_cast: bool) -> PyExpr {
615+
if try_cast {
616+
functions::expr_fn::try_cast_to_type(arg_1.into(), reference.into()).into()
617+
} else {
618+
functions::expr_fn::cast_to_type(arg_1.into(), reference.into()).into()
619+
}
620+
}
614621
expr_fn_vec!(arrow_metadata);
615622
expr_fn_vec!(with_metadata);
616623
expr_fn!(union_tag, arg1);
@@ -970,7 +977,6 @@ pub(crate) fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
970977
m.add_wrapped(wrap_pyfunction!(arrow_try_cast))?;
971978
m.add_wrapped(wrap_pyfunction!(arrow_field))?;
972979
m.add_wrapped(wrap_pyfunction!(cast_to_type))?;
973-
m.add_wrapped(wrap_pyfunction!(try_cast_to_type))?;
974980
m.add_wrapped(wrap_pyfunction!(arrow_metadata))?;
975981
m.add_wrapped(wrap_pyfunction!(with_metadata))?;
976982
m.add_wrapped(wrap_pyfunction!(ascii))?;

python/datafusion/functions.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2982,8 +2982,7 @@ def cast_to_type(value: Expr, type_ref: Expr, *, try_cast: bool = False) -> Expr
29822982
Only the *type* of ``type_ref`` is used; its value is ignored. This is
29832983
useful when the target type comes from another column or expression
29842984
rather than being known up-front. When ``try_cast=True``, casts that
2985-
fail produce NULL instead of erroring (this dispatches to upstream
2986-
``try_cast_to_type``).
2985+
fail produce NULL instead of erroring.
29872986
29882987
Examples:
29892988
>>> ctx = dfn.SessionContext()
@@ -3005,9 +3004,7 @@ def cast_to_type(value: Expr, type_ref: Expr, *, try_cast: bool = False) -> Expr
30053004
>>> result.collect_column("c")[0].as_py() is None
30063005
True
30073006
"""
3008-
if try_cast:
3009-
return Expr(f.try_cast_to_type(value.expr, type_ref.expr))
3010-
return Expr(f.cast_to_type(value.expr, type_ref.expr))
3007+
return Expr(f.cast_to_type(value.expr, type_ref.expr, try_cast=try_cast))
30113008

30123009

30133010
def arrow_metadata(expr: Expr, key: Expr | str | None = None) -> Expr:

0 commit comments

Comments
 (0)