Skip to content

Commit 0b2492d

Browse files
authored
Improve JISON grammar size (#2421)
- UnionClause: Extract UnionOp and UnionableSelect helpers, add ParenthesizedSelect support - FromTable: Extract FromTableAlias helper to consolidate alias patterns - JoinTableAs: Reuse FromTableAlias helper - OverClause: Use optional markers for partition/order clauses - Select: Add ParenthesizedSelect support for SQL-99 compliant UNION handling
1 parent 5af89fb commit 0b2492d

File tree

2 files changed

+919
-965
lines changed

2 files changed

+919
-965
lines changed

src/alasqlparser.jison

Lines changed: 114 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,12 @@ Select
536536
/* if(yy.queries) $$.queries = yy.queries;
537537
delete yy.queries;
538538
*/ }
539+
| ParenthesizedSelect UnionClause OrderClause LimitClause
540+
{
541+
yy.extend($$,$1); yy.extend($$,$2); yy.extend($$,$3); yy.extend($$,$4);
542+
$$ = $1;
543+
if(yy.exists) $$.exists = yy.exists.slice();
544+
}
539545
| SEARCH SearchSelector* IntoClause SearchFrom?
540546
/* SearchLimit? SearchStrategy? SearchTimeout? */
541547
{
@@ -554,6 +560,11 @@ SelectWithoutOrderOrLimit
554560
}
555561
;
556562

563+
ParenthesizedSelect
564+
: LPAR Select RPAR
565+
{ $$ = $2; }
566+
;
567+
557568
PivotClause
558569
: PIVOT LPAR Expression FOR Literal PivotClause2? RPAR AsLiteral?
559570
{ $$ = {pivot:{expr:$3, columnid:$5, inlist:$6, as:$8}}; }
@@ -916,54 +927,31 @@ FromTablesList
916927
;
917928

918929
FromTable
919-
: LPAR Select RPAR Literal
920-
{ $$ = $2; $$.as = $4 }
921-
| LPAR Select RPAR AS Literal
922-
{ $$ = $2; $$.as = $5 }
923-
| LPAR Select RPAR /* default alias */
924-
{ $$ = $2; $$.as = 'default' }
925-
930+
: LPAR Select RPAR FromTableAlias?
931+
{ $$ = $2; $$.as = $4 || 'default'; }
926932
| Json AS? Literal?
927933
{ $$ = new yy.Json({value:$1}); $1.as = $3 }
928-
929-
| Table Literal
930-
{ $$ = $1; $1.as = $2 }
931-
| Table AS Literal
932-
{ $$ = $1; $1.as = $3 }
933-
| Table
934-
{ $$ = $1; }
934+
| Table FromTableAlias?
935+
{ $$ = $1; if($2) $1.as = $2; }
935936
| Table NOT INDEXED
936937
{ $$ = $1; }
937-
| ParamValue Literal
938-
{ $$ = $1; $1.as = $2; }
939-
| ParamValue AS Literal
940-
{ $$ = $1; $1.as = $3; }
941-
| ParamValue
942-
{ $$ = $1; $1.as = 'default'; }
943-
944-
| FuncValue
945-
{ $$ = $1; $1.as = 'default'; }
946-
| FuncValue Literal
947-
{ $$ = $1; $1.as = $2; }
948-
| FuncValue AS Literal
949-
{ $$ = $1; $1.as = $3; }
950-
938+
| ParamValue FromTableAlias?
939+
{ $$ = $1; $1.as = $2 || 'default'; }
940+
| FuncValue FromTableAlias?
941+
{ $$ = $1; $1.as = $2 || 'default'; }
942+
| VarValue FromTableAlias?
943+
{ $$ = $1; $1.as = $2 || 'default'; }
944+
| FromString FromTableAlias?
945+
{ $$ = $1; $1.as = $2 || 'default'; }
951946
| INSERTED
952947
{ $$ = {inserted:true}; }
948+
;
953949

954-
| VarValue
955-
{ $$ = $1; $1.as = 'default'; }
956-
| VarValue Literal
957-
{ $$ = $1; $1.as = $2; }
958-
| VarValue AS Literal
959-
{ $$ = $1; $1.as = $3; }
960-
961-
| FromString
962-
{ $$ = $1; $1.as = 'default'; }
963-
| FromString Literal
964-
{ $$ = $1; $1.as = $2; }
965-
| FromString AS Literal
966-
{ $$ = $1; $1.as = $3; }
950+
FromTableAlias
951+
: Literal
952+
{ $$ = $1; }
953+
| AS Literal
954+
{ $$ = $2; }
967955
;
968956

