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/09 01:18:35 UTC
(spark) branch master updated: [SPARK-52040][PYTHON][SQL][CONNECT] ResolveLateralColumnAliasReference should retain the plan id
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 688281ae13ec [SPARK-52040][PYTHON][SQL][CONNECT] ResolveLateralColumnAliasReference should retain the plan id
688281ae13ec is described below
commit 688281ae13ecfaf5111e28ef54d6c8163c2fda99
Author: Ruifeng Zheng <ru...@apache.org>
AuthorDate: Fri May 9 09:18:19 2025 +0800
[SPARK-52040][PYTHON][SQL][CONNECT] ResolveLateralColumnAliasReference should retain the plan id
### What changes were proposed in this pull request?
ResolveLateralColumnAliasReference should retain the plan id
### Why are the changes needed?
bug fix
before:
```
In [1]: from pyspark.sql import functions as sf
In [2]: df1 = spark.range(10).select((sf.col("id") + sf.lit(1)).alias("x"), (sf.col("x") + sf.lit(1)).alias("y"))
In [3]: df2 = spark.range(10).select(sf.col("id").alias("x"))
In [4]: df1.join(df2, df1.x == df2.x).select(df1.y)
Out[4]: 25/05/08 16:38:28 ERROR ErrorUtils: Spark Connect RPC error during: analyze. UserId: ruifeng.zheng. SessionId: af3deba7-1e48-49fd-adad-2046a72ed341.
org.apache.spark.sql.AnalysisException: [CANNOT_RESOLVE_DATAFRAME_COLUMN] Cannot resolve dataframe column "y". It's probably because of illegal references like `df1.select(df2.col("a"))`. SQLSTATE: 42704
at org.apache.spark.sql.errors.QueryCompilationErrors$.cannotResolveDataFrameColumn(QueryCompilationErrors.scala:4147)
at org.apache.spark.sql.catalyst.analysis.ColumnResolutionHelper.resolveDataFrameColumn(ColumnResolutionHelper.scala:562)
at org.apache.spark.sql.catalyst.analysis.ColumnResolutionHelper.tryResolveDataFrameColumns(ColumnResolutionHelper.scala:537)
```
after:
```
In [1]: from pyspark.sql import functions as sf
In [2]: df1 = spark.range(10).select((sf.col("id") + sf.lit(1)).alias("x"), (sf.col("x") + sf.lit(1)).alias("y"))
In [3]: df2 = spark.range(10).select(sf.col("id").alias("x"))
In [4]: df1.join(df2, df1.x == df2.x).select(df1.y).show()
+---+
| y|
+---+
| 2|
| 3|
| 4|
| 5|
| 6|
| 7|
| 8|
| 9|
| 10|
+---+
```
### Does this PR introduce _any_ user-facing change?
yes, above query works after this change
### How was this patch tested?
added test
### Was this patch authored or co-authored using generative AI tooling?
no
Closes #50831 from zhengruifeng/fix_lca.
Authored-by: Ruifeng Zheng <ru...@apache.org>
Signed-off-by: Wenchen Fan <we...@databricks.com>
---
python/pyspark/sql/tests/test_dataframe.py | 9 +++++++++
.../catalyst/analysis/ResolveLateralColumnAliasReference.scala | 9 ++++++---
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/python/pyspark/sql/tests/test_dataframe.py b/python/pyspark/sql/tests/test_dataframe.py
index e20ab09d8d1e..890ae56ffa52 100644
--- a/python/pyspark/sql/tests/test_dataframe.py
+++ b/python/pyspark/sql/tests/test_dataframe.py
@@ -148,6 +148,15 @@ class DataFrameTestsMixin:
self.assertTrue(df3.columns, ["id", "value", "id", "value"])
self.assertTrue(df3.count() == 20)
+ def test_lateral_column_alias(self):
+ df1 = self.spark.range(10).select(
+ (col("id") + lit(1)).alias("x"), (col("x") + lit(1)).alias("y")
+ )
+ df2 = self.spark.range(10).select(col("id").alias("x"))
+ df3 = df1.join(df2, df1.x == df2.x).select(df1.y)
+ self.assertTrue(df3.columns, ["y"])
+ self.assertTrue(df3.count() == 9)
+
def test_duplicated_column_names(self):
df = self.spark.createDataFrame([(1, 2)], ["c", "c"])
row = df.select("*").first()
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 70f81fe83c4d..632d01f18ec2 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
@@ -194,11 +194,12 @@ object ResolveLateralColumnAliasReference extends Rule[LogicalPlan] {
outerProjectList.update(idx, alias.toAttribute)
innerProjectList += alias
}
-
- p.copy(
+ val newProject = p.copy(
projectList = outerProjectList.toSeq,
child = Project(innerProjectList.toSeq, child)
)
+ newProject.copyTagsFrom(p)
+ newProject
}
case aggOriginal: Aggregate
@@ -269,10 +270,12 @@ object ResolveLateralColumnAliasReference extends Rule[LogicalPlan] {
}
val projectExprs = aggregateExpressions.map(
extractExpressions(_).asInstanceOf[NamedExpression])
- Project(
+ val newProject = Project(
projectList = projectExprs,
child = agg.copy(aggregateExpressions = newAggExprs.asScala.toSeq)
)
+ newProject.copyTagsFrom(agg)
+ newProject
}
case p: LogicalPlan =>
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org