Skip to content

Commit

Permalink
Merge pull request #407 from mindsdb/last-coalence
Browse files Browse the repository at this point in the history
Support LAST in function
  • Loading branch information
ea-rus authored Oct 3, 2024
2 parents 6e39d2e + 1c6ca31 commit af7071d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
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

0 comments on commit af7071d

Please sign in to comment.