969957
FromString
@@ -1024,35 +1012,18 @@ JoinTable
10241012
;
10251013

10261014
JoinTableAs
1027-
: Table
1028-
{ $$ = {table: $1}; }
1029-
| Table Literal
1030-
{ $$ = {table: $1, as: $2 } ; }
1031-
| Table AS Literal
1032-
{ $$ = {table: $1, as: $3 } ; }
1015+
: Table FromTableAlias?
1016+
{ $$ = {table: $1}; if($2) $$.as = $2; }
10331017
| Json AS? Literal?
10341018
{ $$ = {json:new yy.Json({value:$1,as:$3})}; }
1035-
| ParamValue Literal
1036-
{ $$ = {param: $1, as: $2 } ; }
1037-
| ParamValue AS Literal
1038-
{ $$ = {param: $1, as: $3 } ; }
1039-
| LPAR Select RPAR Literal
1040-
{ $$ = {select: $2, as: $4} ; }
1041-
| LPAR Select RPAR AS Literal
1042-
{ $$ = {select: $2, as: $5 } ; }
1043-
| FuncValue
1044-
{ $$ = {func:$1, as:'default'}; }
1045-
| FuncValue Literal
1046-
{ $$ = {func:$1, as: $2}; }
1047-
| FuncValue AS Literal
1048-
{ $$ = {func:$1, as: $3}; }
1049-
1050-
| VarValue
1051-
{ $$ = {variable:$1,as:'default'}; }
1052-
| VarValue Literal
1053-
{ $$ = {variable:$1,as:$2}; }
1054-
| VarValue AS Literal
1055-
{ $$ = {variable:$1,as:$3} }
1019+
| ParamValue FromTableAlias
1020+
{ $$ = {param: $1, as: $2}; }
1021+
| LPAR Select RPAR FromTableAlias
1022+
{ $$ = {select: $2, as: $4}; }
1023+
| FuncValue FromTableAlias?
1024+
{ $$ = {func:$1, as: $2 || 'default'}; }
1025+
| VarValue FromTableAlias?
1026+
{ $$ = {variable:$1, as: $2 || 'default'}; }
10561027
;
10571028

10581029
JoinMode
@@ -1140,23 +1111,39 @@ HavingClause
11401111
;
11411112

11421113
UnionClause
1143-
: { $$ = undefined; }
1144-
| UNION SelectWithoutOrderOrLimit
1145-
{ $$ = {union: $2} ; }
1146-
| UNION ALL SelectWithoutOrderOrLimit
1147-
{ $$ = {unionall: $3} ; }
1148-
| EXCEPT SelectWithoutOrderOrLimit
1149-
{ $$ = {except: $2} ; }
1150-
| INTERSECT SelectWithoutOrderOrLimit
1151-
{ $$ = {intersect: $2} ; }
1152-
| UNION CORRESPONDING SelectWithoutOrderOrLimit
1153-
{ $$ = {union: $3, corresponding:true} ; }
1154-
| UNION ALL CORRESPONDING SelectWithoutOrderOrLimit
1155-
{ $$ = {unionall: $4, corresponding:true} ; }
1156-
| EXCEPT CORRESPONDING SelectWithoutOrderOrLimit
1157-
{ $$ = {except: $3, corresponding:true} ; }
1158-
| INTERSECT CORRESPONDING SelectWithoutOrderOrLimit
1159-
{ $$ = {intersect: $3, corresponding:true} ; }
1114+
: { $$ = undefined; }
1115+
| UnionOp UnionableSelect
1116+
{
1117+
$$ = {};
1118+
$$[$1.op] = $2;
1119+
if($1.corresponding) $$.corresponding = true;
1120+
}
1121+
;
1122+
1123+
UnionOp
1124+
: UNION
1125+
{ $$ = {op: 'union'}; }
1126+
| UNION ALL
1127+
{ $$ = {op: 'unionall'}; }
1128+
| EXCEPT
1129+
{ $$ = {op: 'except'}; }
1130+
| INTERSECT
1131+
{ $$ = {op: 'intersect'}; }
1132+
| UNION CORRESPONDING
1133+
{ $$ = {op: 'union', corresponding: true}; }
1134+
| UNION ALL CORRESPONDING
1135+
{ $$ = {op: 'unionall', corresponding: true}; }
1136+
| EXCEPT CORRESPONDING
1137+
{ $$ = {op: 'except', corresponding: true}; }
1138+
| INTERSECT CORRESPONDING
1139+
{ $$ = {op: 'intersect', corresponding: true}; }
1140+
;
1141+
1142+
UnionableSelect
1143+
: SelectWithoutOrderOrLimit
1144+
{ $$ = $1; }
1145+
| ParenthesizedSelect
1146+
{ $$ = $1; }
11601147
;
11611148

