From https://clickhouse-test-reports.s3.yandex.net/15645/3564ba1c632f182ddfacd996dbbfef2d0db2ed45/fuzzer/report.html#fail1

```
2020.10.06 15:22:04.796774 [ 95 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Debug> executeQuery: (from [::1]:39472) SELECT quantilesDeterministic(0.5, 0.9)(number, number) FROM remote('127.0.0.{1,2}', numbers(1048575))
2020.10.06 15:22:04.797300 [ 95 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Trace> ContextAccess (default): Access granted: CREATE TEMPORARY TABLE, REMOTE ON *.*
2020.10.06 15:22:04.797882 [ 95 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Trace> ContextAccess (default): Access granted: CREATE TEMPORARY TABLE ON *.*
2020.10.06 15:22:04.802513 [ 95 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Trace> ContextAccess (default): Access granted: CREATE TEMPORARY TABLE ON *.*
2020.10.06 15:22:04.802993 [ 95 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Trace> ContextAccess (default): Access granted: CREATE TEMPORARY TABLE ON *.*
2020.10.06 15:22:04.804962 [ 95 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Trace> InterpreterSelectQuery: FetchColumns -> WithMergeableState
2020.10.06 15:22:04.807012 [ 95 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Trace> InterpreterSelectQuery: WithMergeableState -> Complete
2020.10.06 15:22:04.810364 [ 198 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Trace> Connection (127.0.0.2:9000): Connecting. Database: (not specified). User: default
2020.10.06 15:22:04.810758 [ 97 ] {} <Trace> TCPHandlerFactory: TCP Request. Address: [::ffff:127.0.0.1]:35640
2020.10.06 15:22:04.810910 [ 196 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Trace> AggregatingTransform: Aggregating
2020.10.06 15:22:04.810996 [ 196 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Trace> Aggregator: Aggregation method: without_key
2020.10.06 15:22:04.811072 [ 97 ] {} <Debug> TCPHandler: Connected ClickHouse server version 20.10.0, revision: 54441, user: default.
2020.10.06 15:22:04.811694 [ 198 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Trace> Connection (127.0.0.2:9000): Connected to ClickHouse server version 20.10.1.
2020.10.06 15:22:04.815063 [ 97 ] {8141e38d-9866-4703-87c1-37d707b9c557} <Debug> executeQuery: (from [::ffff:127.0.0.1]:35640, initial_query_id: 08e428a7-6547-42bf-a65d-d1a6b5fef24e) SELECT quantilesDeterministic(0.5, 0.9)(number, number) FROM numbers(1048575)
2020.10.06 15:22:04.815611 [ 97 ] {8141e38d-9866-4703-87c1-37d707b9c557} <Trace> ContextAccess (default): Access granted: CREATE TEMPORARY TABLE ON *.*
2020.10.06 15:22:04.817789 [ 97 ] {8141e38d-9866-4703-87c1-37d707b9c557} <Trace> InterpreterSelectQuery: FetchColumns -> WithMergeableState
2020.10.06 15:22:04.820745 [ 193 ] {8141e38d-9866-4703-87c1-37d707b9c557} <Trace> AggregatingTransform: Aggregating
2020.10.06 15:22:04.820838 [ 193 ] {8141e38d-9866-4703-87c1-37d707b9c557} <Trace> Aggregator: Aggregation method: without_key
2020.10.06 15:22:04.870011 [ 196 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Trace> AggregatingTransform: Aggregated. 1048575 to 1 rows (from 8.00 MiB) in 0.0641865 sec. (16336379.145147346 rows/sec., 124.64 MiB/sec.)
2020.10.06 15:22:04.870099 [ 196 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Trace> Aggregator: Merging aggregated data
2020.10.06 15:22:04.870891 [ 196 ] {08e428a7-6547-42bf-a65d-d1a6b5fef24e} <Trace> MergingAggregatedTransform: Reading blocks of partially aggregated data.
2020.10.06 15:22:04.879684 [ 193 ] {8141e38d-9866-4703-87c1-37d707b9c557} <Trace> AggregatingTransform: Aggregated. 1048575 to 1 rows (from 8.00 MiB) in 0.061079131 sec. (17167483.93162306 rows/sec., 130.98 MiB/sec.)
2020.10.06 15:22:04.879762 [ 193 ] {8141e38d-9866-4703-87c1-37d707b9c557} <Trace> Aggregator: Merging aggregated data
2020.10.06 15:22:04.880461 [ 193 ] {8141e38d-9866-4703-87c1-37d707b9c557} <Trace> PipelineExecutor: Thread finished. Total time: 0.060201491 sec. Execution time: 0.05824177 sec. Processing time: 0.001842921 sec. Wait time: 0.0001168 sec.
clickhouse-server: ../src/Common/PODArray.h:376: const T &DB::PODArray<std::__1::pair<unsigned long, unsigned int>, 64, Allocator<false, false>, 0, 0>::operator[](ssize_t) const [T = std::__1::pair<unsigned long, unsigned int>, initial_bytes = 64, TAllocator = Allocator<false, false>, pad_right_ = 0, pad_left_ = 0]: Assertion `(n >= (static_cast<ssize_t>(pad_left_) ? -1 : 0)) && (n <= static_cast<ssize_t>(this->size()))' failed.
2020.10.06 15:22:04.881139 [ 64 ] {} <Trace> BaseDaemon: Received signal 6
2020.10.06 15:22:04.881546 [ 199 ] {} <Fatal> BaseDaemon: ########################################
2020.10.06 15:22:04.882112 [ 199 ] {} <Fatal> BaseDaemon: (version 20.10.1.4848, build id: 628FF441861A815F) (from thread 97) (query_id: 8141e38d-9866-4703-87c1-37d707b9c557) Received signal Aborted (6)
2020.10.06 15:22:04.882301 [ 199 ] {} <Fatal> BaseDaemon: 
2020.10.06 15:22:04.882496 [ 199 ] {} <Fatal> BaseDaemon: Stack trace: 0x7fc6da5e8f47 0x7fc6da5ea8b1 0x7fc6da5da42a 0x7fc6da5da4a2 0x10435c26 0x10435b44 0x10435aad 0x1045b6d9 0x17f58cf2 0xfd38144 0x1897f674 0x1897fab4 0x195772ec 0x1957608c 0x195723e9 0x1957ac88 0x1d22e54c 0x1d22ed5c 0x1d371093 0x1d36dfcd 0x1d36ce58 0x7fc6dadae6db 0x7fc6da6cba3f
2020.10.06 15:22:04.882980 [ 199 ] {} <Fatal> BaseDaemon: 4. /build/glibc-2ORdQG/glibc-2.27/signal/../sysdeps/unix/sysv/linux/raise.c:51: raise @ 0x3ef47 in /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.27.so
2020.10.06 15:22:04.883276 [ 199 ] {} <Fatal> BaseDaemon: 5. /build/glibc-2ORdQG/glibc-2.27/stdlib/abort.c:81: __GI_abort @ 0x408b1 in /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.27.so
2020.10.06 15:22:04.883553 [ 199 ] {} <Fatal> BaseDaemon: 6. /build/glibc-2ORdQG/glibc-2.27/assert/assert.c:89: __assert_fail_base @ 0x3042a in /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.27.so
2020.10.06 15:22:04.883997 [ 199 ] {} <Fatal> BaseDaemon: 7. ? @ 0x304a2 in /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.27.so
2020.10.06 15:22:04.893888 [ 199 ] {} <Fatal> BaseDaemon: 8. /build/obj-x86_64-linux-gnu/../src/Common/PODArray.h:0: DB::PODArray<std::__1::pair<unsigned long, unsigned int>, 64ul, Allocator<false, false>, 0ul, 0ul>::operator[](long) const @ 0x10435c26 in /workspace/clickhouse
2020.10.06 15:22:04.903678 [ 199 ] {} <Fatal> BaseDaemon: 9. /build/obj-x86_64-linux-gnu/../src/AggregateFunctions/ReservoirSamplerDeterministic.h:169: ReservoirSamplerDeterministic<unsigned long, (ReservoirSamplerDeterministicOnEmpty)1>::write(DB::WriteBuffer&) const @ 0x10435b44 in /workspace/clickhouse
2020.10.06 15:22:04.913810 [ 199 ] {} <Fatal> BaseDaemon: 10. /build/obj-x86_64-linux-gnu/../src/AggregateFunctions/QuantileReservoirSamplerDeterministic.h:47: DB::QuantileReservoirSamplerDeterministic<unsigned long>::serialize(DB::WriteBuffer&) const @ 0x10435aad in /workspace/clickhouse
2020.10.06 15:22:04.925088 [ 199 ] {} <Fatal> BaseDaemon: 11. /build/obj-x86_64-linux-gnu/../src/AggregateFunctions/AggregateFunctionQuantile.h:134: DB::AggregateFunctionQuantile<unsigned long, DB::QuantileReservoirSamplerDeterministic<unsigned long>, DB::NameQuantilesDeterministic, true, double, true>::serialize(char const*, DB::WriteBuffer&) const @ 0x1045b6d9 in /workspace/clickhouse
2020.10.06 15:22:04.958822 [ 199 ] {} <Fatal> BaseDaemon: 12. /build/obj-x86_64-linux-gnu/../src/DataTypes/DataTypeAggregateFunction.cpp:118: DB::DataTypeAggregateFunction::serializeBinaryBulk(DB::IColumn const&, DB::WriteBuffer&, unsigned long, unsigned long) const @ 0x17f58cf2 in /workspace/clickhouse
2020.10.06 15:22:04.960514 [ 199 ] {} <Fatal> BaseDaemon: 13. /build/obj-x86_64-linux-gnu/../src/DataTypes/IDataType.h:185: DB::IDataType::serializeBinaryBulkWithMultipleStreams(DB::IColumn const&, unsigned long, unsigned long, DB::IDataType::SerializeBinaryBulkSettings&, std::__1::shared_ptr<DB::IDataType::SerializeBinaryBulkState>&) const @ 0xfd38144 in /workspace/clickhouse
2020.10.06 15:22:04.997478 [ 199 ] {} <Fatal> BaseDaemon: 14. /build/obj-x86_64-linux-gnu/../src/DataStreams/NativeBlockOutputStream.cpp:58: DB::NativeBlockOutputStream::writeData(DB::IDataType const&, COW<DB::IColumn>::immutable_ptr<DB::IColumn> const&, DB::WriteBuffer&, unsigned long, unsigned long) @ 0x1897f674 in /workspace/clickhouse
2020.10.06 15:22:05.005873 [ 188 ] {} <Debug> system.metric_log (20fa8402-c2d9-4e7b-ad54-6839342b3d0c) (MergerMutator): Merge sorted 2262 rows, containing 264 columns (264 merged, 0 gathered) in 0.513521944 sec., 4404.8750524281395 rows/sec., 8.83 MiB/sec.
2020.10.06 15:22:05.034728 [ 199 ] {} <Fatal> BaseDaemon: 15. /build/obj-x86_64-linux-gnu/../src/DataStreams/NativeBlockOutputStream.cpp:124: DB::NativeBlockOutputStream::write(DB::Block const&) @ 0x1897fab4 in /workspace/clickhouse
2020.10.06 15:22:05.055018 [ 188 ] {} <Trace> system.metric_log (20fa8402-c2d9-4e7b-ad54-6839342b3d0c): Renaming temporary part tmp_merge_202010_1_296_59 to 202010_1_296_59.
2020.10.06 15:22:05.056352 [ 188 ] {} <Trace> system.metric_log (20fa8402-c2d9-4e7b-ad54-6839342b3d0c) (MergerMutator): Merged 6 parts: from 202010_1_291_58 to 202010_296_296_0
2020.10.06 15:22:05.056483 [ 188 ] {} <Debug> MemoryTracker: Peak memory usage: 16.36 MiB.
2020.10.06 15:22:05.080312 [ 199 ] {} <Fatal> BaseDaemon: 16. /build/obj-x86_64-linux-gnu/../src/Server/TCPHandler.cpp:1216: DB::TCPHandler::sendData(DB::Block const&) @ 0x195772ec in /workspace/clickhouse
2020.10.06 15:22:05.125986 [ 199 ] {} <Fatal> BaseDaemon: 17. /build/obj-x86_64-linux-gnu/../src/Server/TCPHandler.cpp:618: DB::TCPHandler::processOrdinaryQueryWithProcessors() @ 0x1957608c in /workspace/clickhouse
2020.10.06 15:22:05.170191 [ 199 ] {} <Fatal> BaseDaemon: 18. /build/obj-x86_64-linux-gnu/../src/Server/TCPHandler.cpp:273: DB::TCPHandler::runImpl() @ 0x195723e9 in /workspace/clickhouse
2020.10.06 15:22:05.216264 [ 199 ] {} <Fatal> BaseDaemon: 19. /build/obj-x86_64-linux-gnu/../src/Server/TCPHandler.cpp:1311: DB::TCPHandler::run() @ 0x1957ac88 in /workspace/clickhouse
2020.10.06 15:22:05.267976 [ 199 ] {} <Fatal> BaseDaemon: 20. /build/obj-x86_64-linux-gnu/../contrib/poco/Net/src/TCPServerConnection.cpp:43: Poco::Net::TCPServerConnection::start() @ 0x1d22e54c in /workspace/clickhouse
2020.10.06 15:22:05.320522 [ 199 ] {} <Fatal> BaseDaemon: 21. /build/obj-x86_64-linux-gnu/../contrib/poco/Net/src/TCPServerDispatcher.cpp:114: Poco::Net::TCPServerDispatcher::run() @ 0x1d22ed5c in /workspace/clickhouse
2020.10.06 15:22:05.371826 [ 199 ] {} <Fatal> BaseDaemon: 22. /build/obj-x86_64-linux-gnu/../contrib/poco/Foundation/src/ThreadPool.cpp:199: Poco::PooledThread::run() @ 0x1d371093 in /workspace/clickhouse
2020.10.06 15:22:05.416179 [ 68 ] {} <Trace> SystemLog (system.query_thread_log): Flushing system log, 313 entries to flush
2020.10.06 15:22:05.424861 [ 199 ] {} <Fatal> BaseDaemon: 23. /build/obj-x86_64-linux-gnu/../contrib/poco/Foundation/src/Thread.cpp:56: Poco::(anonymous namespace)::RunnableHolder::run() @ 0x1d36dfcd in /workspace/clickhouse
2020.10.06 15:22:05.433571 [ 68 ] {} <Debug> DiskLocal: Reserving 1.00 MiB on disk `default`, having unreserved 1.08 TiB.
2020.10.06 15:22:05.445462 [ 68 ] {} <Trace> system.query_thread_log (641f7e7c-9710-444c-a6fd-f97afd7e6d51): Renaming temporary part tmp_insert_202010_283_283_0 to 202010_283_283_0.
2020.10.06 15:22:05.447326 [ 68 ] {} <Trace> SystemLog (system.query_thread_log): Flushed system log
2020.10.06 15:22:05.477696 [ 199 ] {} <Fatal> BaseDaemon: 24. /build/obj-x86_64-linux-gnu/../contrib/poco/Foundation/src/Thread_POSIX.cpp:345: Poco::ThreadImpl::runnableEntry(void*) @ 0x1d36ce58 in /workspace/clickhouse
2020.10.06 15:22:05.478044 [ 199 ] {} <Fatal> BaseDaemon: 25. start_thread @ 0x76db in /lib/x86_64-linux-gnu/libpthread-2.27.so
2020.10.06 15:22:05.478459 [ 199 ] {} <Fatal> BaseDaemon: 26. /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
2020.10.06 15:22:06.064988 [ 199 ] {} <Information> SentryWriter: Not sending crash report
```
#pragma once
#include <limits>
#include <algorithm>
#include <climits>
#include <sstream>
#include <AggregateFunctions/ReservoirSampler.h>
#include <common/types.h>
#include <Common/HashTable/Hash.h>
#include <IO/ReadBuffer.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
#include <Common/PODArray.h>
#include <Common/NaNUtils.h>