HyukjinKwon opened a new pull request, #790:
URL: https://github.com/apache/spark-connect-rust/pull/790
### What changes were proposed in this pull request?
PySpark 4.3.0 keeps the arguments of most DataFrame, Column, GroupedData and
functions APIs as Python gave them, and sets them into the plan's protobuf
messages when it builds the plan, at the first request such as `df.schema` or
`collect()`. So their errors come there, as protobuf's (upb's), and a DataFrame
or Column that is not used raises none. The drop-in converted them at the call.
A review probed about 150 calls on both clients against a Spark 4.3.0 server
and found these gaps:
| | PySpark 4.3.0 (now here too) | before |
|---|---|---|
| a str that UTF-8 cannot encode, such as a lone surrogate, in `F.col`,
`lit`, `expr`, `df[name]`, `alias`, `selectExpr`, `filter`,
`withColumn(s)Renamed`, `withMetadata`, `hint`, `drop`, `dropDuplicates`,
`toDF`, `withWatermark`, `unpivot`, `transpose`, `observe`,
`groupBy`/`rollup`/`cube`/`pivot`, `sort`, `describe`, `summary`, `na.*`,
`crosstab`, `freqItems`, `sampleBy`, `join(on=[...])`, `repartitionById` and
the names of UDF arguments | upb's `UnicodeEncodeError` at the first request |
an error at the call |
| `agg({S: "max"})`, `join(df, S)`, `repartition(S)` of such a str | the
same, at the first request | `AttributeError`, `NOT_EXPECTED_TYPE` at the call |
| `explain(S)`, `dropna(how=S)`, `join(how=S)` | `VALUE_NOT_ALLOWED` of the
str as given | `UnicodeEncodeError`, or U+FFFD in the message |
| `spark.addTag(S)` | accepted; the later executions fail until `removeTag`
| refused |
| `DataFrame.alias(5)`, `withColumnRenamed(1, "z")`,
`withColumnsRenamed({"a": 1})`, `unpivot(..., 1, "val")`, `freqItems(["a",
1])`, `na.replace(subset=[1])`, `join(on=["a", 1])` | protobuf's `TypeError` at
the first request | an error at the call |
| bytes for a str, such as `alias(b"q")`, `withColumnRenamed(b"a", b"z")`,
`unpivot(..., b"var", ...)`, `describe(b"ab")` | taken, as protobuf's string
fields take UTF-8 bytes | refused |
| `fillna(2**70)`, `dropna(thresh=2**40)`, `spark.range(2**70)` and its
`step` and `numPartitions`, `tvf.range` | protobuf's errors at the first
request | errors at the call |
| `withColumn(5, col)`, `withMetadata(5, {})` | `AssertionError`
(dataframe.py:961-970) | `TypeError` |
| `F.col(5)`, `F.col(None)` | `TypeError` | the `AttributeError` of
`endswith` |
| `F.expr(5)`, `filter(5)`, `selectExpr(b"a")`, `lateralJoin(on="a")`,
`agg({b"a": ...})` | PySpark's errors, such as the `AttributeError` of
`foreach` | other errors |
| `cast(MyType())` of a DataType subclass, or `cast(DataType())` |
`UNSUPPORTED_OPERATION` at plan build (expressions.py:988) | sent as `CAST(a AS
VOID)` |
**One mechanism.** The new `PlanArgs` of the binding
(`crates/pyspark-rs/src/plan_args.rs`) keeps a value as Python gave it, and
converts it with protobuf's semantics when the plan is built.
- It converts the string, int, double and bool fields as upb does. The first
error, in PySpark's order, is attached to the plan (`DataFrame::unbuildable`)
or to the expression that holds it, so that its first request raises it and
sends nothing.
- The existing deferrals of `withColumns`, `_joinAsOf` and the names of
`Column.alias` use it.
- A DataFrame of such a value keeps its parent's cached schema where PySpark
copies it: `alias`, `filter`, `sort`, `sortWithinPartitions`, `dropDuplicates`,
`hint` and `observe`, as #786 (SPARK-61875) did for the counts and seeds.
**In the core.**
- A `LogicalPlan::Unbuildable` records its error when it is serialized,
after its input, in the same slot as an unbuildable expression. So a request
raises the first error of the plan or of its expressions, in PySpark's order of
the build.
- This also fixes a latent bug: an unbuildable plan inside a subquery was
sent silently.
- Repartition nodes serialize their partition expressions before their
input, as PySpark builds them.
- The thread tags of a session can hold a str that UTF-8 cannot encode, as
PySpark's do, so `SparkSession::tags()` returns a `Result`.
- `explain_string` builds the plan before it checks the mode.
- `spark-connect-core` gets the `BuiltinAttributeError` kind.
`docs/compatibility.md` and `dev/parity/known-differences.md` describe this,
and the differences that remain.
### Why are the changes needed?
So that the arguments of these APIs fail where and as PySpark's fail, take
what PySpark takes, such as bytes for names, and a DataFrame or Column that is
not used raises nothing, as in PySpark. #754 (SPARK-61766) started this for the
counts and seeds of some DataFrame methods, and for the readers.
### Does this PR introduce _any_ user-facing change?
Yes, as above:
- these errors are raised at the first request, with PySpark's types and
messages;
- bytes are taken for names;
- some errors at the call change to PySpark's.
In the Rust API, `SparkSession::tags()` returns a `Result`, and there are
new doc-hidden methods for tags that UTF-8 cannot encode.
Documented differences that remain:
- A DataFrame whose plan fails raises at a (cloud)pickle, where PySpark
pickles it, as for `limit(2.0)` before.
- A join on a tuple renders `schema` as `Column<'schema'>` where PySpark's
is `Column<'_schema'>`, and its `columns` is a Column where PySpark raises
`NOT_ITERABLE`.
- A lone surrogate shows escaped in a `repr`: upper-cased in a cast type,
and doubled inside a list literal.
- An exception other than Python's `AttributeError`, `TypeError`,
`ValueError` and `OverflowError`, and PySpark's error classes, raised by an
object in a join list, is raised at the call.
- The order of the errors within one node can differ in rare cases of
several errors.
- The Window frame bounds, and a `StructField` given as a UDF's return type,
still convert at the call, as documented.
### How was this patch tested?
- **New tests in `test_dropin_offline.py`.** Each fails on master.
- The Column builders, with their repr, copy, deepcopy and pickle.
- The errors at the call.
- About 75 DataFrame calls of a str that UTF-8 cannot encode.
- The values that protobuf refuses, and the bytes it takes, also of `Row`s
in join lists.
- The errors in PySpark's order, and the first error of the build.
- The casts to types that PySpark does not convert.
- The tags, the Observation, and `NOT_NUMERIC_COLUMNS`.
- The cached schemas that PySpark copies.
- **Other tests.** `test_dropin_udf_args_offline.py`: the names and keywords
of UDF and UDTF arguments. The tests that pinned the old behavior are updated:
dropna's thresh, range, fills, protobuf's string fields, literals, numpy
values, int literals and join types.
- **Probes of both clients** match, apart from the documented differences.
- **Python.**
- The offline suite of `coverage.yml`.
- `test_dropin_offline.py` and `test_dropin_udf_args_offline.py` on pandas
2 and 3 (2353 and 2352 passed).
- Live `test_dropin_e2e.py` and `test_statistics_e2e.py` (370 passed).
- **Rust.**
- `cargo test -p apache-spark-connect --release --lib` (753 passed), `-p
apache-spark-connect-core --lib`, and `cargo test -p pyspark-rs --lib`.
- Live `e2e_session_catalog`, `e2e_dataframe`, `e2e_coverage`,
`e2e_integration`, `e2e_new_methods` and `coverage_plan_extra` (96 passed).
- **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy` of the three
crates with `-D warnings`, ruff 0.14.0, `audit_no_stubs.sh` and `scripts/tests`.
### 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]