-
Notifications
You must be signed in to change notification settings - Fork 28.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-19993][SQL] Caching logical plans containing subquery expressions does not work. #17330
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,6 @@ abstract class SubqueryExpression( | |
plan: LogicalPlan, | ||
children: Seq[Expression], | ||
exprId: ExprId) extends PlanExpression[LogicalPlan] { | ||
|
||
override lazy val resolved: Boolean = childrenResolved && plan.resolved | ||
override lazy val references: AttributeSet = | ||
if (plan.resolved) super.references -- plan.outputSet else super.references | ||
|
@@ -59,6 +58,13 @@ abstract class SubqueryExpression( | |
children.zip(p.children).forall(p => p._1.semanticEquals(p._2)) | ||
case _ => false | ||
} | ||
def canonicalize(attrs: AttributeSeq): SubqueryExpression = { | ||
// Normalize the outer references in the subquery plan. | ||
val subPlan = plan.transformAllExpressions { | ||
case OuterReference(r) => QueryPlan.normalizeExprId(r, attrs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cloud-fan Actually you r right. Preserving the OuterReference would be good. |
||
} | ||
withNewPlan(subPlan).canonicalized.asInstanceOf[SubqueryExpression] | ||
} | ||
} | ||
|
||
object SubqueryExpression { | ||
|
@@ -236,6 +242,12 @@ case class ScalarSubquery( | |
override def nullable: Boolean = true | ||
override def withNewPlan(plan: LogicalPlan): ScalarSubquery = copy(plan = plan) | ||
override def toString: String = s"scalar-subquery#${exprId.id} $conditionString" | ||
override lazy val canonicalized: Expression = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can just override There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cloud-fan preCanonicalize is a method in QueryPlan ? Can we override it here ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh sorry it's expression |
||
ScalarSubquery( | ||
plan.canonicalized, | ||
children.map(_.canonicalized), | ||
ExprId(0)) | ||
} | ||
} | ||
|
||
object ScalarSubquery { | ||
|
@@ -268,6 +280,12 @@ case class ListQuery( | |
override def nullable: Boolean = false | ||
override def withNewPlan(plan: LogicalPlan): ListQuery = copy(plan = plan) | ||
override def toString: String = s"list#${exprId.id} $conditionString" | ||
override lazy val canonicalized: Expression = { | ||
ListQuery( | ||
plan.canonicalized, | ||
children.map(_.canonicalized), | ||
ExprId(0)) | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -290,4 +308,10 @@ case class Exists( | |
override def nullable: Boolean = false | ||
override def withNewPlan(plan: LogicalPlan): Exists = copy(plan = plan) | ||
override def toString: String = s"exists#${exprId.id} $conditionString" | ||
override lazy val canonicalized: Expression = { | ||
Exists( | ||
plan.canonicalized, | ||
children.map(_.canonicalized), | ||
ExprId(0)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ import org.scalatest.concurrent.Eventually._ | |
import org.apache.spark.CleanerListener | ||
import org.apache.spark.sql.catalyst.TableIdentifier | ||
import org.apache.spark.sql.catalyst.expressions.SubqueryExpression | ||
import org.apache.spark.sql.execution.RDDScanExec | ||
import org.apache.spark.sql.execution.{RDDScanExec, SparkPlan} | ||
import org.apache.spark.sql.execution.columnar._ | ||
import org.apache.spark.sql.execution.exchange.ShuffleExchange | ||
import org.apache.spark.sql.functions._ | ||
|
@@ -76,6 +76,13 @@ class CachedTableSuite extends QueryTest with SQLTestUtils with SharedSQLContext | |
sum | ||
} | ||
|
||
private def getNumInMemoryTableScanExecs(plan: SparkPlan): Int = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need a better name, this actually get in-memory table recursively, which is different from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cloud-fan So we are operating at the physical plan level in this method where as the other method getNumInMemoryRelations operates at a logical plan level. And in here we are simply counting the the InMemoryTableScanExec nodes in the plan. I have changed the function name to getNumInMemoryTablesRecursively. Does it look ok to you ? |
||
plan.collect { | ||
case InMemoryTableScanExec(_, _, relation) => | ||
getNumInMemoryTableScanExecs(relation.child) + 1 | ||
}.sum | ||
} | ||
|
||
test("withColumn doesn't invalidate cached dataframe") { | ||
var evalCount = 0 | ||
val myUDF = udf((x: String) => { evalCount += 1; "result" }) | ||
|
@@ -670,4 +677,139 @@ class CachedTableSuite extends QueryTest with SQLTestUtils with SharedSQLContext | |
assert(spark.read.parquet(path).filter($"id" > 4).count() == 15) | ||
} | ||
} | ||
|
||
test("SPARK-19993 simple subquery caching") { | ||
withTempView("t1", "t2") { | ||
Seq(1).toDF("c1").createOrReplaceTempView("t1") | ||
Seq(1).toDF("c1").createOrReplaceTempView("t2") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cloud-fan sorry... actually i had some of these tests combined and when i split, i forgot to remove this. Will fix it. |
||
|
||
sql( | ||
""" | ||
|SELECT * FROM t1 | ||
|WHERE | ||
|NOT EXISTS (SELECT * FROM t1) | ||
""".stripMargin).cache() | ||
|
||
val cachedDs = | ||
sql( | ||
""" | ||
|SELECT * FROM t1 | ||
|WHERE | ||
|NOT EXISTS (SELECT * FROM t1) | ||
""".stripMargin) | ||
assert(getNumInMemoryRelations(cachedDs) == 1) | ||
|
||
// Additional predicate in the subquery plan should cause a cache miss | ||
val cachedMissDs = | ||
sql( | ||
""" | ||
|SELECT * FROM t1 | ||
|WHERE | ||
|NOT EXISTS (SELECT * FROM t1 where c1 = 0) | ||
""".stripMargin) | ||
assert(getNumInMemoryRelations(cachedMissDs) == 0) | ||
} | ||
} | ||
|
||
test("SPARK-19993 subquery caching with correlated predicates") { | ||
withTempView("t1", "t2") { | ||
Seq(1).toDF("c1").createOrReplaceTempView("t1") | ||
Seq(1).toDF("c1").createOrReplaceTempView("t2") | ||
|
||
// Simple correlated predicate in subquery | ||
sql( | ||
""" | ||
|SELECT * FROM t1 | ||
|WHERE | ||
|t1.c1 in (SELECT t2.c1 FROM t2 where t1.c1 = t2.c1) | ||
""".stripMargin).cache() | ||
|
||
val cachedDs = | ||
sql( | ||
""" | ||
|SELECT * FROM t1 | ||
|WHERE | ||
|t1.c1 in (SELECT t2.c1 FROM t2 where t1.c1 = t2.c1) | ||
""".stripMargin) | ||
assert(getNumInMemoryRelations(cachedDs) == 1) | ||
} | ||
} | ||
|
||
test("SPARK-19993 subquery with cached underlying relation") { | ||
withTempView("t1", "t2") { | ||
Seq(1).toDF("c1").createOrReplaceTempView("t1") | ||
Seq(1).toDF("c1").createOrReplaceTempView("t2") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cloud-fan sorry... actually i had some of these tests combined and when i split, i forgot to remove this. Will fix it. |
||
spark.catalog.cacheTable("t1") | ||
|
||
// underlying table t1 is cached as well as the query that refers to it. | ||
val ds = | ||
sql( | ||
""" | ||
|SELECT * FROM t1 | ||
|WHERE | ||
|NOT EXISTS (SELECT * FROM t1) | ||
""".stripMargin) | ||
assert(getNumInMemoryRelations(ds) == 2) | ||
|
||
val cachedDs = | ||
sql( | ||
""" | ||
|SELECT * FROM t1 | ||
|WHERE | ||
|NOT EXISTS (SELECT * FROM t1) | ||
""".stripMargin).cache() | ||
assert(getNumInMemoryTableScanExecs(cachedDs.queryExecution.sparkPlan) == 3) | ||
} | ||
} | ||
|
||
test("SPARK-19993 nested subquery caching and scalar + predicate subqueris") { | ||
withTempView("t1", "t2", "t3", "t4") { | ||
Seq(1).toDF("c1").createOrReplaceTempView("t1") | ||
Seq(2).toDF("c1").createOrReplaceTempView("t2") | ||
Seq(1).toDF("c1").createOrReplaceTempView("t3") | ||
Seq(1).toDF("c1").createOrReplaceTempView("t4") | ||
|
||
// Nested predicate subquery | ||
sql( | ||
""" | ||
|SELECT * FROM t1 | ||
|WHERE | ||
|c1 IN (SELECT c1 FROM t2 WHERE c1 IN (SELECT c1 FROM t3 WHERE c1 = 1)) | ||
""".stripMargin).cache() | ||
|
||
val cachedDs = | ||
sql( | ||
""" | ||
|SELECT * FROM t1 | ||
|WHERE | ||
|c1 IN (SELECT c1 FROM t2 WHERE c1 IN (SELECT c1 FROM t3 WHERE c1 = 1)) | ||
""".stripMargin) | ||
assert(getNumInMemoryRelations(cachedDs) == 1) | ||
|
||
// Scalar subquery and predicate subquery | ||
sql( | ||
""" | ||
|SELECT * FROM (SELECT max(c1) FROM t1 GROUP BY c1) | ||
|WHERE | ||
|c1 = (SELECT max(c1) FROM t2 GROUP BY c1) | ||
|OR | ||
|EXISTS (SELECT c1 FROM t3) | ||
|OR | ||
|c1 IN (SELECT c1 FROM t4) | ||
""".stripMargin).cache() | ||
|
||
val cachedDs2 = | ||
sql( | ||
""" | ||
|SELECT * FROM (SELECT max(c1) FROM t1 GROUP BY c1) | ||
|WHERE | ||
|c1 = (SELECT max(c1) FROM t2 GROUP BY c1) | ||
|OR | ||
|EXISTS (SELECT c1 FROM t3) | ||
|OR | ||
|c1 IN (SELECT c1 FROM t4) | ||
""".stripMargin) | ||
assert(getNumInMemoryRelations(cachedDs2) == 1) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normalizedPlan