Skip to content

Add additional set operations #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/sqlParser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ MODE return 'MODE'
OJ return 'OJ'
LIMIT return 'LIMIT'
UNION return 'UNION'
INTERSECT return 'INTERSECT'
EXCEPT return 'EXCEPT'

"," return ','
"=" return '='
Expand Down Expand Up @@ -161,31 +163,36 @@ UNION return 'UNION'

main
: selectClause semicolonOpt EOF { return {nodeType: 'Main', value: $1, hasSemicolon: $2}; }
| unionClause semicolonOpt EOF { return {nodeType: 'Main', value: $1, hasSemicolon: $2}; }
| setClause semicolonOpt EOF { return {nodeType: 'Main', value: $1, hasSemicolon: $2}; }
;

semicolonOpt
: ';' { $$ = true }
| { $$ = false }
;

unionClause
: unionClauseNotParenthesized { $$ = $1 }
| unionClauseParenthesized order_by_opt limit_opt { $$ = $1, $$.orderBy = $2, $$.limit = $3; }
setOperation
: UNION
| INTERSECT
| EXCEPT;

setClause
: setClauseNotParenthesized { $$ = $1 }
| setClauseParenthesized order_by_opt limit_opt { $$ = $1, $$.orderBy = $2, $$.limit = $3; }
;

unionClauseParenthesized
: selectClauseParenthesized UNION distinctOpt selectClauseParenthesized { $$ = { type: 'Union', left: $1, distinctOpt: $3, right: $4 }; }
| selectClauseParenthesized UNION distinctOpt unionClauseParenthesized { $$ = { type: 'Union', left: $1, distinctOpt: $3, right: $4 }; }
setClauseParenthesized
: selectClauseParenthesized setOperation distinctOpt selectClauseParenthesized { $$ = { type: $2.charAt(0).toUpperCase() + $2.substr(1).toLowerCase(), left: $1, distinctOpt: $3, right: $4 }; }
| selectClauseParenthesized setOperation distinctOpt setClauseParenthesized { $$ = { type: $2.charAt(0).toUpperCase() + $2.substr(1).toLowerCase(), left: $1, distinctOpt: $3, right: $4 }; }
;

selectClauseParenthesized
: '(' selectClause ')' { $$ = { type: 'SelectParenthesized', value: $2 }; }
;

unionClauseNotParenthesized
: selectClause UNION distinctOpt selectClause { $$ = { type: 'Union', left: $1, distinctOpt: $3, right: $4 } }
| selectClause UNION distinctOpt unionClauseNotParenthesized { $$ = { type: 'Union', left: $1, distinctOpt: $3, right: $4 } }
setClauseNotParenthesized
: selectClause setOperation distinctOpt selectClause { $$ = { type: $2.charAt(0).toUpperCase() + $2.substr(1).toLowerCase(), left: $1, distinctOpt: $3, right: $4 } }
| selectClause setOperation distinctOpt setClauseNotParenthesized { $$ = { type: $2.charAt(0).toUpperCase() + $2.substr(1).toLowerCase(), left: $1, distinctOpt: $3, right: $4 } }
;

selectClause
Expand Down
16 changes: 16 additions & 0 deletions src/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,22 @@ Sql.prototype.travelUnion = function (ast) {
}
this.travel(ast.right);
}
Sql.prototype.travelIntersect = function (ast) {
this.travel(ast.left);
this.appendKeyword('INTERSECT');
if (ast.distinctOpt) {
this.appendKeyword(ast.distinctOpt)
}
this.travel(ast.right);
}
Sql.prototype.travelExcept = function (ast) {
this.travel(ast.left);
this.appendKeyword('EXCEPT');
if (ast.distinctOpt) {
this.appendKeyword(ast.distinctOpt)
}
this.travel(ast.right);
}
Sql.prototype.travelSelectParenthesized = function (ast) {
this.appendKeyword('(');
this.travel(ast.value);
Expand Down
32 changes: 32 additions & 0 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,5 +377,37 @@ describe('select grammar support', function () {
testParser('select a from dual order by a desc limit 1, 1 union distinct select a from foo order by a limit 1');
});

it ('intersect support', function () {
testParser('select a from dual intersect select a from foo;');
});

it ('intersect Parenthesized support', function () {
testParser('(select a from dual) intersect (select a from foo) order by a desc limit 100, 100;');
});

it ('intersect all support', function () {
testParser('(select a from dual) intersect all (select a from foo) order by a limit 100');
});

it ('intersect distinct support', function () {
testParser('select a from dual order by a desc limit 1, 1 intersect distinct select a from foo order by a limit 1');
});

it ('except support', function () {
testParser('select a from dual except select a from foo;');
});

it ('except Parenthesized support', function () {
testParser('(select a from dual) except (select a from foo) order by a desc limit 100, 100;');
});

it ('except all support', function () {
testParser('(select a from dual) except all (select a from foo) order by a limit 100');
});

it ('except distinct support', function () {
testParser('select a from dual order by a desc limit 1, 1 except distinct select a from foo order by a limit 1');
});

});