Allow optimize_arithmetic_operations_in_aggregate_functions when alias is used.

This commit is contained in:
Amos Bird 2021-09-08 20:10:49 +08:00
parent f2d97e3225
commit f4269ce417
No known key found for this signature in database
GPG Key ID: 80D430DCBECFEDB4
4 changed files with 23 additions and 7 deletions

View File

@ -107,10 +107,7 @@ ASTPtr tryExchangeFunctions(const ASTFunction & func)
|| !supported.find(lower_name)->second.count(child_func->name)) || !supported.find(lower_name)->second.count(child_func->name))
return {}; return {};
/// Cannot rewrite function with alias cause alias could become undefined auto original_alias = func.tryGetAlias();
if (!func.tryGetAlias().empty() || !child_func->tryGetAlias().empty())
return {};
const auto & child_func_args = child_func->arguments->children; const auto & child_func_args = child_func->arguments->children;
const auto * first_literal = child_func_args[0]->as<ASTLiteral>(); const auto * first_literal = child_func_args[0]->as<ASTLiteral>();
const auto * second_literal = child_func_args[1]->as<ASTLiteral>(); const auto * second_literal = child_func_args[1]->as<ASTLiteral>();
@ -132,7 +129,12 @@ ASTPtr tryExchangeFunctions(const ASTFunction & func)
optimized_ast = exchangeExtractSecondArgument(new_name, *child_func); optimized_ast = exchangeExtractSecondArgument(new_name, *child_func);
} }
return optimized_ast; if (optimized_ast)
{
optimized_ast->setAlias(original_alias);
return optimized_ast;
}
return {};
} }
} }

View File

@ -0,0 +1,10 @@
SELECT min(n AS a) + (1 AS b) AS c
FROM
(
SELECT number AS n
FROM numbers(10)
WHERE (1 > 0) AND (n > 0)
)
WHERE (a > 0) AND (b > 0)
HAVING c > 0
2

View File

@ -0,0 +1,4 @@
set optimize_arithmetic_operations_in_aggregate_functions = 1;
explain syntax select min((n as a) + (1 as b)) c from (select number n from numbers(10)) where a > 0 and b > 0 having c > 0;
select min((n as a) + (1 as b)) c from (select number n from numbers(10)) where a > 0 and b > 0 having c > 0;

View File

@ -54,8 +54,8 @@ SELECT
sum(k) sum(k)
FROM columns_transformers FROM columns_transformers
SELECT SELECT
avg(i + 1 AS i), avg(i) + 1,
avg(j + 2 AS j), avg(j) + 2,
avg(k) avg(k)
FROM columns_transformers FROM columns_transformers
SELECT SELECT