Followup to 57853

This commit is contained in:
Dmitry Novik 2024-01-22 14:44:07 +01:00 committed by GitHub
parent 4d6ef8ed86
commit 3d0e915088
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 9 deletions

View File

@ -10,14 +10,6 @@
namespace DB
{
/** Rewrites `sum(column +/- literal)` into two individual functions
* `sum(column)` and `literal * count(column)`.
* sum(column + literal) -> sum(column) + literal * count(column)
* sum(literal + column) -> literal * count(column) + sum(column)
* sum(column - literal) -> sum(column) - literal * count(column)
* sum(literal - column) -> literal * count(column) - sum(column)
*/
namespace
{
@ -29,6 +21,9 @@ public:
void enterImpl(QueryTreeNodePtr & node)
{
if (!getSettings().optimize_arithmetic_operations_in_aggregate_functions)
return;
static const std::unordered_set<String> func_supported = {
"plus",
"minus"

View File

@ -5,6 +5,14 @@
namespace DB
{
/**
* Rewrites `sum(column +/- literal)` into two individual functions
* `sum(column)` and `literal * count(column)`.
* sum(column + literal) -> sum(column) + literal * count(column)
* sum(literal + column) -> literal * count(column) + sum(column)
* sum(column - literal) -> sum(column) - literal * count(column)
* sum(literal - column) -> literal * count(column) - sum(column)
*/
class RewriteSumFunctionWithSumAndCountPass final : public IQueryTreePass
{
public:

View File

@ -752,7 +752,8 @@ void TreeOptimizer::apply(ASTPtr & query, TreeRewriterResult & result,
}
/// Rewrite sum(column +/- literal) function with sum(column) +/- literal * count(column).
rewriteSumFunctionWithSumAndCount(query, tables_with_columns);
if (settings.optimize_arithmetic_operations_in_aggregate_functions)
rewriteSumFunctionWithSumAndCount(query, tables_with_columns);
/// Rewrite date filters to avoid the calls of converters such as toYear, toYYYYMM, etc.
optimizeDateFilters(select_query, tables_with_columns, context);