Skip to content

Commit cfe1ba5

Browse files
viiryadavies
authored andcommitted
[SPARK-12687] [SQL] Support from clause surrounded by ().
JIRA: https://issues.apache.org/jira/browse/SPARK-12687 Some queries such as `(select 1 as a) union (select 2 as a)` can't work. This patch fixes it. Author: Liang-Chi Hsieh <[email protected]> Closes #10660 from viirya/fix-union.
1 parent b9c8353 commit cfe1ba5

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

sql/catalyst/src/main/antlr3/org/apache/spark/sql/catalyst/parser/FromClauseParser.g

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ fromSource
151151
@after { gParent.popMsg(state); }
152152
:
153153
(LPAREN KW_VALUES) => fromSource0
154-
| (LPAREN) => LPAREN joinSource RPAREN -> joinSource
155154
| fromSource0
155+
| (LPAREN joinSource) => LPAREN joinSource RPAREN -> joinSource
156156
;
157157
158158

sql/catalyst/src/main/antlr3/org/apache/spark/sql/catalyst/parser/SparkSqlParser.g

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2216,6 +2216,8 @@ regularBody[boolean topLevel]
22162216
selectStatement[boolean topLevel]
22172217
:
22182218
(
2219+
(
2220+
LPAREN
22192221
s=selectClause
22202222
f=fromClause?
22212223
w=whereClause?
@@ -2227,6 +2229,20 @@ selectStatement[boolean topLevel]
22272229
sort=sortByClause?
22282230
win=window_clause?
22292231
l=limitClause?
2232+
RPAREN
2233+
|
2234+
s=selectClause
2235+
f=fromClause?
2236+
w=whereClause?
2237+
g=groupByClause?
2238+
h=havingClause?
2239+
o=orderByClause?
2240+
c=clusterByClause?
2241+
d=distributeByClause?
2242+
sort=sortByClause?
2243+
win=window_clause?
2244+
l=limitClause?
2245+
)
22302246
-> ^(TOK_QUERY $f? ^(TOK_INSERT ^(TOK_DESTINATION ^(TOK_DIR TOK_TMP_FILE))
22312247
$s $w? $g? $h? $o? $c?
22322248
$d? $sort? $win? $l?))
@@ -2241,7 +2257,10 @@ selectStatement[boolean topLevel]
22412257
22422258
setOpSelectStatement[CommonTree t, boolean topLevel]
22432259
:
2244-
(u=setOperator b=simpleSelectStatement
2260+
((
2261+
u=setOperator LPAREN b=simpleSelectStatement RPAREN
2262+
|
2263+
u=setOperator b=simpleSelectStatement)
22452264
-> {$setOpSelectStatement.tree != null && $u.tree.getType()==SparkSqlParser.TOK_UNIONDISTINCT}?
22462265
^(TOK_QUERY
22472266
^(TOK_FROM

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/CatalystQlSuite.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,9 @@ class CatalystQlSuite extends PlanTest {
2828
paresr.createPlan("select * from t1 union select * from t2")
2929
paresr.createPlan("select * from t1 except select * from t2")
3030
paresr.createPlan("select * from t1 intersect select * from t2")
31+
paresr.createPlan("(select * from t1) union all (select * from t2)")
32+
paresr.createPlan("(select * from t1) union distinct (select * from t2)")
33+
paresr.createPlan("(select * from t1) union (select * from t2)")
34+
paresr.createPlan("select * from ((select * from t1) union (select * from t2)) t")
3135
}
3236
}

0 commit comments

Comments
 (0)