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



##########
File path: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveStrategies.scala
##########
@@ -181,6 +181,46 @@ object HiveAnalysis extends Rule[LogicalPlan] {
   }
 }
 
+/**
+ * Add a repartition by dynamic partition columns before insert Hive table.
+ *
+ * Note that, this rule must be run after `HiveAnalysis`.
+ */
+object RepartitionBeforeInsertHiveTable 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 @ CreateHiveTableAsSelectCommand(table, query, _, _)
+      if query.resolved && DDLUtils.isHiveTable(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))
+      }
+
+    case i @ InsertIntoHiveTable(table, partSpec, query, _, _, _)
+      if query.resolved && DDLUtils.isDatasourceTable(table) && 
table.bucketSpec.isEmpty

Review comment:
       DDLUtils.isHiveTable?




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