Skip to content
Snippets Groups Projects
Commit 78b38109 authored by Luciano Resende's avatar Luciano Resende Committed by Davies Liu
Browse files

[SPARK-13419] [SQL] Update SubquerySuite to use checkAnswer for validation

## What changes were proposed in this pull request?

Change SubquerySuite to validate test results utilizing checkAnswer helper method

## How was this patch tested?

Existing tests

Author: Luciano Resende <lresende@apache.org>

Closes #12269 from lresende/SPARK-13419.
parent 8eedf0b5
No related branches found
No related tags found
No related merge requests found
...@@ -55,32 +55,37 @@ class SubquerySuite extends QueryTest with SharedSQLContext { ...@@ -55,32 +55,37 @@ class SubquerySuite extends QueryTest with SharedSQLContext {
} }
test("simple uncorrelated scalar subquery") { test("simple uncorrelated scalar subquery") {
assertResult(Array(Row(1))) { checkAnswer(
sql("select (select 1 as b) as b").collect() sql("select (select 1 as b) as b"),
} Array(Row(1))
)
assertResult(Array(Row(3))) { checkAnswer(
sql("select (select (select 1) + 1) + 1").collect() sql("select (select (select 1) + 1) + 1"),
} Array(Row(3))
)
// string type // string type
assertResult(Array(Row("s"))) { checkAnswer(
sql("select (select 's' as s) as b").collect() sql("select (select 's' as s) as b"),
} Array(Row("s"))
)
} }
test("uncorrelated scalar subquery in CTE") { test("uncorrelated scalar subquery in CTE") {
assertResult(Array(Row(1))) { checkAnswer(
sql("with t2 as (select 1 as b, 2 as c) " + sql("with t2 as (select 1 as b, 2 as c) " +
"select a from (select 1 as a union all select 2 as a) t " + "select a from (select 1 as a union all select 2 as a) t " +
"where a = (select max(b) from t2) ").collect() "where a = (select max(b) from t2) "),
} Array(Row(1))
)
} }
test("uncorrelated scalar subquery should return null if there is 0 rows") { test("uncorrelated scalar subquery should return null if there is 0 rows") {
assertResult(Array(Row(null))) { checkAnswer(
sql("select (select 's' as s limit 0) as b").collect() sql("select (select 's' as s limit 0) as b"),
} Array(Row(null))
)
} }
test("runtime error when the number of rows is greater than 1") { test("runtime error when the number of rows is greater than 1") {
...@@ -88,29 +93,34 @@ class SubquerySuite extends QueryTest with SharedSQLContext { ...@@ -88,29 +93,34 @@ class SubquerySuite extends QueryTest with SharedSQLContext {
sql("select (select a from (select 1 as a union all select 2 as a) t) as b").collect() sql("select (select a from (select 1 as a union all select 2 as a) t) as b").collect()
} }
assert(error2.getMessage.contains( assert(error2.getMessage.contains(
"more than one row returned by a subquery used as an expression")) "more than one row returned by a subquery used as an expression")
)
} }
test("uncorrelated scalar subquery on a DataFrame generated query") { test("uncorrelated scalar subquery on a DataFrame generated query") {
val df = Seq((1, "one"), (2, "two"), (3, "three")).toDF("key", "value") val df = Seq((1, "one"), (2, "two"), (3, "three")).toDF("key", "value")
df.registerTempTable("subqueryData") df.registerTempTable("subqueryData")
assertResult(Array(Row(4))) { checkAnswer(
sql("select (select key from subqueryData where key > 2 order by key limit 1) + 1").collect() sql("select (select key from subqueryData where key > 2 order by key limit 1) + 1"),
} Array(Row(4))
)
assertResult(Array(Row(-3))) { checkAnswer(
sql("select -(select max(key) from subqueryData)").collect() sql("select -(select max(key) from subqueryData)"),
} Array(Row(-3))
)
assertResult(Array(Row(null))) { checkAnswer(
sql("select (select value from subqueryData limit 0)").collect() sql("select (select value from subqueryData limit 0)"),
} Array(Row(null))
)
assertResult(Array(Row("two"))) { checkAnswer(
sql("select (select min(value) from subqueryData" + sql("select (select min(value) from subqueryData" +
" where key = (select max(key) from subqueryData) - 1)").collect() " where key = (select max(key) from subqueryData) - 1)"),
} Array(Row("two"))
)
} }
test("EXISTS predicate subquery") { test("EXISTS predicate subquery") {
......
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