You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by we...@apache.org on 2025/05/08 12:50:17 UTC

(spark) branch master updated: [SPARK-52037][SQL] Order in LCA inner project lists should respect order in original project list

This is an automated email from the ASF dual-hosted git repository.

wenchen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new bead4389cf32 [SPARK-52037][SQL] Order in LCA inner project lists should respect order in original project list
bead4389cf32 is described below

commit bead4389cf32ec87cb29c361b4f4044fb8b081d2
Author: Mihailo Timotic <mi...@databricks.com>
AuthorDate: Thu May 8 20:50:01 2025 +0800

    [SPARK-52037][SQL] Order in LCA inner project lists should respect order in original project list
    
    ### What changes were proposed in this pull request?
    Enforce that the order in inner project lists generated when resolving LCAs respects ordering of aliases in original project list.
    
    For a query:
    ```SELECT 0 AS a, 1 AS b, 2 AS c, b AS d, a AS e, d AS f, a AS g, g AS h, h AS i```
    
    Before this change, plan would be:
    
    ```
    Project [a, b, 2 AS c, d, a AS e, d AS f, g, h, h AS i]
    +- Project [b, a, d, g, g AS h]
         +- Project [b, a, b AS d, a AS g]
              +- Project [1 AS b, 0 AS a]
                   +- OneRowRelation
    ```
    
    After this change, `a` should come before `b` in all inner `Project`s:
    
    ```
    Project [a, b, 2 AS c, d, a AS e, d AS f, g, h, h AS i]
    +- Project [a, b, d, g, g AS h]
         +- Project [a, b, b AS d, a AS g]
              +- Project [0 AS a, 1 AS b]
                   +- OneRowRelation
    ```
    
    ### Why are the changes needed?
    This change is necessary because single-pass analyzer always respects the order from the original project list there is a logical plan mismatch for the example query.
    
    ### Does this PR introduce _any_ user-facing change?
    No
    
    ### How was this patch tested?
    Added a test case.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    No
    
    Closes #50828 from mihailotim-db/mihailotim-db/lca_ordering.
    
    Authored-by: Mihailo Timotic <mi...@databricks.com>
    Signed-off-by: Wenchen Fan <we...@databricks.com>
---
 .../analysis/ResolveLateralColumnAliasReference.scala         | 11 ++++++++---
 .../scala/org/apache/spark/sql/LateralColumnAliasSuite.scala  | 11 ++++++++++-
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveLateralColumnAliasReference.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveLateralColumnAliasReference.scala
index cb26820a0c79..70f81fe83c4d 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveLateralColumnAliasReference.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveLateralColumnAliasReference.scala
@@ -186,10 +186,15 @@ object ResolveLateralColumnAliasReference extends Rule[LogicalPlan] {
           val outerProjectList = collection.mutable.Seq(newProjectList: _*)
           val innerProjectList =
             collection.mutable.ArrayBuffer(child.output.map(_.asInstanceOf[NamedExpression]): _*)
-          referencedAliases.forEach { case AliasEntry(alias: Alias, idx) =>
-            outerProjectList.update(idx, alias.toAttribute)
-            innerProjectList += alias
+          val referencedAliasesOrderedByProjectListIndex =
+            referencedAliases.asScala.toSeq.sortBy(_.index)
+
+          referencedAliasesOrderedByProjectListIndex.foreach {
+            case AliasEntry(alias: Alias, idx) =>
+              outerProjectList.update(idx, alias.toAttribute)
+              innerProjectList += alias
           }
+
           p.copy(
             projectList = outerProjectList.toSeq,
             child = Project(innerProjectList.toSeq, child)
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/LateralColumnAliasSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/LateralColumnAliasSuite.scala
index ae8d84aadb03..adfdc379e760 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/LateralColumnAliasSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/LateralColumnAliasSuite.scala
@@ -23,7 +23,7 @@ import org.scalatest.Tag
 import org.apache.spark.SparkRuntimeException
 import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, ExpressionSet}
 import org.apache.spark.sql.catalyst.parser.ParseException
-import org.apache.spark.sql.catalyst.plans.logical.Aggregate
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, Project}
 import org.apache.spark.sql.catalyst.trees.TreePattern.OUTER_REFERENCE
 import org.apache.spark.sql.internal.SQLConf
 import org.apache.spark.sql.test.SharedSparkSession
@@ -1383,4 +1383,13 @@ class LateralColumnAliasSuite extends LateralColumnAliasSuiteBase {
         Row("david", false) :: Row("jen", false) :: Nil
     )
   }
+
+  test("Order in inner project lists should respect original project list order") {
+    val plan = sql("SELECT 0 AS a, 1 AS b, b AS c, a AS d").queryExecution.analyzed
+    plan match {
+      case Project(outerProjectList, Project(innerProjectList, _)) =>
+        assert(outerProjectList.map(_.name) == Seq("a", "b", "c", "d"))
+        assert(innerProjectList.map(_.name) == Seq("a", "b"))
+    }
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org