dongjoon-hyun commented on code in PR #60246:
URL: https://github.com/apache/spark/pull/60246#discussion_r4519771796


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExistingRDD.scala:
##########
@@ -205,11 +205,29 @@ object LogicalRDD extends Logging {
 
     val (stats, constraints) = rewriteStatsAndConstraints(logicalPlan, 
optimizedPlan)
 
+    // The executed plan can output other attributes than 
`logicalPlan.output`, e.g. when
+    // `RemoveRedundantAliases` removes a same-name root alias. So map its 
partitioning and ordering
+    // onto `logicalPlan.output` by position, and drop a partitioning that 
still refers to other
+    // attributes, e.g. the input attributes of `SerializeFromObjectExec`.
+    val toOutput = AttributeMap(executedPlan.output.zip(logicalPlan.output))
+    def remap[E <: Expression](e: E): E =
+      e.transform { case a: Attribute => toOutput.getOrElse(a, a) 
}.asInstanceOf[E]
+    val partitioning = firstLeafPartitioning(executedPlan.outputPartitioning) 
match {
+      case p: Expression =>
+        val remapped = remap(p)
+        if (remapped.references.subsetOf(AttributeSet(logicalPlan.output))) {
+          remapped
+        } else {
+          UnknownPartitioning(p.numPartitions)
+        }
+      case p => p
+    }
+
     LogicalRDD(
       originDataset.logicalPlan.output,
       rdd,
-      firstLeafPartitioning(executedPlan.outputPartitioning),
-      executedPlan.outputOrdering,
+      partitioning,
+      executedPlan.outputOrdering.map(remap),

Review Comment:
   Thank you, you're right. I reproduced it with AQE on and off: the checkpoint 
of `c.select($"p")` reports `id ASC` over its output `[p]`, and the window 
returns count 1 for all 128 rows. I updated `fromDataset` to keep only the 
leading sort orders over the output, like `AliasAwareQueryOutputOrdering`, and 
added this case to the test.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/ExistingRDD.scala:
##########
@@ -205,11 +205,29 @@ object LogicalRDD extends Logging {
 
     val (stats, constraints) = rewriteStatsAndConstraints(logicalPlan, 
optimizedPlan)
 
+    // The executed plan can output other attributes than 
`logicalPlan.output`, e.g. when
+    // `RemoveRedundantAliases` removes a same-name root alias. So map its 
partitioning and ordering
+    // onto `logicalPlan.output` by position, and drop a partitioning that 
still refers to other
+    // attributes, e.g. the input attributes of `SerializeFromObjectExec`.
+    val toOutput = AttributeMap(executedPlan.output.zip(logicalPlan.output))
+    def remap[E <: Expression](e: E): E =
+      e.transform { case a: Attribute => toOutput.getOrElse(a, a) 
}.asInstanceOf[E]
+    val partitioning = firstLeafPartitioning(executedPlan.outputPartitioning) 
match {
+      case p: Expression =>
+        val remapped = remap(p)
+        if (remapped.references.subsetOf(AttributeSet(logicalPlan.output))) {

Review Comment:
   Thank you. I confirmed it: with `cache()`, the query returns 64 rows with 
count 2 with AQE on and off, and the base branch returns the same. Since it 
comes from `InMemoryTableScanExec`, not from `LogicalRDD.fromDataset`, I'll fix 
it in a separate JIRA.
   



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