Skip to content
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
5 changes: 3 additions & 2 deletions mindsdb_sql_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,14 +1106,15 @@ def select(self, p):
select.cte = p.ctes
return select

@_('ctes COMMA identifier cte_columns_or_nothing AS LPAREN select RPAREN')
@_('ctes COMMA identifier cte_columns_or_nothing AS LPAREN select RPAREN',
'ctes COMMA identifier cte_columns_or_nothing AS LPAREN union RPAREN')
def ctes(self, p):
ctes = p.ctes
ctes = ctes + [
CommonTableExpression(
name=p.identifier,
columns=p.cte_columns_or_nothing,
query=p.select)
query=p[6])
]
return ctes

Expand Down
45 changes: 45 additions & 0 deletions tests/test_base_sql/test_select_common_table_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,48 @@ def test_cte_nested(self):
assert str(ast).lower() == sql.lower()
assert str(ast) == str(expected_ast)
assert ast.to_tree() == expected_ast.to_tree()

def test_cte_union(self):
sql = """
WITH ta AS (
SELECT 'a' AS a
UNION
SELECT 'b' AS a
), tb AS (
SELECT 'c' AS a
UNION
SELECT 'd' AS a
)
SELECT a FROM ta
UNION
SELECT a FROM tb
"""
ast = parse_sql(sql)

expected_ast = Union(
left=Select(
cte=[
CommonTableExpression(
name=Identifier('ta'),
query=Union(
left=Select(targets=[Constant('a', alias=Identifier('a'))]),
right=Select(targets=[Constant('b', alias=Identifier('a'))])
)
),
CommonTableExpression(
name=Identifier('tb'),
query=Union(
left=Select(targets=[Constant('c', alias=Identifier('a'))]),
right=Select(targets=[Constant('d', alias=Identifier('a'))])
)
),
],
targets=[Identifier('a')],
from_table=Identifier('ta')
),
right=Select(targets=[Identifier('a')], from_table=Identifier('tb'))
)

assert (' '.join(str(ast).split())).lower() == (' '.join(sql.split())).lower()
assert str(ast) == str(expected_ast)
assert ast.to_tree() == expected_ast.to_tree()
Loading