This is an automated email from the ASF dual-hosted git repository.

zhengruifeng pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.x by this push:
     new 1393dd5a9d1c [SPARK-60761][CONNECT][PYTHON] Allow an empty dict in 
Spark Connect DataFrame.withColumns
1393dd5a9d1c is described below

commit 1393dd5a9d1c45c312a12fb62db6582ff79da037
Author: Shuheng (Markus) Ma <[email protected]>
AuthorDate: Wed Nov 11 18:54:58 2026 +0800

    [SPARK-60761][CONNECT][PYTHON] Allow an empty dict in Spark Connect 
DataFrame.withColumns
    
    ### What changes were proposed in this pull request?
    
    Fixes [SPARK-60761](https://issues.apache.org/jira/browse/SPARK-60761).
    
    This PR removes `assert len(columnNames) > 0` from `WithColumns.__init__` 
in `python/pyspark/sql/connect/plan.py`. With it gone, `df.withColumns({})` in 
Spark Connect returns the DataFrame unchanged, the same as in classic PySpark.
    
    The server already handles an empty dict: the client sends it as an empty 
list of columns, and `SparkConnectPlanner.transformWithColumns` passes that to 
`Dataset.withColumns`, which keeps the same columns and rows.
    
    ### Why are the changes needed?
    
    `df.withColumns({})` works in classic PySpark but fails in Spark Connect 
with an `AssertionError` that has no message. An empty dict can come from 
building the column map in code when no columns match a filter. A user on 
Databricks serverless (which uses Spark Connect) hit this and worked around it 
with `df.select("*", *exprs)` ([forum 
thread](https://community.databricks.com/t5/data-engineering/bug-withcolumns-in-pyspark-doesn-t-handle-empty-dictionary/td-p/136875)):
    
    ```python
    # spark = SparkSession.builder.remote("local[2]").getOrCreate() for 
Connect, .master("local[2]") for classic
    from pyspark.sql import functions as F
    
    df = spark.range(3)
    new_cols = {c + "_x": F.col(c) for c in ["missing"] if c in df.columns}  # 
{}
    df.withColumns(new_cols).columns
    # classic: ['id']
    # Connect on master, 4.2.0 and 4.3.0: AssertionError (no message)
    ```
    
    The assert was added in 
[SPARK-41936](https://issues.apache.org/jira/browse/SPARK-41936) (#39445), when 
`DataFrame.withMetadata` (which always passes one column) started reusing the 
`WithColumns` plan.
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes. In Spark Connect, `df.withColumns({})` now returns the DataFrame 
unchanged instead of raising `AssertionError`. Calls with a non-empty dict, and 
classic PySpark, behave as before.
    
    ### How was this patch tested?
    
    Added an empty dict case to `DataFrameTestsMixin.test_with_columns`, which 
runs in classic (`test_dataframe`) and Connect (`test_parity_dataframe`). 
Without the fix it passes in classic and fails in Connect with `AssertionError`.
    
    ```
    cd python && python -m unittest \
      pyspark.sql.tests.test_dataframe.DataFrameTests.test_with_columns \
      
pyspark.sql.tests.connect.test_parity_dataframe.DataFrameParityTests.test_with_columns
    dev/lint-python --mypy
    ruff check python/pyspark/sql/connect/plan.py 
python/pyspark/sql/tests/test_dataframe.py
    ```
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Code (Claude Opus 5.5)
    
    Co-developed with Claude Code (Claude Opus 5.5): the fix, the test and this 
description. The checks above were run locally.
    
    Closes #59967 from hzmsh/SPARK-60761.
    
    Authored-by: Shuheng (Markus) Ma <[email protected]>
    Signed-off-by: Ruifeng Zheng <[email protected]>
    
    (cherry picked from commit 6e64f56eac3d9ccb65a238c4ccb0e6d7965cb94f)
    Signed-off-by: Ruifeng Zheng <[email protected]>
    
    
    skip-checks: true
---
 python/pyspark/sql/connect/plan.py         | 1 -
 python/pyspark/sql/tests/test_dataframe.py | 3 +++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/python/pyspark/sql/connect/plan.py 
b/python/pyspark/sql/connect/plan.py
index fddb18d868e7..158ad3c1b85f 100644
--- a/python/pyspark/sql/connect/plan.py
+++ b/python/pyspark/sql/connect/plan.py
@@ -689,7 +689,6 @@ class WithColumns(LogicalPlan):
         metadata: Optional[Sequence[str]] = None,
     ) -> None:
         assert isinstance(columnNames, list)
-        assert len(columnNames) > 0
         assert all(isinstance(c, str) for c in columnNames)
 
         assert isinstance(columns, list)
diff --git a/python/pyspark/sql/tests/test_dataframe.py 
b/python/pyspark/sql/tests/test_dataframe.py
index 4111e732b84d..58481379debc 100644
--- a/python/pyspark/sql/tests/test_dataframe.py
+++ b/python/pyspark/sql/tests/test_dataframe.py
@@ -688,6 +688,9 @@ class DataFrameTestsMixin:
         self.assertRaises(TypeError, self.df.withColumns, ["key"])
         self.assertRaises(Exception, self.df.withColumns)
 
+        # SPARK-60761: an empty dict returns the DataFrame unchanged
+        assertDataFrameEqual(self.df.withColumns({}), self.df)
+
     def test_with_columns_with_dependencies(self):
         df = self.spark.range(3).withColumns(
             {


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

Reply via email to