You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "timsaucer (via GitHub)" <gi...@apache.org> on 2026/06/04 16:57:09 UTC
[PR] feat: expose arrow_field, arrow_try_cast, cast_to_type, with_metadata [datafusion-python]
timsaucer opened a new pull request, #1568:
URL: https://github.com/apache/datafusion-python/pull/1568
# Which issue does this PR close?
Closes #. No tracking issue; gap surfaced during the v54 upstream coverage audit.
# Rationale for this change
Five scalar functions from `datafusion::functions::expr_fn` (DataFusion 54) were not exposed through the Python bindings. They round out the Arrow type-introspection and casting surface alongside the existing `arrow_typeof`, `arrow_cast`, and `arrow_metadata` wrappers.
# What changes are included in this PR?
- `crates/core/src/functions.rs`: add `arrow_field`, `arrow_try_cast`, `cast_to_type`, `try_cast_to_type`, and `with_metadata` via the existing `expr_fn!` / `expr_fn_vec!` macros, with module registrations.
- `python/datafusion/functions.py`: add Python wrappers with doctest examples and `__all__` entries. The two upstream functions `cast_to_type` and `try_cast_to_type` are combined into a single Python entry point `cast_to_type(value, type_ref, *, try_cast=False)`; passing `try_cast=True` dispatches to `try_cast_to_type`. `arrow_try_cast` mirrors the existing `arrow_cast` argument handling for `Expr` and `str` data-type arguments. `with_metadata` accepts a `dict[str, str]` for ergonomics and expands it to alternating key/value string literals at the FFI boundary.
- `skills/datafusion_python/SKILL.md`: list the new functions and document the `cast_to_type` kwarg behavior so users understand the single-entry-point design.
# Are there any user-facing changes?
Yes. Five new public functions in `datafusion.functions`:
- `arrow_field(expr)`
- `arrow_try_cast(expr, data_type)`
- `cast_to_type(value, type_ref, *, try_cast=False)`
- `with_metadata(expr, metadata)`
No breaking changes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: expose arrow_field, arrow_try_cast, cast_to_type, with_metadata [datafusion-python]
Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer commented on code in PR #1568:
URL: https://github.com/apache/datafusion-python/pull/1568#discussion_r3415609040
##########
crates/core/src/functions.rs:
##########
@@ -607,7 +607,19 @@ expr_fn_vec!(named_struct);
expr_fn!(from_unixtime, unixtime);
expr_fn!(arrow_typeof, arg_1);
expr_fn!(arrow_cast, arg_1 datatype);
+expr_fn!(arrow_try_cast, arg_1 datatype);
+expr_fn!(arrow_field, arg_1);
+#[pyfunction]
+#[pyo3(signature = (arg_1, reference, *, try_cast = false))]
+fn cast_to_type(arg_1: PyExpr, reference: PyExpr, try_cast: bool) -> PyExpr {
+ if try_cast {
+ functions::expr_fn::try_cast_to_type(arg_1.into(), reference.into()).into()
+ } else {
+ functions::expr_fn::cast_to_type(arg_1.into(), reference.into()).into()
+ }
+}
Review Comment:
Good call. Split into separate `cast_to_type` and `try_cast_to_type` functions in a8d3a5ee, matching upstream and the `arrow_cast` / `arrow_try_cast` pair.
##########
python/tests/test_functions.py:
##########
@@ -1299,30 +1299,93 @@ def test_make_time(df):
assert result.column(0)[0].as_py() == time(12, 30)
-def test_arrow_cast(df):
- df = df.select(
- f.arrow_cast(column("b"), "Float64").alias("b_as_float"),
- f.arrow_cast(column("b"), "Int32").alias("b_as_int"),
+@pytest.mark.parametrize("cast_fn", [f.arrow_cast, f.arrow_try_cast])
+@pytest.mark.parametrize(
+ ("data_type", "expected"),
+ [
+ ("Float64", pa.array([4.0, 5.0, 6.0], type=pa.float64())),
+ ("Int32", pa.array([4, 5, 6], type=pa.int32())),
+ (pa.float64(), pa.array([4.0, 5.0, 6.0], type=pa.float64())),
+ (pa.int32(), pa.array([4, 5, 6], type=pa.int32())),
+ (pa.string(), pa.array(["4", "5", "6"], type=pa.string())),
+ ],
+)
+def test_arrow_cast_variants(df, cast_fn, data_type, expected):
+ """arrow_cast / arrow_try_cast accept str and pyarrow target types."""
+ result = df.select(cast_fn(column("b"), data_type).alias("c")).collect()[0]
+ assert result.column(0) == expected
+
+
+@pytest.mark.parametrize("data_type", ["Float64", pa.float64()])
+def test_arrow_try_cast_null_on_failure(data_type):
+ ctx = SessionContext()
+ batch = pa.RecordBatch.from_arrays([pa.array(["1.5", "oops", "3"])], names=["s"])
+ df = ctx.create_dataframe([[batch]])
+
+ result = df.select(f.arrow_try_cast(column("s"), data_type).alias("c")).collect()[0]
+
+ assert result.column(0).to_pylist() == [1.5, None, 3.0]
Review Comment:
Agreed, the `data_type` parametrization was redundant here since the assert does not depend on it and the str-vs-pyarrow distinction is already covered by `test_arrow_cast_variants`. Dropped it in a8d3a5ee.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: expose arrow_field, arrow_try_cast, cast_to_type, with_metadata [datafusion-python]
Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer merged PR #1568:
URL: https://github.com/apache/datafusion-python/pull/1568
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: expose arrow_field, arrow_try_cast, cast_to_type, with_metadata [datafusion-python]
Posted by "nuno-faria (via GitHub)" <gi...@apache.org>.
nuno-faria commented on code in PR #1568:
URL: https://github.com/apache/datafusion-python/pull/1568#discussion_r3367268083
##########
python/tests/test_functions.py:
##########
@@ -1299,30 +1299,93 @@ def test_make_time(df):
assert result.column(0)[0].as_py() == time(12, 30)
-def test_arrow_cast(df):
- df = df.select(
- f.arrow_cast(column("b"), "Float64").alias("b_as_float"),
- f.arrow_cast(column("b"), "Int32").alias("b_as_int"),
+@pytest.mark.parametrize("cast_fn", [f.arrow_cast, f.arrow_try_cast])
+@pytest.mark.parametrize(
+ ("data_type", "expected"),
+ [
+ ("Float64", pa.array([4.0, 5.0, 6.0], type=pa.float64())),
+ ("Int32", pa.array([4, 5, 6], type=pa.int32())),
+ (pa.float64(), pa.array([4.0, 5.0, 6.0], type=pa.float64())),
+ (pa.int32(), pa.array([4, 5, 6], type=pa.int32())),
+ (pa.string(), pa.array(["4", "5", "6"], type=pa.string())),
+ ],
+)
+def test_arrow_cast_variants(df, cast_fn, data_type, expected):
+ """arrow_cast / arrow_try_cast accept str and pyarrow target types."""
+ result = df.select(cast_fn(column("b"), data_type).alias("c")).collect()[0]
+ assert result.column(0) == expected
+
+
+@pytest.mark.parametrize("data_type", ["Float64", pa.float64()])
+def test_arrow_try_cast_null_on_failure(data_type):
+ ctx = SessionContext()
+ batch = pa.RecordBatch.from_arrays([pa.array(["1.5", "oops", "3"])], names=["s"])
+ df = ctx.create_dataframe([[batch]])
+
+ result = df.select(f.arrow_try_cast(column("s"), data_type).alias("c")).collect()[0]
+
+ assert result.column(0).to_pylist() == [1.5, None, 3.0]
Review Comment:
Since the assert is static, is the parameter necessary?
##########
crates/core/src/functions.rs:
##########
@@ -607,7 +607,19 @@ expr_fn_vec!(named_struct);
expr_fn!(from_unixtime, unixtime);
expr_fn!(arrow_typeof, arg_1);
expr_fn!(arrow_cast, arg_1 datatype);
+expr_fn!(arrow_try_cast, arg_1 datatype);
+expr_fn!(arrow_field, arg_1);
+#[pyfunction]
+#[pyo3(signature = (arg_1, reference, *, try_cast = false))]
+fn cast_to_type(arg_1: PyExpr, reference: PyExpr, try_cast: bool) -> PyExpr {
+ if try_cast {
+ functions::expr_fn::try_cast_to_type(arg_1.into(), reference.into()).into()
+ } else {
+ functions::expr_fn::cast_to_type(arg_1.into(), reference.into()).into()
+ }
+}
Review Comment:
Wouldn't it be better to have separate `cast_to_type` and `try_cast_to_type` functions like in upstream? This way it would also be consistent with, e.g., `arrow_cast` and `arrow_try_cast`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org