This is an automated email from the ASF dual-hosted git repository.
HyukjinKwon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark-connect-rust.git
The following commit(s) were added to refs/heads/master by this push:
new ff8993ae [SPARK-61289][CONNECT][PYTHON] Raise NOT_EXPECTED_TYPE for an
argument of a higher-order function that is not a Column or a column name, in
the Python client as PySpark does
ff8993ae is described below
commit ff8993ae6c539e027abcc6333401d0f1ebe84ff8
Author: Timothy Meehan <[email protected]>
AuthorDate: Tue Nov 10 15:51:43 2026 +0900
[SPARK-61289][CONNECT][PYTHON] Raise NOT_EXPECTED_TYPE for an argument of a
higher-order function that is not a Column or a column name, in the Python
client as PySpark does
### What changes were proposed in this pull request?
[SPARK-61289](https://issues.apache.org/jira/browse/SPARK-61289): The
Python client's higher-order functions now check their column arguments as
PySpark 4.3.0 does. `_invoke_higher_order_function` in
`python/pyspark/sql/functions.py` converts each column argument with the
`_column_or_name` helper (PySpark Connect's `_to_col`, already defined in that
module) instead of the local `_to_col`. A Column and a column name are
unchanged. Any other value, such as a Python list, raises `PySpark [...]
The change covers `aggregate`, `array_sort`, `exists`, `filter`, `forall`,
`map_filter`, `map_zip_with`, `reduce`, `transform`, `transform_keys`,
`transform_values` and `zip_with`, which all go through that helper.
### Why are the changes needed?
The local `_to_col` returns anything that is not a str unchanged, so
`F.transform([1, 2, 3], lambda x: x * 10)` sent the list as a literal and
returned `[Row(t=[10, 20, 30])]`. PySpark 4.3.0 raises `[NOT_EXPECTED_TYPE]
Argument \`col\` should be Column or str, got list.` for the same call, so code
written against the Python client could fail on PySpark. The helper of
`functions_generated.py` already has PySpark's behavior and is reused, so the
fix is limited to the higher-order path a [...]
### Does this PR introduce _any_ user-facing change?
Yes. In the Python client, a value that is neither a Column nor a column
name at a column position of one of the higher-order functions now raises
`PySparkTypeError` (`NOT_EXPECTED_TYPE`) at call time, instead of being sent to
the server as a literal. For example, `F.transform([1, 2, 3], lambda x: x *
10)` and `F.exists([1, 2], lambda x: x > 1)` raise, as they do on PySpark
4.3.0. Columns and column names are unaffected.
### How was this patch tested?
- Offline,
`python/tests/test_dropin_offline.py::test_higher_order_functions_check_their_column_arguments_as_pyspark`:
for each of the 12 functions, at every column position (including
`initialValue` of `aggregate` and `reduce`, and both `array_sort` forms), a
list raises `NOT_EXPECTED_TYPE` with the message parameters of PySpark, and a
Column and a column name still build a Column whose repr starts with the
function name. It fails without the change (`DID NOT RAISE`).
- Live,
`python/tests/test_dropin_e2e.py::test_higher_order_functions_take_columns_and_reject_lists_as_pyspark`,
against a Spark 4.3.0 Connect server: `transform` and `exists` with lists
raise, and with array columns they return `[Row(t=[10, 20, 30])]` and
`[Row(e=True)]`, as PySpark does.
- Offline Python suites: `tests/test_dropin_offline.py`,
`tests/test_ml_offline.py`, `tests/test_arrow_handoff.py`.
- Live Python suites: `tests/test_arrow_e2e.py`,
`tests/test_dropin_e2e.py`, `tests/test_ml_e2e.py`.
- Rust (no Rust code changed): `cargo test -p apache-spark-connect
--release --lib` (528 passed), `--no-run` and `--features wasm-udf --no-run`
(both compile), `cargo fmt --all --check` (clean). `cargo clippy --workspace
--all-targets --release` stops with `approx_constant` errors in
`crates/spark-connect/tests/e2e_integration.rs` and `e2e_functions.rs` that are
not in this change.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5 and Claude Haiku 5.5
This pull request and its description were written by Isaac.
Closes #453 from tdcmeehan/SPARK-61289-higher-order-function-argument-types.
Lead-authored-by: Timothy Meehan <[email protected]>
Co-authored-by: Hyukjin Kwon <[email protected]>
Signed-off-by: Hyukjin Kwon <[email protected]>
---
python/pyspark/sql/functions.py | 2 +-
python/tests/test_dropin_e2e.py | 18 ++++++++++++++++
python/tests/test_dropin_offline.py | 42 +++++++++++++++++++++++++++++++++++++
3 files changed, 61 insertions(+), 1 deletion(-)
diff --git a/python/pyspark/sql/functions.py b/python/pyspark/sql/functions.py
index 8d9442b1..148f473d 100644
--- a/python/pyspark/sql/functions.py
+++ b/python/pyspark/sql/functions.py
@@ -549,7 +549,7 @@ def _create_lambda(f):
def _invoke_higher_order_function(name, cols, funs):
- _cols = [_to_col(c) for c in cols]
+ _cols = [_column_or_name(c) for c in cols]
_funs = [_create_lambda(f) for f in funs]
return _invoke_function(name, *_cols, *_funs)
diff --git a/python/tests/test_dropin_e2e.py b/python/tests/test_dropin_e2e.py
index f92bd1ed..e995efe3 100644
--- a/python/tests/test_dropin_e2e.py
+++ b/python/tests/test_dropin_e2e.py
@@ -4627,3 +4627,21 @@ def
test_kmeans_cosine_distance_groups_vectors_by_direction(session):
).withColumn("features", array_to_vector("xs"))
model = KMeans(seed=42, distanceMeasure="cosine",
solver="row").fit(training)
_assert_cluster_partition([r.prediction for r in
model.transform(held_out).orderBy("id").collect()], [0,0,1,1])
+
+
+def
test_higher_order_functions_take_columns_and_reject_lists_as_pyspark(session):
+ # As PySpark's, a Python list is not a column of a higher-order function,
and an array
+ # column is one, and the rows of the lambdas of both are those of the
reference.
+ from pyspark.errors import PySparkTypeError
+
+ for build in (lambda: F.transform([1, 2, 3], lambda x: x * 10),
+ lambda: F.exists([1, 2], lambda x: x > 1)):
+ with pytest.raises(PySparkTypeError) as error:
+ session.range(1).select(build().alias("v")).collect()
+ assert error.value.getCondition() == "NOT_EXPECTED_TYPE"
+ array = F.array(F.lit(1), F.lit(2), F.lit(3))
+ assert session.range(1).select(
+ F.transform(array, lambda x: x * 10).alias("t")).collect() ==
[Row(t=[10, 20, 30])]
+ assert session.range(1).select(
+ F.exists(F.array(F.lit(1), F.lit(2)), lambda x: x >
1).alias("e")).collect() == [
+ Row(e=True)]
diff --git a/python/tests/test_dropin_offline.py
b/python/tests/test_dropin_offline.py
index 881f1a09..de31bde1 100644
--- a/python/tests/test_dropin_offline.py
+++ b/python/tests/test_dropin_offline.py
@@ -3183,6 +3183,48 @@ def test_higher_order_function_validation():
F.transform("arr", lambda x: 123)
+def test_higher_order_functions_check_their_column_arguments_as_pyspark():
+ from pyspark.errors import PySparkTypeError
+ from pyspark.sql import functions as F
+
+ one = lambda x: x * 10 # noqa: E731
+ two = lambda x, y: x + y # noqa: E731
+ three = lambda k, a, b: a # noqa: E731
+ # Each builder takes the column argument in the position under test, and
the name of
+ # the function that the Column it builds starts with.
+ cases = [
+ ("transform", lambda c: F.transform(c, one)),
+ ("exists", lambda c: F.exists(c, one)),
+ ("forall", lambda c: F.forall(c, one)),
+ ("filter", lambda c: F.filter(c, one)),
+ ("aggregate", lambda c: F.aggregate(c, F.lit(0), two)),
+ ("aggregate", lambda c: F.aggregate("a", c, two)),
+ ("reduce", lambda c: F.reduce(c, F.lit(0), two)),
+ ("reduce", lambda c: F.reduce("a", c, two)),
+ ("array_sort", lambda c: F.array_sort(c)),
+ ("array_sort", lambda c: F.array_sort(c, two)),
+ ("zip_with", lambda c: F.zip_with(c, "b", two)),
+ ("zip_with", lambda c: F.zip_with("a", c, two)),
+ ("transform_keys", lambda c: F.transform_keys(c, two)),
+ ("transform_values", lambda c: F.transform_values(c, two)),
+ ("map_filter", lambda c: F.map_filter(c, two)),
+ ("map_zip_with", lambda c: F.map_zip_with(c, "m2", three)),
+ ("map_zip_with", lambda c: F.map_zip_with("m1", c, three)),
+ ]
+ for name, make in cases:
+ # A list is not a column: PySpark names the argument `col` whatever
its position.
+ with pytest.raises(PySparkTypeError) as error:
+ make([1, 2, 3])
+ assert error.value.getCondition() == "NOT_EXPECTED_TYPE", name
+ assert error.value.getMessageParameters() == {
+ "expected_type": "Column or str", "arg_name": "col", "arg_type":
"list"}, name
+ # A Column and a column name are built as before.
+ for arg in (F.col("a"), "a"):
+ column = make(arg)
+ assert type(column).__name__ == "Column"
+ assert repr(column).startswith(f"Column<'{name}("), name
+
+
def test_misc_functions():
from pyspark.sql import functions as F
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]