HyukjinKwon opened a new pull request, #836:
URL: https://github.com/apache/spark-connect-rust/pull/836

   ### What changes were proposed in this pull request?
   
   PySpark 4.3.0 has no proto and no Arrow type of a type of its class only, 
such as `AtomicType()`, `DataType()` or `NumericType()`, nor of a `StructField` 
used as a type:
   - `pyspark_types_to_proto_types` raises `UNSUPPORTED_OPERATION`;
   - `to_arrow_type` raises `UNSUPPORTED_DATA_TYPE_FOR_ARROW_CONVERSION`;
   - `StructType.fromJson` raises `CANNOT_PARSE_DATATYPE` or 
`UNSUPPORTED_DATA_TYPE` of their JSON.
   
   The drop-in lowers such a type into an unparsed core type of its JSON 
(`crates/pyspark-rs/src/types.rs`), and refused it in most places. This PR 
covers the rest:
   
   1. **Pipelines** (`crates/pyspark-rs/src/pipelines.rs`). `register_output` 
converts the schema of a table or a materialized view with 
`pyspark_types_to_proto_types`. `pipeline_define_output` now converts it with 
`py_to_proto_data_type`, so the call and the decorator raise PySpark's 
`UNSUPPORTED_OPERATION` and send nothing. Before, the definition was sent and 
the server answered.
   2. **The reader's `csv`, `json` and `xml` of a DataFrame of strings** (core 
