Skip to content

Commit fd402aa

Browse files
authored
Merge pull request #48 from mindsdb/fix-in
Fix query with single value in IN
2 parents 4aa5641 + 91bca4a commit fd402aa

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

mindsdb_sql_parser/ast/select/operation.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
from mindsdb_sql_parser.ast.base import ASTNode
22
from mindsdb_sql_parser.exceptions import ParsingException
33
from mindsdb_sql_parser.utils import indent
4-
4+
from mindsdb_sql_parser.ast.select.tuple import Tuple
55

66
class Operation(ASTNode):
77
def __init__(self, op, args, *args_, **kwargs):
88
super().__init__(*args_, **kwargs)
99

1010
self.op = ' '.join(op.lower().split())
11-
self.args = list(args)
11+
self.args = []
12+
for item in args:
13+
if isinstance(item, ASTNode) and item.parentheses:
14+
item.parentheses = False
15+
item = Tuple([item])
16+
self.args.append(item)
1217
self.assert_arguments()
1318

1419
def assert_arguments(self):

tests/test_base_sql/test_select_structure.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,18 @@ def test_in_tuple(self):
681681
assert ast.to_tree() == expected_ast.to_tree()
682682
assert str(ast) == str(expected_ast)
683683

684+
sql = "SELECT col FROM tab WHERE col in (1)"
685+
ast = parse_sql(sql)
686+
expected_ast = Select(targets=[Identifier(parts=['col'])],
687+
from_table=Identifier(parts=['tab']),
688+
where=BinaryOperation(op='in',
689+
args=(
690+
Identifier(parts=['col']),
691+
Tuple(items=[Constant(1)])
692+
)))
693+
assert ast.to_tree() == expected_ast.to_tree()
694+
assert str(ast) == str(expected_ast)
695+
684696
def test_count_distinct(self):
685697
sql = "SELECT COUNT(DISTINCT survived) AS uniq_survived FROM titanic"
686698
ast = parse_sql(sql)

0 commit comments

Comments
 (0)