Skip to content

Commit

Permalink
json syntax support
Browse files Browse the repository at this point in the history
  • Loading branch information
ea-rus committed Feb 22, 2024
1 parent b69c09d commit a66d9de
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
6 changes: 3 additions & 3 deletions mindsdb_sql/parser/ast/select/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def get_string(self, *args, **kwargs):
arg_strs = []
for arg in self.args:
arg_str = arg.to_string()
if isinstance(arg, BinaryOperation) or isinstance(arg, BetweenOperation):
# to parens
arg_str = f'({arg_str})'
# if isinstance(arg, BinaryOperation) or isinstance(arg, BetweenOperation):
# # to parens
# arg_str = f'({arg_str})'
arg_strs.append(arg_str)

return f'{arg_strs[0]} {self.op.upper()} {arg_strs[1]}'
Expand Down
4 changes: 3 additions & 1 deletion mindsdb_sql/parser/dialects/mindsdb/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class MindsDBLexer(Lexer):
EQUALS, NEQUALS, GREATER, GEQ, LESS, LEQ,
AND, OR, NOT, IS, IS_NOT,
IN, LIKE, NOT_LIKE, CONCAT, BETWEEN, WINDOW, OVER, PARTITION_BY,
JSON_GET, JSON_GET_STR,

# Data types
CAST, ID, INTEGER, FLOAT, QUOTE_STRING, DQUOTE_STRING, NULL, TRUE, FALSE,
Expand Down Expand Up @@ -263,6 +264,8 @@ class MindsDBLexer(Lexer):
SEMICOLON = r'\;'

# Operators
JSON_GET = r'->'
JSON_GET_STR = r'->>'
PLUS = r'\+'
MINUS = r'-'
DIVIDE = r'/'
Expand All @@ -288,7 +291,6 @@ class MindsDBLexer(Lexer):
OVER = r'\bOVER\b'
PARTITION_BY = r'\bPARTITION BY\b'


# Data types
NULL = r'\bNULL\b'
TRUE = r'\bTRUE\b'
Expand Down
14 changes: 7 additions & 7 deletions mindsdb_sql/parser/dialects/mindsdb/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ class MindsDBParser(Parser):
('left', AND),
('right', UNOT),
('left', EQUALS, NEQUALS),
('nonassoc', LESS, LEQ, GREATER, GEQ, IN, BETWEEN, IS, IS_NOT, NOT_LIKE, LIKE),
('left', JSON_GET),
('left', PLUS, MINUS),
('left', STAR, DIVIDE),
('right', UMINUS), # Unary minus operator, unary not
('nonassoc', LESS, LEQ, GREATER, GEQ, IN, BETWEEN, IS, IS_NOT, NOT_LIKE, LIKE),

)

# Top-level statements
Expand Down Expand Up @@ -1438,17 +1440,15 @@ def expr(self, p):
'expr LIKE expr',
'expr NOT_LIKE expr',
'expr CONCAT expr',
'expr JSON_GET constant',
'expr JSON_GET_STR constant',
'expr IN expr')
def expr(self, p):
if hasattr(p, 'LAST'):
arg1 = Last()
else:
arg1 = p.expr1
if len(p) > 3:
op = ' '.join([p[i] for i in range(1, len(p)-1)])
else:
op = p[1]
return BinaryOperation(op=op, args=(p[0], arg1))
arg1 = p[2]
return BinaryOperation(op=p[1], args=(p[0], arg1))

@_('MINUS expr %prec UMINUS',
'NOT expr %prec UNOT', )
Expand Down
4 changes: 4 additions & 0 deletions tests/test_parser/test_mindsdb/test_selects.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,9 @@ def test_last(self):
assert str(ast) == str(expected_ast)


def test_json(self):
sql = """SELECT col -> '2' from TAB1"""
# TODO
raise


0 comments on commit a66d9de

Please sign in to comment.