You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/02/16 04:26:23 UTC

[GitHub] [spark] c21 commented on a change in pull request #34568: [SPARK-37287][SQL] Pull out dynamic partition and bucket sort from FileFormatWriter

c21 commented on a change in pull request #34568:
URL: https://github.com/apache/spark/pull/34568#discussion_r807520185



##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/V1Writes.scala
##########
@@ -0,0 +1,148 @@
+/*
+ * 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
+
+import org.apache.spark.sql.catalyst.catalog.BucketSpec
+import org.apache.spark.sql.catalyst.expressions.{Ascending, Attribute, AttributeSet, BitwiseAnd, HiveHash, Literal, Pmod, SortOrder}
+import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Sort}
+import org.apache.spark.sql.catalyst.plans.physical.HashPartitioning
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.command.DataWritingCommand
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * V1 write includes both datasoruce and hive, that requires a specific ordering of data.

Review comment:
       nit: `datasoruce` -> `datasource v1`

##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileFormatWriter.scala
##########
@@ -204,29 +158,26 @@ object FileFormatWriter extends Logging {
     // prepares the job, any exception thrown from here shouldn't cause abortJob() to be called.
     committer.setupJob(job)
 
+    val sortColumns = getBucketSortColumns(bucketSpec, dataColumns)
     try {
-      val (rdd, concurrentOutputWriterSpec) = if (orderingMatched) {
-        (empty2NullPlan.execute(), None)
+      val maxWriters = sparkSession.sessionState.conf.maxConcurrentOutputFileWriters
+      val concurrentWritersEnabled = maxWriters > 0 && sortColumns.isEmpty
+      val concurrentOutputWriterSpec = if (concurrentWritersEnabled) {
+        val output = empty2NullPlan.output
+        val enableRadixSort = sparkSession.sessionState.conf.enableRadixSort
+        val outputSchema = empty2NullPlan.schema
+        Some(ConcurrentOutputWriterSpec(maxWriters,
+          () => SortExec.createSorter(

Review comment:
       I feel this refactoring (`SortExec.createSorter`) is not very necessary. Why can't we create a `SortExec` operator and call `createSorter()` as before? What's the advantage of current code compared to before?

##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/V1Writes.scala
##########
@@ -0,0 +1,148 @@
+/*
+ * 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
+
+import org.apache.spark.sql.catalyst.catalog.BucketSpec
+import org.apache.spark.sql.catalyst.expressions.{Ascending, Attribute, AttributeSet, BitwiseAnd, HiveHash, Literal, Pmod, SortOrder}
+import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Sort}
+import org.apache.spark.sql.catalyst.plans.physical.HashPartitioning
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.command.DataWritingCommand
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * V1 write includes both datasoruce and hive, that requires a specific ordering of data.
+ * It should be resolved by [[V1Writes]].
+ *
+ * TODO(SPARK-37333): Specify the required distribution at V1Write
+ */
+trait V1Write extends DataWritingCommand with V1WritesHelper {
+  def partitionColumns: Seq[Attribute] = Seq.empty
+  def numStaticPartitions: Int = 0
+  def bucketSpec: Option[BucketSpec] = None
+  def options: Map[String, String] = Map.empty
+
+  final def requiredOrdering: Seq[SortOrder] = {
+    getSortOrder(
+      outputColumns,
+      partitionColumns,
+      numStaticPartitions,
+      bucketSpec,
+      options)
+  }
+}
+
+/**
+ * A rule that makes sure the v1 write requirement, e.g. requiredOrdering
+ */
+object V1Writes extends Rule[LogicalPlan] with V1WritesHelper {
+  override def apply(plan: LogicalPlan): LogicalPlan = plan match {
+    case write: V1Write =>
+      val partitionSet = AttributeSet(write.partitionColumns)
+      val dataColumns = write.outputColumns.filterNot(partitionSet.contains)
+      val sortColumns = getBucketSortColumns(write.bucketSpec, dataColumns)
+      val newQuery = prepareQuery(write.query, write.requiredOrdering, sortColumns)
+      write.withNewChildren(newQuery :: Nil)
+
+    case _ => plan
+  }
+}
+
+trait V1WritesHelper {

Review comment:
       After looking through the subclasses of this one, I found this class is meant to be a utility class, but not an interface to implement. Shall we change this to `object V1WritesUtils` ?

##########
File path: sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/V1HiveWritesHelper.scala
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.hive.execution
+
+import java.util.Locale
+
+import org.apache.hadoop.hive.ql.ErrorMsg
+import org.apache.hadoop.hive.ql.plan.TableDesc
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.{AnalysisException, SparkSession}
+import org.apache.spark.sql.catalyst.catalog.{BucketSpec, CatalogTable, ExternalCatalogUtils}
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryExecutionErrors}
+import org.apache.spark.sql.execution.datasources.BucketingUtils
+import org.apache.spark.sql.hive.client.HiveClientImpl
+
+trait V1HiveWritesHelper {

Review comment:
       this seems to be a utility class as well, how about `object V1WritesForHiveUtils`?

##########
File path: sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/V1HiveWritesHelper.scala
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.hive.execution
+
+import java.util.Locale
+
+import org.apache.hadoop.hive.ql.ErrorMsg
+import org.apache.hadoop.hive.ql.plan.TableDesc
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.{AnalysisException, SparkSession}
+import org.apache.spark.sql.catalyst.catalog.{BucketSpec, CatalogTable, ExternalCatalogUtils}
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryExecutionErrors}
+import org.apache.spark.sql.execution.datasources.BucketingUtils
+import org.apache.spark.sql.hive.client.HiveClientImpl
+
+trait V1HiveWritesHelper {
+  def options(bucketSpec: Option[BucketSpec]): Map[String, String] = {

Review comment:
       nit: we can make the function name more verbose, e.g. `getOptionsWithHiveBucketWrite`

##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/V1Writes.scala
##########
@@ -0,0 +1,148 @@
+/*
+ * 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
+
+import org.apache.spark.sql.catalyst.catalog.BucketSpec
+import org.apache.spark.sql.catalyst.expressions.{Ascending, Attribute, AttributeSet, BitwiseAnd, HiveHash, Literal, Pmod, SortOrder}
+import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Sort}
+import org.apache.spark.sql.catalyst.plans.physical.HashPartitioning
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.command.DataWritingCommand
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * V1 write includes both datasoruce and hive, that requires a specific ordering of data.
+ * It should be resolved by [[V1Writes]].
+ *
+ * TODO(SPARK-37333): Specify the required distribution at V1Write
+ */
+trait V1Write extends DataWritingCommand with V1WritesHelper {
+  def partitionColumns: Seq[Attribute] = Seq.empty
+  def numStaticPartitions: Int = 0
+  def bucketSpec: Option[BucketSpec] = None
+  def options: Map[String, String] = Map.empty
+
+  final def requiredOrdering: Seq[SortOrder] = {
+    getSortOrder(
+      outputColumns,
+      partitionColumns,
+      numStaticPartitions,
+      bucketSpec,
+      options)
+  }
+}
+
+/**
+ * A rule that makes sure the v1 write requirement, e.g. requiredOrdering
+ */
+object V1Writes extends Rule[LogicalPlan] with V1WritesHelper {
+  override def apply(plan: LogicalPlan): LogicalPlan = plan match {
+    case write: V1Write =>
+      val partitionSet = AttributeSet(write.partitionColumns)
+      val dataColumns = write.outputColumns.filterNot(partitionSet.contains)
+      val sortColumns = getBucketSortColumns(write.bucketSpec, dataColumns)
+      val newQuery = prepareQuery(write.query, write.requiredOrdering, sortColumns)
+      write.withNewChildren(newQuery :: Nil)
+
+    case _ => plan
+  }
+}
+
+trait V1WritesHelper {
+
+  def getBucketSpec(

Review comment:
       nit: how about naming it as `getWriterBucketSpec`? `BucketSpec` is another class in Spark, which is different from `WriterBucketSpec`. Also `bucketSpec` is a parameter, so `getWriterBucketSpec` looks less confusing.




-- 
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: reviews-unsubscribe@spark.apache.org

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



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org