Skip to content

Commit

Permalink
Update SQL grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamugare committed Sep 28, 2024
1 parent 557506f commit 765b201
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion syncode/parsers/grammars/sql_grammar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ bool_parentheses: comparison_type
| "(" bool_expression "OR"i comparison_type ")" -> bool_or
| "EXISTS"i subquery -> exists
comparison_type: equals | not_equals | greater_than | less_than | greater_than_or_equal
| less_than_or_equal | between | in_expr | not_in_expr | subquery_in | is_null | is_not_null
| less_than_or_equal | between | in_expr | not_in_expr | subquery_in | subquery_not_in | is_null | is_not_null | like_expr | not_like_expr

equals: expression_math "=" expression_math
is_null: expression_math "IS"i "NULL"i
is_not_null: expression_math "IS"i "NOT"i "NULL"i
Expand All @@ -165,6 +166,10 @@ greater_than_or_equal: expression_math ">=" expression_math
less_than_or_equal: expression_math "<=" expression_math
between: expression_math "BETWEEN"i expression_math "AND"i expression_math

// `LIKE` and `NOT LIKE`
like_expr: expression_math "LIKE"i expression_math
not_like_expr: expression_math "NOT"i "LIKE"i expression_math

// `IN` and `NOT IN`
in_expr: expression_math "IN"i "(" [expression_math ","]* expression_math ")"
subquery_in: expression_math "IN"i subquery
Expand Down
20 changes: 20 additions & 0 deletions tests/test_grammar_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,23 @@ def test_sql_parser5(self):
print(r)
assert r.remainder == ';'
assert r.remainder_state == RemainderState.MAYBE_COMPLETE

def test_sql_parser6(self):
"""
Tests support for NOT IN
"""
inc_parser.reset()
partial_code = "SELECT AVG(age) FROM student WHERE stuid NOT IN (SELECT DISTINCT"
r = inc_parser.get_acceptable_next_terminals(partial_code)
assert r.remainder == 'DISTINCT'
assert r.remainder_state == RemainderState.MAYBE_COMPLETE

def test_sql_parser7(self):
"""
Tests support for LIKE
"""
inc_parser.reset()
partial_code = "SELECT singer.name , singer.country FROM singer WHERE singer.song_name LIKE '%Hey%'"
r = inc_parser.get_acceptable_next_terminals(partial_code)
assert r.remainder == "'%Hey%'"
assert r.remainder_state == RemainderState.MAYBE_COMPLETE

0 comments on commit 765b201

Please sign in to comment.