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

Support LAST in function #407

Merged
merged 2 commits into from
Oct 3, 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
10 changes: 9 additions & 1 deletion mindsdb_sql/parser/dialects/mindsdb/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class MindsDBParser(Parser):
('nonassoc', LESS, LEQ, GREATER, GEQ, IN, NOT_IN, BETWEEN, IS, IS_NOT, NOT_LIKE, LIKE),
('left', JSON_GET),
('left', PLUS, MINUS),
('left', STAR, DIVIDE),
('left', STAR, DIVIDE, TYPECAST),
('right', UMINUS), # Unary minus operator, unary not

)
Expand Down Expand Up @@ -1416,6 +1416,14 @@ def function(self, p):
args = p.expr_list_or_nothing
if not args:
args = []
for i, arg in enumerate(args):
if (
isinstance(arg, Identifier)
and len(arg.parts) == 1
and arg.parts[0].lower() == 'last'
):
args[i] = Last()

namespace = None
if hasattr(p, 'identifier'):
if len(p.identifier.parts) > 1:
Expand Down
20 changes: 15 additions & 5 deletions tests/test_parser/test_base_sql/test_select_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,12 +1106,22 @@ def test_alternative_casting(self):
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'))
])
expected_ast = Select(
targets=[
TypeCast(type_name='DATE', arg=Constant('1998-12-01')),
BinaryOperation(op='+', args=[
Identifier('col0'),
TypeCast(type_name='DATE', arg=Identifier('col1'), alias=Identifier('col2')),
])
],
from_table=Identifier('t1'),
where=BinaryOperation(op='>', args=[
Identifier('col0'),
TypeCast(type_name='DATE', arg=Identifier('col1')),
])
)

sql = f"SELECT '1998-12-01'::DATE, col1::DATE col2"
sql = f"SELECT '1998-12-01'::DATE, col0 + col1::DATE col2 from t1 where col0 > col1::DATE"
ast = parse_sql(sql)
assert str(ast) == str(expected_ast)

Expand Down
29 changes: 20 additions & 9 deletions tests/test_parser/test_mindsdb/test_selects.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,32 @@ def test_select_limit_negative(self):
assert ast.to_tree() == expected_ast.to_tree()
assert str(ast) == str(expected_ast)


def test_last(self):
sql = """SELECT * FROM t1 t where t.id>last"""
sql = """SELECT * FROM t1 t where t.id>last and t.x > coalence(last, 0)"""

ast = parse_sql(sql, dialect='mindsdb')
expected_ast = Select(
targets=[Star()],
from_table=Identifier(parts=['t1'], alias=Identifier('t')),
where=BinaryOperation(
op='>',
args=[
Identifier(parts=['t', 'id']),
Last()
]
)
where=BinaryOperation(op='and', args=[
BinaryOperation(
op='>',
args=[
Identifier(parts=['t', 'id']),
Last()
]
),
BinaryOperation(
op='>',
args=[
Identifier(parts=['t', 'x']),
Function(op='coalence', args=[
Last(),
Constant(0)
])
]
),
])
)

assert ast.to_tree() == expected_ast.to_tree()
Expand Down
Loading