hhdri opened a new pull request, #60520:
URL: https://github.com/apache/spark/pull/60520

   ### What changes were proposed in this pull request?
   
   This PR makes analysis fail with a new error condition, `NESTED_SORT_ORDER` 
(SQLSTATE `42K0E`), when a sort direction is applied to an expression that is 
already a sort expression, for example:
   
   ```scala
   spark.range(10).orderBy($"id".desc.asc)
   ```
   
   ```python
   spark.range(10).orderBy(desc("id"), ascending=False)
   ```
   
   The check is added to both analyzers:
   
   - `CheckAnalysis`, for the fixed-point analyzer.
   - `ExpressionResolver.resolveSortOrder`, for the single-pass resolver, so 
that both analyzers agree on the result (otherwise the dual run fails with 
`HYBRID_ANALYZER_EXCEPTION.FIXED_POINT_FAILED_SINGLE_PASS_SUCCEEDED`).
   
   Because the check runs on the analyzed plan on the server, it covers the 
Scala and Python APIs, both classic and Spark Connect, in `orderBy`/`sort`, 
`sortWithinPartitions` and window specifications.
   
   This supersedes #44283, which was closed as stale and can no longer be 
reopened. That PR added a `require` to the `SortOrder` constructor instead. 
That failed while expressions were still being built and only covered the 
classic API, so this PR moves the check into analysis.
   
   ### Why are the changes needed?
   
   Today analysis accepts a nested sort order, `SortOrder(SortOrder(id, 
Descending), Ascending)`, and the query fails at execution time with an 
internal error that does not point at the user's code:
   
   ```
   org.apache.spark.SparkException: [INTERNAL_ERROR] Cannot generate code for 
expression: input[0, bigint, false] DESC NULLS LAST SQLSTATE: XX000
   ```
   
   With this PR:
   
   ```
   org.apache.spark.sql.AnalysisException: [NESTED_SORT_ORDER] Cannot apply a 
sort direction to the sort expression "id DESC NULLS LAST". Specify the sort 
direction and null ordering of an expression only once, for example by removing 
the extra asc/desc call or the `ascending` argument. SQLSTATE: 42K0E
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A query that evaluates a nested sort order now fails analysis with 
`NESTED_SORT_ORDER` instead of failing at execution time with `INTERNAL_ERROR`.
   
   A query whose nested sort the optimizer removes, such as 
`df.orderBy($"id".desc.asc).count()` (the sort is eliminated under an 
order-irrelevant aggregate), and operations that only analyze the query, such 
as `df.schema` or `df.explain()`, used to succeed and now fail too. This is 
described in the SQL migration guide for 4.4.
   
   An alternative would be to have `asc`/`desc` on a sort expression replace 
its direction instead of nesting it. That would keep these cases working, but 
`orderBy(desc("id"), ascending=False)` would then silently drop one of two 
conflicting directions, so this PR reports an error instead.
   
   ### How was this patch tested?
   
   New tests:
   
   - `AnalysisErrorSuite`: nested sort orders in `Sort` and in a window 
specification.
   - `DataFrameSuite`: `orderBy`, `sortWithinPartitions` and `Window.orderBy`, 
including the error's query context.
   - `test_dataframe.py` (`test_sort_ascending_with_sort_expression`): the 
PySpark `ascending=False` case from the JIRA ticket. This runs for both classic 
and Spark Connect through the parity suite.
   
   I also ran the existing suites locally:
   
   ```
   build/sbt 'core/testOnly org.apache.spark.SparkThrowableSuite'
   build/sbt 'catalyst/testOnly *AnalysisErrorSuite *AnalysisSuite 
*SortOrderExpressionsSuite org.apache.spark.sql.catalyst.analysis.resolver.*'
   build/sbt 'sql/testOnly org.apache.spark.sql.DataFrameSuite 
org.apache.spark.sql.DataFrameWindowFunctionsSuite 
org.apache.spark.sql.DataFrameWindowFramesSuite 
org.apache.spark.sql.errors.QueryCompilationErrorsSuite 
org.apache.spark.sql.analysis.resolver.*'
   python/run-tests --testnames 'pyspark.sql.tests.test_dataframe 
DataFrameTests.test_sort_ascending_with_sort_expression'
   python/run-tests --testnames 
'pyspark.sql.tests.connect.test_parity_dataframe 
DataFrameParityTests.test_sort_ascending_with_sort_expression'
   ```
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   


-- 
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