Skip to content

Commit 6c6cc20

Browse files
committed
Fix for sub queries that use UNION
1 parent 98e3303 commit 6c6cc20

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/sqlParser.jison

+6-1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ main
197197
| unionClause semicolonOpt EOF { return {nodeType: 'Main', value: $1, hasSemicolon: $2}; }
198198
;
199199

200+
selectUnionClause
201+
: selectClause { $$ = {type: 'SelectUnion', value: $1}; }
202+
| unionClause { $$ = {type: 'SelectUnion', value: $1}; }
203+
;
204+
200205
semicolonOpt
201206
: ';' { $$ = true }
202207
| { $$ = false }
@@ -673,6 +678,6 @@ index_hint
673678
;
674679
table_factor
675680
: identifier partitionOpt aliasOpt index_hint_list_opt { $$ = { type: 'TableFactor', value: $1, partition: $2, alias: $3.alias, hasAs: $3.hasAs, indexHintOpt: $4 } }
676-
| '(' selectClause ')' aliasOpt { $$ = { type: 'TableFactor', value: { type: 'SubQuery', value: $2 }, alias: $4.alias, hasAs: $4.hasAs} }
681+
| '(' selectUnionClause ')' aliasOpt { $$ = { type: 'TableFactor', value: { type: 'SubQuery', value: $2 }, alias: $4.alias, hasAs: $4.hasAs} }
677682
| '(' table_references ')' { $$ = $2; $$.hasParentheses = true }
678683
;

src/stringify.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ if (!sqlParser) {
1414

1515
Sql.prototype.travel = function(ast) {
1616
if (!ast) return;
17-
17+
1818
if (typeof ast === 'string') {
1919
return this.append(ast);
2020
}
21-
21+
2222
var processor = this['travel' + ast.type];
2323
try {
2424
processor.call(this, ast);
2525
} catch (error) {
26-
throw new Error(error)
26+
throw new Error(`Error when trying to process travel${ast.type}:`, error)
2727
}
2828
};
2929

@@ -292,6 +292,9 @@ if (!sqlParser) {
292292
this.travel(ast.value);
293293
this.append(')', true);
294294
};
295+
Sql.prototype.travelSelectUnion = function(ast) {
296+
this.travel(ast.value);
297+
};
295298
Sql.prototype.travelIdentifierExpr = function(ast) {
296299
this.append('{');
297300
this.travel(ast.identifier);

test/main.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -419,4 +419,22 @@ describe('select grammar support', function() {
419419
it('bugfix table alias2', function() {
420420
testParser('select a.* from a t1 join b t2 on t1.a = t2.a')
421421
})
422+
423+
it('UNION ALL sub query', function() {
424+
testParser(`
425+
SELECT
426+
col
427+
FROM(
428+
SELECT
429+
col
430+
FROM tableA
431+
432+
UNION ALL
433+
434+
SELECT
435+
col
436+
FROM tableB
437+
)
438+
`)
439+
})
422440
});

0 commit comments

Comments
 (0)