2012-06-15 19:32:25 +00:00
|
|
|
#pragma once
|
2010-03-01 16:59:51 +00:00
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
#include <iostream>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2023-03-15 07:33:45 +00:00
|
|
|
#include <base/defines.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
#include <IO/WriteBuffer.h>
|
2010-03-01 16:59:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ATTEMPT_TO_READ_AFTER_EOF;
|
2023-04-11 07:47:33 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2020-02-25 18:10:48 +00:00
|
|
|
}
|
2010-03-01 16:59:51 +00:00
|
|
|
|
|
|
|
|
2023-04-05 07:50:26 +00:00
|
|
|
/// Variable-Length Quantity (VLQ) Base-128 compression, also known as Variable Byte (VB) or Varint encoding.
|
2023-03-15 07:33:45 +00:00
|
|
|
|
2023-04-05 07:50:26 +00:00
|
|
|
/// Write UInt64 in variable length format (base128)
|
2010-03-01 16:59:51 +00:00
|
|
|
void writeVarUInt(UInt64 x, std::ostream & ostr);
|
2010-06-01 14:12:28 +00:00
|
|
|
void writeVarUInt(UInt64 x, WriteBuffer & ostr);
|
2011-03-17 19:45:21 +00:00
|
|
|
char * writeVarUInt(UInt64 x, char * ostr);
|
2010-03-01 16:59:51 +00:00
|
|
|
|
2023-04-05 07:50:26 +00:00
|
|
|
/// NOTE: Due to historical reasons, only values up to 1<<63-1 can be safely encoded/decoded (bigger values are not idempotent under
|
|
|
|
/// encoding/decoding). This cannot be changed without breaking backward compatibility (some drivers, e.g. clickhouse-rs (Rust), have the
|
|
|
|
/// same limitation, others support the full 1<<64 range, e.g. clickhouse-driver (Python))
|
|
|
|
constexpr UInt64 VAR_UINT_MAX = (1ULL<<63) - 1;
|
2010-03-01 16:59:51 +00:00
|
|
|
|
2023-04-05 07:50:26 +00:00
|
|
|
/// Read UInt64, written in variable length format (base128)
|
2010-03-01 16:59:51 +00:00
|
|
|
void readVarUInt(UInt64 & x, std::istream & istr);
|
2010-06-01 14:12:28 +00:00
|
|
|
void readVarUInt(UInt64 & x, ReadBuffer & istr);
|
2011-03-22 20:36:01 +00:00
|
|
|
const char * readVarUInt(UInt64 & x, const char * istr, size_t size);
|
2010-03-01 16:59:51 +00:00
|
|
|
|
|
|
|
|
2023-04-05 07:50:26 +00:00
|
|
|
/// Get the length of UInt64 in VarUInt format
|
2010-03-01 16:59:51 +00:00
|
|
|
size_t getLengthOfVarUInt(UInt64 x);
|
|
|
|
|
2023-04-05 07:50:26 +00:00
|
|
|
/// Get the Int64 length in VarInt format
|
2010-07-06 18:50:24 +00:00
|
|
|
size_t getLengthOfVarInt(Int64 x);
|
|
|
|
|
2010-03-01 16:59:51 +00:00
|
|
|
|
2023-04-05 07:50:26 +00:00
|
|
|
/// Write Int64 in variable length format (base128)
|
2010-06-01 14:12:28 +00:00
|
|
|
template <typename OUT>
|
|
|
|
inline void writeVarInt(Int64 x, OUT & ostr)
|
2010-03-01 16:59:51 +00:00
|
|
|
{
|
|
|
|
writeVarUInt(static_cast<UInt64>((x << 1) ^ (x >> 63)), ostr);
|
|
|
|
}
|
|
|
|
|
2011-03-22 20:36:01 +00:00
|
|
|
inline char * writeVarInt(Int64 x, char * ostr)
|
|
|
|
{
|
|
|
|
return writeVarUInt(static_cast<UInt64>((x << 1) ^ (x >> 63)), ostr);
|
|
|
|
}
|
|
|
|
|
2010-03-01 16:59:51 +00:00
|
|
|
|
2023-04-05 07:50:26 +00:00
|
|
|
/// Read Int64, written in variable length format (base128)
|
2010-06-01 14:12:28 +00:00
|
|
|
template <typename IN>
|
|
|
|
inline void readVarInt(Int64 & x, IN & istr)
|
2010-03-01 16:59:51 +00:00
|
|
|
{
|
|
|
|
readVarUInt(*reinterpret_cast<UInt64*>(&x), istr);
|
|
|
|
x = (static_cast<UInt64>(x) >> 1) ^ -(x & 1);
|
|
|
|
}
|
|
|
|
|
2011-03-22 20:36:01 +00:00
|
|
|
inline const char * readVarInt(Int64 & x, const char * istr, size_t size)
|
|
|
|
{
|
|
|
|
const char * res = readVarUInt(*reinterpret_cast<UInt64*>(&x), istr, size);
|
|
|
|
x = (static_cast<UInt64>(x) >> 1) ^ -(x & 1);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-03-01 16:59:51 +00:00
|
|
|
|
2010-06-01 14:12:28 +00:00
|
|
|
inline void writeVarT(UInt64 x, std::ostream & ostr) { writeVarUInt(x, ostr); }
|
|
|
|
inline void writeVarT(Int64 x, std::ostream & ostr) { writeVarInt(x, ostr); }
|
|
|
|
inline void writeVarT(UInt64 x, WriteBuffer & ostr) { writeVarUInt(x, ostr); }
|
|
|
|
inline void writeVarT(Int64 x, WriteBuffer & ostr) { writeVarInt(x, ostr); }
|
2011-03-22 20:36:01 +00:00
|
|
|
inline char * writeVarT(UInt64 x, char * & ostr) { return writeVarUInt(x, ostr); }
|
|
|
|
inline char * writeVarT(Int64 x, char * & ostr) { return writeVarInt(x, ostr); }
|
2010-05-18 18:51:51 +00:00
|
|
|
|
2010-06-01 14:12:28 +00:00
|
|
|
inline void readVarT(UInt64 & x, std::istream & istr) { readVarUInt(x, istr); }
|
|
|
|
inline void readVarT(Int64 & x, std::istream & istr) { readVarInt(x, istr); }
|
|
|
|
inline void readVarT(UInt64 & x, ReadBuffer & istr) { readVarUInt(x, istr); }
|
|
|
|
inline void readVarT(Int64 & x, ReadBuffer & istr) { readVarInt(x, istr); }
|
2011-03-22 20:36:01 +00:00
|
|
|
inline const char * readVarT(UInt64 & x, const char * istr, size_t size) { return readVarUInt(x, istr, size); }
|
|
|
|
inline const char * readVarT(Int64 & x, const char * istr, size_t size) { return readVarInt(x, istr, size); }
|
2010-05-18 18:51:51 +00:00
|
|
|
|
2012-06-15 19:32:25 +00:00
|
|
|
|
2017-10-14 01:03:35 +00:00
|
|
|
/// For [U]Int32, [U]Int16, size_t.
|
2012-06-15 19:32:25 +00:00
|
|
|
|
|
|
|
inline void readVarUInt(UInt32 & x, ReadBuffer & istr)
|
|
|
|
{
|
|
|
|
UInt64 tmp;
|
|
|
|
readVarUInt(tmp, istr);
|
2022-10-07 10:46:45 +00:00
|
|
|
x = static_cast<UInt32>(tmp);
|
2010-03-01 16:59:51 +00:00
|
|
|
}
|
|
|
|
|
2012-06-15 19:32:25 +00:00
|
|
|
inline void readVarInt(Int32 & x, ReadBuffer & istr)
|
|
|
|
{
|
|
|
|
Int64 tmp;
|
|
|
|
readVarInt(tmp, istr);
|
2022-10-07 10:46:45 +00:00
|
|
|
x = static_cast<Int32>(tmp);
|
2012-06-15 19:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void readVarUInt(UInt16 & x, ReadBuffer & istr)
|
|
|
|
{
|
|
|
|
UInt64 tmp;
|
|
|
|
readVarUInt(tmp, istr);
|
|
|
|
x = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void readVarInt(Int16 & x, ReadBuffer & istr)
|
|
|
|
{
|
|
|
|
Int64 tmp;
|
|
|
|
readVarInt(tmp, istr);
|
|
|
|
x = tmp;
|
|
|
|
}
|
|
|
|
|
2017-10-14 04:39:14 +00:00
|
|
|
template <typename T>
|
2022-03-15 19:37:28 +00:00
|
|
|
requires (!std::is_same_v<T, UInt64>)
|
|
|
|
inline void readVarUInt(T & x, ReadBuffer & istr)
|
2017-10-14 01:03:35 +00:00
|
|
|
{
|
|
|
|
UInt64 tmp;
|
|
|
|
readVarUInt(tmp, istr);
|
|
|
|
x = tmp;
|
|
|
|
}
|
|
|
|
|
2014-11-15 18:57:49 +00:00
|
|
|
|
2019-04-10 20:27:14 +00:00
|
|
|
[[noreturn]] inline void throwReadAfterEOF()
|
2014-11-15 18:57:49 +00:00
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ATTEMPT_TO_READ_AFTER_EOF, "Attempt to read after eof");
|
2014-11-15 18:57:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 23:46:18 +00:00
|
|
|
template <bool fast>
|
2018-08-25 18:23:26 +00:00
|
|
|
inline void readVarUIntImpl(UInt64 & x, ReadBuffer & istr)
|
2014-11-15 18:57:49 +00:00
|
|
|
{
|
2016-04-15 02:28:56 +00:00
|
|
|
x = 0;
|
|
|
|
for (size_t i = 0; i < 9; ++i)
|
2014-11-15 18:57:49 +00:00
|
|
|
{
|
2018-08-25 18:23:26 +00:00
|
|
|
if constexpr (!fast)
|
|
|
|
if (istr.eof())
|
|
|
|
throwReadAfterEOF();
|
2016-04-15 02:28:56 +00:00
|
|
|
|
2022-03-13 11:59:20 +00:00
|
|
|
UInt64 byte = *istr.position(); /// NOLINT
|
2016-04-15 02:28:56 +00:00
|
|
|
++istr.position();
|
2016-08-04 05:19:37 +00:00
|
|
|
x |= (byte & 0x7F) << (7 * i);
|
2016-04-15 02:28:56 +00:00
|
|
|
|
|
|
|
if (!(byte & 0x80))
|
|
|
|
return;
|
2014-11-15 18:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 18:23:26 +00:00
|
|
|
inline void readVarUInt(UInt64 & x, ReadBuffer & istr)
|
|
|
|
{
|
|
|
|
if (istr.buffer().end() - istr.position() >= 9)
|
|
|
|
return readVarUIntImpl<true>(x, istr);
|
|
|
|
return readVarUIntImpl<false>(x, istr);
|
|
|
|
}
|
|
|
|
|
2014-11-15 18:57:49 +00:00
|
|
|
|
|
|
|
inline void readVarUInt(UInt64 & x, std::istream & istr)
|
|
|
|
{
|
2016-08-04 05:19:37 +00:00
|
|
|
x = 0;
|
|
|
|
for (size_t i = 0; i < 9; ++i)
|
2014-11-15 18:57:49 +00:00
|
|
|
{
|
2016-10-26 22:27:38 +00:00
|
|
|
UInt64 byte = istr.get();
|
2016-08-04 05:19:37 +00:00
|
|
|
x |= (byte & 0x7F) << (7 * i);
|
|
|
|
|
|
|
|
if (!(byte & 0x80))
|
|
|
|
return;
|
2014-11-15 18:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 05:32:58 +00:00
|
|
|
inline const char * readVarUInt(UInt64 & x, const char * istr, size_t size)
|
|
|
|
{
|
|
|
|
const char * end = istr + size;
|
|
|
|
|
|
|
|
x = 0;
|
|
|
|
for (size_t i = 0; i < 9; ++i)
|
|
|
|
{
|
|
|
|
if (istr == end)
|
|
|
|
throwReadAfterEOF();
|
|
|
|
|
2022-03-13 11:59:20 +00:00
|
|
|
UInt64 byte = *istr; /// NOLINT
|
2016-08-04 05:32:58 +00:00
|
|
|
++istr;
|
|
|
|
x |= (byte & 0x7F) << (7 * i);
|
|
|
|
|
|
|
|
if (!(byte & 0x80))
|
|
|
|
return istr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return istr;
|
|
|
|
}
|
|
|
|
|
2023-04-11 07:47:33 +00:00
|
|
|
[[noreturn]] inline void throwValueTooLargeForVarIntEncodingException(UInt64 x)
|
|
|
|
{
|
2023-04-11 11:59:04 +00:00
|
|
|
/// Under practical circumstances, we should virtually never end up here but AST Fuzzer manages to create superlarge input integers
|
|
|
|
/// which trigger this exception. Intentionally not throwing LOGICAL_ERROR or calling abort() or [ch]assert(false), so AST Fuzzer
|
|
|
|
/// can swallow the exception and continue to run.
|
2023-04-11 07:47:33 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Value {} is too large for VarInt encoding", x);
|
|
|
|
}
|
2014-11-15 18:57:49 +00:00
|
|
|
|
|
|
|
inline void writeVarUInt(UInt64 x, WriteBuffer & ostr)
|
|
|
|
{
|
2023-04-11 07:47:33 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (x > VAR_UINT_MAX)
|
|
|
|
throwValueTooLargeForVarIntEncodingException(x);
|
|
|
|
#endif
|
2016-08-04 05:19:37 +00:00
|
|
|
for (size_t i = 0; i < 9; ++i)
|
2014-11-15 18:57:49 +00:00
|
|
|
{
|
2016-08-04 05:19:37 +00:00
|
|
|
uint8_t byte = x & 0x7F;
|
2016-08-04 05:32:58 +00:00
|
|
|
if (x > 0x7F)
|
|
|
|
byte |= 0x80;
|
2016-08-04 05:19:37 +00:00
|
|
|
|
|
|
|
ostr.nextIfAtEnd();
|
2016-08-04 05:32:58 +00:00
|
|
|
*ostr.position() = byte;
|
2016-08-04 05:19:37 +00:00
|
|
|
++ostr.position();
|
|
|
|
|
|
|
|
x >>= 7;
|
|
|
|
if (!x)
|
|
|
|
return;
|
2014-11-15 18:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-04 05:32:58 +00:00
|
|
|
inline void writeVarUInt(UInt64 x, std::ostream & ostr)
|
2014-11-15 18:57:49 +00:00
|
|
|
{
|
2023-04-11 07:47:33 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (x > VAR_UINT_MAX)
|
|
|
|
throwValueTooLargeForVarIntEncodingException(x);
|
|
|
|
#endif
|
2016-08-04 05:19:37 +00:00
|
|
|
for (size_t i = 0; i < 9; ++i)
|
2014-11-15 18:57:49 +00:00
|
|
|
{
|
2016-08-04 05:19:37 +00:00
|
|
|
uint8_t byte = x & 0x7F;
|
2016-08-04 05:32:58 +00:00
|
|
|
if (x > 0x7F)
|
|
|
|
byte |= 0x80;
|
2016-08-04 05:19:37 +00:00
|
|
|
|
2016-08-04 05:32:58 +00:00
|
|
|
ostr.put(byte);
|
2016-08-04 05:19:37 +00:00
|
|
|
|
|
|
|
x >>= 7;
|
|
|
|
if (!x)
|
2016-08-04 05:32:58 +00:00
|
|
|
return;
|
2014-11-15 18:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-04 05:32:58 +00:00
|
|
|
inline char * writeVarUInt(UInt64 x, char * ostr)
|
2014-11-15 18:57:49 +00:00
|
|
|
{
|
2023-04-11 07:47:33 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (x > VAR_UINT_MAX)
|
|
|
|
throwValueTooLargeForVarIntEncodingException(x);
|
|
|
|
#endif
|
2016-08-04 05:19:37 +00:00
|
|
|
for (size_t i = 0; i < 9; ++i)
|
2014-11-15 18:57:49 +00:00
|
|
|
{
|
2016-08-04 05:32:58 +00:00
|
|
|
uint8_t byte = x & 0x7F;
|
|
|
|
if (x > 0x7F)
|
|
|
|
byte |= 0x80;
|
2016-08-04 05:19:37 +00:00
|
|
|
|
2016-08-04 05:32:58 +00:00
|
|
|
*ostr = byte;
|
|
|
|
++ostr;
|
2016-08-04 05:19:37 +00:00
|
|
|
|
2016-08-04 05:32:58 +00:00
|
|
|
x >>= 7;
|
|
|
|
if (!x)
|
|
|
|
return ostr;
|
2014-11-15 18:57:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 05:32:58 +00:00
|
|
|
return ostr;
|
2014-11-15 18:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline size_t getLengthOfVarUInt(UInt64 x)
|
|
|
|
{
|
|
|
|
return x < (1ULL << 7) ? 1
|
|
|
|
: (x < (1ULL << 14) ? 2
|
|
|
|
: (x < (1ULL << 21) ? 3
|
|
|
|
: (x < (1ULL << 28) ? 4
|
|
|
|
: (x < (1ULL << 35) ? 5
|
|
|
|
: (x < (1ULL << 42) ? 6
|
|
|
|
: (x < (1ULL << 49) ? 7
|
|
|
|
: (x < (1ULL << 56) ? 8
|
|
|
|
: 9)))))));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline size_t getLengthOfVarInt(Int64 x)
|
|
|
|
{
|
|
|
|
return getLengthOfVarUInt(static_cast<UInt64>((x << 1) ^ (x >> 63)));
|
|
|
|
}
|
|
|
|
|
2012-06-15 19:32:25 +00:00
|
|
|
}
|