Skip to content

Commit 1375f34

Browse files
gengliangwanggatorsmile
authored andcommitted
[SPARK-25979][SQL] Window function: allow parentheses around window reference
## What changes were proposed in this pull request? Very minor parser bug, but possibly problematic for code-generated queries: Consider the following two queries: ``` SELECT avg(k) OVER (w) FROM kv WINDOW w AS (PARTITION BY v ORDER BY w) ORDER BY 1 ``` and ``` SELECT avg(k) OVER w FROM kv WINDOW w AS (PARTITION BY v ORDER BY w) ORDER BY 1 ``` The former, with parens around the OVER condition, fails to parse while the latter, without parens, succeeds: ``` Error in SQL statement: ParseException: mismatched input '(' expecting {<EOF>, ',', 'FROM', 'WHERE', 'GROUP', 'ORDER', 'HAVING', 'LIMIT', 'LATERAL', 'WINDOW', 'UNION', 'EXCEPT', 'MINUS', 'INTERSECT', 'SORT', 'CLUSTER', 'DISTRIBUTE'}(line 1, pos 19) == SQL == SELECT avg(k) OVER (w) FROM kv WINDOW w AS (PARTITION BY v ORDER BY w) ORDER BY 1 -------------------^^^ ``` This was found when running the cockroach DB tests. I tried PostgreSQL, The SQL with parentheses is also workable. ## How was this patch tested? Unit test Closes apache#22987 from gengliangwang/windowParentheses. Authored-by: Gengliang Wang <[email protected]> Signed-off-by: gatorsmile <[email protected]> (cherry picked from commit 1db7997) Signed-off-by: gatorsmile <[email protected]>
1 parent bb58a97 commit 1375f34

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ namedWindow
690690

691691
windowSpec
692692
: name=identifier #windowRef
693+
| '('name=identifier')' #windowRef
693694
| '('
694695
( CLUSTER BY partition+=expression (',' partition+=expression)*
695696
| ((PARTITION | DISTRIBUTE) BY partition+=expression (',' partition+=expression)*)?

sql/core/src/test/resources/sql-tests/inputs/window.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,9 @@ last_value(false, false) OVER w AS last_value_contain_null
109109
FROM testData
110110
WINDOW w AS ()
111111
ORDER BY cate, val;
112+
113+
-- parentheses around window reference
114+
SELECT cate, sum(val) OVER (w)
115+
FROM testData
116+
WHERE val is not null
117+
WINDOW w AS (PARTITION BY cate ORDER BY val);

sql/core/src/test/resources/sql-tests/results/window.sql.out

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Automatically generated by SQLQueryTestSuite
2-
-- Number of queries: 22
2+
-- Number of queries: 23
33

44

55
-- !query 0
@@ -363,3 +363,20 @@ NULL a false true false false true false
363363
1 b false true false false true false
364364
2 b false true false false true false
365365
3 b false true false false true false
366+
367+
368+
-- !query 22
369+
SELECT cate, sum(val) OVER (w)
370+
FROM testData
371+
WHERE val is not null
372+
WINDOW w AS (PARTITION BY cate ORDER BY val)
373+
-- !query 22 schema
374+
struct<cate:string,sum(CAST(val AS BIGINT)) OVER (PARTITION BY cate ORDER BY val ASC NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW):bigint>
375+
-- !query 22 output
376+
NULL 3
377+
a 2
378+
a 2
379+
a 4
380+
b 1
381+
b 3
382+
b 6

0 commit comments

Comments
 (0)