From 3b40099578b474cc2ba26980148c666edb55c3c5 Mon Sep 17 00:00:00 2001 From: feng lv Date: Wed, 17 Feb 2021 08:26:52 +0000 Subject: [PATCH] fix subquery with limit --- src/Interpreters/InterpreterSelectQuery.cpp | 17 +++++++++++++++-- .../01720_union_distinct_with_limit.reference | 1 + .../01720_union_distinct_with_limit.sql | 8 ++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/01720_union_distinct_with_limit.reference create mode 100644 tests/queries/0_stateless/01720_union_distinct_with_limit.sql diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 84de6fa4e6..a325a8d332 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -784,9 +784,22 @@ static bool hasWithTotalsInAnySubqueryInFromClause(const ASTSelectQuery & query) { if (const auto * ast_union = query_table->as()) { + ///NOTE: Child of subquery can be ASTSelectWithUnionQuery or ASTSelectQuery, + /// and after normalization, the height of the AST tree is at most 2 for (const auto & elem : ast_union->list_of_selects->children) - if (hasWithTotalsInAnySubqueryInFromClause(elem->as())) - return true; + { + if (const auto * child_union = elem->as()) + { + for (const auto & child_elem : child_union->list_of_selects->children) + if (hasWithTotalsInAnySubqueryInFromClause(child_elem->as())) + return true; + } + else + { + if (hasWithTotalsInAnySubqueryInFromClause(elem->as())) + return true; + } + } } } diff --git a/tests/queries/0_stateless/01720_union_distinct_with_limit.reference b/tests/queries/0_stateless/01720_union_distinct_with_limit.reference new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/tests/queries/0_stateless/01720_union_distinct_with_limit.reference @@ -0,0 +1 @@ +1 diff --git a/tests/queries/0_stateless/01720_union_distinct_with_limit.sql b/tests/queries/0_stateless/01720_union_distinct_with_limit.sql new file mode 100644 index 0000000000..9fc5b3eafd --- /dev/null +++ b/tests/queries/0_stateless/01720_union_distinct_with_limit.sql @@ -0,0 +1,8 @@ +SELECT x +FROM +( + SELECT 1 AS x + UNION DISTINCT + SELECT 1 +) +LIMIT 1; -- GitLab