Skip to content
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

Add support for parenthesized expressions & EXISTS subquery #126

Merged
merged 3 commits into from
Mar 29, 2023
Merged
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
25 changes: 24 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ module.exports = grammar({
keyword_unlogged: _ => make_keyword("unlogged"),
keyword_union: _ => make_keyword("union"),
keyword_all: _ => make_keyword("all"),
keyword_any: _ => make_keyword("any"),
keyword_some: _ => make_keyword("some"),
keyword_except: _ => make_keyword("except"),
keyword_intersect: _ => make_keyword("intersect"),
keyword_returning: _ => make_keyword("returning"),
Expand Down Expand Up @@ -1482,6 +1484,11 @@ module.exports = grammar({
paren_list(field('parameter', $._expression)),
),

exists: $ => seq(
matthias-Q marked this conversation as resolved.
Show resolved Hide resolved
$.keyword_exists,
$.subquery,
),

partition_by: $ => seq(
$.keyword_partition,
$.keyword_by,
Expand Down Expand Up @@ -1791,12 +1798,14 @@ module.exports = grammar({
$.cast,
alias($.implicit_cast, $.cast),
$._aggregate_function,
$.exists,
$.invocation,
$.binary_expression,
$.unary_expression,
$.array,
$.interval,
$.between_expression,
seq("(", $._expression, ")"),
)
),

Expand All @@ -1818,7 +1827,6 @@ module.exports = grammar({
['<>', 'binary_relation'],
[$.keyword_is, 'binary_is'],
[$.is_not, 'binary_is'],
[$.keyword_in, 'binary_in'],
[$.keyword_like, 'pattern_matching'],
[$.not_like, 'pattern_matching'],
[$.similar_to, 'pattern_matching'],
Expand All @@ -1844,12 +1852,26 @@ module.exports = grammar({
field('right', $._expression)
))
),
...[
[$.keyword_in, 'binary_in'],
// @TODO: there's an unclear precedence issue here
// [$.not_in, 'binary_in'],
].map(([operator, precedence]) =>
prec.left(precedence, seq(
field('left', $._expression),
field('operator', operator),
field('right', choice($.list, $.subquery))
))
),
),

unary_expression: $ => choice(
...[
[$.keyword_not, 'unary_not'],
[$.bang, 'unary_not'],
[$.keyword_any, 'unary_not'],
[$.keyword_some, 'unary_not'],
[$.keyword_all, 'unary_not'],
].map(([operator, precedence]) =>
prec.left(precedence, seq(
field('operator', operator),
Expand Down Expand Up @@ -1877,6 +1899,7 @@ module.exports = grammar({
'(',
$.select,
optional($.from),
optional(";"),
')',
),

Expand Down
149 changes: 149 additions & 0 deletions test/corpus/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,152 @@ GROUP BY some_field;
(keyword_by)
(field
(identifier))))))

================================================================================
Unary ANY, ALL, SOME
================================================================================

SELECT *
FROM foo
WHERE id = ANY (
SELECT 1
)

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(all_fields)))
(from
(keyword_from)
(relation
(table_reference
name: (identifier)))
(where
(keyword_where)
predicate: (binary_expression
left: (field
name: (identifier))
right: (unary_expression
operator: (keyword_any)
operand: (subquery
(select
(keyword_select)
(select_expression
(term
value: (literal)))))))))))

================================================================================
In with subquery
================================================================================

SELECT *
FROM foo
WHERE id IN (
SELECT 1
FROM bar;
)

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(all_fields)))
(from
(keyword_from)
(relation
(table_reference
name: (identifier)))
(where
(keyword_where)
predicate: (binary_expression
left: (field
name: (identifier))
operator: (keyword_in)
right: (subquery
(select
(keyword_select)
(select_expression
(term
value: (literal))))
(from
(keyword_from)
(relation
(table_reference
name: (identifier))))))))))

================================================================================
exists & not exists
================================================================================

SELECT *
FROM foo
WHERE (
NOT EXISTS (
SELECT 1
FROM bar
WHERE 0;
) OR EXISTS (
SELECT 1
FROM baz
WHERE 1;
)
)

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(all_fields)))
(from
(keyword_from)
(relation
(table_reference
name: (identifier)))
(where
(keyword_where)
predicate: (binary_expression
left: (unary_expression
operator: (keyword_not)
operand: (exists
(keyword_exists)
(subquery
(select
(keyword_select)
(select_expression
(term
value: (literal))))
(from
(keyword_from)
(relation
(table_reference
name: (identifier)))
(where
(keyword_where)
predicate: (literal))))))
operator: (keyword_or)
right: (exists
(keyword_exists)
(subquery
(select
(keyword_select)
(select_expression
(term
value: (literal))))
(from
(keyword_from)
(relation
(table_reference
name: (identifier)))
(where
(keyword_where)
predicate: (literal))))))))))
45 changes: 39 additions & 6 deletions test/corpus/select.txt
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,8 @@ FROM my_table;
(keyword_distinct)
(select_expression
(term
value: (list
(field
name: (identifier))))))
value: (field
name: (identifier)))))
(from
(keyword_from)
(relation
Expand Down Expand Up @@ -722,9 +721,8 @@ FROM my_table;
value: (count
name: (keyword_count)
(keyword_distinct)
parameter: (list
(field
name: (identifier)))))))
parameter: (field
name: (identifier))))))
(from
(keyword_from)
(relation
Expand Down Expand Up @@ -2402,3 +2400,38 @@ where a not between 1 and 3
(literal)
(keyword_and)
(literal))))))

================================================================================
parameterized expressions
================================================================================

SELECT 1
FROM foo
WHERE (foo OR bar) AND baz

--------------------------------------------------------------------------------

(program
(statement
(select
(keyword_select)
(select_expression
(term
value: (literal))))
(from
(keyword_from)
(relation
(table_reference
name: (identifier)))
(where
(keyword_where)
predicate: (binary_expression
left: (binary_expression
left: (field
name: (identifier))
operator: (keyword_or)
right: (field
name: (identifier)))
operator: (keyword_and)
right: (field
name: (identifier)))))))