11621149
OrderClause
@@ -1420,14 +1407,9 @@ AggrValue
14201407
;
14211408

14221409
OverClause
1423-
:
1424-
{$$ = undefined; }
1425-
| OVER LPAR OverPartitionClause RPAR
1426-
{ $$ = new yy.Over(); yy.extend($$,$3); }
1427-
| OVER LPAR OverOrderByClause RPAR
1428-
{ $$ = new yy.Over(); yy.extend($$,$3); }
1429-
| OVER LPAR OverPartitionClause OverOrderByClause RPAR
1430-
{ $$ = new yy.Over(); yy.extend($$,$3); yy.extend($$,$4);}
1410+
: { $$ = undefined; }
1411+
| OVER LPAR OverPartitionClause? OverOrderByClause? RPAR
1412+
{ $$ = new yy.Over(); yy.extend($$,$3); yy.extend($$,$4); }
14311413
;
14321414

14331415
OverPartitionClause
@@ -1889,45 +1871,45 @@ Delete
18891871
/* INSERT */
18901872

18911873
Insert
1892-
: INSERT Into TargetTable Values ValuesListsList OutputClause
1893-
{ $$ = new yy.Insert({into:$3, values: $5}); yy.extend($$,$6); }
1894-
| INSERT Into TargetTable ValuesListsList OutputClause
1895-
{ $$ = new yy.Insert({into:$3, values: $4}); yy.extend($$,$5); }
1896-
| INSERT IGNORE Into TargetTable Values ValuesListsList OutputClause
1897-
{ $$ = new yy.Insert({into:$4, values: $6, ignore:true}); yy.extend($$,$7); }
1898-
| INSERT IGNORE Into TargetTable ValuesListsList OutputClause
1899-
{ $$ = new yy.Insert({into:$4, values: $5, ignore:true}); yy.extend($$,$6); }
1900-
| INSERT IGNORE Into TargetTable LPAR ColumnsList RPAR Values ValuesListsList OutputClause
1901-
{ $$ = new yy.Insert({into:$4, columns: $6, values: $9, ignore:true}); yy.extend($$,$10); }
1902-
| INSERT IGNORE Into TargetTable LPAR ColumnsList RPAR ValuesListsList OutputClause
1903-
{ $$ = new yy.Insert({into:$4, columns: $6, values: $8, ignore:true}); yy.extend($$,$9); }
1904-
| INSERT IGNORE Into TargetTable Select OutputClause
1905-
{ $$ = new yy.Insert({into:$4, select: $5, ignore:true}); yy.extend($$,$6); }
1906-
| INSERT IGNORE Into TargetTable LPAR ColumnsList RPAR Select OutputClause
1907-
{ $$ = new yy.Insert({into:$4, columns: $6, select: $8, ignore:true}); yy.extend($$,$9); }
1908-
| INSERT OR REPLACE Into TargetTable Values ValuesListsList OutputClause
1909-
{ $$ = new yy.Insert({into:$5, values: $7, orreplace:true}); yy.extend($$,$8); }
1910-
| INSERT OR REPLACE Into TargetTable ValuesListsList OutputClause
1911-
{ $$ = new yy.Insert({into:$5, values: $6, orreplace:true}); yy.extend($$,$7); }
1912-
| REPLACE Into TargetTable Values ValuesListsList OutputClause
1913-
{ $$ = new yy.Insert({into:$3, values: $5, orreplace:true}); yy.extend($$,$6); }
1914-
| REPLACE Into TargetTable ValuesListsList OutputClause
1915-
{ $$ = new yy.Insert({into:$3, values: $4, orreplace:true}); yy.extend($$,$5); }
1916-
| INSERT Into TargetTable DEFAULT Values OutputClause
1917-
{ $$ = new yy.Insert({into:$3, "default": true}); yy.extend($$,$6); }
1918-
| INSERT Into TargetTable LPAR ColumnsList RPAR Values ValuesListsList OutputClause
1919-
{ $$ = new yy.Insert({into:$3, columns: $5, values: $8}); yy.extend($$,$9); }
1920-
| INSERT Into TargetTable LPAR ColumnsList RPAR ValuesListsList OutputClause
1921-
{ $$ = new yy.Insert({into:$3, columns: $5, values: $7}); yy.extend($$,$9); }
1922-
| INSERT Into TargetTable Select OutputClause
1923-
{ $$ = new yy.Insert({into:$3, select: $4}); yy.extend($$,$5); }
1924-
| INSERT OR REPLACE Into TargetTable Select OutputClause
1925-
{ $$ = new yy.Insert({into:$5, select: $6, orreplace:true}); yy.extend($$,$7); }
1926-
| INSERT Into TargetTable LPAR ColumnsList RPAR Select OutputClause
1927-
{ $$ = new yy.Insert({into:$3, columns: $5, select: $7}); yy.extend($$,$9); }
1928-
| INSERT Into TargetTable SET SetColumnsList OutputClause
1929-
{ $$ = new yy.Insert({into:$3, setcolumns: $5}); yy.extend($$,$6); }
1930-
;
1874+
: INSERT Into TargetTable Values ValuesListsList OutputClause
1875+
{ $$ = new yy.Insert({into:$3, values: $5}); yy.extend($$,$6); }
1876+
| INSERT Into TargetTable ValuesListsList OutputClause
1877+
{ $$ = new yy.Insert({into:$3, values: $4}); yy.extend($$,$5); }
1878+
| INSERT IGNORE Into TargetTable Values ValuesListsList OutputClause
1879+
{ $$ = new yy.Insert({into:$4, values: $6, ignore:true}); yy.extend($$,$7); }
1880+
| INSERT IGNORE Into TargetTable ValuesListsList OutputClause
1881+
{ $$ = new yy.Insert({into:$4, values: $5, ignore:true}); yy.extend($$,$6); }
1882+
| INSERT IGNORE Into TargetTable LPAR ColumnsList RPAR Values ValuesListsList OutputClause
1883+
{ $$ = new yy.Insert({into:$4, columns: $6, values: $9, ignore:true}); yy.extend($$,$10); }
1884+
| INSERT IGNORE Into TargetTable LPAR ColumnsList RPAR ValuesListsList OutputClause
1885+
{ $$ = new yy.Insert({into:$4, columns: $6, values: $8, ignore:true}); yy.extend($$,$9); }
1886+
| INSERT IGNORE Into TargetTable Select OutputClause
1887+
{ $$ = new yy.Insert({into:$4, select: $5, ignore:true}); yy.extend($$,$6); }
1888+
| INSERT IGNORE Into TargetTable LPAR ColumnsList RPAR Select OutputClause
1889+
{ $$ = new yy.Insert({into:$4, columns: $6, select: $8, ignore:true}); yy.extend($$,$9); }
1890+
| INSERT OR REPLACE Into TargetTable Values ValuesListsList OutputClause
1891+
{ $$ = new yy.Insert({into:$5, values: $7, orreplace:true}); yy.extend($$,$8); }
1892+
| INSERT OR REPLACE Into TargetTable ValuesListsList OutputClause
1893+
{ $$ = new yy.Insert({into:$5, values: $6, orreplace:true}); yy.extend($$,$7); }
1894+
| REPLACE Into TargetTable Values ValuesListsList OutputClause
1895+
{ $$ = new yy.Insert({into:$3, values: $5, orreplace:true}); yy.extend($$,$6); }
1896+
| REPLACE Into TargetTable ValuesListsList OutputClause
1897+
{ $$ = new yy.Insert({into:$3, values: $4, orreplace:true}); yy.extend($$,$5); }
1898+
| INSERT Into TargetTable DEFAULT Values OutputClause
1899+
{ $$ = new yy.Insert({into:$3, "default": true}); yy.extend($$,$6); }
1900+
| INSERT Into TargetTable LPAR ColumnsList RPAR Values ValuesListsList OutputClause
1901+
{ $$ = new yy.Insert({into:$3, columns: $5, values: $8}); yy.extend($$,$9); }
1902+
| INSERT Into TargetTable LPAR ColumnsList RPAR ValuesListsList OutputClause
1903+
{ $$ = new yy.Insert({into:$3, columns: $5, values: $7}); yy.extend($$,$8); }
1904+
| INSERT Into TargetTable Select OutputClause
1905+
{ $$ = new yy.Insert({into:$3, select: $4}); yy.extend($$,$5); }
1906+
| INSERT OR REPLACE Into TargetTable Select OutputClause
1907+
{ $$ = new yy.Insert({into:$5, select: $6, orreplace:true}); yy.extend($$,$7); }
1908+
| INSERT Into TargetTable LPAR ColumnsList RPAR Select OutputClause
1909+
{ $$ = new yy.Insert({into:$3, columns: $5, select: $7}); yy.extend($$,$8); }
1910+
| INSERT Into TargetTable SET SetColumnsList OutputClause
1911+
{ $$ = new yy.Insert({into:$3, setcolumns: $5}); yy.extend($$,$6); }
1912+
;
19311913

19321914
Values
19331915
: VALUES

0 commit comments

Comments
 (0)