sunchao commented on a change in pull request #29066: URL: https://github.com/apache/spark/pull/29066#discussion_r535526809
########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2Writes.scala ########## @@ -0,0 +1,102 @@ +/* + * 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 java.util.UUID + +import org.apache.spark.SparkException +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.expressions.PredicateHelper +import org.apache.spark.sql.catalyst.plans.logical.{AppendData, Command, LogicalPlan, OverwriteByExpression, OverwritePartitionsDynamic} +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.connector.catalog.Table +import org.apache.spark.sql.connector.write.{LogicalWriteInfoImpl, SupportsDynamicOverwrite, SupportsOverwrite, SupportsTruncate, Write, WriteBuilder} +import org.apache.spark.sql.execution.datasources.DataSourceStrategy +import org.apache.spark.sql.sources.{AlwaysTrue, Filter} + +/** + * A rule that constructs [[Write]]s. + */ +object V2Writes extends Rule[LogicalPlan] with PredicateHelper { Review comment: Hmm, care to explain where are these rules before? I don't see anything moved to this new rule. ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/WriteToDataSourceV2Exec.scala ########## @@ -322,6 +322,27 @@ trait BatchWriteHelper { } } +trait V2ExistingTableWriteExec extends V2TableWriteExec { + def session: SparkSession + def relation: DataSourceV2Relation + def write: Option[BatchWrite] = None Review comment: Does this have to be an option? or it will always be non-empty? ########## File path: sql/core/src/test/scala/org/apache/spark/sql/connector/WriteDistributionAndOrderingSuite.scala ########## @@ -0,0 +1,594 @@ +/* + * 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.connector + +import java.util +import java.util.Collections + +import org.scalatest.BeforeAndAfter + +import org.apache.spark.sql.{catalyst, DataFrame, QueryTest} +import org.apache.spark.sql.catalyst.analysis.{TableAlreadyExistsException, UnresolvedAttribute} +import org.apache.spark.sql.catalyst.plans.physical +import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning, RangePartitioning, UnknownPartitioning} +import org.apache.spark.sql.connector.catalog.{Identifier, Table} +import org.apache.spark.sql.connector.distributions.{Distribution, Distributions} +import org.apache.spark.sql.connector.expressions.{Expression, FieldReference, NullOrdering, SortDirection, SortOrder, Transform} +import org.apache.spark.sql.connector.expressions.LogicalExpressions._ +import org.apache.spark.sql.connector.write.{BatchWrite, LogicalWriteInfo, RequiresDistributionAndOrdering, SupportsDynamicOverwrite, SupportsOverwrite, SupportsTruncate, Write, WriteBuilder} +import org.apache.spark.sql.execution.{QueryExecution, SortExec, SparkPlan} +import org.apache.spark.sql.execution.datasources.v2.V2TableWriteExec +import org.apache.spark.sql.execution.exchange.ShuffleExchangeExec +import org.apache.spark.sql.functions.lit +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.sources.Filter +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.{IntegerType, StringType, StructType} +import org.apache.spark.sql.util.{CaseInsensitiveStringMap, QueryExecutionListener} + +class WriteDistributionAndOrderingSuite + extends QueryTest with SharedSparkSession with BeforeAndAfter { + + import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ + + before { + spark.conf.set("spark.sql.catalog.testcat", classOf[ExtendedInMemoryTableCatalog].getName) + } + + after { + spark.sessionState.catalogManager.reset() + spark.sessionState.conf.clear() + } + + private val writeOperations = Seq("append", "overwrite", "overwriteDynamic") + + private val namespace = Array("ns1") + private val ident = Identifier.of(namespace, "test_table") + private val tableNameAsString = "testcat." + ident.toString + private val emptyProps = Collections.emptyMap[String, String] + private val schema = new StructType() + .add("id", IntegerType) + .add("data", StringType) + + writeOperations.foreach { operation => + test(s"ordered distribution and sort with same exprs ($operation)") { + val ordering = Array[SortOrder]( + sort(FieldReference("data"), SortDirection.ASCENDING, NullOrdering.NULLS_FIRST) + ) + val distribution = Distributions.ordered(ordering) + + val writeOrdering = Seq( + catalyst.expressions.SortOrder( + UnresolvedAttribute("data"), + catalyst.expressions.Ascending, + catalyst.expressions.NullsFirst, + Set.empty + ) + ) + val numShufflePartitions = SQLConf.get.numShufflePartitions + val writePartitioning = RangePartitioning(writeOrdering, numShufflePartitions) + + checkWriteRequirements( + tableDistribution = distribution, + tableOrdering = ordering, + expectedWritePartitioning = writePartitioning, + expectedWriteOrdering = writeOrdering, + writeOperation = operation + ) + } + } + + writeOperations.foreach { operation => + test(s"clustered distribution and sort with same exprs ($operation)") { + val ordering = Array[SortOrder]( + sort(FieldReference("data"), SortDirection.DESCENDING, NullOrdering.NULLS_FIRST), + sort(FieldReference("id"), SortDirection.ASCENDING, NullOrdering.NULLS_FIRST) + ) + val clustering = Array[Expression](FieldReference("data"), FieldReference("id")) + val distribution = Distributions.clustered(clustering) + + val writeOrdering = Seq( + catalyst.expressions.SortOrder( + attr("data"), + catalyst.expressions.Descending, + catalyst.expressions.NullsFirst, + Set.empty + ), + catalyst.expressions.SortOrder( + attr("id"), + catalyst.expressions.Ascending, + catalyst.expressions.NullsFirst, + Set.empty + ) + ) + val writePartitioningExprs = Seq(attr("data"), attr("id")) + val numShufflePartitions = SQLConf.get.numShufflePartitions + val writePartitioning = HashPartitioning(writePartitioningExprs, numShufflePartitions) + + checkWriteRequirements( + tableDistribution = distribution, + tableOrdering = ordering, + expectedWritePartitioning = writePartitioning, + expectedWriteOrdering = writeOrdering, + writeOperation = operation + ) + } + } + + writeOperations.foreach { operation => + test(s"clustered distribution and sort with extended exprs ($operation)") { + val ordering = Array[SortOrder]( + sort(FieldReference("data"), SortDirection.DESCENDING, NullOrdering.NULLS_FIRST), + sort(FieldReference("id"), SortDirection.ASCENDING, NullOrdering.NULLS_FIRST) + ) + val clustering = Array[Expression](FieldReference("data")) + val distribution = Distributions.clustered(clustering) + + val writeOrdering = Seq( + catalyst.expressions.SortOrder( + attr("data"), + catalyst.expressions.Descending, + catalyst.expressions.NullsFirst, + Set.empty + ), + catalyst.expressions.SortOrder( + attr("id"), + catalyst.expressions.Ascending, + catalyst.expressions.NullsFirst, + Set.empty + ) + ) + val writePartitioningExprs = Seq(attr("data")) + val numShufflePartitions = SQLConf.get.numShufflePartitions + val writePartitioning = HashPartitioning(writePartitioningExprs, numShufflePartitions) + + checkWriteRequirements( + tableDistribution = distribution, + tableOrdering = ordering, + expectedWritePartitioning = writePartitioning, + expectedWriteOrdering = writeOrdering, + writeOperation = operation + ) + } + } + + writeOperations.foreach { operation => + test(s"unspecified distribution and local sort ($operation)") { + val ordering = Array[SortOrder]( + sort(FieldReference("data"), SortDirection.DESCENDING, NullOrdering.NULLS_FIRST) + ) + val distribution = Distributions.unspecified() + + val writeOrdering = Seq( + catalyst.expressions.SortOrder( + attr("data"), + catalyst.expressions.Descending, + catalyst.expressions.NullsFirst, + Set.empty + ) + ) + val writePartitioning = UnknownPartitioning(0) + + checkWriteRequirements( + tableDistribution = distribution, + tableOrdering = ordering, + expectedWritePartitioning = writePartitioning, + expectedWriteOrdering = writeOrdering, + writeOperation = operation + ) + } + } + + writeOperations.foreach { operation => + test(s"unspecified distribution and no sort ($operation)") { + val ordering = Array.empty[SortOrder] + val distribution = Distributions.unspecified() + + val writeOrdering = Seq.empty[catalyst.expressions.SortOrder] + val writePartitioning = UnknownPartitioning(0) + + checkWriteRequirements( + tableDistribution = distribution, + tableOrdering = ordering, + expectedWritePartitioning = writePartitioning, + expectedWriteOrdering = writeOrdering, + writeOperation = operation + ) + } + } + + writeOperations.foreach { operation => + test(s"ordered distribution and sort with manual global sort ($operation)") { + val ordering = Array[SortOrder]( + sort(FieldReference("data"), SortDirection.ASCENDING, NullOrdering.NULLS_FIRST), + sort(FieldReference("id"), SortDirection.ASCENDING, NullOrdering.NULLS_FIRST) + ) + val distribution = Distributions.ordered(ordering) + + val writeOrdering = Seq( + catalyst.expressions.SortOrder( + UnresolvedAttribute("data"), + catalyst.expressions.Ascending, + catalyst.expressions.NullsFirst, + Set.empty + ), + catalyst.expressions.SortOrder( + UnresolvedAttribute("id"), + catalyst.expressions.Ascending, + catalyst.expressions.NullsFirst, + Set.empty + ) + ) + val numShufflePartitions = SQLConf.get.numShufflePartitions + val writePartitioning = RangePartitioning(writeOrdering, numShufflePartitions) + + checkWriteRequirements( + tableDistribution = distribution, + tableOrdering = ordering, + expectedWritePartitioning = writePartitioning, + expectedWriteOrdering = writeOrdering, + writeTransform = df => df.orderBy("data", "id"), + writeOperation = operation + ) + } + } + + writeOperations.foreach { operation => + test(s"ordered distribution and sort with incompatible global sort ($operation)") { + val ordering = Array[SortOrder]( + sort(FieldReference("data"), SortDirection.ASCENDING, NullOrdering.NULLS_FIRST), + sort(FieldReference("id"), SortDirection.ASCENDING, NullOrdering.NULLS_FIRST) + ) + val distribution = Distributions.ordered(ordering) + + val writeOrdering = Seq( + catalyst.expressions.SortOrder( + UnresolvedAttribute("data"), + catalyst.expressions.Ascending, + catalyst.expressions.NullsFirst, + Set.empty + ), + catalyst.expressions.SortOrder( + UnresolvedAttribute("id"), + catalyst.expressions.Ascending, + catalyst.expressions.NullsFirst, + Set.empty + ) + ) + val numShufflePartitions = SQLConf.get.numShufflePartitions + val writePartitioning = RangePartitioning(writeOrdering, numShufflePartitions) + + checkWriteRequirements( + tableDistribution = distribution, + tableOrdering = ordering, + expectedWritePartitioning = writePartitioning, + expectedWriteOrdering = writeOrdering, + writeTransform = df => df.orderBy(df("data").desc, df("id").asc), + writeOperation = operation + ) + } + } + + writeOperations.foreach { operation => + test(s"ordered distribution and sort with manual local sort ($operation)") { + val ordering = Array[SortOrder]( + sort(FieldReference("data"), SortDirection.ASCENDING, NullOrdering.NULLS_FIRST), + sort(FieldReference("id"), SortDirection.ASCENDING, NullOrdering.NULLS_FIRST) + ) + val distribution = Distributions.ordered(ordering) + + val writeOrdering = Seq( + catalyst.expressions.SortOrder( + UnresolvedAttribute("data"), + catalyst.expressions.Ascending, + catalyst.expressions.NullsFirst, + Set.empty + ), + catalyst.expressions.SortOrder( + UnresolvedAttribute("id"), + catalyst.expressions.Ascending, + catalyst.expressions.NullsFirst, + Set.empty + ) + ) + val numShufflePartitions = SQLConf.get.numShufflePartitions + val writePartitioning = RangePartitioning(writeOrdering, numShufflePartitions) + + checkWriteRequirements( + tableDistribution = distribution, + tableOrdering = ordering, + expectedWritePartitioning = writePartitioning, + expectedWriteOrdering = writeOrdering, + writeTransform = df => df.sortWithinPartitions("data", "id"), + writeOperation = operation + ) + } + } + + // TODO: do we need to dedup repartitions too? RepartitionByExpr -> Projects -> RepartitionByExpr + writeOperations.foreach { operation => + ignore(s"ordered distribution and sort with manual repartition ($operation)") { Review comment: why is this ignored? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
