peter-toth commented on code in PR #59410:
URL: https://github.com/apache/spark/pull/59410#discussion_r4267596147
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/KeyGroupedPartitioningSuite.scala:
##########
@@ -2908,6 +2908,72 @@ class KeyGroupedPartitioningSuite extends
DistributionAndOrderingSuiteBase {
}
}
+ test("SPARK-60203: SPJ with join-key subset projects originalPartitionValues
when AQE " +
+ "is enabled") {
+ val table1 = "table1"
+ val table2 = "table2"
+ val cols = Array(
+ Column.create("id", StringType),
+ Column.create("date", StringType),
+ Column.create("data", StringType))
+ // The join key is the second partition key of `table1` (`bucket(32,
id)`), after the
+ // leading identity key `date`. After SPJ pushdown, callers such as
ValidateRequirements
+ // invoke `createShuffleSpec` on the scan's projected
`outputPartitioning`, which
+ // re-projects `originalPartitionValues` using the already-trimmed
expressions.
+ createTable(table1, cols,
+ Array(identity("date"), bucket(32, "id"), bucket(32, "data")))
+ sql(s"INSERT INTO testcat.ns.$table1 VALUES " +
+ "('a', '2026-05-01', 'aa'), " +
+ "('b', '2026-05-01', 'bb'), " +
+ "('c', '2026-05-02', 'aa')")
+
+ createTable(table2, cols, Array(bucket(32, "id")))
+ sql(s"INSERT INTO testcat.ns.$table2 VALUES " +
+ "('a', '2026-05-01', 'xx'), " +
+ "('b', '2026-05-01', 'yy'), " +
+ "('d', '2026-05-01', 'zz')")
+
+ Seq(true, false).foreach { partiallyClustered =>
+ withSQLConf(
+ SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.SKEW_JOIN_ENABLED.key -> "true",
+ SQLConf.REQUIRE_ALL_CLUSTER_KEYS_FOR_CO_PARTITION.key -> "false",
+ SQLConf.V2_BUCKETING_ALLOW_JOIN_KEYS_SUBSET_OF_PARTITION_KEYS.key ->
"true",
+ SQLConf.V2_BUCKETING_PARTIALLY_CLUSTERED_DISTRIBUTION_ENABLED.key ->
+ partiallyClustered.toString) {
+ val df = sql(
+ s"""
+ |${selectWithMergeJoinHint("t1", "t2")}
+ |t1.id, t1.date, t1.data AS t1data, t2.data AS t2data
+ |FROM testcat.ns.$table1 t1 JOIN testcat.ns.$table2 t2
+ |ON t1.id = t2.id
+ |ORDER BY t1.id, t1.date, t1data
+ |""".stripMargin)
+ val executedPlan = df.queryExecution.executedPlan
+ val scans = collectScans(executedPlan)
+ assert(
+ scans.exists(_.spjParams.joinKeyPositions.contains(Seq(1))),
+ "SPJ should project the scan partitioning down to the bucket join
key")
+ val shuffles = collectShuffles(executedPlan)
+ assert(shuffles.isEmpty, "SPJ should be triggered")
+ val smjs = collect(executedPlan) { case j: SortMergeJoinExec => j }
+ assert(smjs.size == 1, "expected a sort-merge join")
+ val smj = smjs.head
+ val projectedScan =
scans.find(_.spjParams.joinKeyPositions.contains(Seq(1))).get
+ val kgp =
projectedScan.outputPartitioning.asInstanceOf[KeyGroupedPartitioning]
+ assert(kgp.originalPartitionValues.nonEmpty)
+ assert(kgp.originalPartitionValues.forall(_.numFields ==
kgp.expressions.length),
+ "originalPartitionValues should be projected to the join keys")
+ // Without the fix, this throws when a string leading partition value
is read as int.
Review Comment:
**Finding 2.** This test checks the arity of the projected rows, not their
values. I changed the fix to project onto `bucket(data)` instead of
`bucket(id)` and the test still passed.
The comment here doesn't hold for this source either. Without the fix
`createShuffleSpec` returns normally, since `GenericInternalRow.get` doesn't
check the type. The rows just hold the `date` strings, and the test fails only
at the assertion on line 2966.
Comparing against the scan's own raw keys catches both cases. I ran the
version below. It passes with the fix, and fails both without it and with the
wrong-position projection. It needs `import
org.apache.spark.sql.connector.read.HasPartitionKey`.
```scala
assert(kgp.originalPartitionValues.forall(_.numFields ==
kgp.expressions.length),
"originalPartitionValues should be projected to the join keys")
// Each split's `bucket(32, id)` value, the second field of its raw
partition key.
val expectedOriginals = projectedScan.inputPartitions.map(
_.asInstanceOf[HasPartitionKey].partitionKey().getInt(1))
assert(kgp.originalPartitionValues.map(_.getInt(0)).sorted ==
expectedOriginals.sorted)
```
The explicit `createShuffleSpec` call can then go, or its comment can say it
is the call that threw for Iceberg. Also, the comment on lines 2920-2922 only
holds for `partiallyClustered = false`. A partially clustered partitioning
fails `satisfies`, so `ValidateRequirements` returns before it calls
`createShuffleSpec`.
--
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]