rekbun commented on code in PR #59522:
URL: https://github.com/apache/spark/pull/59522#discussion_r4530302690


##########
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:
   Done, as you suggested. When packing by size gives more groups than the cap, 
`pack` packs again with a target of the scan's total size, including opening 
costs, divided by the cap. It logs the same warning as `FilePartition`, naming 
the packing `maxPartitionBytes` key. A scan with an input that reports no size 
is still grouped by count into at most that many tasks. The config doc, the 
tuning guide, the config table, the `Batch.planInputPartitions` javadoc and the 
DataSource V2 guide now say this. A committed `PythonDataSourceSuite` test 
covers the count path with a Python data source. `V2ScanPartitionPackingSuite` 
covers the cap, and the "real unkeyed readers" test in 
`PackV2ScanPartitionsReadSuite` covers your 8-input case end to end. The `:38` 
assertion no longer sets a cap.
   
   The rule itself now matches files too. An input joins a group unless its 
size would take the group past the target, and it then adds its size plus the 
opening cost. The minimum-count target rounds down, as `maxSplitBytes` does, 
and `openCostInBytes` is internal, as `spark.sql.files.openCostInBytes` is. 
"sized inputs pack as spark.sql.files.* packs files of the same sizes" in 
`PackV2ScanPartitionsSuite` runs fixed and random sizes through 
`FilePartition.getFilePartitions` and `pack` and compares the groups. Before 
grouping, file scans also split files into chunks of at most `maxSplitBytes` 
and sort them by size. Packing does neither: it keeps the scan's order and 
never splits an input.



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