Skip to content

Commit

Permalink
fix read/write DecimalField (#6719)
Browse files Browse the repository at this point in the history
  • Loading branch information
yl-lisen authored and Jasmine-ge committed Dec 9, 2024
1 parent 6ca67dd commit 53443a5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
24 changes: 20 additions & 4 deletions src/Common/FieldVisitorWriteBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,26 @@ void FieldVisitorWriteBinary::operator() (const Int256 & x, WriteBuffer & buf) c
void FieldVisitorWriteBinary::operator() (const UUID & x, WriteBuffer & buf) const { writeBinary(x, buf); }
void FieldVisitorWriteBinary::operator() (const IPv4 & x, WriteBuffer & buf) const { writeBinary(x, buf); }
void FieldVisitorWriteBinary::operator() (const IPv6 & x, WriteBuffer & buf) const { writeBinary(x, buf); }
void FieldVisitorWriteBinary::operator() (const DecimalField<Decimal32> & x, WriteBuffer & buf) const { writeBinary(x.getValue(), buf); }
void FieldVisitorWriteBinary::operator() (const DecimalField<Decimal64> & x, WriteBuffer & buf) const { writeBinary(x.getValue(), buf); }
void FieldVisitorWriteBinary::operator() (const DecimalField<Decimal128> & x, WriteBuffer & buf) const { writeBinary(x.getValue(), buf); }
void FieldVisitorWriteBinary::operator() (const DecimalField<Decimal256> & x, WriteBuffer & buf) const { writeBinary(x.getValue(), buf); }
void FieldVisitorWriteBinary::operator() (const DecimalField<Decimal32> & x, WriteBuffer & buf) const
{
writeBinary(x.getValue(), buf);
writeVarUInt(x.getScale(), buf);
}
void FieldVisitorWriteBinary::operator() (const DecimalField<Decimal64> & x, WriteBuffer & buf) const
{
writeBinary(x.getValue(), buf);
writeVarUInt(x.getScale(), buf);
}
void FieldVisitorWriteBinary::operator() (const DecimalField<Decimal128> & x, WriteBuffer & buf) const
{
writeBinary(x.getValue(), buf);
writeVarUInt(x.getScale(), buf);
}
void FieldVisitorWriteBinary::operator() (const DecimalField<Decimal256> & x, WriteBuffer & buf) const
{
writeBinary(x.getValue(), buf);
writeVarUInt(x.getScale(), buf);
}
void FieldVisitorWriteBinary::operator() (const AggregateFunctionStateData & x, WriteBuffer & buf) const
{
writeStringBinary(x.name, buf);
Expand Down
50 changes: 48 additions & 2 deletions src/Core/Field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,43 @@ inline Field getBinaryValue(UInt8 type, ReadBuffer & buf)
readBinary(value, buf);
return bool(value);
}
case Field::Types::Decimal32:
{
Decimal32 value;
UInt32 scale;
readBinary(value, buf);
readVarUInt(scale, buf);
return DecimalField<Decimal32>(value, scale);
}
case Field::Types::Decimal64:
{
Decimal64 value;
UInt32 scale;
readBinary(value, buf);
readVarUInt(scale, buf);
return DecimalField<Decimal64>(value, scale);
}
case Field::Types::Decimal128:
{
Decimal128 value;
UInt32 scale;
readBinary(value, buf);
readVarUInt(scale, buf);
return DecimalField<Decimal128>(value, scale);
}
case Field::Types::Decimal256:
{
Decimal256 value;
UInt32 scale;
readBinary(value, buf);
readVarUInt(scale, buf);
return DecimalField<Decimal256>(value, scale);
}
}
return Field();
throw Exception(ErrorCodes::INCORRECT_DATA, "Unknown field type {}", std::to_string(type));
}

void readBinary(Array & x, ReadBuffer & buf)
void readBinaryArray(Array & x, ReadBuffer & buf)
{
size_t size;
UInt8 type;
Expand Down Expand Up @@ -292,6 +324,20 @@ void writeFieldText(const Field & x, WriteBuffer & buf)
buf.write(res.data(), res.size());
}

void writeFieldBinary(const Field & x, WriteBuffer & buf)
{
const UInt8 type = x.getType();
writeBinary(type, buf);

Field::dispatch([&buf] (const auto & value) { FieldVisitorWriteBinary()(value, buf); }, x);
}

Field readFieldBinary(ReadBuffer & buf)
{
UInt8 type;
readBinary(type, buf);
return getBinaryValue(type, buf);
}

String Field::dump() const
{
Expand Down

0 comments on commit 53443a5

Please sign in to comment.