Skip to content

Commit ba5a0e6

Browse files
committed
test
1 parent 98f1b0c commit ba5a0e6

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

mindsdb_sql_parser/ast/select/union.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ def __init__(self,
99
left,
1010
right,
1111
unique=True,
12+
distinct_key=False,
1213
*args, **kwargs):
1314
super().__init__(*args, **kwargs)
1415
self.left = left
1516
self.right = right
1617
self.unique = unique
18+
self.distinct_key = distinct_key
1719

1820
if self.alias:
1921
self.parentheses = True
@@ -26,7 +28,7 @@ def to_tree(self, *args, level=0, **kwargs):
2628
right_str = f'\n{ind1}right=\n{self.right.to_tree(level=level + 2)},'
2729

2830
cls_name = self.__class__.__name__
29-
out_str = f'{ind}{cls_name}(unique={repr(self.unique)},' \
31+
out_str = f'{ind}{cls_name}(unique={repr(self.unique)}, distinct_key={repr(self.distinct_key)}' \
3032
f'{left_str}' \
3133
f'{right_str}' \
3234
f'\n{ind})'
@@ -38,6 +40,8 @@ def get_string(self, *args, **kwargs):
3840
keyword = self.operation
3941
if not self.unique:
4042
keyword += ' ALL'
43+
if self.distinct_key:
44+
keyword += ' DISTINCT'
4145
out_str = f"""{left_str}\n{keyword}\n{right_str}"""
4246

4347
return out_str

tests/test_base_sql/test_union.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,26 @@ def test_single_select_error(self):
1212

1313
def test_union_base(self):
1414
for keyword, cls in {'union': Union, 'intersect': Intersect, 'except': Except}.items():
15-
sql = f"""SELECT col1 FROM tab1
16-
{keyword}
17-
SELECT col1 FROM tab2"""
15+
for rule in ['', 'distinct']:
16+
sql = f"""SELECT col1 FROM tab1
17+
{keyword} {rule}
18+
SELECT col1 FROM tab2"""
1819

19-
ast = parse_sql(sql)
20-
expected_ast = cls(unique=True,
21-
left=Select(targets=[Identifier('col1')],
22-
from_table=Identifier(parts=['tab1']),
23-
),
24-
right=Select(targets=[Identifier('col1')],
25-
from_table=Identifier(parts=['tab2']),
26-
),
27-
)
28-
assert ast.to_tree() == expected_ast.to_tree()
29-
assert str(ast) == str(expected_ast)
20+
ast = parse_sql(sql)
21+
expected_ast = cls(
22+
unique=True,
23+
distinct_key=rule == 'distinct',
24+
left=Select(
25+
targets=[Identifier('col1')],
26+
from_table=Identifier(parts=['tab1']),
27+
),
28+
right=Select(
29+
targets=[Identifier('col1')],
30+
from_table=Identifier(parts=['tab2']),
31+
),
32+
)
33+
assert ast.to_tree() == expected_ast.to_tree()
34+
assert str(ast) == str(expected_ast)
3035

3136
def test_union_all(self):
3237
for keyword, cls in {'union': Union, 'intersect': Intersect, 'except': Except}.items():

0 commit comments

Comments
 (0)