This is an automated email from the ASF dual-hosted git repository.
dongjoon-hyun pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.1 by this push:
new 2d7fe4c8b7da [SPARK-59710][SQL] Fix JDBC V2 join pushdown for
partitioned reads and partial AVG
2d7fe4c8b7da is described below
commit 2d7fe4c8b7da4b3bcbd602bc224b9195ab2030c4
Author: Uros Stankovic <[email protected]>
AuthorDate: Wed Sep 23 08:35:09 2026 +0900
[SPARK-59710][SQL] Fix JDBC V2 join pushdown for partitioned reads and
partial AVG
### What changes were proposed in this pull request?
Fix two JDBC Data Source V2 join pushdown bugs:
- Keep reads with `partitionColumn` configured as Spark-side joins,
preserving partitioned JDBC reads.
- Preserve pushed-join column aliases when rewriting a partially pushed
`AVG` into `SUM` and `COUNT`.
Add SQL regressions to `JDBCV2JoinPushdownIntegrationSuiteBase`.
### Why are the changes needed?
- Join pushdown creates a JDBC `query`, which cannot be combined with
`partitionColumn`. Partitioned inputs currently fails here:
https://github.com/apache/spark/blob/5de052869182bf8e91cd206b4ac730ffbb10bd92/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JDBCOptions.scala#L148
- There is an optimizer rule that pushdown partial `AVG` aggregation,
normalization in join pushdown uses the original relation output instead of the
pushed-join aliases. This can silently aggregate the wrong column: the H2
self-join regression returns `5.413043` instead of `4.413043` without the fix
(correctness issue).
### Does this PR introduce _any_ user-facing change?
No, bug fixes
### How was this patch tested?
The shared SQL tests cover partitioned reads with one and two partitions,
and compare AVG results with one partition, two partitions, and join pushdown
disabled. The AVG test is skipped when aggregate pushdown is unsupported.
Confirmed both original regressions fail before their fixes. Also reran the
revised three-way AVG test without its fix and confirmed the incorrect result
above.
Validation on the committed changes with JDK 21 and H2: all 24 tests in
`JDBCV2JoinPushdownSuite` and all 3 selected partial-pushdown tests in
`JDBCV2Suite` passed.
```sh
build/sbt 'sql/testOnly *JDBCV2JoinPushdownSuite' \
'sql/testOnly *JDBCV2Suite -- -z "partial push-down"'
```
Other databases' integration suites were not run.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: OpenAI Codex CLI 0.154.0
Closes #58958 from urosstan-db/urosstan-db/jdbc-dsv2-join-investigation.
Authored-by: Uros Stankovic <[email protected]>
Signed-off-by: Hyukjin Kwon <[email protected]>
(cherry picked from commit 6bef3be02be2451d82bc25f01e1f801689a424f2)
Signed-off-by: Hyukjin Kwon <[email protected]>
(cherry picked from commit c6a988836411272d8cac845547fdf8ae4a0c91df)
Signed-off-by: Dongjoon Hyun <[email protected]>
(cherry picked from commit 60824d416f85748116407506a4ce11cd8f49ff1f)
Signed-off-by: Dongjoon Hyun <[email protected]>
(cherry picked from commit cf9bfb06d7d0238975f2430b7bb33bdab635b8cb)
Signed-off-by: Dongjoon Hyun <[email protected]>
---
.../datasources/v2/V2ScanRelationPushDown.scala | 4 +-
.../datasources/v2/jdbc/JDBCScanBuilder.scala | 6 +++
.../JDBCV2JoinPushdownIntegrationSuiteBase.scala | 61 ++++++++++++++++++++++
3 files changed, 69 insertions(+), 2 deletions(-)
diff --git
a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala
b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala
index adfd5ceacd67..41e72c289b61 100644
---
a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala
+++
b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala
@@ -513,8 +513,8 @@ object V2ScanRelationPushDown extends Rule[LogicalPlan]
with PredicateHelper {
aggExprToOutputOrdinal.clear()
val newAggregates =
collectAggregates(newResultExpressions, aggExprToOutputOrdinal)
- val newNormalizedAggExprs = DataSourceStrategy.normalizeExprs(
- newAggregates,
holder.relation.output).asInstanceOf[Seq[AggregateExpression]]
+ val newNormalizedAggExprs =
+ normalizeExpressions(newAggregates,
holder).asInstanceOf[Seq[AggregateExpression]]
val newTranslatedAggOpt = DataSourceStrategy.translateAggregation(
newNormalizedAggExprs, normalizedGroupingExpr)
if (newTranslatedAggOpt.isEmpty) {
diff --git
a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/jdbc/JDBCScanBuilder.scala
b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/jdbc/JDBCScanBuilder.scala
index b758ddd35e0d..ea8203210735 100644
---
a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/jdbc/JDBCScanBuilder.scala
+++
b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/jdbc/JDBCScanBuilder.scala
@@ -185,6 +185,12 @@ case class JDBCScanBuilder(
return false
}
+ // Join pushdown uses the query option, which cannot be combined with
partitionColumn.
+ if (jdbcOptions.partitionColumn.isDefined) {
+ logDebug("Skipping JDBC join pushdown because partitionColumn is
defined.")
+ return false
+ }
+
val joinTypeStringOption = joinType match {
case JoinType.INNER_JOIN => Some("INNER JOIN")
case JoinType.LEFT_OUTER_JOIN => Some("LEFT JOIN")
diff --git
a/sql/core/src/test/scala/org/apache/spark/sql/jdbc/v2/JDBCV2JoinPushdownIntegrationSuiteBase.scala
b/sql/core/src/test/scala/org/apache/spark/sql/jdbc/v2/JDBCV2JoinPushdownIntegrationSuiteBase.scala
index d0639f77d6da..7c50ac5866b0 100644
---
a/sql/core/src/test/scala/org/apache/spark/sql/jdbc/v2/JDBCV2JoinPushdownIntegrationSuiteBase.scala
+++
b/sql/core/src/test/scala/org/apache/spark/sql/jdbc/v2/JDBCV2JoinPushdownIntegrationSuiteBase.scala
@@ -302,6 +302,31 @@ trait JDBCV2JoinPushdownIntegrationSuiteBase
}
}
+ gridTest("Join pushdown preserves partitioned input reads")(Seq(1, 2)) {
numPartitions =>
+ val tableOptions = s"""WITH (
+ |'partitionColumn' '${caseConvert("id")}',
+ |'lowerBound' '0',
+ |'upperBound' '11',
+ |'numPartitions' '$numPartitions')""".stripMargin
+ val sqlQuery = s"""
+ |SELECT a.id, b.id
+ |FROM $catalogAndNamespace.$casedJoinTableName1 $tableOptions a
+ |JOIN $catalogAndNamespace.$casedJoinTableName1 $tableOptions b ON a.id
= b.id + 1
+ |""".stripMargin
+
+ val expectedRows = for {
+ (leftId, _, _) <- table1Data
+ (rightId, _, _) <- table1Data
+ if leftId == rightId + 1
+ } yield Row(leftId, rightId)
+
+ withSQLConf(SQLConf.DATA_SOURCE_V2_JOIN_PUSHDOWN.key -> "true") {
+ val df = sql(sqlQuery)
+ checkJoinNotPushed(df)
+ checkAnswer(df, expectedRows)
+ }
+ }
+
test("Test multi-way self join with conditions") {
val sqlQuery = s"""
|SELECT * FROM
@@ -480,6 +505,42 @@ trait JDBCV2JoinPushdownIntegrationSuiteBase
}
}
+ test("Join pushdown preserves aliases in partially pushed averages") {
+ assume(supportsAggregatePushdown, "Aggregate pushdown is not supported")
+ def sqlPartitionedQuery(numPartitions: Int): String = {
+ val idCol = caseConvert("id")
+ // numPartitions alone is valid and selects the partial aggregate path
when greater than 1.
+ val tableOptions = s"WITH ('numPartitions' '$numPartitions')"
+ // Some databases return an integer for AVG over integer input, so use
decimal input.
+ s"""
+ |SELECT avg(CAST(b.$idCol AS DECIMAL(10, 2)))
+ |FROM $catalogAndNamespace.$casedJoinTableName1 $tableOptions a
+ |JOIN $catalogAndNamespace.$casedJoinTableName1 $tableOptions b ON
a.$idCol = b.$idCol + 1
+ |""".stripMargin
+ }
+
+ val rowsWithJoinPushdown =
withSQLConf(SQLConf.DATA_SOURCE_V2_JOIN_PUSHDOWN.key -> "true") {
+ val completeAgg = sql(sqlPartitionedQuery(numPartitions = 1))
+ checkJoinPushed(completeAgg)
+ checkAggregateRemoved(completeAgg, pushed = true)
+ val expectedRows = completeAgg.collect().toSeq
+ assert(expectedRows.head.get(0) != null)
+
+ val partialAgg = sql(sqlPartitionedQuery(numPartitions = 2))
+ checkJoinPushed(partialAgg)
+ checkAggregateRemoved(partialAgg, pushed = false)
+ checkAnswer(partialAgg, expectedRows)
+ expectedRows
+ }
+
+ withSQLConf(SQLConf.DATA_SOURCE_V2_JOIN_PUSHDOWN.key -> "false") {
+ val df = sql(sqlPartitionedQuery(numPartitions = 2))
+ checkJoinNotPushed(df)
+ checkAggregateRemoved(df, pushed = false)
+ checkAnswer(df, rowsWithJoinPushdown)
+ }
+ }
+
test("Test aggregate on top of multi-way self join") {
val sqlQuery = s"""
|SELECT min(a.id + b.id), min(a.id), min(c.id - 2)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]