Fix global-with with subqueries

This commit is contained in:
Amos Bird 2021-02-14 14:20:23 +08:00
parent 8ff458a2bf
commit ed49367fc7
No known key found for this signature in database
GPG Key ID: 80D430DCBECFEDB4
5 changed files with 10 additions and 4 deletions

View File

@ -329,7 +329,7 @@ InterpreterSelectWithUnionQuery::buildCurrentChildInterpreter(const ASTPtr & ast
InterpreterSelectWithUnionQuery::~InterpreterSelectWithUnionQuery() = default;
Block InterpreterSelectWithUnionQuery::getSampleBlock(const ASTPtr & query_ptr_, const Context & context_)
Block InterpreterSelectWithUnionQuery::getSampleBlock(const ASTPtr & query_ptr_, const Context & context_, bool is_subquery)
{
auto & cache = context_.getSampleBlockCache();
/// Using query string because query_ptr changes for every internal SELECT
@ -339,7 +339,11 @@ Block InterpreterSelectWithUnionQuery::getSampleBlock(const ASTPtr & query_ptr_,
return cache[key];
}
return cache[key] = InterpreterSelectWithUnionQuery(query_ptr_, context_, SelectQueryOptions().analyze()).getSampleBlock();
if (is_subquery)
return cache[key]
= InterpreterSelectWithUnionQuery(query_ptr_, context_, SelectQueryOptions().subquery().analyze()).getSampleBlock();
else
return cache[key] = InterpreterSelectWithUnionQuery(query_ptr_, context_, SelectQueryOptions().analyze()).getSampleBlock();
}

View File

@ -35,7 +35,8 @@ public:
static Block getSampleBlock(
const ASTPtr & query_ptr_,
const Context & context_);
const Context & context_,
bool is_subquery = false);
virtual void ignoreWithTotals() override;

View File

@ -84,7 +84,7 @@ static NamesAndTypesList getColumnsFromTableExpression(
if (table_expression.subquery)
{
const auto & subquery = table_expression.subquery->children.at(0);
names_and_type_list = InterpreterSelectWithUnionQuery::getSampleBlock(subquery, context).getNamesAndTypesList();
names_and_type_list = InterpreterSelectWithUnionQuery::getSampleBlock(subquery, context, true).getNamesAndTypesList();
}
else if (table_expression.table_function)
{

View File

@ -0,0 +1 @@
WITH (SELECT count(distinct colU) from tabA) AS withA, (SELECT count(distinct colU) from tabA) AS withB SELECT withA / withB AS ratio FROM (SELECT date AS period, colX FROM (SELECT date, if(colA IN (SELECT colB FROM tabC), 0, colA) AS colX FROM tabB) AS tempB GROUP BY period, colX) AS main; -- {serverError 60}