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 af7b452e [SPARK-61539][CONNECT][PYTHON] Raise PySpark's TypeError for 
a jdbc property whose key is not a str, in the Python client
af7b452e is described below

commit af7b452ed0657b83664cf864ddc9199fc6c3d31f
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Thu Nov 12 21:05:45 2026 +0900

    [SPARK-61539][CONNECT][PYTHON] Raise PySpark's TypeError for a jdbc 
property whose key is not a str, in the Python client
    
    ### What changes were proposed in this pull request?
    
    A follow-up from a review of #550 (B-5 of the review).
    
    PySpark's `DataFrameReader.jdbc` and `DataFrameWriter.jdbc` pass the 
properties as `self.options(**properties)` (`connect/readwriter.py:547`, 
`:551`, `:957`). A key that is not a str raises the `TypeError` of that 
unpacking, `keywords must be strings`, before any property is set. By then the 
reader or writer already has its format, plus the options of a `column` on the 
reader or the mode on the writer.
    
    The Python client turned each key into its `str()` and set it as an option 
name. So `properties={b"user": "u"}` set an option `b'user'`, and the `user` 
property was silently lost; `{1: "v"}` set an option `1`.
    
    Both jdbc methods (`crates/pyspark-rs/src/readwriter.rs`, 
`crates/pyspark-rs/src/dataframe.rs`) now:
    - raise PySpark's `TypeError` for such a key;
    - keep the reader's or writer's settings as PySpark's do;
    - set a str key as protobuf takes it (`str_argument`), not through `str()`.
    
    ### Why are the changes needed?
    
    To match PySpark 4.3.0. Probed with official pyspark-client 4.3.0 (Python 
3.11, protobuf 7.36.2): for the keys `1`, `b"user"` and `None`, and for 
`{"user": 1, 2: "x"}`, both jdbc methods raise `TypeError: keywords must be 
strings`, and the reader's options stay empty. Ours set the stringified keys 
and sent the read.
    
    ### Does this PR introduce _any_ user-facing change?
    
    Yes. In the Python client, a jdbc property whose key is not a str raises 
PySpark's `TypeError` instead of setting another option name.
    
    ### How was this patch tested?
    
    - `test_jdbc_properties_are_keywords_as_pyspark` 
(`python/tests/test_dropin_offline.py`, new) covers:
      - the reader without and with a `column`, and the writer;
      - the keys `1`, `b"user"` and `None`;
      - the reader's options after the error;
      - that nothing is sent.
    
      It fails without the change (`DID NOT RAISE`).
    - The existing jdbc test, 
`test_reader_and_writer_check_their_arguments_as_pyspark`, passes.
    - `cargo build -p pyspark-rs --release`, `cargo fmt --all --check` and 
`ruff check`.
    
    ### 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 #568 from HyukjinKwon/SPARK-61539-jdbc-property-keys.
    
    Authored-by: Hyukjin Kwon <[email protected]>
    Signed-off-by: Hyukjin Kwon <[email protected]>
---
 crates/pyspark-rs/src/dataframe.rs  | 14 +++++++++++++-
 crates/pyspark-rs/src/readwriter.rs | 15 ++++++++++++++-
 python/tests/test_dropin_offline.py | 26 ++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/crates/pyspark-rs/src/dataframe.rs 
b/crates/pyspark-rs/src/dataframe.rs
index 741e4021..036ea0a1 100644
--- a/crates/pyspark-rs/src/dataframe.rs
+++ b/crates/pyspark-rs/src/dataframe.rs
@@ -3688,9 +3688,21 @@ impl PyDataFrameWriter {
             w = w.mode(m);
         }
         if let Some(props) = properties {
+            // As PySpark's `options(**properties)`, whose unpacking raises 
for a key that is not
+            // a str before any property is set, with the format and the mode 
kept on the writer.
+            if props
+                .keys()
+                .iter()
+                .any(|key| !key.is_instance_of::<pyo3::types::PyString>())
+            {
+                self.inner.set(w);
+                return Err(pyo3::exceptions::PyTypeError::new_err(
+                    "keywords must be strings",
+                ));
+            }
             for (key, value) in props.iter() {
                 if let Some(value) = crate::coerce_option_value(&value)? {
-                    w = w.option(&crate::str_text(&key)?, &value);
+                    w = w.option(&crate::str_argument(&key)?, &value);
                 }
             }
         }
