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 215f5c4d [SPARK-61476][CONNECT][PYTHON] Unpack only a list in 
TableArg.partitionBy and orderBy, as PySpark does
215f5c4d is described below

commit 215f5c4d49827a7aa2fc2e074f9b92f9953802b1
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Thu Nov 12 09:45:58 2026 +0900

    [SPARK-61476][CONNECT][PYTHON] Unpack only a list in TableArg.partitionBy 
and orderBy, as PySpark does
    
    ### What changes were proposed in this pull request?
    
    `TableArg.partitionBy` and `TableArg.orderBy` (`df.asTable()`) now unpack 
only a single list, as PySpark's TableArg `_to_cols` does. They use the strict 
`window_columns` helper that `Window.partitionBy` and `Window.orderBy` already 
use, so a tuple among the columns raises `NOT_EXPECTED_TYPE` and a bad column 
reports the argument `col`. AGENTS.md is corrected: the `*cols` unpacking rule 
is carried by per-family helpers, not by `to_column_list` alone.
    
    ### Why are the changes needed?
    
    TableArg used `to_column_list`, which also unpacks a lone tuple and names 
the argument `cols`. PySpark 4.3.0 does neither:
    
    ```python
    # before: accepted; PySpark 4.3.0 raises
    spark.range(3).asTable().partitionBy(("id",))
    # PySparkTypeError NOT_EXPECTED_TYPE {arg_name: col, arg_type: tuple}
    
    # before: arg_name `cols`; PySpark 4.3.0 names `col`
    spark.range(3).asTable().partitionBy("id", ["name"])
    # PySparkTypeError NOT_EXPECTED_TYPE {arg_name: col, arg_type: list}
    ```
    
    This is a follow-up of SPARK-61409 (#489).
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes. `asTable().partitionBy(...)` and `asTable().orderBy(...)` with a tuple 
among the columns now raise `PySparkTypeError` (`NOT_EXPECTED_TYPE`) as PySpark 
does, and the error names the argument `col`. A lone list still works.
    
    ### How was this patch tested?
    
    A new offline test in `python/tests/test_dropin_offline.py`, which fails 
without the fix and passes with it. Also ran the offline suite (1836 passed), 
the Connect e2e subset for table arguments, windows and UDTFs against a Spark 
4.3.0 server, the Rust library tests, the `--no-run` builds (with and without 
`wasm-udf`), `cargo fmt --check`, `scripts/audit_no_stubs.sh` and the ruff 
gates.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Opus 5.5
    
    This pull request and its description were written by Isaac.
    
    Closes #525 from HyukjinKwon/SPARK-61476-tablearg.
    
    Authored-by: Hyukjin Kwon <[email protected]>
    Signed-off-by: Hyukjin Kwon <[email protected]>
---
 AGENTS.md                           | 13 ++++++++-----
 crates/pyspark-rs/src/tablearg.rs   |  6 +++---
 crates/pyspark-rs/src/window.rs     | 11 +++++++----
 python/tests/test_dropin_offline.py | 24 ++++++++++++++++++++++++
 4 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/AGENTS.md b/AGENTS.md
index fbca09c1..64b91595 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -476,11 +476,14 @@ For every method matching a pyspark name:
     special-case them client-side (e.g. faking `distributed_sequence_id` with 
a `row_number`
     window — it is wrong and non-distributed).
 - **`*cols` methods unpack a single list.** `df.select(["a","b"])` must behave 
like
-  `df.select("a","b")` (pyspark unpacks a lone list/tuple arg). Handled 
centrally in
-  `to_column_list`, so it covers `select`/`sort`/`groupBy`/... at once — don't 
re-solve per method.
-  The exceptions are `repartition` and `repartitionByRange`, which give 
PySpark's `_to_cols` the
-  `*cols` tuple itself, so a list among their columns is `NOT_EXPECTED_TYPE`
-  (`to_columns_as_given`).
+  `df.select("a","b")`. The rule lives in the helper each family shares, so 
reuse the one
+  that matches the method rather than re-solving it per method: 
`to_column_list` (`select`,
+  which also unpacks a lone tuple); `unwrap_sequence` 
(`sort`/`orderBy`/`sortWithinPartitions`,
+  and `groupBy`/`rollup`/`cube` through `grouping_columns`); `window_columns` 
(`Window.partitionBy`/
+  `orderBy` and `TableArg.partitionBy`/`orderBy`, which unpack a lone list but 
not a tuple, as
+  PySpark's `_to_cols` does). The exceptions are `repartition` and 
`repartitionByRange`, which give
+  PySpark's `_to_cols` the `*cols` tuple itself, so a list among their columns 
is
+  `NOT_EXPECTED_TYPE` (`to_columns_as_given`).
 - **Pipelines (SDP):** The pure-Python surface vendors cleanly with one edit 
to imports. The
   Connect-server path requires a dedicated Rust `PipelineCommand` seam with 
high-level methods
   on the session, separate from drop-in parity work.
diff --git a/crates/pyspark-rs/src/tablearg.rs 
b/crates/pyspark-rs/src/tablearg.rs
index ef2df060..c98cc4aa 100644
--- a/crates/pyspark-rs/src/tablearg.rs
+++ b/crates/pyspark-rs/src/tablearg.rs
@@ -3,8 +3,8 @@
 use pyo3::prelude::*;
 use spark_connect::table_arg::TableArg;
 
-use crate::dataframe::to_column_list;
 use crate::errors::ResultExt;
+use crate::window::window_columns;
 
 /// A table argument for a table-valued function, mirroring
 /// `pyspark.sql.connect.table_arg.TableArg`. Built via `df.asTable()`, then 
optionally
@@ -26,7 +26,7 @@ impl PyTableArg {
     #[pyo3(signature = (*cols))]
     #[allow(non_snake_case)]
     fn partitionBy(&self, cols: Vec<Bound<'_, PyAny>>) -> PyResult<PyTableArg> 
{
-        let columns = to_column_list(cols)?;
+        let columns = window_columns(cols)?;
         Ok(PyTableArg {
             inner: self.inner.clone().partition_by(columns).to_pyerr()?,
         })
@@ -36,7 +36,7 @@ impl PyTableArg {
     #[pyo3(signature = (*cols))]
     #[allow(non_snake_case)]
     fn orderBy(&self, cols: Vec<Bound<'_, PyAny>>) -> PyResult<PyTableArg> {
-        let columns = to_column_list(cols)?;
+        let columns = window_columns(cols)?;
         Ok(PyTableArg {
             inner: self.inner.clone().order_by(columns).to_pyerr()?,
         })
diff --git a/crates/pyspark-rs/src/window.rs b/crates/pyspark-rs/src/window.rs
index 5d5239a0..5b797f9b 100644
--- a/crates/pyspark-rs/src/window.rs
+++ b/crates/pyspark-rs/src/window.rs
@@ -8,10 +8,13 @@ use spark_connect::window::{FrameBound, FrameType, 
WindowSpec};
 
 use crate::column::PyColumn;
 
-/// The columns of `Window.partitionBy` and `orderBy`, as PySpark Connect's 
`_to_cols`: a single
-/// list is the columns, each a Column or a column name (not a literal), else
-/// `NOT_EXPECTED_TYPE` of the argument `col`, as its `F._to_col`.
-fn window_columns(cols: Vec<Bound<'_, PyAny>>) -> 
PyResult<Vec<spark_connect::column::Column>> {
+/// The columns of `Window.partitionBy` and `orderBy`, and of 
`TableArg.partitionBy` and
+/// `orderBy`, as PySpark Connect's `_to_cols`: a single list is the columns, 
each a Column or
+/// a column name (not a literal), else `NOT_EXPECTED_TYPE` of the argument 
`col`, as its
+/// `F._to_col`.
+pub(crate) fn window_columns(
+    cols: Vec<Bound<'_, PyAny>>,
+) -> PyResult<Vec<spark_connect::column::Column>> {
     let cols = match cols.as_slice() {
         [only] if only.is_instance_of::<pyo3::types::PyList>() => {
             only.try_iter()?.collect::<PyResult<Vec<_>>>()?
diff --git a/python/tests/test_dropin_offline.py 
b/python/tests/test_dropin_offline.py
index af8308e1..d4768a5e 100644
--- a/python/tests/test_dropin_offline.py
+++ b/python/tests/test_dropin_offline.py
@@ -1396,6 +1396,30 @@ def 
test_repartition_does_not_unpack_a_list_among_its_columns_as_pyspark(mock_co
     df.repartitionByRange(2, "id", F.col("id"))
 
 
+def 
test_table_arg_partition_and_order_unpack_only_a_list_as_pyspark(mock_connect):
+    # As PySpark's TableArg, whose _to_cols unpacks a single list but not a 
tuple: a tuple
+    # among the columns of partitionBy or orderBy is NOT_EXPECTED_TYPE, and so 
is a list among
+    # several of them, both of the argument col.
+    from pyspark.errors import PySparkTypeError
+
+    spark, _ = mock_connect
+    table = spark.range(1).asTable()
+    for call, arg_type in [
+        (lambda: table.partitionBy(("id",)), "tuple"),
+        (lambda: table.partitionBy("id", ["id"]), "list"),
+        (lambda: table.partitionBy([1]), "int"),
+        (lambda: table.partitionBy("id").orderBy(("id",)), "tuple"),
+        (lambda: table.partitionBy("id").orderBy("id", ["id"]), "list"),
+    ]:
+        with pytest.raises(PySparkTypeError) as e:
+            call()
+        assert e.value.getCondition() == "NOT_EXPECTED_TYPE"
+        assert e.value.getMessageParameters() == {
+            "expected_type": "Column or str", "arg_name": "col", "arg_type": 
arg_type}
+    # A lone list is unpacked.
+    table.partitionBy(["id"]).orderBy(["id"])
+
+
 def 
test_is_empty_sends_a_limit_of_one_row_over_an_empty_projection_as_pyspark(mock_connect):
     # As PySpark's select().take(1): Limit(1) over a Project with no 
expressions.
     spark, service = mock_connect


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to