Skip to content

Commit

Permalink
negative constant instead of unary operation
Browse files Browse the repository at this point in the history
  • Loading branch information
ea-rus committed Aug 29, 2023
1 parent 0cd611e commit e6df6b0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 4 additions & 0 deletions mindsdb_sql/parser/dialects/mindsdb/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,10 @@ def expr(self, p):
def expr(self, p):
return UnaryOperation(op=p[0], args=(p.expr,))

@_('MINUS constant %prec UMINUS')
def constant(self, p):
return Constant(-p.constant.value)

# update fields list
@_('update_parameter',
'update_parameter_list COMMA update_parameter')
Expand Down
12 changes: 6 additions & 6 deletions mindsdb_sql/planner/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ def recursively_extract_column_values(op, row_dict, predictor):
id = op.args[0]
value = op.args[1]

if (
isinstance(value, UnaryOperation)
and value.op == '-'
and isinstance(value.args[0], Constant)
):
value = Constant(-value.args[0].value)
# if (
# isinstance(value, UnaryOperation)
# and value.op == '-'
# and isinstance(value.args[0], Constant)
# ):
# value = Constant(-value.args[0].value)

if not (
isinstance(id, Identifier)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_parser/test_mindsdb/test_selects.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,15 @@ def test_join_using(self):

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

def test_select_limit_negative(self):
sql = """SELECT * FROM t1 LIMIT -1"""

ast = parse_sql(sql, dialect='mindsdb')
expected_ast = Select(targets=[Star()],
from_table=Identifier(parts=['t1']),
limit=Constant(-1))

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

0 comments on commit e6df6b0

Please sign in to comment.