cloud-fan commented on code in PR #51979:
URL: https://github.com/apache/spark/pull/51979#discussion_r2269754846


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/KeyGroupedPartitionedScan.scala:
##########
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.RowOrdering
+import org.apache.spark.sql.catalyst.plans.physical.{KeyGroupedPartitioning, 
KeyGroupedShuffleSpec}
+import org.apache.spark.sql.catalyst.util.InternalRowComparableWrapper
+import org.apache.spark.sql.execution.datasources.v2.StoragePartitionJoinParams
+
+/** Base trait for a data source scan capable of producing a key-grouped 
output. */
+trait KeyGroupedPartitionedScan[T] {
+  /**
+   * The output partitioning of this scan after applying any pushed-down SPJ 
parameters.
+   *
+   * @param basePartitioning  The original key-grouped partitioning of the 
scan.
+   * @param spjParams         SPJ parameters for the scan.
+   */
+  def getOutputKeyGroupedPartitioning(
+      basePartitioning: KeyGroupedPartitioning,
+      spjParams: StoragePartitionJoinParams): KeyGroupedPartitioning = {
+    val expressions = spjParams.joinKeyPositions match {
+      case Some(projectionPositions) =>
+        projectionPositions.map(i => basePartitioning.expressions(i))
+      case _ => basePartitioning.expressions
+    }
+
+    val newPartValues = spjParams.commonPartitionValues match {
+      case Some(commonPartValues) =>
+        // We allow duplicated partition values if
+        // 
`spark.sql.sources.v2.bucketing.partiallyClusteredDistribution.enabled` is true
+         commonPartValues.flatMap {
+           case (partValue, numSplits) => Seq.fill(numSplits)(partValue)
+         }
+      case None =>
+        spjParams.joinKeyPositions match {
+          case Some(projectionPositions) => 
basePartitioning.partitionValues.map{r =>
+            val projectedRow = KeyGroupedPartitioning.project(expressions,
+              projectionPositions, r)
+            InternalRowComparableWrapper(projectedRow, expressions)
+          }.distinct.map(_.row)
+          case _ => basePartitioning.partitionValues
+        }
+    }
+    basePartitioning.copy(expressions = expressions, numPartitions = 
newPartValues.length,
+      partitionValues = newPartValues)
+  }
+
+  /**
+   * Re-groups the input partitions for this scan based on the provided SPJ 
params, returning a list
+   * of partitions to be scanned by each scan task.
+   *
+   * @param p                      The output KeyGroupedPartitioning of this 
scan.
+   * @param spjParams              SPJ parameters for the scan.
+   * @param filteredPartitions     The input partitions (after applying 
filtering) to be
+   *                               re-grouped for this scan, initially grouped 
by partition value.
+   * @param partitionValueAccessor Accessor for the partition values (as an 
[[InternalRow]])
+   */
+  def getInputPartitionGrouping(
+      p: KeyGroupedPartitioning,
+      spjParams: StoragePartitionJoinParams,

Review Comment:
   just for my curiosity: what's the relationship between `p.expressions` and 
`spjParams.keyGroupedPartitioning`?



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