You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2021/07/13 10:18:36 UTC

[GitHub] [iceberg] izchen commented on a change in pull request #2141: Spark: Add DistributionAndOrderingUtils

izchen commented on a change in pull request #2141:
URL: https://github.com/apache/iceberg/pull/2141#discussion_r668627749



##########
File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/utils/DistributionAndOrderingUtils.scala
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.catalyst.utils
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst
+import org.apache.spark.sql.catalyst.analysis.Resolver
+import org.apache.spark.sql.catalyst.expressions.IcebergBucketTransform
+import org.apache.spark.sql.catalyst.expressions.IcebergDayTransform
+import org.apache.spark.sql.catalyst.expressions.IcebergHourTransform
+import org.apache.spark.sql.catalyst.expressions.IcebergMonthTransform
+import org.apache.spark.sql.catalyst.expressions.IcebergYearTransform
+import org.apache.spark.sql.catalyst.expressions.NamedExpression
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.plans.logical.RepartitionByExpression
+import org.apache.spark.sql.catalyst.plans.logical.Sort
+import org.apache.spark.sql.connector.catalog.CatalogV2Implicits
+import org.apache.spark.sql.connector.expressions.BucketTransform
+import org.apache.spark.sql.connector.expressions.DaysTransform
+import org.apache.spark.sql.connector.expressions.Expression
+import org.apache.spark.sql.connector.expressions.FieldReference
+import org.apache.spark.sql.connector.expressions.HoursTransform
+import org.apache.spark.sql.connector.expressions.IdentityTransform
+import org.apache.spark.sql.connector.expressions.MonthsTransform
+import org.apache.spark.sql.connector.expressions.NamedReference
+import org.apache.spark.sql.connector.expressions.Transform
+import org.apache.spark.sql.connector.expressions.YearsTransform
+import org.apache.spark.sql.connector.iceberg.distributions.ClusteredDistribution
+import org.apache.spark.sql.connector.iceberg.distributions.Distribution
+import org.apache.spark.sql.connector.iceberg.distributions.OrderedDistribution
+import org.apache.spark.sql.connector.iceberg.distributions.UnspecifiedDistribution
+import org.apache.spark.sql.connector.iceberg.expressions.NullOrdering
+import org.apache.spark.sql.connector.iceberg.expressions.SortDirection
+import org.apache.spark.sql.connector.iceberg.expressions.SortOrder
+import org.apache.spark.sql.internal.SQLConf
+
+object DistributionAndOrderingUtils {
+
+  def prepareQuery(
+      requiredDistribution: Distribution,
+      requiredOrdering: Seq[SortOrder],
+      query: LogicalPlan,
+      conf: SQLConf): LogicalPlan = {
+
+    val resolver = conf.resolver
+
+    val distribution = requiredDistribution match {
+      case d: OrderedDistribution =>
+        d.ordering.map(e => toCatalyst(e, query, resolver))
+      case d: ClusteredDistribution =>
+        d.clustering.map(e => toCatalyst(e, query, resolver))
+      case _: UnspecifiedDistribution =>
+        Array.empty[catalyst.expressions.Expression]
+    }
+
+    val queryWithDistribution = if (distribution.nonEmpty) {
+      val numShufflePartitions = conf.numShufflePartitions
+      // the conversion to catalyst expressions above produces SortOrder expressions
+      // for OrderedDistribution and generic expressions for ClusteredDistribution
+      // this allows RepartitionByExpression to pick either range or hash partitioning
+      RepartitionByExpression(distribution, query, numShufflePartitions)

Review comment:
       When `WRITE_DISTRIBUTION_MODE = range`, before this PR, Logical Plan is
   
   ```
   Sort [dt#8 ASC NULLS FIRST, v#7 ASC NULLS FIRST], true
   +- Repartition 2000, true
      +- MergeInto 
   ```
   After this PR, Logical Plan is
   
   ```
   Sort [dt#8 ASC NULLS FIRST, v#7 ASC NULLS FIRST], false
   +- RepartitionByExpression [dt#8], 2000
      +- Repartition 2000, true
         +- MergeInto
   ```
   
   In my opinion, the conversion of `global sort` to `local sort + range partitioning` is correct, but here we need to consider the `CollapseRepartition` rule in Spark `Optimizer`. In this case, this rule will eliminate the `Repartition 2000, true` node.
   
   Please take a look here, thanks @aokolnychyi @rdblue 




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org