2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2020-07-28 12:37:44 +00:00
|
|
|
#include <IO/ReadBufferFromString.h>
|
|
|
|
#include <IO/readDecimalText.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Field.h>
|
2018-09-02 01:12:32 +00:00
|
|
|
#include <Core/DecimalComparison.h>
|
2021-06-14 04:13:35 +00:00
|
|
|
#include <Common/FieldVisitorDump.h>
|
|
|
|
#include <Common/FieldVisitorToString.h>
|
|
|
|
#include <Common/FieldVisitorWriteBinary.h>
|
2015-10-12 07:05:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-07-28 12:37:44 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_RESTORE_FROM_FIELD_DUMP;
|
|
|
|
extern const int DECIMAL_OVERFLOW;
|
|
|
|
}
|
|
|
|
|
2020-11-02 06:05:53 +00:00
|
|
|
inline Field getBinaryValue(UInt8 type, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-08-16 08:03:23 +00:00
|
|
|
case Field::Types::Null:
|
|
|
|
{
|
|
|
|
return Field();
|
2020-11-02 06:05:53 +00:00
|
|
|
}
|
2021-08-16 08:03:23 +00:00
|
|
|
case Field::Types::UInt64:
|
|
|
|
{
|
2020-11-02 06:05:53 +00:00
|
|
|
UInt64 value;
|
2021-08-16 08:03:23 +00:00
|
|
|
readVarUInt(value, buf);
|
2020-11-02 06:05:53 +00:00
|
|
|
return value;
|
|
|
|
}
|
2021-08-16 08:03:23 +00:00
|
|
|
case Field::Types::UInt128:
|
|
|
|
{
|
2020-11-02 06:05:53 +00:00
|
|
|
UInt128 value;
|
2021-08-16 08:03:23 +00:00
|
|
|
readBinary(value, buf);
|
2020-11-02 06:05:53 +00:00
|
|
|
return value;
|
|
|
|
}
|
2021-08-16 08:03:23 +00:00
|
|
|
case Field::Types::UInt256:
|
|
|
|
{
|
|
|
|
UInt256 value;
|
|
|
|
readBinary(value, buf);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
case Field::Types::UUID:
|
|
|
|
{
|
|
|
|
UUID value;
|
|
|
|
readBinary(value, buf);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
case Field::Types::Int64:
|
|
|
|
{
|
2020-11-02 06:05:53 +00:00
|
|
|
Int64 value;
|
2021-08-16 08:03:23 +00:00
|
|
|
readVarInt(value, buf);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
case Field::Types::Int128:
|
|
|
|
{
|
|
|
|
Int128 value;
|
|
|
|
readBinary(value, buf);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
case Field::Types::Int256:
|
|
|
|
{
|
|
|
|
Int256 value;
|
|
|
|
readBinary(value, buf);
|
2020-11-02 06:05:53 +00:00
|
|
|
return value;
|
|
|
|
}
|
2021-08-16 08:03:23 +00:00
|
|
|
case Field::Types::Float64:
|
|
|
|
{
|
2020-11-02 06:05:53 +00:00
|
|
|
Float64 value;
|
2021-08-16 08:03:23 +00:00
|
|
|
readFloatBinary(value, buf);
|
2020-11-02 06:05:53 +00:00
|
|
|
return value;
|
|
|
|
}
|
2021-08-16 08:03:23 +00:00
|
|
|
case Field::Types::String:
|
|
|
|
{
|
2020-11-02 06:05:53 +00:00
|
|
|
std::string value;
|
2021-08-16 08:03:23 +00:00
|
|
|
readStringBinary(value, buf);
|
2020-11-02 06:05:53 +00:00
|
|
|
return value;
|
|
|
|
}
|
2021-08-16 08:03:23 +00:00
|
|
|
case Field::Types::Array:
|
|
|
|
{
|
2020-11-02 06:05:53 +00:00
|
|
|
Array value;
|
2021-08-16 08:03:23 +00:00
|
|
|
readBinary(value, buf);
|
2020-11-02 06:05:53 +00:00
|
|
|
return value;
|
|
|
|
}
|
2021-08-16 08:03:23 +00:00
|
|
|
case Field::Types::Tuple:
|
|
|
|
{
|
2020-11-02 06:05:53 +00:00
|
|
|
Tuple value;
|
2021-08-16 08:03:23 +00:00
|
|
|
readBinary(value, buf);
|
2020-11-02 06:05:53 +00:00
|
|
|
return value;
|
|
|
|
}
|
2021-08-16 08:03:23 +00:00
|
|
|
case Field::Types::Map:
|
|
|
|
{
|
2020-11-02 06:05:53 +00:00
|
|
|
Map value;
|
2021-08-16 08:03:23 +00:00
|
|
|
readBinary(value, buf);
|
2020-11-02 06:05:53 +00:00
|
|
|
return value;
|
|
|
|
}
|
2021-08-20 21:11:22 +00:00
|
|
|
case Field::Types::Object:
|
|
|
|
{
|
|
|
|
Object value;
|
|
|
|
readBinary(value, buf);
|
|
|
|
return value;
|
|
|
|
}
|
2021-08-16 08:03:23 +00:00
|
|
|
case Field::Types::AggregateFunctionState:
|
|
|
|
{
|
2020-11-02 06:05:53 +00:00
|
|
|
AggregateFunctionStateData value;
|
2021-08-16 08:03:23 +00:00
|
|
|
readStringBinary(value.name, buf);
|
|
|
|
readStringBinary(value.data, buf);
|
2020-11-02 06:05:53 +00:00
|
|
|
return value;
|
|
|
|
}
|
2022-01-18 11:52:27 +00:00
|
|
|
case Field::Types::Bool:
|
|
|
|
{
|
|
|
|
UInt8 value;
|
|
|
|
readBinary(value, buf);
|
|
|
|
return bool(value);
|
|
|
|
}
|
2020-11-02 06:05:53 +00:00
|
|
|
}
|
2021-08-16 08:03:23 +00:00
|
|
|
return Field();
|
2020-11-02 06:05:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
void readBinary(Array & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
UInt8 type;
|
2021-08-16 08:03:23 +00:00
|
|
|
readBinary(type, buf);
|
|
|
|
readBinary(size, buf);
|
2020-07-28 12:37:44 +00:00
|
|
|
|
|
|
|
for (size_t index = 0; index < size; ++index)
|
2020-11-02 06:05:53 +00:00
|
|
|
x.push_back(getBinaryValue(type, buf));
|
2020-07-28 12:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeBinary(const Array & x, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
UInt8 type = Field::Types::Null;
|
|
|
|
size_t size = x.size();
|
|
|
|
if (size)
|
|
|
|
type = x.front().getType();
|
2021-08-16 08:03:23 +00:00
|
|
|
writeBinary(type, buf);
|
|
|
|
writeBinary(size, buf);
|
2020-07-28 12:37:44 +00:00
|
|
|
|
|
|
|
for (const auto & elem : x)
|
2021-06-14 04:13:35 +00:00
|
|
|
Field::dispatch([&buf] (const auto & value) { FieldVisitorWriteBinary()(value, buf); }, elem);
|
2020-07-28 12:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeText(const Array & x, WriteBuffer & buf)
|
|
|
|
{
|
2021-08-16 08:03:23 +00:00
|
|
|
String res = applyVisitor(FieldVisitorToString(), Field(x));
|
2020-07-28 12:37:44 +00:00
|
|
|
buf.write(res.data(), res.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void readBinary(Tuple & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
size_t size;
|
2021-08-16 08:03:23 +00:00
|
|
|
readBinary(size, buf);
|
2020-07-28 12:37:44 +00:00
|
|
|
|
|
|
|
for (size_t index = 0; index < size; ++index)
|
2015-10-12 07:05:54 +00:00
|
|
|
{
|
|
|
|
UInt8 type;
|
2021-08-16 08:03:23 +00:00
|
|
|
readBinary(type, buf);
|
2020-11-02 06:05:53 +00:00
|
|
|
x.push_back(getBinaryValue(type, buf));
|
2015-10-12 07:05:54 +00:00
|
|
|
}
|
2020-07-28 12:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeBinary(const Tuple & x, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
const size_t size = x.size();
|
2021-08-16 08:03:23 +00:00
|
|
|
writeBinary(size, buf);
|
2015-10-12 07:05:54 +00:00
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
for (const auto & elem : x)
|
2015-10-12 07:05:54 +00:00
|
|
|
{
|
2020-07-28 12:37:44 +00:00
|
|
|
const UInt8 type = elem.getType();
|
2021-08-16 08:03:23 +00:00
|
|
|
writeBinary(type, buf);
|
2021-06-14 04:13:35 +00:00
|
|
|
Field::dispatch([&buf] (const auto & value) { FieldVisitorWriteBinary()(value, buf); }, elem);
|
2015-10-12 07:05:54 +00:00
|
|
|
}
|
2020-07-28 12:37:44 +00:00
|
|
|
}
|
2015-10-12 07:05:54 +00:00
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
void writeText(const Tuple & x, WriteBuffer & buf)
|
|
|
|
{
|
2021-08-16 08:03:23 +00:00
|
|
|
writeFieldText(Field(x), buf);
|
2020-07-28 12:37:44 +00:00
|
|
|
}
|
|
|
|
|
2020-10-10 06:49:03 +00:00
|
|
|
void readBinary(Map & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
size_t size;
|
2021-08-16 08:03:23 +00:00
|
|
|
readBinary(size, buf);
|
2020-10-10 06:49:03 +00:00
|
|
|
|
|
|
|
for (size_t index = 0; index < size; ++index)
|
|
|
|
{
|
|
|
|
UInt8 type;
|
2021-08-16 08:03:23 +00:00
|
|
|
readBinary(type, buf);
|
2020-11-02 06:05:53 +00:00
|
|
|
x.push_back(getBinaryValue(type, buf));
|
2020-10-10 06:49:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeBinary(const Map & x, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
const size_t size = x.size();
|
2021-08-16 08:03:23 +00:00
|
|
|
writeBinary(size, buf);
|
2020-10-10 06:49:03 +00:00
|
|
|
|
|
|
|
for (const auto & elem : x)
|
|
|
|
{
|
|
|
|
const UInt8 type = elem.getType();
|
2021-08-16 08:03:23 +00:00
|
|
|
writeBinary(type, buf);
|
2021-06-14 04:13:35 +00:00
|
|
|
Field::dispatch([&buf] (const auto & value) { FieldVisitorWriteBinary()(value, buf); }, elem);
|
2020-10-10 06:49:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeText(const Map & x, WriteBuffer & buf)
|
|
|
|
{
|
2021-08-16 08:03:23 +00:00
|
|
|
writeFieldText(Field(x), buf);
|
2020-10-10 06:49:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 21:11:22 +00:00
|
|
|
void readBinary(Object & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
readBinary(size, buf);
|
|
|
|
|
|
|
|
for (size_t index = 0; index < size; ++index)
|
|
|
|
{
|
|
|
|
UInt8 type;
|
|
|
|
String key;
|
|
|
|
readBinary(type, buf);
|
|
|
|
readBinary(key, buf);
|
|
|
|
x[key] = getBinaryValue(type, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeBinary(const Object & x, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
const size_t size = x.size();
|
|
|
|
writeBinary(size, buf);
|
|
|
|
|
|
|
|
for (const auto & [key, value] : x)
|
|
|
|
{
|
|
|
|
const UInt8 type = value.getType();
|
|
|
|
writeBinary(type, buf);
|
|
|
|
writeBinary(key, buf);
|
|
|
|
Field::dispatch([&buf] (const auto & val) { FieldVisitorWriteBinary()(val, buf); }, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeText(const Object & x, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
writeFieldText(Field(x), buf);
|
|
|
|
}
|
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
template <typename T>
|
|
|
|
void readQuoted(DecimalField<T> & x, ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
assertChar('\'', buf);
|
|
|
|
T value;
|
|
|
|
UInt32 scale;
|
|
|
|
int32_t exponent;
|
|
|
|
uint32_t max_digits = static_cast<uint32_t>(-1);
|
|
|
|
readDigits<true>(buf, value, max_digits, exponent, true);
|
|
|
|
if (exponent > 0)
|
2015-10-12 07:05:54 +00:00
|
|
|
{
|
2020-07-28 12:37:44 +00:00
|
|
|
scale = 0;
|
2020-08-19 11:52:17 +00:00
|
|
|
if (common::mulOverflow(value.value, DecimalUtils::scaleMultiplier<T>(exponent), value.value))
|
2020-07-28 12:37:44 +00:00
|
|
|
throw Exception("Decimal math overflow", ErrorCodes::DECIMAL_OVERFLOW);
|
2015-10-12 07:05:54 +00:00
|
|
|
}
|
2020-07-28 12:37:44 +00:00
|
|
|
else
|
|
|
|
scale = -exponent;
|
|
|
|
assertChar('\'', buf);
|
|
|
|
x = DecimalField<T>{value, scale};
|
|
|
|
}
|
|
|
|
|
|
|
|
template void readQuoted<Decimal32>(DecimalField<Decimal32> & x, ReadBuffer & buf);
|
|
|
|
template void readQuoted<Decimal64>(DecimalField<Decimal64> & x, ReadBuffer & buf);
|
|
|
|
template void readQuoted<Decimal128>(DecimalField<Decimal128> & x, ReadBuffer & buf);
|
2020-08-19 11:52:17 +00:00
|
|
|
template void readQuoted<Decimal256>(DecimalField<Decimal256> & x, ReadBuffer & buf);
|
2020-07-28 12:37:44 +00:00
|
|
|
|
|
|
|
void writeFieldText(const Field & x, WriteBuffer & buf)
|
|
|
|
{
|
2021-06-14 04:13:35 +00:00
|
|
|
String res = Field::dispatch(FieldVisitorToString(), x);
|
2020-07-28 12:37:44 +00:00
|
|
|
buf.write(res.data(), res.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String Field::dump() const
|
|
|
|
{
|
2021-06-14 04:13:35 +00:00
|
|
|
return applyVisitor(FieldVisitorDump(), *this);
|
2020-07-28 12:37:44 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 16:11:35 +00:00
|
|
|
Field Field::restoreFromDump(std::string_view dump_)
|
2020-07-28 12:37:44 +00:00
|
|
|
{
|
|
|
|
auto show_error = [&dump_]
|
2015-12-24 14:31:41 +00:00
|
|
|
{
|
2020-07-28 12:37:44 +00:00
|
|
|
throw Exception("Couldn't restore Field from dump: " + String{dump_}, ErrorCodes::CANNOT_RESTORE_FROM_FIELD_DUMP);
|
|
|
|
};
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
std::string_view dump = dump_;
|
|
|
|
trim(dump);
|
|
|
|
|
|
|
|
if (dump == "NULL")
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::string_view prefix = std::string_view{"Int64_"};
|
|
|
|
if (dump.starts_with(prefix))
|
|
|
|
{
|
|
|
|
Int64 value = parseFromString<Int64>(dump.substr(prefix.length()));
|
|
|
|
return value;
|
2015-12-24 14:31:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
prefix = std::string_view{"UInt64_"};
|
|
|
|
if (dump.starts_with(prefix))
|
2015-12-24 14:31:41 +00:00
|
|
|
{
|
2020-07-28 12:37:44 +00:00
|
|
|
UInt64 value = parseFromString<UInt64>(dump.substr(prefix.length()));
|
|
|
|
return value;
|
|
|
|
}
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2020-08-19 11:52:17 +00:00
|
|
|
prefix = std::string_view{"Int128_"};
|
|
|
|
if (dump.starts_with(prefix))
|
|
|
|
{
|
|
|
|
Int128 value = parseFromString<Int128>(dump.substr(prefix.length()));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-01-27 00:54:57 +00:00
|
|
|
prefix = std::string_view{"UInt128_"};
|
|
|
|
if (dump.starts_with(prefix))
|
|
|
|
{
|
|
|
|
UInt128 value = parseFromString<UInt128>(dump.substr(prefix.length()));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-08-19 11:52:17 +00:00
|
|
|
prefix = std::string_view{"Int256_"};
|
|
|
|
if (dump.starts_with(prefix))
|
|
|
|
{
|
2020-09-01 09:54:50 +00:00
|
|
|
Int256 value = parseFromString<Int256>(dump.substr(prefix.length()));
|
2020-08-19 11:52:17 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix = std::string_view{"UInt256_"};
|
|
|
|
if (dump.starts_with(prefix))
|
|
|
|
{
|
2020-09-01 09:54:50 +00:00
|
|
|
UInt256 value = parseFromString<UInt256>(dump.substr(prefix.length()));
|
2020-08-19 11:52:17 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
prefix = std::string_view{"Float64_"};
|
|
|
|
if (dump.starts_with(prefix))
|
|
|
|
{
|
|
|
|
Float64 value = parseFromString<Float64>(dump.substr(prefix.length()));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix = std::string_view{"Decimal32_"};
|
|
|
|
if (dump_.starts_with(prefix))
|
|
|
|
{
|
|
|
|
DecimalField<Decimal32> decimal;
|
|
|
|
ReadBufferFromString buf{dump.substr(prefix.length())};
|
|
|
|
readQuoted(decimal, buf);
|
|
|
|
return decimal;
|
2015-12-24 14:31:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
prefix = std::string_view{"Decimal64_"};
|
|
|
|
if (dump_.starts_with(prefix))
|
2015-12-24 14:31:41 +00:00
|
|
|
{
|
2020-07-28 12:37:44 +00:00
|
|
|
DecimalField<Decimal64> decimal;
|
|
|
|
ReadBufferFromString buf{dump.substr(prefix.length())};
|
|
|
|
readQuoted(decimal, buf);
|
|
|
|
return decimal;
|
2019-10-18 15:57:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
prefix = std::string_view{"Decimal128_"};
|
|
|
|
if (dump_.starts_with(prefix))
|
2019-10-18 15:57:05 +00:00
|
|
|
{
|
2020-07-28 12:37:44 +00:00
|
|
|
DecimalField<Decimal128> decimal;
|
|
|
|
ReadBufferFromString buf{dump.substr(prefix.length())};
|
|
|
|
readQuoted(decimal, buf);
|
|
|
|
return decimal;
|
2015-12-24 14:31:41 +00:00
|
|
|
}
|
2018-08-09 16:32:01 +00:00
|
|
|
|
2020-08-19 11:52:17 +00:00
|
|
|
prefix = std::string_view{"Decimal256_"};
|
|
|
|
if (dump_.starts_with(prefix))
|
|
|
|
{
|
|
|
|
DecimalField<Decimal256> decimal;
|
|
|
|
ReadBufferFromString buf{dump.substr(prefix.length())};
|
|
|
|
readQuoted(decimal, buf);
|
|
|
|
return decimal;
|
|
|
|
}
|
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
if (dump.starts_with("\'"))
|
|
|
|
{
|
|
|
|
String str;
|
|
|
|
ReadBufferFromString buf{dump};
|
|
|
|
readQuoted(str, buf);
|
|
|
|
return str;
|
|
|
|
}
|
2018-08-09 16:32:01 +00:00
|
|
|
|
2022-01-18 11:52:27 +00:00
|
|
|
prefix = std::string_view{"Bool_"};
|
|
|
|
if (dump.starts_with(prefix))
|
|
|
|
{
|
|
|
|
bool value = parseFromString<bool>(dump.substr(prefix.length()));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
prefix = std::string_view{"Array_["};
|
|
|
|
if (dump.starts_with(prefix))
|
2018-08-09 16:32:01 +00:00
|
|
|
{
|
2020-07-28 12:37:44 +00:00
|
|
|
std::string_view tail = dump.substr(prefix.length());
|
|
|
|
trimLeft(tail);
|
|
|
|
Array array;
|
|
|
|
while (tail != "]")
|
|
|
|
{
|
|
|
|
size_t separator = tail.find_first_of(",]");
|
|
|
|
if (separator == std::string_view::npos)
|
|
|
|
show_error();
|
|
|
|
bool comma = (tail[separator] == ',');
|
|
|
|
std::string_view element = tail.substr(0, separator);
|
|
|
|
tail.remove_prefix(separator);
|
|
|
|
if (comma)
|
|
|
|
tail.remove_prefix(1);
|
|
|
|
trimLeft(tail);
|
|
|
|
if (!comma && tail != "]")
|
|
|
|
show_error();
|
|
|
|
array.push_back(Field::restoreFromDump(element));
|
|
|
|
}
|
|
|
|
return array;
|
2018-08-09 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
prefix = std::string_view{"Tuple_("};
|
|
|
|
if (dump.starts_with(prefix))
|
2018-08-09 16:32:01 +00:00
|
|
|
{
|
2020-07-28 12:37:44 +00:00
|
|
|
std::string_view tail = dump.substr(prefix.length());
|
|
|
|
trimLeft(tail);
|
|
|
|
Tuple tuple;
|
|
|
|
while (tail != ")")
|
|
|
|
{
|
|
|
|
size_t separator = tail.find_first_of(",)");
|
|
|
|
if (separator == std::string_view::npos)
|
|
|
|
show_error();
|
|
|
|
bool comma = (tail[separator] == ',');
|
|
|
|
std::string_view element = tail.substr(0, separator);
|
|
|
|
tail.remove_prefix(separator);
|
|
|
|
if (comma)
|
|
|
|
tail.remove_prefix(1);
|
|
|
|
trimLeft(tail);
|
|
|
|
if (!comma && tail != ")")
|
|
|
|
show_error();
|
|
|
|
tuple.push_back(Field::restoreFromDump(element));
|
|
|
|
}
|
|
|
|
return tuple;
|
2018-08-09 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-10 06:49:03 +00:00
|
|
|
prefix = std::string_view{"Map_("};
|
|
|
|
if (dump.starts_with(prefix))
|
|
|
|
{
|
|
|
|
std::string_view tail = dump.substr(prefix.length());
|
|
|
|
trimLeft(tail);
|
|
|
|
Map map;
|
|
|
|
while (tail != ")")
|
|
|
|
{
|
|
|
|
size_t separator = tail.find_first_of(",)");
|
|
|
|
if (separator == std::string_view::npos)
|
|
|
|
show_error();
|
|
|
|
bool comma = (tail[separator] == ',');
|
|
|
|
std::string_view element = tail.substr(0, separator);
|
|
|
|
tail.remove_prefix(separator);
|
|
|
|
if (comma)
|
|
|
|
tail.remove_prefix(1);
|
|
|
|
trimLeft(tail);
|
|
|
|
if (!comma && tail != ")")
|
|
|
|
show_error();
|
|
|
|
map.push_back(Field::restoreFromDump(element));
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
prefix = std::string_view{"AggregateFunctionState_("};
|
|
|
|
if (dump.starts_with(prefix))
|
2018-08-09 16:32:01 +00:00
|
|
|
{
|
2020-07-28 12:37:44 +00:00
|
|
|
std::string_view after_prefix = dump.substr(prefix.length());
|
|
|
|
size_t comma = after_prefix.find(',');
|
|
|
|
size_t end = after_prefix.find(')', comma + 1);
|
|
|
|
if ((comma == std::string_view::npos) || (end != after_prefix.length() - 1))
|
|
|
|
show_error();
|
|
|
|
std::string_view name_view = after_prefix.substr(0, comma);
|
|
|
|
std::string_view data_view = after_prefix.substr(comma + 1, end - comma - 1);
|
|
|
|
trim(name_view);
|
|
|
|
trim(data_view);
|
|
|
|
ReadBufferFromString name_buf{name_view};
|
|
|
|
ReadBufferFromString data_buf{data_view};
|
|
|
|
AggregateFunctionStateData res;
|
|
|
|
readQuotedString(res.name, name_buf);
|
|
|
|
readQuotedString(res.data, data_buf);
|
|
|
|
return res;
|
2018-08-09 16:32:01 +00:00
|
|
|
}
|
2018-08-23 14:03:37 +00:00
|
|
|
|
2020-07-28 12:37:44 +00:00
|
|
|
show_error();
|
2022-10-07 19:20:14 +00:00
|
|
|
UNREACHABLE();
|
2020-07-28 12:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2021-05-07 23:35:11 +00:00
|
|
|
bool decimalEqual(T x, T y, UInt32 x_scale, UInt32 y_scale)
|
2020-07-28 12:37:44 +00:00
|
|
|
{
|
|
|
|
using Comparator = DecimalComparison<T, T, EqualsOp>;
|
|
|
|
return Comparator::compare(x, y, x_scale, y_scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2021-05-07 23:35:11 +00:00
|
|
|
bool decimalLess(T x, T y, UInt32 x_scale, UInt32 y_scale)
|
2020-07-28 12:37:44 +00:00
|
|
|
{
|
|
|
|
using Comparator = DecimalComparison<T, T, LessOp>;
|
|
|
|
return Comparator::compare(x, y, x_scale, y_scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2021-05-07 23:35:11 +00:00
|
|
|
bool decimalLessOrEqual(T x, T y, UInt32 x_scale, UInt32 y_scale)
|
2020-07-28 12:37:44 +00:00
|
|
|
{
|
|
|
|
using Comparator = DecimalComparison<T, T, LessOrEqualsOp>;
|
|
|
|
return Comparator::compare(x, y, x_scale, y_scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-07 23:35:11 +00:00
|
|
|
template bool decimalEqual<Decimal32>(Decimal32 x, Decimal32 y, UInt32 x_scale, UInt32 y_scale);
|
|
|
|
template bool decimalEqual<Decimal64>(Decimal64 x, Decimal64 y, UInt32 x_scale, UInt32 y_scale);
|
|
|
|
template bool decimalEqual<Decimal128>(Decimal128 x, Decimal128 y, UInt32 x_scale, UInt32 y_scale);
|
|
|
|
template bool decimalEqual<Decimal256>(Decimal256 x, Decimal256 y, UInt32 x_scale, UInt32 y_scale);
|
|
|
|
template bool decimalEqual<DateTime64>(DateTime64 x, DateTime64 y, UInt32 x_scale, UInt32 y_scale);
|
2018-08-23 14:03:37 +00:00
|
|
|
|
2021-05-07 23:35:11 +00:00
|
|
|
template bool decimalLess<Decimal32>(Decimal32 x, Decimal32 y, UInt32 x_scale, UInt32 y_scale);
|
|
|
|
template bool decimalLess<Decimal64>(Decimal64 x, Decimal64 y, UInt32 x_scale, UInt32 y_scale);
|
|
|
|
template bool decimalLess<Decimal128>(Decimal128 x, Decimal128 y, UInt32 x_scale, UInt32 y_scale);
|
|
|
|
template bool decimalLess<Decimal256>(Decimal256 x, Decimal256 y, UInt32 x_scale, UInt32 y_scale);
|
|
|
|
template bool decimalLess<DateTime64>(DateTime64 x, DateTime64 y, UInt32 x_scale, UInt32 y_scale);
|
2018-08-23 14:03:37 +00:00
|
|
|
|
2021-05-07 23:35:11 +00:00
|
|
|
template bool decimalLessOrEqual<Decimal32>(Decimal32 x, Decimal32 y, UInt32 x_scale, UInt32 y_scale);
|
|
|
|
template bool decimalLessOrEqual<Decimal64>(Decimal64 x, Decimal64 y, UInt32 x_scale, UInt32 y_scale);
|
|
|
|
template bool decimalLessOrEqual<Decimal128>(Decimal128 x, Decimal128 y, UInt32 x_scale, UInt32 y_scale);
|
|
|
|
template bool decimalLessOrEqual<Decimal256>(Decimal256 x, Decimal256 y, UInt32 x_scale, UInt32 y_scale);
|
|
|
|
template bool decimalLessOrEqual<DateTime64>(DateTime64 x, DateTime64 y, UInt32 x_scale, UInt32 y_scale);
|
2020-08-19 11:52:17 +00:00
|
|
|
|
2020-11-12 20:23:26 +00:00
|
|
|
|
2021-08-27 14:09:15 +00:00
|
|
|
inline void writeText(const Null & x, WriteBuffer & buf)
|
2020-08-05 21:53:35 +00:00
|
|
|
{
|
2021-08-29 19:00:00 +00:00
|
|
|
if (x.isNegativeInfinity())
|
2021-09-06 15:59:46 +00:00
|
|
|
writeText("-Inf", buf);
|
2021-08-29 19:00:00 +00:00
|
|
|
if (x.isPositiveInfinity())
|
2021-09-06 15:59:46 +00:00
|
|
|
writeText("+Inf", buf);
|
2021-08-27 14:09:15 +00:00
|
|
|
else
|
2021-09-06 15:59:46 +00:00
|
|
|
writeText("NULL", buf);
|
2021-01-02 09:47:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 15:45:44 +00:00
|
|
|
String toString(const Field & x)
|
|
|
|
{
|
|
|
|
return Field::dispatch(
|
|
|
|
[] (const auto & value)
|
|
|
|
{
|
|
|
|
// Use explicit type to prevent implicit construction of Field and
|
|
|
|
// infinite recursion into toString<Field>.
|
|
|
|
return toString<decltype(value)>(value);
|
|
|
|
},
|
|
|
|
x);
|
|
|
|
}
|
|
|
|
|
2022-06-23 20:04:06 +00:00
|
|
|
String fieldTypeToString(Field::Types::Which type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Field::Types::Which::Null: return "Null";
|
|
|
|
case Field::Types::Which::Array: return "Array";
|
|
|
|
case Field::Types::Which::Tuple: return "Tuple";
|
|
|
|
case Field::Types::Which::Map: return "Map";
|
|
|
|
case Field::Types::Which::Object: return "Object";
|
|
|
|
case Field::Types::Which::AggregateFunctionState: return "AggregateFunctionState";
|
|
|
|
case Field::Types::Which::Bool: return "Bool";
|
|
|
|
case Field::Types::Which::String: return "String";
|
|
|
|
case Field::Types::Which::Decimal32: return "Decimal32";
|
|
|
|
case Field::Types::Which::Decimal64: return "Decimal64";
|
|
|
|
case Field::Types::Which::Decimal128: return "Decimal128";
|
|
|
|
case Field::Types::Which::Decimal256: return "Decimal256";
|
|
|
|
case Field::Types::Which::Float64: return "Float64";
|
|
|
|
case Field::Types::Which::Int64: return "Int64";
|
|
|
|
case Field::Types::Which::Int128: return "Int128";
|
|
|
|
case Field::Types::Which::Int256: return "Int256";
|
|
|
|
case Field::Types::Which::UInt64: return "UInt64";
|
|
|
|
case Field::Types::Which::UInt128: return "UInt128";
|
|
|
|
case Field::Types::Which::UInt256: return "UInt256";
|
|
|
|
case Field::Types::Which::UUID: return "UUID";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-24 14:31:41 +00:00
|
|
|
}
|