Skip to content

Commit a85db5e

Browse files
committed
[SPARK-18609][SQL][WIP]Fix when CTE with Join between two table with same column name
1 parent dbf842b commit a85db5e

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import org.apache.spark.api.java.function.FilterFunction
2626
import org.apache.spark.sql.AnalysisException
2727
import org.apache.spark.sql.catalyst.{CatalystConf, SimpleCatalystConf}
2828
import org.apache.spark.sql.catalyst.analysis._
29-
import org.apache.spark.sql.catalyst.catalog.{InMemoryCatalog, SessionCatalog}
29+
import org.apache.spark.sql.catalyst.catalog.{CatalogRelation, InMemoryCatalog, SessionCatalog}
3030
import org.apache.spark.sql.catalyst.expressions._
3131
import org.apache.spark.sql.catalyst.expressions.aggregate._
3232
import org.apache.spark.sql.catalyst.expressions.Literal.{FalseLiteral, TrueLiteral}
@@ -200,6 +200,7 @@ object RemoveAliasOnlyProject extends Rule[LogicalPlan] {
200200
case plan: Project if plan eq proj => plan.child
201201
case plan => plan transformExpressions {
202202
case a: Attribute if attrMap.contains(a) => attrMap(a)
203+
case b: Alias if (attrMap.exists(_._1.exprId == b.exprId)) => b.child
203204
}
204205
}
205206
}.getOrElse(plan)
@@ -416,8 +417,8 @@ object ColumnPruning extends Rule[LogicalPlan] {
416417
case w: Window if w.windowExpressions.isEmpty => w.child
417418

418419
// Eliminate no-op Projects
419-
case p @ Project(_, child) if sameOutput(child.output, p.output) => child
420-
420+
case p @ Project(_, child) if sameOutput(child.output, p.output) =>
421+
if (child.isInstanceOf[CatalogRelation]) p else child
421422
// Can't prune the columns on LeafNode
422423
case p @ Project(_, _: LeafNode) => p
423424

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,6 +2011,22 @@ class SQLQuerySuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
20112011
}
20122012
}
20132013

2014+
test("test CTE with join between two table with the same column name ") {
2015+
sql("DROP TABLE IF EXISTS p1")
2016+
sql("DROP TABLE IF EXISTS p2")
2017+
sql("CREATE TABLE p1 (col String)" )
2018+
sql("CREATE TABLE p2 (col String)")
2019+
2020+
assert(
2021+
sql(
2022+
"""
2023+
| WITH CTE AS
2024+
| (SELECT s2.col as col FROM p1
2025+
| CROSS JOIN (SELECT e.col as col FROM p2 E) s2)
2026+
| SELECT T1.col as c1,T2.col as c2 FROM CTE T1 CROSS JOIN CTE T2
2027+
""".stripMargin).collect.isEmpty)
2028+
}
2029+
20142030
def testCommandAvailable(command: String): Boolean = {
20152031
val attempt = Try(Process(command).run(ProcessLogger(_ => ())).exitValue())
20162032
attempt.isSuccess && attempt.get == 0

0 commit comments

Comments
 (0)