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 ef4d179b [MINOR][CONNECT][DOCS] Say that an empty sort fails when it
is analyzed or run, and keeps a cached schema as PySpark's does
ef4d179b is described below
commit ef4d179b14f9d80ab8f79c13f966783f5d87696d
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Fri Nov 13 15:54:11 2026 +0900
[MINOR][CONNECT][DOCS] Say that an empty sort fails when it is analyzed or
run, and keeps a cached schema as PySpark's does
### What changes were proposed in this pull request?
A follow-up of the post-merge review of #603. The rustdoc of
`DataFrame::sort`, `order_by` and `sort_within_partitions` now says what an
empty `columns` does:
- It builds a sort that the server rejects when the DataFrame is analyzed
or run, with `CONNECT_INVALID_PLAN.ASSERTION_FAILURE`. The old text said only
"when the DataFrame runs".
- Its `schema()` returns the cached schema of the input without asking the
server, as PySpark's `sort([])` does: PySpark's sort sets `res._cached_schema =
self._cached_schema`.
- PySpark's `df.sort()` with no arguments still raises `CANNOT_BE_EMPTY` at
the call. The unverified "as with the Scala client" claim is dropped; Scala's
client analyzes each Dataset lazily, so it would ask the server.
The behavior is unchanged. New tests pin it, since it matches PySpark 4.3.0.
### Why are the changes needed?
The review found that the docs left out the analysis-time rejection and the
cached schema. With the input's schema cached, `df.sort([]).schema` returns the
input's schema with no error while `collect()` fails. That holds for PySpark
4.3.0 and for this client alike (checked with the reference client), so the
docs now say so instead of the behavior changing.
### Does this PR introduce _any_ user-facing change?
No. It changes rustdoc and adds tests.
### How was this patch tested?
New tests. They guard existing parity, so they pass without the doc change
too:
- Rust unit (offline):
`an_empty_sort_keeps_the_cached_schema_of_its_input_as_pyspark_does`.
- Rust e2e: `an_empty_sort_keeps_the_cached_schema_and_fails_when_it_runs`.
After `schema()` is cached, each empty sort's `schema()` equals the input's,
and `collect()` fails with `CONNECT_INVALID_PLAN.ASSERTION_FAILURE`.
- Python e2e:
`test_empty_sort_list_keeps_the_cached_schema_and_fails_when_it_runs`, for
`sort([])`, `orderBy([])` and `sortWithinPartitions([])`. A probe gives
identical results on PySpark 4.3.0.
Also passing, against Spark 4.3.0 with `TZ=UTC`:
- the Rust lib tests, the `--no-run` builds, `cargo fmt` and the stub audit;
- `e2e_dataframe`: 18 passed;
- CI's offline Python set: 3307 passed;
- `test_dropin_e2e.py`: 279 passed;
- ruff.
### 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 #625 from HyukjinKwon/MINOR-empty-sort-schema.
Authored-by: Hyukjin Kwon <[email protected]>
Signed-off-by: Hyukjin Kwon <[email protected]>
---
crates/spark-connect/src/dataframe.rs | 46 +++++++++++++++++++++++++----
crates/spark-connect/tests/e2e_dataframe.rs | 37 ++++++++++++++++++++++-
python/tests/test_dropin_e2e.py | 17 +++++++++++
3 files changed, 93 insertions(+), 7 deletions(-)
diff --git a/crates/spark-connect/src/dataframe.rs
b/crates/spark-connect/src/dataframe.rs
index 68836e9a..73ca1c6f 100644
--- a/crates/spark-connect/src/dataframe.rs
+++ b/crates/spark-connect/src/dataframe.rs
@@ -620,9 +620,10 @@ impl DataFrame {
/// nulls variants, sorts as it says; any other column is sorted
ascending, nulls first,
/// as PySpark's `sort` sorts it.
///
- /// An empty `columns` builds a sort that the server rejects when the
DataFrame runs, with
- /// `CONNECT_INVALID_PLAN.ASSERTION_FAILURE`, as with the Scala client.
PySpark's `df.sort()`
- /// raises `CANNOT_BE_EMPTY` at the call instead.
+ /// An empty `columns` builds a sort that the server rejects when the
DataFrame is analyzed
+ /// or run, with `CONNECT_INVALID_PLAN.ASSERTION_FAILURE`. Its `schema()`
returns the cached
+ /// schema of this DataFrame without asking the server, as PySpark's
`sort([])` does. PySpark's
+ /// `df.sort()` raises `CANNOT_BE_EMPTY` at the call instead.
pub fn sort<C: Into<Column>>(&self, columns: impl IntoIterator<Item = C>)
-> DataFrame {
let plan = LogicalPlan::Sort {
input: Arc::clone(&self.plan),
@@ -633,7 +634,7 @@ impl DataFrame {
}
/// Alias for [`DataFrame::sort`]. An empty `columns` fails as it does for
- /// [`DataFrame::sort`], when the DataFrame runs.
+ /// [`DataFrame::sort`], when the DataFrame is analyzed or run.
pub fn order_by<C: Into<Column>>(&self, columns: impl IntoIterator<Item =
C>) -> DataFrame {
self.sort(columns)
}
@@ -2372,8 +2373,8 @@ impl DataFrame {
}
/// Sort within partitions (local sort), by `columns` as
[`DataFrame::sort`] sorts by them.
- /// An empty `columns` fails as it does for [`DataFrame::sort`], when the
DataFrame runs,
- /// where PySpark's `df.sortWithinPartitions()` raises `CANNOT_BE_EMPTY`
at the call.
+ /// An empty `columns` fails as it does for [`DataFrame::sort`], when the
DataFrame is analyzed
+ /// or run, where PySpark's `df.sortWithinPartitions()` raises
`CANNOT_BE_EMPTY` at the call.
pub fn sort_within_partitions<C: Into<Column>>(
&self,
columns: impl IntoIterator<Item = C>,
@@ -5131,6 +5132,39 @@ mod cache_tests {
.is_none());
}
+ #[test]
+ fn an_empty_sort_keeps_the_cached_schema_of_its_input_as_pyspark_does() {
+ let spark = SparkSession::builder()
+ .remote("sc://localhost:15002")
+ .get_or_create()
+ .unwrap();
+ let df = spark.range(3).unwrap();
+ let schema = DataType::Struct { fields: vec![] };
+ *df.schema_cache.lock().unwrap() = Some(Arc::new(schema.clone()));
+
+ // The schema of a sort is the cached schema of its input, whatever
the form of the
+ // sort and whether or not it has columns: PySpark's `sort([])` keeps
it too.
+ assert_eq!(
+ df.sort(Vec::<Column>::new()).cached_schema().as_deref(),
+ Some(&schema)
+ );
+ assert_eq!(
+ df.order_by(Vec::<Column>::new()).cached_schema().as_deref(),
+ Some(&schema)
+ );
+ assert_eq!(
+ df.sort_within_partitions(Vec::<Column>::new())
+ .cached_schema()
+ .as_deref(),
+ Some(&schema)
+ );
+ assert_eq!(df.sort(["id"]).cached_schema().as_deref(), Some(&schema));
+ assert_eq!(
+ df.sort_within_partitions(["id"]).cached_schema().as_deref(),
+ Some(&schema)
+ );
+ }
+
#[test]
fn schema_cache_recovers_after_analysis_panics() {
let spark = SparkSession::builder()
diff --git a/crates/spark-connect/tests/e2e_dataframe.rs
b/crates/spark-connect/tests/e2e_dataframe.rs
index 168cc9eb..ba984d12 100644
--- a/crates/spark-connect/tests/e2e_dataframe.rs
+++ b/crates/spark-connect/tests/e2e_dataframe.rs
@@ -6,7 +6,7 @@
mod common;
use common::{session, should_run};
-use spark_connect::column::{col, lit};
+use spark_connect::column::{col, lit, Column};
use spark_connect::dataframe::DataFrame;
use spark_connect::functions as f;
use spark_connect::plan::JoinType;
@@ -311,6 +311,41 @@ fn list_methods_take_any_iterable_on_the_server() {
);
}
+/// A sort of no columns keeps the cached schema of its input, as PySpark's
`sort([])` does, so its
+/// `schema()` returns that schema without an error, while the server rejects
the sort when it runs.
+#[test]
+fn an_empty_sort_keeps_the_cached_schema_and_fails_when_it_runs() {
+ if !should_run() {
+ return;
+ }
+ let s = session();
+ let df = s.range(3).unwrap();
+ let schema = df.schema().unwrap();
+
+ let empty = || Vec::<Column>::new();
+ let sorts = [
+ ("sort", df.sort(empty())),
+ ("order_by", df.order_by(empty())),
+ ("sort_within_partitions", df.sort_within_partitions(empty())),
+ ];
+ for (name, sorted) in sorts {
+ assert_eq!(sorted.schema().unwrap(), schema, "{name} of no columns");
+ let err = sorted
+ .collect()
+ .err()
+ .unwrap_or_else(|| panic!("{name} of no columns runs"));
+ assert_eq!(
+ err.get_condition().as_deref(),
+ Some("CONNECT_INVALID_PLAN.ASSERTION_FAILURE"),
+ "{name}: {err}"
+ );
+ }
+
+ let sorted = df.sort(["id"]);
+ assert_eq!(sorted.schema().unwrap(), schema);
+ assert_eq!(sorted.count().unwrap(), 3);
+}
+
/// A pivot on a date, timestamp or decimal column takes typed values. The
counts match those
/// of PySpark 4.3.0 pivoting the same column on the same values as strings,
which the server
/// casts to the column's type.
diff --git a/python/tests/test_dropin_e2e.py b/python/tests/test_dropin_e2e.py
index feeca9eb..87da5ed0 100644
--- a/python/tests/test_dropin_e2e.py
+++ b/python/tests/test_dropin_e2e.py
@@ -1098,6 +1098,23 @@ ARROW_TABLE_ROWS = [
]
+def
test_empty_sort_list_keeps_the_cached_schema_and_fails_when_it_runs(session):
+ # As PySpark's: a sort of an empty list keeps the cached schema of its
input, so its schema
+ # is returned without asking the server, while the server rejects the sort
when it runs.
+ df = session.range(3)
+ schema = df.schema
+ for sort in (
+ lambda d: d.sort([]),
+ lambda d: d.orderBy([]),
+ lambda d: d.sortWithinPartitions([]),
+ ):
+ empty = sort(df)
+ assert empty.schema == schema
+ with pytest.raises(Exception) as error:
+ empty.collect()
+ assert error.value.getCondition() ==
"CONNECT_INVALID_PLAN.ASSERTION_FAILURE"
+
+
def test_pickled_sort_columns_sort_on_the_server_as_pyspark(session, tmp_path):
# As PySpark's: a pickled sort Column is a sort order again, so orderBy, a
window and
# repartitionByRange sort by it as by the Column built without the
pickling, and a
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]