After upgrading to `21.11.6.7` from `21.6.6.51` some of our `INSERT`s start failing with the following error:
```
2021.12.14 09:54:05.538718 [ 373 ] {} <Error> void DB::StorageBuffer::backgroundFlush(): Code: 215. DB::Exception: Column `name` is not under aggregate function and not in GROUP BY: While processing name, toStartOfInterval(platform_time, toIntervalHour(1)) AS platform_time, anyLastSimpleState(publisher) AS publisher, if((sumSimpleStateIf(1, isFinite(value)) AS count) > 0, argMinStateIf(value, platform_time, isFinite(value)), argMinState(nan, fromUnixTimestamp64Nano(CAST(9223372036854775807, 'Int64')))) AS open, if(count > 0, argMaxStateIf(value, platform_time, isFinite(value)), argMaxState(nan, fromUnixTimestamp64Nano(CAST(0, 'Int64')))) AS close, coalesce(minSimpleStateOrNullIf(value, isFinite(value)), inf) AS min, coalesce(maxSimpleStateOrNullIf(value, isFinite(value)), -inf) AS max, sumSimpleStateIf(value, isFinite(value)) AS sum, sumSimpleStateIf(value * value, isFinite(value)) AS sum_of_squares, count, sumSimpleStateIf(1, NOT isFinite(value)) AS non_finite_count, argMaxState(NOT isFinite(value), platform_time) AS ends_with_non_finite. (NOT_AN_AGGREGATE), Stack trace (when copying this message, always include the lines below):

0. DB::Exception::Exception(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int, bool) @ 0x9b722d4 in /usr/bin/clickhouse
1. DB::ActionsMatcher::visit(DB::ASTIdentifier const&, std::__1::shared_ptr<DB::IAST> const&, DB::ActionsMatcher::Data&) @ 0x120cd5f1 in /usr/bin/clickhouse
2. DB::ActionsMatcher::visit(DB::ASTExpressionList&, std::__1::shared_ptr<DB::IAST> const&, DB::ActionsMatcher::Data&) @ 0x120d5318 in /usr/bin/clickhouse
3. DB::InDepthNodeVisitor<DB::ActionsMatcher, true, false, std::__1::shared_ptr<DB::IAST> const>::visit(std::__1::shared_ptr<DB::IAST> const&) @ 0x120a46b7 in /usr/bin/clickhouse
4. DB::ExpressionAnalyzer::getRootActions(std::__1::shared_ptr<DB::IAST> const&, bool, std::__1::shared_ptr<DB::ActionsDAG>&, bool) @ 0x120a44cb in /usr/bin/clickhouse
5. DB::SelectQueryExpressionAnalyzer::appendSelect(DB::ExpressionActionsChain&, bool) @ 0x120aef09 in /usr/bin/clickhouse
6. DB::ExpressionAnalysisResult::ExpressionAnalysisResult(DB::SelectQueryExpressionAnalyzer&, std::__1::shared_ptr<DB::StorageInMemoryMetadata const> const&, bool, bool, bool, std::__1::shared_ptr<DB::FilterDAGInfo> const&, DB::Block const&) @ 0x120b3b70 in /usr/bin/clickhouse
7. DB::InterpreterSelectQuery::getSampleBlockImpl() @ 0x1233a78d in /usr/bin/clickhouse
8. ? @ 0x123332e4 in /usr/bin/clickhouse
9. DB::InterpreterSelectQuery::InterpreterSelectQuery(std::__1::shared_ptr<DB::IAST> const&, std::__1::shared_ptr<DB::Context const>, std::__1::optional<DB::Pipe>, std::__1::shared_ptr<DB::IStorage> const&, DB::SelectQueryOptions const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, std::__1::shared_ptr<DB::StorageInMemoryMetadata const> const&, std::__1::unordered_map<DB::PreparedSetKey, std::__1::shared_ptr<DB::Set>, DB::PreparedSetKey::Hash, std::__1::equal_to<DB::PreparedSetKey>, std::__1::allocator<std::__1::pair<DB::PreparedSetKey const, std::__1::shared_ptr<DB::Set> > > >) @ 0x1232dd47 in /usr/bin/clickhouse
10. DB::InterpreterSelectQuery::InterpreterSelectQuery(std::__1::shared_ptr<DB::IAST> const&, std::__1::shared_ptr<DB::Context const>, DB::SelectQueryOptions const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) @ 0x1232c6d4 in /usr/bin/clickhouse
11. DB::buildPushingToViewsChain(std::__1::shared_ptr<DB::IStorage> const&, std::__1::shared_ptr<DB::StorageInMemoryMetadata const> const&, std::__1::shared_ptr<DB::Context const>, std::__1::shared_ptr<DB::IAST> const&, bool, DB::ThreadStatus*, std::__1::atomic<unsigned long>*, DB::Block const&) @ 0x13362587 in /usr/bin/clickhouse
12. DB::InterpreterInsertQuery::buildChainImpl(std::__1::shared_ptr<DB::IStorage> const&, std::__1::shared_ptr<DB::StorageInMemoryMetadata const> const&, DB::Block const&, DB::ThreadStatus*, std::__1::atomic<unsigned long>*) @ 0x12312b7b in /usr/bin/clickhouse
13. DB::InterpreterInsertQuery::execute() @ 0x12314f89 in /usr/bin/clickhouse
14. DB::StorageBuffer::writeBlockToDestination(DB::Block const&, std::__1::shared_ptr<DB::IStorage>) @ 0x12be8091 in /usr/bin/clickhouse
15. DB::StorageBuffer::flushBuffer(DB::StorageBuffer::Buffer&, bool, bool, bool) @ 0x12be5667 in /usr/bin/clickhouse
16. DB::StorageBuffer::backgroundFlush() @ 0x12be8e85 in /usr/bin/clickhouse
17. DB::BackgroundSchedulePoolTaskInfo::execute() @ 0x11f0eb6e in /usr/bin/clickhouse
18. DB::BackgroundSchedulePool::threadFunction() @ 0x11f11547 in /usr/bin/clickhouse
19. ? @ 0x11f124b3 in /usr/bin/clickhouse
20. ThreadPoolImpl<std::__1::thread>::worker(std::__1::__list_iterator<std::__1::thread, void*>) @ 0x9bb4397 in /usr/bin/clickhouse
21. ? @ 0x9bb7d9d in /usr/bin/clickhouse
22. start_thread @ 0x9609 in /usr/lib/x86_64-linux-gnu/libpthread-2.31.so
23. __clone @ 0x122293 in /usr/lib/x86_64-linux-gnu/libc-2.31.so
 (version 21.11.6.7 (official build))
```

