allisonwang-db commented on code in PR #37074:
URL: https://github.com/apache/spark/pull/37074#discussion_r920409369
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -742,12 +742,22 @@ object ColumnPruning extends Rule[LogicalPlan] {
* order, otherwise lower Projects can be missed.
*/
private def removeProjectBeforeFilter(plan: LogicalPlan): LogicalPlan = plan
transformUp {
- case p1 @ Project(_, f @ Filter(_, p2 @ Project(_, child)))
+ case p1 @ Project(_, f @ Filter(e, p2 @ Project(_, child)))
if p2.outputSet.subsetOf(child.outputSet) &&
// We only remove attribute-only project.
- p2.projectList.forall(_.isInstanceOf[AttributeReference]) =>
+ p2.projectList.forall(_.isInstanceOf[AttributeReference]) &&
+ !hasConflictingAttrsWithSubquery(e, child) =>
Review Comment:
Can we add a comment here to explain why this check is needed?
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala:
##########
@@ -72,12 +72,22 @@ object RewritePredicateSubquery extends Rule[LogicalPlan]
with PredicateHelper {
val outerRefs = outerPlan.outputSet ++ outerReferences
val duplicates = outerRefs.intersect(subplan.outputSet)
if (duplicates.nonEmpty) {
- condition.foreach { e =>
+ def throwOnConflictingAttrs(attrs: AttributeSet): Unit = {
Review Comment:
nit: we can move this to QueryCompilationError
##########
sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala:
##########
@@ -1788,4 +1788,60 @@ class SubquerySuite extends QueryTest with
SharedSparkSession with AdaptiveSpark
}.getMessage.contains("Correlated column is not allowed in predicate"))
}
}
+
+ test("SPARK-39672: Fix removing project before filter with correlated
subquery") {
+ withTempView("v1", "v2") {
+ Seq((1, 2, 3), (4, 5, 6)).toDF("a", "b", "c").createTempView("v1")
+ Seq((1, 3, 5), (4, 5, 6)).toDF("a", "b", "c").createTempView("v2")
+
+ def findProjectExec(df: DataFrame): Seq[ProjectExec] = {
+ df.queryExecution.sparkPlan.collect {
Review Comment:
Would it be better to check the logical plan?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]