Some aggregate functions are commutative with some arithmetic expressions.

```
avg(x + const) = avg(x) + const
```

This is applicable for aggregate functions: `sum`, `avg`, `min`, `max` and operations: `+`, `-`, `*`, `/` (but not for `intDiv` and `modulo`).

In case of integer overflow it either works correctly (for `sum`) or we may assume that integer overflow is implementation specific behaviour and apply optimization under this assumption.

**Possible implementation**

AST rewrite with InDepthNodeVisitor.
Should not rewrite if expression has alias.
#pragma once
#include <Parsers/IAST.h>
#include <Interpreters/InDepthNodeVisitor.h>
namespace DB
{
class ASTFunction;
class ArithmeticOperationsInAgrFuncMatcher
{
public:
    struct Data {};
    static void visit(ASTPtr & ast, Data & data);
    static void visit(const ASTFunction &, ASTPtr & ast, Data & data);
    static bool needChildVisit(const ASTPtr & node, const ASTPtr & child);
};
using ArithmeticOperationsInAgrFuncVisitor = InDepthNodeVisitor<ArithmeticOperationsInAgrFuncMatcher, false>;
}