You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by pt...@apache.org on 2026/04/02 17:03:52 UTC
(spark) branch branch-4.0 updated: [SPARK-56321][SQL] Fix `AnalysisException` when scan reports transform-based ordering via `SupportsReportOrdering`
This is an automated email from the ASF dual-hosted git repository.
ptoth pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.0 by this push:
new 1d18ad15184c [SPARK-56321][SQL] Fix `AnalysisException` when scan reports transform-based ordering via `SupportsReportOrdering`
1d18ad15184c is described below
commit 1d18ad15184c6520849cc1fde1e3cae9a9b36ed8
Author: Peter Toth <pe...@gmail.com>
AuthorDate: Thu Apr 2 19:02:49 2026 +0200
[SPARK-56321][SQL] Fix `AnalysisException` when scan reports transform-based ordering via `SupportsReportOrdering`
### What changes were proposed in this pull request?
`V2ScanPartitioningAndOrdering.ordering` was calling `V2ExpressionUtils.toCatalystOrdering` without the `funCatalog` argument. This meant that function-based sort expressions reported by a data source via `SupportsReportOrdering` (e.g. `bucket(n, col)`) could not be resolved against the function catalog and caused an `AnalysisException` during query planning.
The fix passes `relation.funCatalog` as the third argument, consistent with how `toCatalystOpt` is already called in the `partitioning` rule of the same object.
`InMemoryBaseTable` is also updated to use a new `InMemoryBatchScanWithOrdering` inner class (implementing `SupportsReportOrdering`) when the table is created with a non-empty ordering. This makes the test infrastructure correctly exercise the `SupportsReportOrdering` code path.
### Why are the changes needed?
Without the function catalog, sort orders involving catalog functions reported by `SupportsReportOrdering` cannot be resolved, causing an `AnalysisException` during query planning instead of correctly recognizing the reported ordering.
### Does this PR introduce _any_ user-facing change?
Yes. Data sources implementing `SupportsReportOrdering` with function-based sort expressions (e.g. `bucket`) that require the function catalog will now have those sort orders correctly recognized by Spark instead of throwing an `AnalysisException`.
### How was this patch tested?
A new test `SPARK-56321: scan with SupportsReportOrdering and function-based sort order` is added to `WriteDistributionAndOrderingSuite`. It creates a table with `bucket(4, "id")` partitioning and ordering, queries it, and asserts that the scan reports a non-empty `outputOrdering` with a `TransformExpression` — verifying the bucket transform was resolved correctly via the function catalog.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Sonnet 4.6
Closes #55137 from peter-toth/SPARK-56321-use-function-catalog-in-v2scanpartitioningandordering.
Authored-by: Peter Toth <pe...@gmail.com>
Signed-off-by: Peter Toth <pe...@gmail.com>
(cherry picked from commit 671c65fd3d2f357e8e0d50d6298c4a70ac996ab7)
Signed-off-by: Peter Toth <pe...@gmail.com>
---
.../sql/connector/catalog/InMemoryBaseTable.scala | 21 ++++++++++++++++++--
.../v2/V2ScanPartitioningAndOrdering.scala | 3 ++-
.../WriteDistributionAndOrderingSuite.scala | 23 ++++++++++++++++++++--
3 files changed, 42 insertions(+), 5 deletions(-)
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/connector/catalog/InMemoryBaseTable.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/connector/catalog/InMemoryBaseTable.scala
index ec858e62aa37..0ee37e58bd39 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/connector/catalog/InMemoryBaseTable.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/connector/catalog/InMemoryBaseTable.scala
@@ -346,8 +346,15 @@ abstract class InMemoryBaseTable(
private var _pushedFilters: Array[Filter] = Array.empty
override def build: Scan = {
- val scan = InMemoryBatchScan(
- data.map(_.asInstanceOf[InputPartition]).toImmutableArraySeq, schema, tableSchema, options)
+ val scan = if (InMemoryBaseTable.this.ordering.nonEmpty) {
+ new InMemoryBatchScanWithOrdering(
+ data.map(_.asInstanceOf[InputPartition]).toImmutableArraySeq, schema, tableSchema,
+ options)
+ } else {
+ InMemoryBatchScan(
+ data.map(_.asInstanceOf[InputPartition]).toImmutableArraySeq, schema, tableSchema,
+ options)
+ }
if (evaluableFilters.nonEmpty) {
scan.filter(evaluableFilters)
}
@@ -508,6 +515,16 @@ abstract class InMemoryBaseTable(
}
}
+ private class InMemoryBatchScanWithOrdering(
+ data: Seq[InputPartition],
+ readSchema: StructType,
+ tableSchema: StructType,
+ options: CaseInsensitiveStringMap)
+ extends InMemoryBatchScan(data, readSchema, tableSchema, options)
+ with SupportsReportOrdering {
+ override def outputOrdering(): Array[SortOrder] = InMemoryBaseTable.this.ordering
+ }
+
abstract class InMemoryWriterBuilder(val info: LogicalWriteInfo)
extends SupportsTruncate with SupportsDynamicOverwrite with SupportsStreamingUpdateAsAppend {
diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanPartitioningAndOrdering.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanPartitioningAndOrdering.scala
index 7ee4374b4578..657bc207261e 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanPartitioningAndOrdering.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanPartitioningAndOrdering.scala
@@ -69,7 +69,8 @@ object V2ScanPartitioningAndOrdering extends Rule[LogicalPlan] with Logging {
private def ordering(plan: LogicalPlan) = plan.transformDown {
case d @ DataSourceV2ScanRelation(relation, scan: SupportsReportOrdering, _, _, _) =>
- val ordering = V2ExpressionUtils.toCatalystOrdering(scan.outputOrdering(), relation)
+ val ordering =
+ V2ExpressionUtils.toCatalystOrdering(scan.outputOrdering(), relation, relation.funCatalog)
d.copy(ordering = Some(ordering))
}
}
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/connector/WriteDistributionAndOrderingSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/connector/WriteDistributionAndOrderingSuite.scala
index f62e092138a9..8fd83c17ae1d 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/connector/WriteDistributionAndOrderingSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/connector/WriteDistributionAndOrderingSuite.scala
@@ -22,7 +22,7 @@ import java.sql.Date
import java.util.Collections
import org.apache.spark.sql.{catalyst, AnalysisException, DataFrame, Row}
-import org.apache.spark.sql.catalyst.expressions.{ApplyFunctionExpression, Cast, Literal}
+import org.apache.spark.sql.catalyst.expressions.{ApplyFunctionExpression, Cast, Literal, TransformExpression}
import org.apache.spark.sql.catalyst.expressions.objects.Invoke
import org.apache.spark.sql.catalyst.plans.physical
import org.apache.spark.sql.catalyst.plans.physical.{CoalescedBoundary, CoalescedHashPartitioning, HashPartitioning, RangePartitioning, UnknownPartitioning}
@@ -30,9 +30,10 @@ import org.apache.spark.sql.connector.catalog.{Column, Identifier}
import org.apache.spark.sql.connector.catalog.functions._
import org.apache.spark.sql.connector.distributions.{Distribution, Distributions}
import org.apache.spark.sql.connector.expressions._
-import org.apache.spark.sql.connector.expressions.LogicalExpressions._
+import org.apache.spark.sql.connector.expressions.Expressions._
import org.apache.spark.sql.execution.{QueryExecution, SortExec, SparkPlan}
import org.apache.spark.sql.execution.adaptive.AQEShuffleReadExec
+import org.apache.spark.sql.execution.datasources.v2.BatchScanExec
import org.apache.spark.sql.execution.datasources.v2.V2TableWriteExec
import org.apache.spark.sql.execution.exchange.ShuffleExchangeLike
import org.apache.spark.sql.execution.streaming.MemoryStream
@@ -1488,4 +1489,22 @@ class WriteDistributionAndOrderingSuite extends DistributionAndOrderingSuiteBase
Seq(None)
}
}
+
+ test("SPARK-56321: Scan with SupportsReportOrdering and function-based sort order") {
+ val bucketById = bucket(4, "id")
+ val tableOrdering = Array(sort(bucketById, SortDirection.ASCENDING, NullOrdering.NULLS_FIRST))
+ catalog.createTable(ident, columns, Array(bucketById), emptyProps,
+ Distributions.unspecified(), tableOrdering, None, None)
+
+ sql(s"INSERT INTO testcat.ns1.test_table VALUES (1, 'a', date '2021-01-01')")
+
+ val df = sql("SELECT id, data FROM testcat.ns1.test_table")
+ val scans = collect(df.queryExecution.executedPlan) { case s: BatchScanExec => s }
+ assert(scans.size === 1)
+ val ordering = scans.head.outputOrdering
+ assert(ordering.nonEmpty,
+ "scan should report non-empty outputOrdering via SupportsReportOrdering")
+ assert(ordering.head.child.isInstanceOf[TransformExpression],
+ "bucket-based sort order should resolve to a TransformExpression")
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org