Skip to content

Commit bdc08ab

Browse files
committed
[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 <[email protected]> Closes #17930 from cloud-fan/tmp. (cherry picked from commit 789bdbe) Signed-off-by: Wenchen Fan <[email protected]>
1 parent 69786ea commit bdc08ab

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ trait CheckAnalysis extends PredicateHelper {
128128
}
129129

130130
case s @ ScalarSubquery(query, conditions, _) =>
131+
checkAnalysis(query)
132+
131133
// If no correlation, the output must be exactly one column
132134
if (conditions.isEmpty && query.output.size != 1) {
133135
failAnalysis(
@@ -186,7 +188,6 @@ trait CheckAnalysis extends PredicateHelper {
186188
case fail => failAnalysis(s"Correlated scalar subqueries must be Aggregated: $fail")
187189
}
188190
}
189-
checkAnalysis(query)
190191
s
191192

192193
case s: SubqueryExpression =>

sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class SubquerySuite extends QueryTest with SharedSQLContext {
7272
}
7373
}
7474

75-
test("rdd deserialization does not crash [SPARK-15791]") {
75+
test("SPARK-15791: rdd deserialization does not crash") {
7676
sql("select (select 1 as b) as b").rdd.count()
7777
}
7878

@@ -839,4 +839,12 @@ class SubquerySuite extends QueryTest with SharedSQLContext {
839839
Row(0) :: Row(1) :: Nil)
840840
}
841841
}
842+
843+
test("SPARK-20688: correctly check analysis for scalar sub-queries") {
844+
withTempView("t") {
845+
Seq(1 -> "a").toDF("i", "j").createTempView("t")
846+
val e = intercept[AnalysisException](sql("SELECT (SELECT count(*) FROM t WHERE a = 1)"))
847+
assert(e.message.contains("cannot resolve '`a`' given input columns: [i, j]"))
848+
}
849+
}
842850
}

0 commit comments

Comments
 (0)