manuzhang commented on a change in pull request #28032:
URL: https://github.com/apache/spark/pull/28032#discussion_r472799591



##########
File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/DataSourceStrategy.scala
##########
@@ -229,6 +229,46 @@ case class DataSourceAnalysis(conf: SQLConf) extends 
Rule[LogicalPlan] with Cast
   }
 }
 
+/**
+ * Add a repartition by dynamic partition columns before insert Datasource 
table.
+ *
+ * Note that, this rule must be run after `DataSourceAnalysis`.
+ */
+case class RepartitionBeforeInsertDataSourceTable(conf: SQLConf) extends 
Rule[LogicalPlan] {
+  override def apply(plan: LogicalPlan): LogicalPlan = {
+    if (conf.repartitionBeforeInsert) {
+      insertRepartition(plan)
+    } else {
+      plan
+    }
+  }
+
+  private def insertRepartition(plan: LogicalPlan): LogicalPlan = plan 
resolveOperators {
+    case c @ CreateDataSourceTableAsSelectCommand(table, _, query, _)
+      if query.resolved && DDLUtils.isDatasourceTable(table) && 
table.bucketSpec.isEmpty
+        && table.partitionColumnNames.nonEmpty =>
+      val dynamicPartExps = table.partitionColumnNames.flatMap(n => 
query.output.find(_.name == n))
+      query match {
+        case RepartitionByExpression(partExpressions, _, _) if partExpressions 
== dynamicPartExps =>
+          c
+        case _ =>
+          c.copy(query = RepartitionByExpression(dynamicPartExps, query, 
conf.numShufflePartitions))

Review comment:
       We can take advantage of AQE in case of data skew if we set 
`optNumPartitions=None`




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

Reply via email to