rekbun commented on code in PR #59522:
URL: https://github.com/apache/spark/pull/59522#discussion_r4530300950
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/GroupPartitionsExec.scala:
##########
@@ -863,30 +865,32 @@ private[sql] object GroupPartitionsExec {
*
* Unlike Spark's default coalescer which tries to minimize data movement,
this coalescer
* groups partitions based on their partition keys to maintain the grouping
semantics
- * required for storage-partitioned operations.
+ * required for storage-partitioned operations. V2 scan partition packing uses
it for its groups
+ * too.
*
* @param groupedPartitions Sequence where each element is a sequence of input
partition
- * indices that should be grouped together
+ * indices that should be grouped together. Only the
driver reads it,
+ * so it is not serialized into tasks.
*/
class GroupedPartitionCoalescer(
Review Comment:
Moved to SPARK-61279 (#60256): the transient groups, the tie spreading and
the single-partition groups. The `:135` test is now that PR's "groups over a
parent without locations take their first input's location" test. It coalesces
a mapped RDD, as `GroupPartitionsExec` does over a scan, since the source the
`:135` test read is a fixture of this PR. Its other test covers the tie
spreading and single-partition groups that `:175-179` checked, so I dropped
those checks and the source's `hosts` option here. The description's "With the
option off" paragraph is gone too. This PR keeps only the call to the coalescer
and a sentence in its scaladoc that mentions packing.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanPartitionPacking.scala:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.datasources.v2
+
+import scala.collection.mutable
+
+import org.apache.spark.sql.connector.read.{HasPartitionStatistics,
InputPartition}
+
+/**
+ * Immutable sizing policy captured when a scan is prepared, before runtime
filtering. It holds the
+ * configured values only. A default that depends on the cluster, such as the
minimum partition
+ * count, is resolved when the scan executes, so equal scans planned at
different times stay equal.
+ */
+case class V2ScanPartitionPacking(
+ maxPartitionBytes: Long,
+ openCostInBytes: Long,
+ minPartitionNum: Option[Int],
+ maxPartitionNum: Option[Int]) {
+
+ /**
+ * Group adjacent inputs by estimated size, keeping an oversized input on
its own. If any input
+ * lacks a valid size, the whole scan is grouped by count into at most
`maxPartitionNum`
+ * balanced ranges when that cap is set. Returns None when every input stays
a partition of its
+ * own, without building a group for each.
+ */
+ def pack(partitions: Seq[InputPartition], defaultMinPartitionNum: => Int):
Option[Seq[Range]] = {
+ val numInputs = partitions.length
+ // Unboxed, allocated at the first valid size and read only while every
input reports one.
+ var costs: Array[Long] = null
+ var total = 0L
+ var sized = true
+ val inputs = partitions.iterator
+ var index = 0
+ while (sized && inputs.hasNext) {
+ inputs.next() match {
+ case p: HasPartitionStatistics =>
+ val bytes = p.sizeInBytes()
+ sized = bytes.isPresent && bytes.getAsLong >= 0
+ if (sized) {
+ if (costs == null) costs = new Array[Long](numInputs)
+ costs(index) = add(bytes.getAsLong, openCostInBytes)
+ total = add(total, costs(index))
+ }
+ case _ => sized = false
+ }
+ index += 1
+ }
+ if (!sized) {
+ // A cap of at least the number of inputs leaves each of them on its own.
+ maxPartitionNum.filter(_ < numInputs).map(byCount(numInputs, _))
+ } else if (numInputs == 0) {
+ None
+ } else {
+ bySize(costs, total, minPartitionNum.getOrElse(defaultMinPartitionNum))
Review Comment:
Moved to SPARK-61279 (#60256): the transient groups, the tie spreading and
the single-partition groups. The `:135` test is now that PR's "groups over a
parent without locations take their first input's location" test. It coalesces
a mapped RDD, as `GroupPartitionsExec` does over a scan, since the source the
`:135` test read is a fixture of this PR. Its other test covers the tie
spreading and single-partition groups that `:175-179` checked, so I dropped
those checks and the source's `hosts` option here. The description's "With the
option off" paragraph is gone too. This PR keeps only the call to the coalescer
and a sentence in its scaladoc that mentions packing.
--
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]