``` sql
SELECT length(topKWeighted(2, -9223372036854775808)(number, 1025))
FROM system.numbers
```
https://clickhouse-test-reports.s3.yandex.net/13647/216436a221fd0d7fc0ad62eea8056e279771c439/fuzzer/report.html#fail1
```
2020.09.03 20:38:22.984277 [ 19670 ] <Fatal> BaseDaemon: ########################################
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:22.984734 [ 19670 ] <Fatal> BaseDaemon: (version 20.8.1.1, build id: 8B84E4174FE44D74) (from thread 19572) (query_id: 8eec1b47-8321-43b6-9cb5-e5187113340d) Received signal Segmentation fault (11)
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:22.984949 [ 19670 ] <Fatal> BaseDaemon: Address: 0xffffbffffffffff8 Access: read. Address not mapped to object.
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:22.985161 [ 19670 ] <Fatal> BaseDaemon: Stack trace: 0x115c4e83 0x116ccd1b 0x116ce9cc 0x3a7f38bc 0x3a7fa07a 0x3d6470fa 0x3d63de0a 0x3d025646 0x3d020b96 0x3d01c3d3 0x3d01ad3e 0x3d04d388 0x10e7476a 0x10e8159c 0x7f6a7f1756db 0x7f6a7ea92a3f
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:22.990571 [ 19670 ] <Fatal> BaseDaemon: 4. /build/obj-x86_64-linux-gnu/../src/Common/SpaceSaving.h:0: DB::SpaceSaving<unsigned long, HashCRC32<unsigned long> >::insert(unsigned long const&, unsigned long, unsigned long) @ 0x115c4e83 in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.002762 [ 19670 ] <Fatal> BaseDaemon: 5. /build/obj-x86_64-linux-gnu/../src/AggregateFunctions/AggregateFunctionTopK.h:63: DB::AggregateFunctionTopK<unsigned long, true>::add(char*, DB::IColumn const**, unsigned long, DB::Arena*) const @ 0x116ccd1b in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.015047 [ 19670 ] <Fatal> BaseDaemon: 6. /build/obj-x86_64-linux-gnu/../src/AggregateFunctions/IAggregateFunction.h:229: DB::IAggregateFunctionHelper<DB::AggregateFunctionTopK<unsigned long, true> >::addBatchSinglePlace(unsigned long, char*, DB::IColumn const**, DB::Arena*) const @ 0x116ce9cc in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.018350 [ 19670 ] <Fatal> BaseDaemon: 7. /build/obj-x86_64-linux-gnu/../src/Interpreters/Aggregator.cpp:615: DB::Aggregator::executeWithoutKeyImpl(char*&, unsigned long, DB::Aggregator::AggregateFunctionInstruction*, DB::Arena*) @ 0x3a7f38bc in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.021832 [ 19670 ] <Fatal> BaseDaemon: 8. /build/obj-x86_64-linux-gnu/../src/Interpreters/Aggregator.cpp:782: 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, 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&) @ 0x3a7fa07a in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.025868 [ 19670 ] <Fatal> BaseDaemon: 9. /build/obj-x86_64-linux-gnu/../src/Processors/Transforms/AggregatingTransform.cpp:525: DB::AggregatingTransform::consume(DB::Chunk) @ 0x3d6470fa in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.029623 [ 19670 ] <Fatal> BaseDaemon: 10. /build/obj-x86_64-linux-gnu/../src/Processors/Transforms/AggregatingTransform.cpp:495: DB::AggregatingTransform::work() @ 0x3d63de0a in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.033383 [ 19670 ] <Fatal> BaseDaemon: 11. /build/obj-x86_64-linux-gnu/../src/Processors/Executors/PipelineExecutor.cpp:0: std::__1::__function::__func<DB::PipelineExecutor::addJob(DB::ExecutingGraph::Node*)::$_0, std::__1::allocator<DB::PipelineExecutor::addJob(DB::ExecutingGraph::Node*)::$_0>, void ()>::operator()() @ 0x3d025646 in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.037179 [ 19670 ] <Fatal> BaseDaemon: 12. /build/obj-x86_64-linux-gnu/../contrib/libcxx/include/functional:0: DB::PipelineExecutor::executeStepImpl(unsigned long, unsigned long, std::__1::atomic<bool>*) @ 0x3d020b96 in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.040819 [ 19670 ] <Fatal> BaseDaemon: 13. /build/obj-x86_64-linux-gnu/../src/Processors/Executors/PipelineExecutor.cpp:0: DB::PipelineExecutor::executeImpl(unsigned long) @ 0x3d01c3d3 in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.044565 [ 19670 ] <Fatal> BaseDaemon: 14. /build/obj-x86_64-linux-gnu/../contrib/libcxx/include/memory:2587: DB::PipelineExecutor::execute(unsigned long) @ 0x3d01ad3e in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.048216 [ 19670 ] <Fatal> BaseDaemon: 15. /build/obj-x86_64-linux-gnu/../contrib/libcxx/include/atomic:957: std::__1::__function::__func<ThreadFromGlobalPool::ThreadFromGlobalPool<DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long)::$_0>(DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long)::$_0&&)::'lambda'(), std::__1::allocator<ThreadFromGlobalPool::ThreadFromGlobalPool<DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long)::$_0>(DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long)::$_0&&)::'lambda'()>, void ()>::operator()() @ 0x3d04d388 in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.048617 [ 19670 ] <Fatal> BaseDaemon: 16. /build/obj-x86_64-linux-gnu/../src/Common/CurrentMetrics.h:74: ThreadPoolImpl<std::__1::thread>::worker(std::__1::__list_iterator<std::__1::thread, void*>) @ 0x10e7476a in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.049311 [ 19670 ] <Fatal> BaseDaemon: 17. /build/obj-x86_64-linux-gnu/../contrib/libcxx/include/memory:2615: void* std::__1::__thread_proxy<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 ()>, int, std::__1::optional<unsigned long>)::'lambda1'()> >(void*) @ 0x10e8159c in /home/qoega/memory-ch/output/binary/clickhouse
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.049446 [ 19670 ] <Fatal> BaseDaemon: 18. start_thread @ 0x76db in /lib/x86_64-linux-gnu/libpthread-2.27.so
[qoega-qyp.sas.yp-c.yandex.net] 2020.09.03 20:38:23.049782 [ 19670 ] <Fatal> BaseDaemon: 19. /build/glibc-2ORdQG/glibc-2.27/misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:97: __clone @ 0x121a3f in /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.27.so
```
#include <AggregateFunctions/AggregateFunctionFactory.h>
#include <AggregateFunctions/AggregateFunctionTopK.h>
#include <AggregateFunctions/Helpers.h>
#include <AggregateFunctions/FactoryHelpers.h>
#include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypeDateTime.h>
#include "registerAggregateFunctions.h"
#define TOP_K_MAX_SIZE 0xFFFFFF
namespace DB
{
namespace ErrorCodes
{
    extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
    extern const int ARGUMENT_OUT_OF_BOUND;
    extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
namespace
{
template <bool is_weighted>
class AggregateFunctionTopKDate : public AggregateFunctionTopK<DataTypeDate::FieldType, is_weighted>