diff --git a/crates/pyspark-rs/src/readwriter.rs 
b/crates/pyspark-rs/src/readwriter.rs
index 552e50ee..a66a2618 100644
--- a/crates/pyspark-rs/src/readwriter.rs
+++ b/crates/pyspark-rs/src/readwriter.rs
@@ -732,8 +732,21 @@ impl PyDataFrameReader {
             None => predicates,
         };
         if let Some(properties) = properties {
+            // As PySpark's `options(**properties)`, whose unpacking raises 
for a key that is not
+            // a str before any property is set, with the format and the 
options of a column kept
+            // on the reader.
+            if properties
+                .keys()
+                .iter()
+                .any(|key| !key.is_instance_of::<pyo3::types::PyString>())
+            {
+                self.inner = r;
+                return Err(pyo3::exceptions::PyTypeError::new_err(
+                    "keywords must be strings",
+                ));
+            }
             for (key, value) in properties.iter() {
-                r = set_option_skip_none(r, &crate::str_text(&key)?, 
Some(&value))?;
+                r = set_option_skip_none(r, &crate::str_argument(&key)?, 
Some(&value))?;
             }
         }
         // PySpark's jdbc also leaves url/dbtable on the reader.
diff --git a/python/tests/test_dropin_offline.py 
b/python/tests/test_dropin_offline.py
index 2d1698d9..06019aa0 100644
--- a/python/tests/test_dropin_offline.py
+++ b/python/tests/test_dropin_offline.py
@@ -22440,6 +22440,32 @@ def 
test_reader_and_writer_check_their_arguments_as_pyspark(mock_connect):
         read(None).columns
         assert _sent_data_source_paths(service.analyze_requests[-1]) == []
 
+
+def test_jdbc_properties_are_keywords_as_pyspark(mock_connect):
+    # As PySpark's jdbc of the reader and the writer, which pass the 
properties as
+    # `options(**properties)`: a key that is not a str raises the TypeError of 
the unpacking
+    # before any property is set, with the format (and the options of a 
column, the mode) already
+    # set, where a key went through str() as another option name before.
+    spark, service = mock_connect
+    df = spark.range(1)
+    sent = len(service.execute_requests), len(service.analyze_requests)
+    for key in (1, b"user", None):
+        reader = spark.read
+        with pytest.raises(TypeError) as error:
+            reader.jdbc("jdbc:x", "t", properties={"user": "u", key: "v"})
+        assert type(error.value) is TypeError and str(error.value) == 
"keywords must be strings"
+        assert reader._format == "jdbc" and reader._options == {}
+        reader = spark.read
+        with pytest.raises(TypeError, match="^keywords must be strings$"):
+            reader.jdbc("jdbc:x", "t", column="c", lowerBound=1, upperBound=2, 
numPartitions=3,
+                        properties={key: "v"})
+        assert reader._options == {
+            "partitionColumn": "c", "lowerBound": "1", "upperBound": "2", 
"numPartitions": "3"}
+        with pytest.raises(TypeError, match="^keywords must be strings$"):
+            df.write.jdbc("jdbc:x", "t", mode="append", properties={key: "v"})
+    assert (len(service.execute_requests), len(service.analyze_requests)) == 
sent
+
+
 def test_reader_paths_are_checked_as_pyspark(offline_spark):
     from pyspark.errors import PySparkTypeError
 


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

Reply via email to