HyukjinKwon opened a new pull request, #786:
URL: https://github.com/apache/spark-connect-rust/pull/786
### What changes were proposed in this pull request?
Four plan-time gaps that a review found against PySpark 4.3.0, each probed
on both clients against a Spark 4.3.0 server.
**1. The cached schema of DataFrames whose counts or seeds fail at plan
build.** Since #754 (SPARK-61766), a count or a seed that PySpark refuses at
plan build gives a DataFrame whose plan fails at its first request. That
DataFrame dropped its parent's cached schema, so `df.schema`, `columns` and the
like sent an AnalyzePlan, or failed. PySpark's `limit`, `coalesce`,
`repartition`, `repartitionByRange`, `repartitionById`, `sample` and
`randomSplit` copy `_cached_schema`. They now keep it:
- the core has a doc-hidden `DataFrame::unbuildable_with_cached_schema`;
- `offset`, `sampleBy`, `withColumns` and `_joinAsOf` do not keep it, as
PySpark's do not.
**2. Copies and pickles keep the instance attributes**, as #754 made those
of the writers, the readers and the stream writer keep them.
| object | before | now, as PySpark |
|---|---|---|
| `df.na`, `df.stat`, `spark.tvf`, `spark.dataSource`, `PandasCogroupedOps`,
`StreamingQuery` | `copy.copy`, `copy.deepcopy` and cloudpickle dropped the
attributes set on the instance | kept |
| `StreamingQueryManager`, `RuntimeConf`, the merge builder | copies dropped
them | kept |
| the merge actions, `SparkSession.Builder` | no `__dict__` | a `__dict__`
and weak references, as PySpark's plain classes |
| `pickle.dumps(spark.streams)` | a hardcoded `TypeError` about
`'_thread.lock'` | the `AttributeError` about `creator`, and the
`'_thread.lock'` `TypeError` of a deep copy and of cloudpickle at protocols 2
to 5 |
| the conf, the merge builder and its actions at protocols 0 and 1 | "cannot
pickle '_thread._local' object" | "cannot pickle '_local' object" |
**3. Python origins.** The origins of these expressions differed from
PySpark's, which this PR makes them:
- `groupBy(...).count()` is `count(lit(1))`, with the `alias` origin on its
`.alias("count")` only (group.py:212);
- `sum`, `avg`, `mean`, `min` and `max` give the `col` origin to their
columns only (group.py:181). This uses a new doc-hidden core
`GroupedData::numeric_agg_columns`;
- `agg({...})` gives the `col` origin to its columns, not to the functions;
- `repartitionByRange` of names wraps them in `asc_nulls_first`, of that
origin (dataframe.py:272-274);
- the conditions of a join or `_joinAsOf` of a list of Columns. PySpark
combines them with `&` when it builds the plan (plan.py:1150 and 1235), so
their `__and__` origin is of the call site of each request, not of the join. A
new doc-hidden `ExpressionNode::PlanBuildOrigin` takes its origin when the plan
is built.
**4. The warning of a window of no partitions.** PySpark's
`WindowExpression.to_plan` warns `WARN WindowExpression: No Partition Defined
for Window operation! ...` at each plan build of such a window
(expressions.py:1125-1130). The drop-in never warned.
- The core's new doc-hidden `plan_build` module holds a process-wide
`PlanBuildListener`, which a client of the core can set. The core tells it of
such a window once it has serialized the window function, and asks it for the
origins of the expressions that PySpark makes at plan build. Without a
listener, which is the case of the Rust API, nothing changes.
- The binding's listener calls `_warn_no_partition()` of
`pyspark.sql.connect.expressions`, a `warnings.warn` of PySpark's text. So the
warning's category, module, registry and filters are PySpark's. It is not
raised while a DataFrame or Column is pickled, as PySpark's pickle builds no
plan.
`docs/compatibility.md` and `dev/parity/known-differences.md` describe
these, and the differences that remain.
### Why are the changes needed?
So that these DataFrames, objects, origins and warnings behave as PySpark's.
The origins go to the server, which shows them in the query context of its
errors.
### Does this PR introduce _any_ user-facing change?
Yes, as above:
- the cached schema is kept in more cases;
- copies and pickles keep the instance attributes, and some pickle errors
change;
- the origins of those expressions change;
- a window of no partitions warns at each plan build.
Documented differences that remain:
- A DataFrame or Column that is unpickled, and a deep copy of a DataFrame,
send the relation that was built at the pickle or the copy. So their later
requests do not warn, and keep the `__and__` origin of that time.
- `DataFrame._plan.plan()` is the drop-in's placeholder, so it does not warn
there.
- The warning's line number is of the drop-in's module.
- `vars()` of these objects holds only the attributes set by the user.
- `copy.copy(SparkSession.builder)` does not share `_options` and the hook
factories, where PySpark's copy does.
### How was this patch tested?
- **New tests in `test_dropin_offline.py`.** 19 of their 32 cases fail with
master's extension; the others cover classes and errors that were already right.
-
`test_counts_and_seeds_that_fail_the_plan_keep_the_cached_schema_as_pyspark`;
-
`test_aggregates_range_partitions_and_join_conditions_carry_pysparks_origins`,
whose expected origins are the output of both clients for the same script;
- `test_windows_of_no_partitions_warn_at_each_plan_build_as_pyspark`;
- `test_copies_and_pickles_keep_the_instance_attributes_as_pyspark` (22
cases);
- `test_plain_pickles_of_the_uncopyable_objects_fail_as_pyspark`;
-
`test_builders_merge_actions_and_queries_keep_their_instance_attributes_as_pyspark`.
- `test_pickles_of_the_objects_at_every_protocol_as_pyspark` is updated: it
pinned the errors at protocols 0 and 1, which are not PySpark's.
- Rust unit tests of the listener and of the cached schema.
- **Probes of both clients** match:
- the cached schemas;
- the origins of the fragments and call-site lines, including the
`__and__` at the line of the `collect()`;
- the window warning: its message, category, module, registry, show-once,
and an error filter. They also match its count over 45 operations, such as
`schema`, `collect`, `count`, `show`, `toPandas`, `toArrow`, `explain`,
`cache`, copies, `sameSemantics` and joins;
- the copies, and the pickles of the stream manager at protocols 0 to 5.
- `cargo test -p apache-spark-connect --release --lib` (746 passed), and its
`--tests` against a Spark 4.3.0 server (1302 passed).
- Python: the offline suite of `coverage.yml` on pandas 2,
`test_dropin_offline.py` on pandas 3, and live `test_dropin_e2e.py` (299
passed).
- Lint: `cargo fmt --all --check`, `cargo +1.100.0 clippy -p
apache-spark-connect -p pyspark-rs --all-targets -- -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]