Skip to content

Commit

Permalink
[SPARK-15232][SQL] Add subquery SQL building tests to LogicalPlanToSQ…
Browse files Browse the repository at this point in the history
…LSuite
  • Loading branch information
dongjoon-hyun committed Jul 27, 2016
1 parent bc4851a commit 5b313fc
Showing 1 changed file with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,159 @@ class LogicalPlanToSQLSuite extends SQLBuilderTest with SQLTestUtils {
}
}

test("mapjoin_subquery") {
checkSQL(
"""
|SELECT subq.key1, z.value
|FROM (SELECT x.key as key1, x.value as value1, y.key as key2, y.value as value2
| FROM src1 x JOIN src y ON (x.key = y.key)) subq
|JOIN srcpart z ON (subq.key1 = z.key and z.ds='2008-04-08' and z.hr=11)
|ORDER BY subq.key1, z.value
""".stripMargin,
"mapjoin_subquery")
}

test("subq2") {
checkSQL(
"""
|SELECT a.k, a.c
|FROM (SELECT b.key as k, count(1) as c
| FROM src b
| GROUP BY b.key) a
|WHERE a.k >= 90
""".stripMargin,
"subq2")
}

test("subquery_exists") {
checkSQL(
"""
|select *
|from src b
|where exists (select a.key
| from src a
| where b.value = a.value and a.key = b.key and a.value > 'val_9')
""".stripMargin,
"subquery_exists_1")

checkSQL(
"""
|select *
|from (select *
| from src b
| where exists (select a.key
| from src a
| where b.value = a.value and a.key = b.key and a.value > 'val_9')) a
""".stripMargin,
"subquery_exists_2")
}

test("subquery_exists_having") {
checkSQL(
"""
|select b.key, count(*)
|from src b
|group by b.key
|having exists (select a.key
| from src a
| where a.key = b.key and a.value > 'val_9')
""".stripMargin,
"subquery_exists_having_1")

checkSQL(
"""
|select *
|from (select b.key, count(*)
| from src b
| group by b.key
| having exists (select a.key
| from src a
| where a.key = b.key and a.value > 'val_9')) a
""".stripMargin,
"subquery_exists_having_2")

checkSQL(
"""
|select b.key, min(b.value)
|from src b
|group by b.key
|having exists (select a.key
| from src a
| where a.value > 'val_9' and a.value = min(b.value))
""".stripMargin,
"subquery_exists_having_3")
}

test("subquery_notexists") {
checkSQL(
"""
|select *
|from src b
|where not exists (select a.key
| from src a
| where b.value = a.value and a.key = b.key and a.value > 'val_2')
""".stripMargin,
"subquery_notexists_1")

checkSQL(
"""
|select *
|from src b
|where not exists (select a.key
| from src a
| where b.value = a.value and a.value > 'val_2')
""".stripMargin,
"subquery_notexists_2")
}

test("subquery_notexists_having") {
checkSQL(
"""
|select *
|from src b
|group by key, value
|having not exists (select a.key
| from src a
| where b.value = a.value and a.key = b.key and a.value > 'val_12')
""".stripMargin,
"subquery_notexists_having_1")

checkSQL(
"""
|select *
|from src b
|group by key, value
|having not exists (select distinct a.key
| from src a
| where b.value = a.value and a.value > 'val_12')
""".stripMargin,
"subquery_notexists_having_2")
}

test("subquery_in_having") {
checkSQL(
"""
|select key, count(*)
|from src
|group by key
|having count(*) in (select count(*) from src s1 where s1.key = '90' group by s1.key)
|order by key
""".stripMargin,
"subquery_in_having_1")

checkSQL(
"""
|select b.key, min(b.value)
|from src b
|group by b.key
|having b.key in (select a.key
| from src a
| where a.value > 'val_9' and a.value = min(b.value))
|order by b.key
""".stripMargin,
"subquery_in_having_2")
}

test("SPARK-14933 - select orc table") {
withTable("orc_t") {
sql("create table orc_t stored as orc as select 1 as c1, 'abc' as c2")
Expand Down

0 comments on commit 5b313fc

Please sign in to comment.