Maybe related to https://github.com/ClickHouse/ClickHouse/pull/28502

## Tables
### indicator_log_buffer
The table that handles `INSERT`s.
```sql
CREATE TABLE indicator_log_buffer AS indicator_log
    ENGINE = Buffer(
        currentDatabase(), indicator_log,
        /* num_layers */ 1,
        /* min_time/max_time (sec) */ 1, 1,
        /* min_rows/max_rows */ 1000000, 1000000,
        /* min_bytes/max_bytes */ 100000000, 100000000
    );
```

### indicator_log
The underlying table of `indicator_log_buffer`.
```sql
CREATE TABLE indicator_log (
    name                    String,
    platform_time           DateTime64(9),
    trace_id                UInt64,
    value                   Float64,
    publisher               LowCardinality(String),

    INDEX trace_id_index trace_id TYPE minmax GRANULARITY 3
)
    ENGINE = ReplacingMergeTree
    PARTITION BY toYYYYMM(platform_time)
    ORDER BY (name, platform_time);
```

### indicator_xxx_log
The aggregated table. 
```sql
CREATE TABLE indicator_1sec_log (
    name                    LowCardinality(String),
    platform_time           DateTime64(9),
    publisher               SimpleAggregateFunction(anyLast, LowCardinality(String)),

    open                    AggregateFunction(argMin, Float64, DateTime64(9)),
    close                   AggregateFunction(argMax, Float64, DateTime64(9)),
    min                     SimpleAggregateFunction(min, Float64),
    max                     SimpleAggregateFunction(max, Float64),
    sum                     SimpleAggregateFunction(sum, Float64),
    sum_of_squares          SimpleAggregateFunction(sum, Float64),
    count                   SimpleAggregateFunction(sum, UInt64),
    non_finite_count        SimpleAggregateFunction(sum, UInt64),
    ends_with_non_finite    AggregateFunction(argMax, UInt8, DateTime64(9))
)
    ENGINE = AggregatingMergeTree
    PARTITION BY toYYYYMM(platform_time)
    ORDER BY (name, platform_time);

CREATE MATERIALIZED VIEW indicator_1sec_log_handler TO indicator_1sec_log
AS SELECT
    name, -- << ERROR
    toStartOfInterval(platform_time, INTERVAL 1 second) AS platform_time,
    anyLastSimpleState(publisher) AS publisher,

    count > 0 ? argMinStateIf(value, indicator_log.platform_time, isFinite(value))
              : argMinState(nan, fromUnixTimestamp64Nano(CAST(9223372036854775807, 'Int64'))) AS open,

    count > 0 ? argMaxStateIf(value, indicator_log.platform_time, isFinite(value))
              : argMaxState(nan, fromUnixTimestamp64Nano(CAST(0, 'Int64'))) AS close,

    coalesce(minSimpleStateOrNullIf(value, isFinite(value)), inf) AS min,
    coalesce(maxSimpleStateOrNullIf(value, isFinite(value)), -inf) AS max,
    sumSimpleStateIf(value, isFinite(value)) AS sum,
    sumSimpleStateIf(value * value, isFinite(value)) AS sum_of_squares,
    sumSimpleStateIf(1, isFinite(value)) AS count,
    sumSimpleStateIf(1, NOT isFinite(value)) AS non_finite_count,
    argMaxState(NOT isFinite(value), indicator_log.platform_time) AS ends_with_non_finite
FROM indicator_log
GROUP BY (name, platform_time);
```
bool remove_duplicates = select_options.remove_duplicates;
    const auto & settings = getContext()->getSettingsRef();
    const NameSet & source_columns_set = result.source_columns_set;
    if (table_join)
    {
        result.analyzed_join = table_join;
        result.analyzed_join->resetCollected();
    }
    else 
        result.analyzed_join = std::make_shared<TableJoin>();
    if (remove_duplicates)
        renameDuplicatedColumns(select_query);
    if (tables_with_columns.size() > 1)
    {
        const auto & right_table = tables_with_columns[1];
        auto & cols_from_joined = result.analyzed_join->columns_from_joined_table;
        cols_from_joined = right_table.columns;
        
        
        cols_from_joined.insert(cols_from_joined.end(), right_table.hidden_columns.begin(), right_table.hidden_columns.end());
        result.analyzed_join->deduplicateAndQualifyColumnNames(
            source_columns_set, right_table.table.getQualifiedNamePrefix());
    }
    translateQualifiedNames(query, *select_query, source_columns_set, tables_with_columns);
    
    LogicalExpressionsOptimizer(select_query, settings.optimize_min_equality_disjunction_chain_length.value).perform();
    NameSet all_source_columns_set = source_columns_set;
    if (table_join)
    {
        for (const auto & [name, _] : table_join->columns_from_joined_table)