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

Release 0.17.3 #394

Merged
merged 6 commits into from
Aug 9, 2024
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
2 changes: 1 addition & 1 deletion mindsdb_sql/__about__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__title__ = 'mindsdb_sql'
__package_name__ = 'mindsdb_sql'
__version__ = '0.17.2'
__version__ = '0.17.3'
__description__ = "Pure python SQL parser"
__email__ = "[email protected]"
__author__ = 'MindsDB Inc'
Expand Down
6 changes: 4 additions & 2 deletions mindsdb_sql/parser/dialects/mindsdb/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ class MindsDBLexer(Lexer):
# Operators
PLUS, MINUS, MATCH, NOT_MATCH, DIVIDE, MODULO,
EQUALS, NEQUALS, GREATER, GEQ, LESS, LEQ,
AND, OR, NOT, IS, IS_NOT,
AND, OR, NOT, IS, IS_NOT, TYPECAST,
IN, NOT_IN, LIKE, NOT_LIKE, CONCAT, BETWEEN, WINDOW, OVER, PARTITION_BY,
JSON_GET, JSON_GET_STR, INTERVAL,

# Data types
CAST, ID, INTEGER, FLOAT, QUOTE_STRING, DQUOTE_STRING, NULL, TRUE, FALSE,
CAST, ID, INTEGER, DATE, FLOAT, QUOTE_STRING, DQUOTE_STRING, NULL, TRUE, FALSE,

}

Expand Down Expand Up @@ -148,6 +148,7 @@ class MindsDBLexer(Lexer):
CONVERT = r'\bCONVERT\b'
DESCRIBE = r'\bDESCRIBE\b'
BEGIN = r'\bBEGIN\b'
DATE = r'\bDATE\b'

# SHOW
SHOW = r'\bSHOW\b'
Expand Down Expand Up @@ -251,6 +252,7 @@ class MindsDBLexer(Lexer):
VALUES = r'\bVALUES\b'

# Special
TYPECAST = r'\:\:'
DOT = r'\.'
COMMA = r','
LPAREN = r'\('
Expand Down
21 changes: 20 additions & 1 deletion mindsdb_sql/parser/dialects/mindsdb/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1374,8 +1374,17 @@ def function(self, p):
return Function(op=name, args=args, namespace=namespace)

@_('INTERVAL string')
@_('INTERVAL string id')
@_('INTERVAL integer id')
def expr(self, p):
return Interval(p.string)
if hasattr(p, 'id'):
if hasattr(p, 'integer'):
info = f'{p.integer} {p.id}'
else:
info = f'{p.string} {p.id}'
else:
info = p.string
return Interval(info)

@_('EXISTS LPAREN select RPAREN')
def function(self, p):
Expand Down Expand Up @@ -1412,6 +1421,15 @@ def expr(self, p):
def expr(self, p):
return TypeCast(arg=p.expr, type_name=str(p.id))

@_('DATE string')
def expr(self, p):
return TypeCast(arg=Constant(p.string), type_name=p.DATE)

@_('expr TYPECAST id')
@_('expr TYPECAST DATE')
def expr(self, p):
return TypeCast(arg=p.expr, type_name=p[2])

@_('enumeration')
def expr_list(self, p):
return p.enumeration
Expand Down Expand Up @@ -1643,6 +1661,7 @@ def function_name(self, p):
@_('ID',
'BEGIN',
'CAST',
'DATE',
'CHANNEL',
'CHARSET',
'CODE',
Expand Down
49 changes: 25 additions & 24 deletions tests/test_parser/test_base_sql/test_misc_sql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,32 +230,33 @@ def test_set_version(self):
assert ast.to_tree() == expected_ast.to_tree()
assert str(ast) == str(expected_ast)


def test_interval(self):
sql = """
select interval '1 day'+1 from aaa
where 'a' > interval "3 day 1 min"
"""

expected_ast = Select(
targets=[
BinaryOperation(op='+', args=[
Interval('1 day'),
Constant(1)
])
],
from_table=Identifier('aaa'),
where=BinaryOperation(
op='>',
args=[
Constant('a'),
Interval('3 day 1 min'),
]
for value in ('1 day', "'1' day", "'1 day'"):
sql = f"""
select interval {value} + 1 from aaa
where 'a' > interval "3 day 1 min"
"""

expected_ast = Select(
targets=[
BinaryOperation(op='+', args=[
Interval('1 day'),
Constant(1)
])
],
from_table=Identifier('aaa'),
where=BinaryOperation(
op='>',
args=[
Constant('a'),
Interval('3 day 1 min'),
]
)
)
)

ast = parse_sql(sql)

assert str(ast).lower() == str(expected_ast).lower()
assert ast.to_tree() == expected_ast.to_tree()
ast = parse_sql(sql)

assert str(ast).lower() == str(expected_ast).lower()
assert ast.to_tree() == expected_ast.to_tree()

34 changes: 33 additions & 1 deletion tests/test_parser/test_base_sql/test_select_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,4 +1093,36 @@ def test_substring(self):
):

ast = parse_sql(sql)
assert str(ast) == str(expected_ast)
assert str(ast) == str(expected_ast)

def test_alternative_casting(self):

# int
expected_ast = Select(targets=[
TypeCast(type_name='CHAR', arg=Constant('1998')),
TypeCast(type_name='CHAR', arg=Identifier('col1'), alias=Identifier('col2'))
])

sql = f"SELECT '1998'::CHAR, col1::CHAR col2"
ast = parse_sql(sql)
assert str(ast) == str(expected_ast)

# date
expected_ast = Select(targets=[
TypeCast(type_name='DATE', arg=Constant('1998-12-01')),
TypeCast(type_name='DATE', arg=Identifier('col1'), alias=Identifier('col2'))
])

sql = f"SELECT '1998-12-01'::DATE, col1::DATE col2"
ast = parse_sql(sql)
assert str(ast) == str(expected_ast)

#
expected_ast = Select(targets=[
TypeCast(type_name='DATE', arg=Constant('1998-12-01')),
])

sql = f"SELECT DATE '1998-12-01'"
ast = parse_sql(sql)
assert str(ast) == str(expected_ast)