Skip to content

Commit

Permalink
Merge pull request #1977 from taozhi8833998/feat-top-union-snowflake
Browse files Browse the repository at this point in the history
feat: support top clause and fix multiple union all in snowflake
  • Loading branch information
taozhi8833998 authored Jun 26, 2024
2 parents f20bf65 + 144acc1 commit 44bed0a
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pegjs/athena.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
stmt.parentheses = true;
return {
expr: stmt,
Expand Down
2 changes: 1 addition & 1 deletion pegjs/bigquery.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,7 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
stmt.parentheses = true;
return {
expr: stmt,
Expand Down
2 changes: 1 addition & 1 deletion pegjs/db2.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
stmt.parentheses = true;
return {
expr: stmt,
Expand Down
4 changes: 2 additions & 2 deletions pegjs/flinksql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1871,11 +1871,11 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/* => {
expr: union_stmt & { parentheses: true; };
as?: alias_clause;
join: join_op;
join: join_op | set_op;
on?: on_clause;
}*/
stmt.parentheses = true;
Expand Down
2 changes: 1 addition & 1 deletion pegjs/hive.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
stmt.parentheses = true;
return {
expr: stmt,
Expand Down
2 changes: 1 addition & 1 deletion pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,7 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:set_op_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:set_op_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
stmt.parentheses = true;
return {
expr: stmt,
Expand Down
2 changes: 1 addition & 1 deletion pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2366,7 +2366,7 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:set_op_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:set_op_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
stmt.parentheses = true;
return {
expr: stmt,
Expand Down
2 changes: 1 addition & 1 deletion pegjs/noql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3010,7 +3010,7 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:(union_stmt / table_ref_list) __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:(union_stmt / table_ref_list) __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/* => {
expr: (union_stmt || table_ref_list) & { parentheses: true; };
as?: alias_clause;
Expand Down
4 changes: 2 additions & 2 deletions pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3211,11 +3211,11 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:(union_stmt / table_ref_list) __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:(union_stmt / table_ref_list) __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/* => {
expr: (union_stmt | table_ref_list) & { parentheses: true; };
as?: alias_clause;
join: join_op;
join: join_op | set_op;
on?: on_clause;
}*/
if (Array.isArray(stmt)) stmt = { type: 'tables', expr: stmt }
Expand Down
4 changes: 2 additions & 2 deletions pegjs/redshift.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3044,11 +3044,11 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:(union_stmt / table_ref_list) __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:(union_stmt / table_ref_list) __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/* => {
expr: (union_stmt || table_ref_list) & { parentheses: true; };
as?: alias_clause;
join: join_op;
join: join_op | set_op;
on?: on_clause;
}*/
if (Array.isArray(stmt)) stmt = { type: 'tables', expr: stmt }
Expand Down
23 changes: 21 additions & 2 deletions pegjs/snowflake.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,7 @@ select_stmt_nake
= __ cte:with_clause? __ KW_SELECT ___
opts:option_clause? __
d:distinct_on? __
top:top_clause? __
c:column_clause __
ci:into_clause? __
f:from_clause? __
Expand All @@ -2085,6 +2086,7 @@ select_stmt_nake
groupby?: group_by_clause;
having?: having_clause;
orderby?: order_by_clause;
top?: top_clause;
limit?: limit_clause;
window?: window_clause;
}*/
Expand All @@ -2108,12 +2110,27 @@ select_stmt_nake
having: h,
qualify: q,
orderby: o,
top,
limit: l,
window: win,
...getLocationObject()
};
}

top_clause
= KW_TOP __ LPAREN __ n:number __ RPAREN __ p:('PERCENT'i)? {
return {
value: n,
percent: p && p.toLowerCase(),
parentheses: true,
}
}
/ KW_TOP __ n:number __ p:('PERCENT'i)? {
return {
value: n,
percent: p && p.toLowerCase()
}
}
// MySQL extensions to standard SQL
option_clause
= head:query_option tail:(__ query_option)* {
Expand Down Expand Up @@ -2389,11 +2406,11 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:(union_stmt / table_ref_list) __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:(union_stmt / table_ref_list) __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/* => {
expr: (union_stmt || table_ref_list) & { parentheses: true; };
as?: alias_clause;
join: join_op;
join: join_op | set_op;
on?: on_clause;
}*/
if (Array.isArray(stmt)) stmt = { type: 'tables', expr: stmt }
Expand All @@ -2407,6 +2424,7 @@ table_join
};
}


//NOTE that, the table assigned to `var` shouldn't write in `table_join`
table_base
= KW_DUAL {
Expand Down Expand Up @@ -4245,6 +4263,7 @@ KW_DEFAULT = "DEFAULT"i !ident_start
KW_NOT_NULL = "NOT NULL"i !ident_start
KW_TRUE = "TRUE"i !ident_start
KW_TO = "TO"i !ident_start
KW_TOP = "TOP"i !ident_start
KW_FALSE = "FALSE"i !ident_start

KW_SHOW = "SHOW"i !ident_start
Expand Down
2 changes: 1 addition & 1 deletion pegjs/sqlite.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1514,7 +1514,7 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
stmt.parentheses = true;
return {
expr: stmt,
Expand Down
4 changes: 2 additions & 2 deletions pegjs/transactsql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@ select_stmt_nake
= __ cte:with_clause? __ KW_SELECT ___
opts:option_clause? __
d:KW_DISTINCT? __
top: top_clause? __
top:top_clause? __
c:column_clause __
f:from_clause? __
w:where_clause? __
Expand Down Expand Up @@ -1727,7 +1727,7 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:union_stmt __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
stmt.parentheses = true;
return {
expr: stmt,
Expand Down
4 changes: 2 additions & 2 deletions pegjs/trino.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2404,11 +2404,11 @@ table_join
t.on = expr;
return t;
}
/ op:join_op __ LPAREN __ stmt:(union_stmt / table_ref_list) __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/ op:(join_op / set_op) __ LPAREN __ stmt:(union_stmt / table_ref_list) __ RPAREN __ alias:alias_clause? __ expr:on_clause? {
/* => {
expr: (union_stmt || table_ref_list) & { parentheses: true; };
as?: alias_clause;
join: join_op;
join: join_op | set_op;
on?: on_clause;
}*/
if (Array.isArray(stmt)) stmt = { type: 'tables', expr: stmt }
Expand Down
14 changes: 14 additions & 0 deletions test/snowflake.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ describe('snowflake', () => {
'SELECT "A", "B", COUNT(*) FROM "T" GROUP BY ALL'
]
},
{
title: 'top clause in select',
sql: [
'select top 10 * from cleansed_hosts',
'SELECT TOP 10 * FROM "cleansed_hosts"'
]
},
{
title: 'multiple union all',
sql: [
'select * from ( ( ( select * from some_table ) union all ( select * from some_table ) ) union all ( select * from some_table ) )',
'SELECT * FROM (((SELECT * FROM "some_table") UNION ALL (SELECT * FROM "some_table")) UNION ALL (SELECT * FROM "some_table"))'
]
},
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand Down

0 comments on commit 44bed0a

Please sign in to comment.