ulysses-you commented on code in PR #37831:
URL: https://github.com/apache/spark/pull/37831#discussion_r966615335


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/V1Writes.scala:
##########
@@ -102,6 +121,83 @@ object V1Writes extends Rule[LogicalPlan] with 
SQLConfHelper {
   }
 }
 
+/**
+ * This rule is used to eliminate dynamic partition to static partition for v1 
writes if the
+ * partition columns is foldable, so that we can avoid unnecessary sort for 
dynamic partition.
+ *
+ * For example, a pure SQL:
+ * {{{
+ *   INSERT INTO TABLE t1 PARTITION(p) SELECT c, 'a' as p FROM t2
+ *   =>
+ *   INSERT INTO TABLE t1 PARTITION(p='a') SELECT c FROM t2
+ * }}}
+ */
+object EliminateV1DynamicPartitionWrites extends Rule[LogicalPlan] {
+
+  @tailrec
+  private def queryOutput(p: LogicalPlan): Seq[NamedExpression] = p match {
+    case p: Project => p.projectList
+    case f: Filter => queryOutput(f.child)
+    case r: RepartitionOperation => queryOutput(r.child)
+    case r: RebalancePartitions => queryOutput(r.child)
+    case s: Sort => queryOutput(s.child)
+    case l: LocalLimit => queryOutput(l.child)
+    case l: GlobalLimit => queryOutput(l.child)
+    case _ => Seq.empty
+  }
+
+  private def getPartitionSpecString(part: Any): String = {
+    if (part == null) {
+      null
+    } else {
+      assert(part.isInstanceOf[UTF8String])
+      
ExternalCatalogUtils.getPartitionSpecString(part.asInstanceOf[UTF8String].toString)

Review Comment:
   null and empty partition are treated as null, and the final partition path 
will be `__HIVE_DEFAULT_PARTITION__`



-- 
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: [email protected]

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