Build from master with memory sanitiser. Enabled analyzer.
I run queries with SQLancer and server crashed. Same stack traces are reported from 3 threads but they are the same.
I could not reproduce error by running the same query after server restart. Probably some data race?


```
2022.11.09 21:19:55.754257 [ 590431 ] {} <Fatal> BaseDaemon: ########################################
2022.11.09 21:19:55.754463 [ 590431 ] {} <Fatal> BaseDaemon: (version 22.11.1.1, build id: 1A0CF0D623D74E69E4B6392E62612FD583BED321) (from thread 590424) (query_id: ee741a39-2222-4e9d-a276-7d3c85c5c5f4) (query: SELECT COUNT(-1361338261) FROM t3 GROUP BY t3.c0 SETTINGS aggregate_functions_null_for_empty=1, enable_optimize_predicate_expression=0) Received signal Segmentation fault (11)
2022.11.09 21:19:55.754672 [ 590431 ] {} <Fatal> BaseDaemon: Address: NULL pointer. Access: read. Address not mapped to object.
2022.11.09 21:19:55.754806 [ 590431 ] {} <Fatal> BaseDaemon: Stack trace: 0x4116a75d 0x40c03a53 0x40c0f8af 0x488baa42 0x488b39da 0x480aef58 0x48075a9d 0x4807b892 0x27d5848d 0x27d663a3 0x7f558c6fe609 0x7f558c623163
2022.11.09 21:19:56.837904 [ 590431 ] {} <Fatal> BaseDaemon: 4. ./build_docker/../src/Interpreters/Aggregator.cpp:1220: void DB::Aggregator::executeImplBatch<false, true, false, DB::AggregationMethodStringNoCache<StringHashMap<char*, Allocator<true, true>>>>(DB::AggregationMethodStringNoCache<StringHashMap<char*, Allocator<true, true>>>&, DB::AggregationMethodStringNoCache<StringHashMap<char*, Allocator<true, true>>>::State&, DB::Arena*, unsigned long, unsigned long, DB::Aggregator::AggregateFunctionInstruction*, char*) const @ 0x4116a75d in /home/ubuntu/memory-ch/clickhouse
2022.11.09 21:20:00.061769 [ 590431 ] {} <Fatal> BaseDaemon: 5.1. inlined from ./build_docker/../src/Interpreters/Aggregator.cpp:1021: void DB::Aggregator::executeImpl<DB::AggregationMethodStringNoCache<StringHashMap<char*, Allocator<true, true>>>>(DB::AggregationMethodStringNoCache<StringHashMap<char*, Allocator<true, true>>>&, DB::Arena*, unsigned long, unsigned long, std::__1::vector<DB::IColumn const*, std::__1::allocator<DB::IColumn const*>>&, DB::Aggregator::AggregateFunctionInstruction*, bool, char*) const
2022.11.09 21:20:00.061964 [ 590431 ] {} <Fatal> BaseDaemon: 5. ./build_docker/../src/Interpreters/Aggregator.cpp:987: DB::Aggregator::executeImpl(DB::AggregatedDataVariants&, unsigned long, unsigned long, std::__1::vector<DB::IColumn const*, std::__1::allocator<DB::IColumn const*>>&, DB::Aggregator::AggregateFunctionInstruction*, bool, char*) const @ 0x40c03a53 in /home/ubuntu/memory-ch/clickhouse
2022.11.09 21:20:04.587204 [ 590431 ] {} <Fatal> BaseDaemon: 6. ./build_docker/../src/Interpreters/Aggregator.cpp:1532: DB::Aggregator::executeOnBlock(std::__1::vector<COW<DB::IColumn>::immutable_ptr<DB::IColumn>, std::__1::allocator<COW<DB::IColumn>::immutable_ptr<DB::IColumn>>>, unsigned long, unsigned long, DB::AggregatedDataVariants&, std::__1::vector<DB::IColumn const*, std::__1::allocator<DB::IColumn const*>>&, std::__1::vector<std::__1::vector<DB::IColumn const*, std::__1::allocator<DB::IColumn const*>>, std::__1::allocator<std::__1::vector<DB::IColumn const*, std::__1::allocator<DB::IColumn const*>>>>&, bool&) const @ 0x40c0f8af in /home/ubuntu/memory-ch/clickhouse
2022.11.09 21:20:05.030638 [ 590431 ] {} <Fatal> BaseDaemon: 7. ./build_docker/../src/Processors/Transforms/AggregatingTransform.cpp:533: DB::AggregatingTransform::consume(DB::Chunk) @ 0x488baa42 in /home/ubuntu/memory-ch/clickhouse
2022.11.09 21:20:05.460506 [ 590431 ] {} <Fatal> BaseDaemon: 8. ./build_docker/../src/Processors/Transforms/AggregatingTransform.cpp:492: DB::AggregatingTransform::work() @ 0x488b39da in /home/ubuntu/memory-ch/clickhouse
2022.11.09 21:20:05.505231 [ 590431 ] {} <Fatal> BaseDaemon: 9.1. inlined from ./build_docker/../src/Processors/Executors/ExecutionThreadContext.cpp:0: DB::executeJob(DB::ExecutingGraph::Node*, DB::ReadProgressCallback*)
2022.11.09 21:20:05.505347 [ 590431 ] {} <Fatal> BaseDaemon: 9. ./build_docker/../src/Processors/Executors/ExecutionThreadContext.cpp:92: DB::ExecutionThreadContext::executeTask() @ 0x480aef58 in /home/ubuntu/memory-ch/clickhouse
2022.11.09 21:20:05.667828 [ 590431 ] {} <Fatal> BaseDaemon: 10. ./build_docker/../src/Processors/Executors/PipelineExecutor.cpp:229: DB::PipelineExecutor::executeStepImpl(unsigned long, std::__1::atomic<bool>*) @ 0x48075a9d in /home/ubuntu/memory-ch/clickhouse
2022.11.09 21:20:05.857874 [ 590431 ] {} <Fatal> BaseDaemon: 11.1. inlined from ./build_docker/../src/Common/ThreadPool.h:197: operator()
2022.11.09 21:20:05.858104 [ 590431 ] {} <Fatal> BaseDaemon: 11.2. inlined from ./build_docker/../contrib/libcxx/include/__functional/invoke.h:394: decltype(std::declval<DB::PipelineExecutor::spawnThreads()::$_0>()()) std::__1::__invoke[abi:v15003]<ThreadFromGlobalPoolImpl<true>::ThreadFromGlobalPoolImpl<DB::PipelineExecutor::spawnThreads()::$_0>(DB::PipelineExecutor::spawnThreads()::$_0&&)::'lambda'()&>(DB::PipelineExecutor::spawnThreads()::$_0&&)
2022.11.09 21:20:05.858259 [ 590431 ] {} <Fatal> BaseDaemon: 11.3. inlined from ./build_docker/../contrib/libcxx/include/__functional/invoke.h:479: void std::__1::__invoke_void_return_wrapper<void, true>::__call<ThreadFromGlobalPoolImpl<true>::ThreadFromGlobalPoolImpl<DB::PipelineExecutor::spawnThreads()::$_0>(DB::PipelineExecutor::spawnThreads()::$_0&&)::'lambda'()&>(ThreadFromGlobalPoolImpl<true>::ThreadFromGlobalPoolImpl<DB::PipelineExecutor::spawnThreads()::$_0>(DB::PipelineExecutor::spawnThreads()::$_0&&)::'lambda'()&)
2022.11.09 21:20:05.858398 [ 590431 ] {} <Fatal> BaseDaemon: 11.4. inlined from ./build_docker/../contrib/libcxx/include/__functional/function.h:235: std::__1::__function::__default_alloc_func<ThreadFromGlobalPoolImpl<true>::ThreadFromGlobalPoolImpl<DB::PipelineExecutor::spawnThreads()::$_0>(DB::PipelineExecutor::spawnThreads()::$_0&&)::'lambda'(), void ()>::operator()[abi:v15003]()
2022.11.09 21:20:05.858464 [ 590431 ] {} <Fatal> BaseDaemon: 11. ./build_docker/../contrib/libcxx/include/__functional/function.h:716: void std::__1::__function::__policy_invoker<void ()>::__call_impl<std::__1::__function::__default_alloc_func<ThreadFromGlobalPoolImpl<true>::ThreadFromGlobalPoolImpl<DB::PipelineExecutor::spawnThreads()::$_0>(DB::PipelineExecutor::spawnThreads()::$_0&&)::'lambda'(), void ()>>(std::__1::__function::__policy_storage const*) @ 0x4807b892 in /home/ubuntu/memory-ch/clickhouse
2022.11.09 21:20:05.990310 [ 590431 ] {} <Fatal> BaseDaemon: 12. ./build_docker/../contrib/libcxx/include/__functional/function.h:0: ThreadPoolImpl<std::__1::thread>::worker(std::__1::__list_iterator<std::__1::thread, void*>) @ 0x27d5848d in /home/ubuntu/memory-ch/clickhouse
2022.11.09 21:20:06.153378 [ 590431 ] {} <Fatal> BaseDaemon: 13. ./build_docker/../src/Common/ThreadPool.cpp:0: void* std::__1::__thread_proxy[abi:v15003]<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct>>, void ThreadPoolImpl<std::__1::thread>::scheduleImpl<void>(std::__1::function<void ()>, long, std::__1::optional<unsigned long>, bool)::'lambda0'()>>(void*) @ 0x27d663a3 in /home/ubuntu/memory-ch/clickhouse
2022.11.09 21:20:06.153552 [ 590431 ] {} <Fatal> BaseDaemon: 14. ? @ 0x7f558c6fe609 in ?
2022.11.09 21:20:06.153625 [ 590431 ] {} <Fatal> BaseDaemon: 15. __clone @ 0x7f558c623163 in ?
2022.11.09 21:20:08.939618 [ 590431 ] {} <Fatal> BaseDaemon: Integrity check of the executable skipped because the reference checksum could not be read. (calculated checksum: 2FEF42EBB429C3871389025BFA50FD20)
```
{
        auto * function_node = node->as<FunctionNode>();
        if (!function_node || !function_node->isAggregateFunction() || (function_node->getFunctionName() != "count" && function_node->getFunctionName() != "sum"))
            return;
        if (function_node->getArguments().getNodes().size() != 1)
            return;
        auto & first_argument = function_node->getArguments().getNodes()[0];
        auto first_argument_constant_value = first_argument->getConstantValueOrNull();
        if (!first_argument_constant_value)
            return;
        const auto & first_argument_constant_literal = first_argument_constant_value->getValue();
        if (function_node->getFunctionName() == "count" && !first_argument_constant_literal.isNull())
        {
            function_node->getArguments().getNodes().clear();
        }
        else if (function_node->getFunctionName() == "sum" && first_argument_constant_literal.getType() == Field::Types::UInt64 &&
            first_argument_constant_literal.get<UInt64>() == 1)
        {
            auto result_type = function_node->getResultType();
            AggregateFunctionProperties properties;
            auto aggregate_function = AggregateFunctionFactory::instance().get("count", {}, {}, properties);
            function_node->resolveAsAggregateFunction(std::move(aggregate_function), std::move(result_type));
            function_node->getArguments().getNodes().clear();
        }
    }
};
}
void NormalizeCountVariantsPass::run(QueryTreeNodePtr query_tree_node, ContextPtr)