diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala index 7f7e23eca3481..8be02c00759df 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala @@ -90,7 +90,10 @@ trait PredicateHelper { * Returns true iff `expr` could be evaluated as a condition within join. */ protected def canEvaluateWithinJoin(expr: Expression): Boolean = expr match { - case e: SubqueryExpression => false + case l: ListQuery => false + case e: SubqueryExpression => + // non-correlated subquery will be replaced as literal + e.children.isEmpty case a: AttributeReference => true case e: Unevaluable => false case e => e.children.forall(canEvaluateWithinJoin) diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/JoinOptimizationSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/JoinOptimizationSuite.scala index f8b6ff643b2ea..182a2071ff0bd 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/JoinOptimizationSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/JoinOptimizationSuite.scala @@ -136,9 +136,9 @@ class JoinOptimizationSuite extends PlanTest { } } - test("reorder inner joins - don't put predicate with subquery into join condition") { + test("reorder inner joins - don't put predicate with IN subquery into join condition") { // ReorderJoin collects all predicates and try to put them into join condition when creating - // ordered join. If a predicate with a subquery is in a join condition instead of a filter + // ordered join. If a predicate with an IN subquery is in a join condition instead of a filter // condition, `RewritePredicateSubquery.rewriteExistentialExpr` would fail to convert the // subquery to an ExistenceJoin, and thus result in error. val x = testRelation.subquery('x)