`crates/spark-connect/src/readwriter.rs`). PySpark's `Parse` plan reads a JSON 
schema with `StructType.fromJson` when it is built. Its 
`_parse_datatype_json_value` raises `CANNOT_PARSE_DATATYPE` of a type name that 
it does not parse (`atomic`, but also `null` and `calendarinterval`, which the 
core's parser takes), and `UNSUPPORTED_DATA_TYPE` of an object of another type 
(a `StructField`'s JSON). Such a schema now makes an unbuildable plan 
(`parse_schema_error`), which raises PySpark's error at each request, after its 
input builds, and sends nothing. Before, the call raised the core's 
`INVALID_DATATYPE_STRING` or `INVALID_DATATYPE_FORMAT`, or the plan was sent.
   3. **The SQL type of a user-defined type** (`nested_lowered_in` of 
`crates/pyspark-rs/src/types.rs`). PySpark's `pyspark_types_to_proto_types` and 
`to_arrow_type` convert the `sqlType()` of a UDT too. A UDT whose SQL type 
holds such a type now raises PySpark's `UNSUPPORTED_OPERATION` from the plans 
of a cast, a UDF that is not Arrow-optimized, a Java UDF, 
`Catalog.createTable`, a UDTF and `DataFrame.to`. Before, those plans were sent 
for the server to refuse 
(`CONNECT_INVALID_PLAN.DATA_TYPE_UNSUPPORTED_PROTO_TO_CATALYST`). A tree string 
does not print the SQL type, as PySpark's does not.
   4. **The order of the errors of local rows** 
(`crates/pyspark-rs/src/session.rs`, `crates/pyspark-rs/src/infer.rs`). 
PySpark's `LocalDataToArrowConversion.convert` runs the converters of the 
columns, a UDT's `serialize` too, before `to_arrow_schema` raises 
`UNSUPPORTED_DATA_TYPE_FOR_ARROW_CONVERSION` of such a type 
(`python/pyspark/sql/conversion.py:976-1006`). The drop-in raised it before it 
converted the values (`LocalChecks.nested`), and the rows of a UDT of such a 
SQL type raised `TypeError: unsupported value type for createDataFrame`. Now 
`infer::convert` passes such a type through, as PySpark has no converter of it, 
and `infer::arrow_schema_error` refuses it, depth first with the char, varchar 
and interval types. So these come first, as in PySpark:
      - the errors of a UDT's `serialize`;
      - those of a decimal, a binary or a non-nullable value;
      - a `CharType` field before the type.
   5. **The schema JSON of a relation of local data** (core 
`DataType::json_as_pyspark`, used by `plan.rs` and `arrow_input.rs`). PySpark 
sends the `json()` of the schema, which writes `"atomic"` of `AtomicType()` and 
a field's JSON of a `StructField` as a type. The core wrote the `unparsed(...)` 
text of its lowered type, also of a NumPy array and of data of no rows. The 
server's `PARSE_SYNTAX_ERROR` of that schema then echoed another JSON. The 
schema of a `LocalRelation` and the schema chunk of a chunked cached relation 
now hold PySpark's JSON.
   
   This builds on SPARK-61960, whose readers and `applyInPandasWithState` send 
the `json()` of the schema object. The core's parse therefore no longer reads 
the `unparsed(...)` of a lowered type, and a literal `unparsed("atomic")` 
raises PySpark's `CANNOT_PARSE_DATATYPE` of it.
   
   `transformWithState(InPandas)` of such an output type still sends its plan. 
Its file, `group.rs`, is being reworked in other PRs, and the fix will follow 
there.
   
   ### Why are the changes needed?
   
   To raise PySpark's errors, at the same point, for these types. Before, the 
server answered with other errors for the same calls, the reader's call raised 
the core's error, and local rows raised a `TypeError`, or PySpark's error ahead 
of the errors that PySpark raises first.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, the errors. With `schema = StructType([StructField("a", 
AtomicType())])`, and a UDT `U` whose `sqlType()` is 
`StructType([StructField("v", AtomicType())])`:
   
   | Call | PySpark 4.3.0, and now | Before |
   |---|---|---|
   | `spark.read.schema(schema).csv(strings)` | made; each request raises 
`[CANNOT_PARSE_DATATYPE] Unable to parse datatype. atomic.` | the call raises 
`[INVALID_DATATYPE_STRING]` |
   | `@dp.materialized_view(schema=schema)` | the decorator raises 
`[UNSUPPORTED_OPERATION] data type AtomicType() is not supported.` | the 
definition is sent, and the server refuses it |
   | `df.select(col("id").cast(U())).schema` | `[UNSUPPORTED_OPERATION] data 
type AtomicType() is not supported.` | 
`[CONNECT_INVALID_PLAN.DATA_TYPE_UNSUPPORTED_PROTO_TO_CATALYST]` |
   | `spark.createDataFrame([(1,)], StructType([StructField("a", U())]))` | 
`[UNSUPPORTED_DATA_TYPE_FOR_ARROW_CONVERSION] AtomicType() is not supported in 
conversion to Arrow.` | `TypeError: unsupported value type for createDataFrame: 
int` |
   | `spark.createDataFrame([(None,)], StructType([StructField("a", 
AtomicType(), False)]))` | `PySparkValueError: input for AtomicType() must not 
be None` | `[UNSUPPORTED_DATA_TYPE_FOR_ARROW_CONVERSION] AtomicType() ...` |
   | `spark.createDataFrame(np.zeros((0, 1)), schema).collect()` | the server's 
`PARSE_SYNTAX_ERROR` of `{"fields":[{..."type":"atomic"}],...}` | the same, of 
`"type":"unparsed(\"atomic\")"` |
   
   ### How was this patch tested?
   
   - Offline, with the mock server, against official PySpark 4.3.0's messages:
     - `test_connect_registry_refuses_a_schema_of_types_of_no_proto_as_pyspark` 
(`test_dropin_offline.py`): the call and the decorators of tables and 
materialized views; no request is sent.
     - 
`test_reader_parse_raises_pysparks_errors_of_a_schema_that_its_json_parse_refuses`
 (`test_dropin_offline.py`): `csv`, `json` and `xml` of six schemas, at 
`schema` and `collect`; no request is sent.
     - `test_local_relations_send_pysparks_json_of_their_schema` 
(`test_dropin_offline.py`): the schema JSON of the relations of a NumPy array, 
an empty pandas DataFrame and empty rows.
     - 
`test_user_defined_types_of_sql_types_without_a_proto_raise_pysparks_errors` 
(`test_dropin_types_columns_offline.py`): casts and `try_cast`s of the UDT 
alone and in an array, a map and a struct, a UDF, a Java UDF, `createTable`, a 
UDTF, `DataFrame.to`, the local rows, and the tree string.
     - 
`test_local_rows_of_a_type_without_an_arrow_type_convert_their_values_first_as_pysparks`
 (`test_dropin_types_columns_offline.py`): the order of the errors of local 
rows.
   
     On master these tests fail.
   - Rust unit tests:
     - 
`parse_raises_pysparks_errors_of_the_types_of_a_json_schema_that_it_does_not_parse`
 (`readwriter.rs`);
     - 
`json_as_pyspark_writes_the_json_of_the_types_that_the_python_client_lowered` 
(`types.rs`).
   - 
`test_create_dataframe_of_a_nested_type_of_its_class_only_sends_pysparks_json` 
in `test_dropin_e2e.py`, against a Spark 4.3.0 server: the server's 
`PARSE_SYNTAX_ERROR` shows PySpark's JSON, as `pyspark-client` 4.3.0's does.
   - Probes against the same server, with `pyspark-client` 4.3.0 and the 
drop-in, compared line by line: the cases of the paths of abstract types, the 
reader's parse and the order of the errors of local rows. All match, except 
`transformWithState(InPandas)`, which is left for later.
   - The Rust gates:
     - `cargo fmt --all --check`;
     - `cargo +1.100.0 clippy -p apache-spark-connect-core -p 
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`;
     - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p apache-spark-connect 
-p apache-spark-connect-core`;
     - `cargo test -p pyspark-rs --lib` as `rust.yml` runs it: 13 passed;
     - `cargo test -p apache-spark-connect -p apache-spark-connect-core 
--release` as `rust.yml` runs it: 1676 passed, 18 ignored.
   - The Python gates:
     - ruff 0.14.0 `check` and `format --check`, `test_ruff_excludes.py` and 
`scripts/audit_no_stubs.sh`;
     - `gen_type_stubs.py --pyspark-src <v4.3.0>/python --check` and 
`gen_fn_dispatch.py --check`;
     - `test_type_stubs_offline.py` with mypy 2.4.1: 7 passed;
     - the guards of SPARK-61851.
   - The offline suite of `coverage.yml`: 6329 passed and 77 skipped. The skips 
are those of master: 68 for "not a public constructor", 8 that need 
`SPARK_REMOTE`, and `test_type_stubs_offline.py`, which needs mypy and ran 
above.
   - `test_dropin_e2e.py` against a Spark 4.3.0 server: 317 passed.
   
   ### 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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to