Skip to content
Snippets Groups Projects
Commit bdc08ab6 authored by Wenchen Fan's avatar Wenchen Fan
Browse files

[SPARK-20688][SQL] correctly check analysis for scalar sub-queries


In `CheckAnalysis`, we should call `checkAnalysis` for `ScalarSubquery` at the beginning, as later we will call `plan.output` which is invalid if `plan` is not resolved.

new regression test

Author: Wenchen Fan <wenchen@databricks.com>

Closes #17930 from cloud-fan/tmp.

(cherry picked from commit 789bdbe3)
Signed-off-by: default avatarWenchen Fan <wenchen@databricks.com>
parent 69786ea3
No related branches found
No related tags found
No related merge requests found
...@@ -128,6 +128,8 @@ trait CheckAnalysis extends PredicateHelper { ...@@ -128,6 +128,8 @@ trait CheckAnalysis extends PredicateHelper {
} }
case s @ ScalarSubquery(query, conditions, _) => case s @ ScalarSubquery(query, conditions, _) =>
checkAnalysis(query)
// If no correlation, the output must be exactly one column // If no correlation, the output must be exactly one column
if (conditions.isEmpty && query.output.size != 1) { if (conditions.isEmpty && query.output.size != 1) {
failAnalysis( failAnalysis(
...@@ -186,7 +188,6 @@ trait CheckAnalysis extends PredicateHelper { ...@@ -186,7 +188,6 @@ trait CheckAnalysis extends PredicateHelper {
case fail => failAnalysis(s"Correlated scalar subqueries must be Aggregated: $fail") case fail => failAnalysis(s"Correlated scalar subqueries must be Aggregated: $fail")
} }
} }
checkAnalysis(query)
s s
case s: SubqueryExpression => case s: SubqueryExpression =>
......
...@@ -72,7 +72,7 @@ class SubquerySuite extends QueryTest with SharedSQLContext { ...@@ -72,7 +72,7 @@ class SubquerySuite extends QueryTest with SharedSQLContext {
} }
} }
test("rdd deserialization does not crash [SPARK-15791]") { test("SPARK-15791: rdd deserialization does not crash") {
sql("select (select 1 as b) as b").rdd.count() sql("select (select 1 as b) as b").rdd.count()
} }
...@@ -839,4 +839,12 @@ class SubquerySuite extends QueryTest with SharedSQLContext { ...@@ -839,4 +839,12 @@ class SubquerySuite extends QueryTest with SharedSQLContext {
Row(0) :: Row(1) :: Nil) Row(0) :: Row(1) :: Nil)
} }
} }
test("SPARK-20688: correctly check analysis for scalar sub-queries") {
withTempView("t") {
Seq(1 -> "a").toDF("i", "j").createTempView("t")
val e = intercept[AnalysisException](sql("SELECT (SELECT count(*) FROM t WHERE a = 1)"))
assert(e.message.contains("cannot resolve '`a`' given input columns: [i, j]"))
}
}
} }
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