Skip to content

Commit fc63b89

Browse files
Cache Table is not working while subquery has alias in its project list
1 parent ab431f8 commit fc63b89

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/LogicalPlan.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,25 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] with Logging {
135135
/** Args that have cleaned such that differences in expression id should not affect equality */
136136
protected lazy val cleanArgs: Seq[Any] = {
137137
val input = children.flatMap(_.output)
138+
def cleanExpression(e: Expression) = e match {
139+
case a: Alias =>
140+
// As the root of the expression, Alias will always take an arbitrary exprId, we need
141+
// to erase that for equality testing.
142+
val cleanedExprId = Alias(a.child, a.name)(ExprId(-1), a.qualifiers)
143+
BindReferences.bindReference(cleanedExprId, input, allowFailures = true)
144+
case other => BindReferences.bindReference(other, input, allowFailures = true)
145+
}
146+
138147
productIterator.map {
139148
// Children are checked using sameResult above.
140149
case tn: TreeNode[_] if containsChild(tn) => null
141-
case e: Expression => BindReferences.bindReference(e, input, allowFailures = true)
150+
case e: Expression => cleanExpression(e)
142151
case s: Option[_] => s.map {
143-
case e: Expression => BindReferences.bindReference(e, input, allowFailures = true)
152+
case e: Expression => cleanExpression(e)
144153
case other => other
145154
}
146155
case s: Seq[_] => s.map {
147-
case e: Expression => BindReferences.bindReference(e, input, allowFailures = true)
156+
case e: Expression => cleanExpression(e)
148157
case other => other
149158
}
150159
case other => other

sql/hive/src/test/scala/org/apache/spark/sql/hive/CachedTableSuite.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,18 @@ class CachedTableSuite extends QueryTest {
203203
sql("DROP TABLE refreshTable")
204204
Utils.deleteRecursively(tempPath)
205205
}
206+
207+
test("SPARK-10327 Cache Table is not working while subquery has alias in its project list") {
208+
import org.apache.spark.sql.hive.execution.HiveTableScan
209+
sql("select key, value, key + 1 from src").registerTempTable("abc")
210+
cacheTable("abc")
211+
212+
val sparkPlan = sql(
213+
"""select a.key, b.key, c.key from
214+
|abc a join abc b on a.key=b.key
215+
|join abc c on a.key=c.key""".stripMargin).queryExecution.sparkPlan
216+
217+
assert(sparkPlan.collect { case e: InMemoryColumnarTableScan => e }.size === 3)
218+
assert(sparkPlan.collect { case e: HiveTableScan => e }.size === 0)
219+
}
206220
}

0 commit comments

Comments
 (0)