cloud-fan commented on code in PR #39475:
URL: https://github.com/apache/spark/pull/39475#discussion_r1065830984


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/V1Writes.scala:
##########
@@ -91,32 +92,31 @@ object V1Writes extends Rule[LogicalPlan] with 
SQLConfHelper {
   }
 
   private def prepareQuery(write: V1WriteCommand, query: LogicalPlan): 
LogicalPlan = {
-    val hasEmpty2Null = query.exists(p => hasEmptyToNull(p.expressions))
-    val empty2NullPlan = if (hasEmpty2Null) {
-      query
-    } else {
-      val projectList = convertEmptyToNull(query.output, 
write.partitionColumns)
-      if (projectList.isEmpty) query else Project(projectList, query)
-    }
+    val projectList = convertEmptyToNull(query.output, write.partitionColumns)
+    val empty2NullPlan = if (projectList.isEmpty) query else 
Project(projectList, query)
     assert(empty2NullPlan.output.length == query.output.length)
     val attrMap = AttributeMap(query.output.zip(empty2NullPlan.output))
-
-    // Rewrite the attribute references in the required ordering to use the 
new output.
-    val requiredOrdering = write.requiredOrdering.map(_.transform {
-      case a: Attribute => attrMap.getOrElse(a, a)
-    }.asInstanceOf[SortOrder])
+    // `Empty2Null` does not affect ordering. The required sort ordering 
direction is `Ascending`
+    // and the null ordering is `NullsFirst`. Both empty and null value are at 
the top of rows.
+    // Here we use the original required ordering to compare if matches so 
that we can preserve the
+    // user-specified sort ordering with such case:
+    // `INSERT INTO TABLE t PARTITION(p) SELECT c, p FROM t SORT BY p, c`.
+    val requiredOrdering = write.requiredOrdering
     val outputOrdering = query.outputOrdering
-    // Check if the ordering is already matched to ensure the idempotency of 
the rule.
     val orderingMatched = isOrderingMatched(requiredOrdering, outputOrdering)
     if (orderingMatched) {
       empty2NullPlan

Review Comment:
   Even if we don't add the sort here, the caller will rewrite the attributes 
in v1 command which will update required orderng expression as well, right?



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