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


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/exchange/PackV2ScanPartitions.scala:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.exchange
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.expressions.SortOrder
+import org.apache.spark.sql.catalyst.plans.physical.{Distribution, 
UnspecifiedDistribution}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution._
+import org.apache.spark.sql.execution.aggregate.BaseAggregateExec
+import org.apache.spark.sql.execution.command.DataWritingCommandExec
+import org.apache.spark.sql.execution.datasources.WriteFilesExec
+import org.apache.spark.sql.execution.datasources.v2.{BatchScanExec, 
V2ScanPartitionPacking, V2TableWriteExec}
+import org.apache.spark.sql.execution.joins.{BroadcastHashJoinExec, 
BroadcastNestedLoopJoinExec}
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * Pack V2 scan inputs after distribution and ordering requirements have been 
planned.
+ * A shuffle or broadcast exchange consumes neither input property. Otherwise, 
propagate
+ * consumers' requirements conservatively through known operators, including a 
broadcast
+ * join's streamed side. Keep unknown operators and explicit coalesces 
unchanged.
+ *
+ * Packing happens inside BatchScanExec, before partial aggregation and 
UnsafeRow conversion.
+ * No keyed or ordered output is claimed, and scans used by 
GroupPartitionsExec stay unchanged.
+ *
+ * Like `DisableUnnecessaryBucketedScan`, this is a separate rule that runs 
after
+ * `EnsureRequirements` has planned every exchange. Under AQE, 
`requiredDistribution` is the
+ * distribution the final plan must satisfy, the same one `EnsureRequirements` 
is given.
+ */
+case class PackV2ScanPartitions(
+    requiredDistribution: Option[Distribution] = None) extends Rule[SparkPlan] 
{
+
+  override def apply(plan: SparkPlan): SparkPlan = {
+    if (!conf.v2ScanPartitionPackingEnabled) {
+      return plan
+    }
+    lazy val options = V2ScanPartitionPacking(
+      conf.v2ScanPartitionPackingMaxPartitionBytes,
+      conf.v2ScanPartitionPackingOpenCostInBytes,
+      conf.v2ScanPartitionPackingMinPartitionNum.getOrElse(
+        conf.getConf(SQLConf.LEAF_NODE_DEFAULT_PARALLELISM).getOrElse(
+          SparkSession.active.sparkContext.defaultParallelism)),
+      conf.v2ScanPartitionPackingMaxPartitionNum)
+
+    def rewrite(node: SparkPlan, needsPartitioning: Boolean, needsOrdering: 
Boolean): SparkPlan = {
+      node match {
+        case scan: BatchScanExec if !needsPartitioning && !needsOrdering &&
+            scan.partitionPacking.isEmpty &&

Review Comment:
   Done in dc626a7c49a: `FileScan`s are excluded, with your 8-file Parquet case 
as a test.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -2644,6 +2644,61 @@ object SQLConf {
       .booleanConf
       .createWithDefault(true)
 
+  val V2_SCAN_PARTITION_PACKING_ENABLED =

Review Comment:
   Done in dc626a7c49a: the configs follow `FILES_MAX_PARTITION_NUM`.
   



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