Skip to content
Snippets Groups Projects
Commit 06f5dc84 authored by Nattavut Sutyanyong's avatar Nattavut Sutyanyong Committed by Herman van Hovell
Browse files

[SPARK-16804][SQL] Correlated subqueries containing non-deterministic...

[SPARK-16804][SQL] Correlated subqueries containing non-deterministic operations return incorrect results

## What changes were proposed in this pull request?

This patch fixes the incorrect results in the rule ResolveSubquery in Catalyst's Analysis phase by returning an error message when the LIMIT is found in the path from the parent table to the correlated predicate in the subquery.

## How was this patch tested?

./dev/run-tests
a new unit test on the problematic pattern.

Author: Nattavut Sutyanyong <nsy.can@gmail.com>

Closes #14411 from nsyca/master.
parent e10ca8de
No related branches found
No related tags found
No related merge requests found
......@@ -1021,6 +1021,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
......
......@@ -548,5 +548,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)
}
}
......@@ -571,4 +571,33 @@ 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)
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment