Skip to content

Commit 2c170d8

Browse files
Copilotmathiasrw
andauthored
Fix UNION ALL across NOT IN clause to close #1146 (#2404)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com> Co-authored-by: M. Wulff <m@rawu.dk>
1 parent ec7831f commit 2c170d8

File tree

3 files changed

+341
-19
lines changed

3 files changed

+341
-19
lines changed

src/alasqlparser.jison

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,28 +1128,87 @@ UnionClause
11281128

11291129
UnionOp
11301130
: UNION
1131-
{ $$ = {op: 'union'}; }
1131+
{
1132+
// Save and reset queries for nested SELECT using stack pattern
1133+
if(!yy.queriesStack) yy.queriesStack = [];
1134+
yy.queriesStack.push(yy.queries || []);
1135+
yy.queries = [];
1136+
$$ = {op: 'union'};
1137+
}
11321138
| UNION ALL
1133-
{ $$ = {op: 'unionall'}; }
1139+
{
1140+
if(!yy.queriesStack) yy.queriesStack = [];
1141+
yy.queriesStack.push(yy.queries || []);
1142+
yy.queries = [];
1143+
$$ = {op: 'unionall'};
1144+
}
11341145
| EXCEPT
1135-
{ $$ = {op: 'except'}; }
1146+
{
1147+
if(!yy.queriesStack) yy.queriesStack = [];
1148+
yy.queriesStack.push(yy.queries || []);
1149+
yy.queries = [];
1150+
$$ = {op: 'except'};
1151+
}
11361152
| INTERSECT
1137-
{ $$ = {op: 'intersect'}; }
1153+
{
1154+
if(!yy.queriesStack) yy.queriesStack = [];
1155+
yy.queriesStack.push(yy.queries || []);
1156+
yy.queries = [];
1157+
$$ = {op: 'intersect'};
1158+
}
11381159
| UNION CORRESPONDING
1139-
{ $$ = {op: 'union', corresponding: true}; }
1160+
{
1161+
if(!yy.queriesStack) yy.queriesStack = [];
1162+
yy.queriesStack.push(yy.queries || []);
1163+
yy.queries = [];
1164+
$$ = {op: 'union', corresponding: true};
1165+
}
11401166
| UNION ALL CORRESPONDING
1141-
{ $$ = {op: 'unionall', corresponding: true}; }
1167+
{
1168+
if(!yy.queriesStack) yy.queriesStack = [];
1169+
yy.queriesStack.push(yy.queries || []);
1170+
yy.queries = [];
1171+
$$ = {op: 'unionall', corresponding: true};
1172+
}
11421173
| EXCEPT CORRESPONDING
1143-
{ $$ = {op: 'except', corresponding: true}; }
1174+
{
1175+
if(!yy.queriesStack) yy.queriesStack = [];
1176+
yy.queriesStack.push(yy.queries || []);
1177+
yy.queries = [];
1178+
$$ = {op: 'except', corresponding: true};
1179+
}
11441180
| INTERSECT CORRESPONDING
1145-
{ $$ = {op: 'intersect', corresponding: true}; }
1181+
{
1182+
if(!yy.queriesStack) yy.queriesStack = [];
1183+
yy.queriesStack.push(yy.queries || []);
1184+
yy.queries = [];
1185+
$$ = {op: 'intersect', corresponding: true};
1186+
}
11461187
;
11471188

11481189
UnionableSelect
11491190
: SelectWithoutOrderOrLimit
1150-
{ $$ = $1; }
1191+
{
1192+
// Restore parent queries from stack and assign current queries to nested SELECT
1193+
if(yy.queriesStack && yy.queriesStack.length > 0) {
1194+
if(yy.queries && yy.queries.length > 0) {
1195+
$1.queries = yy.queries;
1196+
}
1197+
yy.queries = yy.queriesStack.pop();
1198+
}
1199+
$$ = $1;
1200+
}
11511201
| ParenthesizedSelect
1152-
{ $$ = $1; }
1202+
{
1203+
// Restore parent queries from stack and assign current queries to nested SELECT
1204+
if(yy.queriesStack && yy.queriesStack.length > 0) {
1205+
if(yy.queries && yy.queries.length > 0) {
1206+
$1.queries = yy.queries;
1207+
}
1208+
yy.queries = yy.queriesStack.pop();
1209+
}
1210+
$$ = $1;
1211+
}
11531212
;
11541213

11551214
OrderClause

src/alasqlparser.js

Lines changed: 62 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)