Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,19 @@ class Analyzer(
case e: Expand =>
failOnOuterReferenceInSubTree(e, "an EXPAND")
e
case l : LocalLimit =>
failOnOuterReferenceInSubTree(l, "a LIMIT")
l
// Since LIMIT <n> is represented as GlobalLimit(<n>, (LocalLimit (<n>, child))
// and we are walking bottom up, we will fail on LocalLimit before
// reaching GlobalLimit.
// The code below is just a safety net.
case g : GlobalLimit =>
failOnOuterReferenceInSubTree(g, "a LIMIT")
g
case s : Sample =>
failOnOuterReferenceInSubTree(s, "a TABLESAMPLE")
s
case p =>
failOnOuterReference(p)
p
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ object PushDownPredicate extends Rule[LogicalPlan] with PredicateHelper {
// state and all the input rows processed before. In another word, the order of input rows
// matters for non-deterministic expressions, while pushing down predicates changes the order.
case filter @ Filter(condition, project @ Project(fields, grandChild))
if fields.forall(_.deterministic) =>
if fields.forall(_.deterministic) && canPushThroughCondition(grandChild, condition) =>

// Create a map of Aliases to their values from the child projection.
// e.g., 'SELECT a + b AS c, d ...' produces Map(c -> a + b).
Expand Down Expand Up @@ -1161,6 +1161,20 @@ object PushDownPredicate extends Rule[LogicalPlan] with PredicateHelper {
filter
}
}

/**
* Check if we can safely push a filter through a projection, by making sure that predicate
* subqueries in the condition do not contain the same attributes as the plan they are moved
* into. This can happen when the plan and predicate subquery have the same source.
*/
private def canPushThroughCondition(plan: LogicalPlan, condition: Expression): Boolean = {
val attributes = plan.outputSet
val matched = condition.find {
case PredicateSubquery(p, _, _, _) => p.outputSet.intersect(attributes).nonEmpty
case _ => false
}
matched.isEmpty
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,5 +533,22 @@ class AnalysisErrorSuite extends AnalysisTest {
Exists(Union(LocalRelation(b), Filter(EqualTo(OuterReference(a), c), LocalRelation(c)))),
LocalRelation(a))
assertAnalysisError(plan3, "Accessing outer query column is not allowed in" :: Nil)

val plan4 = Filter(
Exists(
Limit(1,
Filter(EqualTo(OuterReference(a), b), LocalRelation(b)))
),
LocalRelation(a))
assertAnalysisError(plan4, "Accessing outer query column is not allowed in a LIMIT" :: Nil)

val plan5 = Filter(
Exists(
Sample(0.0, 0.5, false, 1L,
Filter(EqualTo(OuterReference(a), b), LocalRelation(b)))().select('b)
),
LocalRelation(a))
assertAnalysisError(plan5,
"Accessing outer query column is not allowed in a TABLESAMPLE" :: Nil)
}
}
44 changes: 44 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,48 @@ class SubquerySuite extends QueryTest with SharedSQLContext {
Row(1.0, false) :: Row(1.0, false) :: Row(2.0, true) :: Row(2.0, true) ::
Row(3.0, false) :: Row(5.0, true) :: Row(null, false) :: Row(null, true) :: Nil)
}

test("SPARK-16804: Correlated subqueries containing LIMIT - 1") {
withTempView("onerow") {
Seq(1).toDF("c1").createOrReplaceTempView("onerow")

checkAnswer(
sql(
"""
| select c1 from onerow t1
| where exists (select 1 from onerow t2 where t1.c1=t2.c1)
| and exists (select 1 from onerow LIMIT 1)""".stripMargin),
Row(1) :: Nil)
}
}

test("SPARK-16804: Correlated subqueries containing LIMIT - 2") {
withTempView("onerow") {
Seq(1).toDF("c1").createOrReplaceTempView("onerow")

checkAnswer(
sql(
"""
| select c1 from onerow t1
| where exists (select 1
| from (select 1 from onerow t2 LIMIT 1)
| where t1.c1=t2.c1)""".stripMargin),
Row(1) :: Nil)
}
}

test("SPARK-17337: Incorrect column resolution leads to incorrect results") {
withTempView("t1", "t2") {
Seq(1, 2).toDF("c1").createOrReplaceTempView("t1")
Seq(1).toDF("c2").createOrReplaceTempView("t2")
checkAnswer(
sql(
"""
| select *
| from (select t2.c2+1 as c3
| from t1 left join t2 on t1.c1=t2.c2) t3
| where c3 not in (select c2 from t2)""".stripMargin),
Row(2) :: Nil)
}
}
}