Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
ucasfl committed Feb 26, 2021
1 parent 731faea commit 4c30c10
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 15 deletions.
23 changes: 16 additions & 7 deletions src/Interpreters/NormalizeSelectWithUnionQueryVisitor.cpp
Expand Up @@ -20,7 +20,7 @@ void NormalizeSelectWithUnionQueryMatcher::getSelectsFromUnionListNode(ASTPtr &
return;
}

selects.push_back(std::move(ast_select));
selects.push_back(ast_select);
}

void NormalizeSelectWithUnionQueryMatcher::visit(ASTPtr & ast, Data & data)
Expand Down Expand Up @@ -53,15 +53,16 @@ void NormalizeSelectWithUnionQueryMatcher::visit(ASTSelectWithUnionQuery & ast,

if (union_modes[i] == ASTSelectWithUnionQuery::Mode::ALL)
{
if (auto * inner_union = select_list[i + 1]->as<ASTSelectWithUnionQuery>())
if (auto * inner_union = select_list[i + 1]->as<ASTSelectWithUnionQuery>();
inner_union && inner_union->union_mode == ASTSelectWithUnionQuery::Mode::ALL)
{
/// Inner_union is an UNION ALL list, just lift up
for (auto child = inner_union->list_of_selects->children.rbegin(); child != inner_union->list_of_selects->children.rend();
++child)
selects.push_back(std::move(*child));
selects.push_back(*child);
}
else
selects.push_back(std::move(select_list[i + 1]));
selects.push_back(select_list[i + 1]);
}
/// flatten all left nodes and current node to a UNION DISTINCT list
else if (union_modes[i] == ASTSelectWithUnionQuery::Mode::DISTINCT)
Expand All @@ -85,15 +86,23 @@ void NormalizeSelectWithUnionQueryMatcher::visit(ASTSelectWithUnionQuery & ast,
/// No UNION DISTINCT or only one child in select_list
if (i == -1)
{
if (auto * inner_union = select_list[0]->as<ASTSelectWithUnionQuery>())
if (auto * inner_union = select_list[0]->as<ASTSelectWithUnionQuery>();
inner_union && inner_union->union_mode == ASTSelectWithUnionQuery::Mode::ALL)
{
/// Inner_union is an UNION ALL list, just lift it up
for (auto child = inner_union->list_of_selects->children.rbegin(); child != inner_union->list_of_selects->children.rend();
++child)
selects.push_back(std::move(*child));
selects.push_back(*child);
}
else
selects.push_back(std::move(select_list[0]));
selects.push_back(select_list[0]);
}

/// Just one union type child, lift it up
if (selects.size() == 1 && selects[0]->as<ASTSelectWithUnionQuery>())
{
ast = *(selects[0]->as<ASTSelectWithUnionQuery>());
return;
}

// reverse children list
Expand Down
9 changes: 1 addition & 8 deletions src/Interpreters/executeQuery.cpp
Expand Up @@ -480,13 +480,6 @@ static std::tuple<ASTPtr, BlockIO> executeQueryImpl(
NormalizeSelectWithUnionQueryVisitor::Data data{context.getSettingsRef().union_default_mode};
NormalizeSelectWithUnionQueryVisitor{data}.visit(ast);

/// After normalization, if it only has one ASTSelectWithUnionQuery child,
/// we can lift it up, this can reduce one unnecessary recursion later in interpreter phase
auto select_union = ast->as<ASTSelectWithUnionQuery>();
if (select_union && select_union->list_of_selects->children.size() == 1
&& select_union->list_of_selects->children.at(0)->as<ASTSelectWithUnionQuery>())
ast = std::move(select_union->list_of_selects->children.at(0));

query = serializeAST(*ast);

/// Check the limits.
Expand Down Expand Up @@ -888,7 +881,7 @@ static std::tuple<ASTPtr, BlockIO> executeQueryImpl(
LOG_DEBUG(&Poco::Logger::get("executeQuery"), "Query pipeline:\n{}", msg_buf.str());
}
}
}
}
catch (...)
{
if (!internal)
Expand Down
1 change: 1 addition & 0 deletions src/Interpreters/ya.make
Expand Up @@ -111,6 +111,7 @@ SRCS(
MetricLog.cpp
MutationsInterpreter.cpp
MySQL/InterpretersMySQLDDLQuery.cpp
NormalizeSelectWithUnionQueryVisitor.cpp
NullableUtils.cpp
OpenTelemetrySpanLog.cpp
OptimizeIfChains.cpp
Expand Down
@@ -0,0 +1,66 @@
SELECT 1
UNION ALL
SELECT 1
UNION ALL
SELECT 1
UNION ALL
SELECT 1
UNION ALL
SELECT 1

SELECT 1
UNION ALL
(
SELECT 1
UNION DISTINCT
SELECT 1
UNION DISTINCT
SELECT 1
)
UNION ALL
SELECT 1

SELECT x
FROM
(
SELECT 1 AS x
UNION ALL
(
SELECT 1
UNION DISTINCT
SELECT 1
UNION DISTINCT
SELECT 1
)
UNION ALL
SELECT 1
)

SELECT x
FROM
(
SELECT 1 AS x
UNION ALL
SELECT 1
UNION ALL
SELECT 1
)

SELECT 1
UNION DISTINCT
SELECT 1
UNION DISTINCT
SELECT 1

SELECT 1


(
SELECT 1
UNION DISTINCT
SELECT 1
UNION DISTINCT
SELECT 1
)
UNION ALL
SELECT 1
86 changes: 86 additions & 0 deletions tests/queries/0_stateless/01732_explain_syntax_union_query.sql
@@ -0,0 +1,86 @@
EXPLAIN SYNTAX
SELECT 1
UNION ALL
(
SELECT 1
UNION ALL
(
SELECT 1
UNION ALL
SELECT 1
)
UNION ALL
SELECT 1
);

SELECT ' ';

EXPLAIN SYNTAX
SELECT 1
UNION ALL
(
SELECT 1
UNION DISTINCT
(
SELECT 1
UNION ALL
SELECT 1
)
UNION ALL
SELECT 1
);

SELECT ' ';

EXPLAIN SYNTAX
SELECT x
FROM
(
SELECT 1 AS x
UNION ALL
(
SELECT 1
UNION DISTINCT
(
SELECT 1
UNION ALL
SELECT 1
)
UNION ALL
SELECT 1
)
);

SELECT ' ';

EXPLAIN SYNTAX
SELECT x
FROM
(
SELECT 1 AS x
UNION ALL
(
SELECT 1
UNION ALL
SELECT 1
)
);

SELECT ' ';

EXPLAIN SYNTAX
SELECT 1
UNION ALL
SELECT 1
UNION DISTINCT
SELECT 1;

SELECT ' ';

EXPLAIN SYNTAX
(((((((((((((((SELECT 1)))))))))))))));

SELECT ' ';

EXPLAIN SYNTAX
(((((((((((((((SELECT 1 UNION DISTINCT SELECT 1))) UNION DISTINCT SELECT 1)))) UNION ALL SELECT 1))))))));

0 comments on commit 4c30c10

Please sign in to comment.