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 4e3025c3 [SPARK-61287][CONNECT][PYTHON] Keep the column as the left 
operand of a reflected & and | in the Python client, as PySpark does
4e3025c3 is described below

commit 4e3025c31532aa6aa1e773a9bc1f197611dd1218
Author: Timothy Meehan <[email protected]>
AuthorDate: Tue Nov 10 15:50:41 2026 +0900

    [SPARK-61287][CONNECT][PYTHON] Keep the column as the left operand of a 
reflected & and | in the Python client, as PySpark does
    
    ### What changes were proposed in this pull request?
    
    [SPARK-61287](https://issues.apache.org/jira/browse/SPARK-61287): keep the 
column as the left operand of a reflected `&` and `|` in the Python client, as 
PySpark does.
    
    - `Column.__rand__` and `Column.__ror__` in 
`crates/pyspark-rs/src/column.rs` now build `self AND other` and `self OR 
other`. They built `other AND self` and `other OR self`.
    - Offline test 
`test_reflected_and_or_keep_the_column_as_the_left_operand_as_pyspark` in 
`python/tests/test_dropin_offline.py` checks the plan of `True & col`, `True | 
col`, `False & col` and `False | col`, and that `col & True` and `col | True` 
are unchanged.
    - Live test `test_reflected_and_or_name_the_column_first_as_pyspark` in 
`python/tests/test_dropin_e2e.py` checks that the columns of `df.select(True & 
F.col("b"), True | F.col("b"))` are `['(b AND true)', '(b OR true)']`, as the 
reference client returns them.
    
    ### Why are the changes needed?
    
    In PySpark, `True & col` and `True | col` build `(col AND true)` and `(col 
OR true)`, because `Column.__rand__` and `Column.__ror__` keep the column as 
the left operand. The Python client built `(true AND col)` and `(true OR col)`, 
so the names of the resulting columns differed from PySpark's. Against an OSS 
Spark 4.3.0 Connect server:
    
    - pyspark-client 4.3.0: `['(b AND true)', '(b OR true)']`
    - before this change: `['(true AND b)', '(true OR b)']`
    - after this change: `['(b AND true)', '(b OR true)']`
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes, the name of the column that a reflected `&` or `|` creates now matches 
PySpark's: `(b AND true)` where the client named it `(true AND b)`. The value 
of the expression is unchanged, because AND and OR are commutative.
    
    ### How was this patch tested?
    
    - New offline test 
`test_reflected_and_or_keep_the_column_as_the_left_operand_as_pyspark` 
(`python/tests/test_dropin_offline.py`): the function and argument order of 
`True & col`, `True | col`, `False & col` and `False | col`, and `col & True` 
and `col | True` unchanged.
    - New live test `test_reflected_and_or_name_the_column_first_as_pyspark` 
(`python/tests/test_dropin_e2e.py`): `.columns` is `['(b AND true)', '(b OR 
true)']`, as pyspark-client 4.3.0 returns on the same server.
    - Both new tests fail on the previous build and pass with this change.
    - `cargo test -p apache-spark-connect --release --lib` (528 passed), the 
build of all tests, also with `wasm-udf`, and `cargo fmt --all --check` pass. 
Clippy reports no diagnostics in `crates/pyspark-rs/src/column.rs`.
    - Offline Python suites `tests/test_dropin_offline.py 
tests/test_ml_offline.py tests/test_arrow_handoff.py`: 2928 passed, 8 skipped, 
against the drop-in built from this change.
    - Live Python suites `tests/test_arrow_e2e.py tests/test_dropin_e2e.py 
tests/test_ml_e2e.py` against a local OSS Spark 4.3.0 Connect server: 237 
passed, 1 skipped.
    
    ### 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 #452 from tdcmeehan/SPARK-61287-reflected-and-or-operands.
    
    Authored-by: Timothy Meehan <[email protected]>
    Signed-off-by: Hyukjin Kwon <[email protected]>
---
 crates/pyspark-rs/src/column.rs     |  4 ++--
 python/tests/test_dropin_e2e.py     |  9 +++++++++
 python/tests/test_dropin_offline.py | 22 ++++++++++++++++++++++
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/crates/pyspark-rs/src/column.rs b/crates/pyspark-rs/src/column.rs
index 08306e63..d7402378 100644
--- a/crates/pyspark-rs/src/column.rs
+++ b/crates/pyspark-rs/src/column.rs
@@ -811,12 +811,12 @@ impl PyColumn {
     fn __rand__(&self, py: Python<'_>, other: Py<PyAny>) -> PyResult<PyColumn> 
{
         let _origin = crate::origin::with_origin("__rand__");
         let o = to_column(&other.bind(py))?;
-        Ok(PyColumn::new(o.and(self.column.clone())))
+        Ok(PyColumn::new(self.column.clone().and(o)))
     }
     fn __ror__(&self, py: Python<'_>, other: Py<PyAny>) -> PyResult<PyColumn> {
         let _origin = crate::origin::with_origin("__ror__");
         let o = to_column(&other.bind(py))?;
-        Ok(PyColumn::new(o.or(self.column.clone())))
+        Ok(PyColumn::new(self.column.clone().or(o)))
     }
 
     /// `col ** other`. Mirrors `Column.__pow__` = `power(self, other)`.
diff --git a/python/tests/test_dropin_e2e.py b/python/tests/test_dropin_e2e.py
index 851aaad5..c1d4260e 100644
--- a/python/tests/test_dropin_e2e.py
+++ b/python/tests/test_dropin_e2e.py
@@ -325,6 +325,15 @@ def 
test_udtfs_and_udfs_of_a_dataframe_or_the_session_raise_no_active_session_as
 
 
 
+def test_reflected_and_or_name_the_column_first_as_pyspark(session):
+    # As PySpark's Column: True & col and True | col name (col AND true) and 
(col OR true).
+    df = session.range(2).select((F.col("id") > 0).alias("b"))
+    assert df.select(True & F.col("b"), True | F.col("b")).columns == [
+        "(b AND true)",
+        "(b OR true)",
+    ]
+
+
 def test_renames_are_applied_in_their_order_as_pyspark(session):
     # As PySpark's withColumnsRenamed of a dict, whose renames the server 
applies in their order.
     df = session.range(10)
diff --git a/python/tests/test_dropin_offline.py 
b/python/tests/test_dropin_offline.py
index d7467988..16b2dab8 100644
--- a/python/tests/test_dropin_offline.py
+++ b/python/tests/test_dropin_offline.py
@@ -5483,6 +5483,28 @@ def 
test_columns_have_the_python_2_operators_of_pyspark():
     assert [repr(e) for e in (column + 1).__getnewargs__()] == ["+(a, 1)"]
 
 
+def test_reflected_and_or_keep_the_column_as_the_left_operand_as_pyspark():
+    # As PySpark's Column: True & col is (col AND true), so the names of the 
columns match.
+    from pyspark.sql import functions as F
+
+    def operands(column):
+        function = column.to_plan(None).unresolved_function
+        return function.function_name, [
+            a.unresolved_attribute.unparsed_identifier
+            if a.HasField("unresolved_attribute")
+            else a.literal.boolean
+            for a in function.arguments
+        ]
+
+    column = F.col("b")
+    assert operands(True & column) == ("and", ["b", True])
+    assert operands(True | column) == ("or", ["b", True])
+    assert operands(False & column) == ("and", ["b", False])
+    assert operands(False | column) == ("or", ["b", False])
+    assert operands(column & True) == ("and", ["b", True])
+    assert operands(column | True) == ("or", ["b", True])
+
+
 def test_column_with_alias_chain():
     """Test chaining column methods."""
     from pyspark.sql import functions as F


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

Reply via email to