From a5b1496fe9169ed16ad23a1b15eaa2d0873bea71 Mon Sep 17 00:00:00 2001 From: Alexander Kuzmenkov Date: Tue, 8 Oct 2019 21:20:20 +0300 Subject: [PATCH] Serialize Null Fields correctly in DataTypeNullable. --- dbms/src/DataTypes/DataTypeNullable.cpp | 31 +++++++++++++++++++ dbms/src/DataTypes/DataTypeNullable.h | 6 ++-- .../01016_null_part_minmax.reference | 0 .../0_stateless/01016_null_part_minmax.sql | 3 ++ 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 dbms/tests/queries/0_stateless/01016_null_part_minmax.reference create mode 100644 dbms/tests/queries/0_stateless/01016_null_part_minmax.sql diff --git a/dbms/src/DataTypes/DataTypeNullable.cpp b/dbms/src/DataTypes/DataTypeNullable.cpp index 376b4b6dad0..fb75f1c7b62 100644 --- a/dbms/src/DataTypes/DataTypeNullable.cpp +++ b/dbms/src/DataTypes/DataTypeNullable.cpp @@ -118,6 +118,33 @@ void DataTypeNullable::deserializeBinaryBulkWithMultipleStreams( } +void DataTypeNullable::serializeBinary(const Field & field, WriteBuffer & ostr) const +{ + if (field.isNull()) + { + writeBinary(true, ostr); + } + else + { + writeBinary(false, ostr); + nested_data_type->serializeBinary(field, ostr); + } +} + +void DataTypeNullable::deserializeBinary(Field & field, ReadBuffer & istr) const +{ + bool is_null = false; + readBinary(is_null, istr); + if (!is_null) + { + nested_data_type->deserializeBinary(field, istr); + } + else + { + field = Null(); + } +} + void DataTypeNullable::serializeBinary(const IColumn & column, size_t row_num, WriteBuffer & ostr) const { const ColumnNullable & col = assert_cast(column); @@ -416,6 +443,10 @@ MutableColumnPtr DataTypeNullable::createColumn() const return ColumnNullable::create(nested_data_type->createColumn(), ColumnUInt8::create()); } +Field DataTypeNullable::getDefault() const +{ + return Null(); +} size_t DataTypeNullable::getSizeOfValueInMemory() const { diff --git a/dbms/src/DataTypes/DataTypeNullable.h b/dbms/src/DataTypes/DataTypeNullable.h index 5bacdb39ff4..49b14eefc6c 100644 --- a/dbms/src/DataTypes/DataTypeNullable.h +++ b/dbms/src/DataTypes/DataTypeNullable.h @@ -45,8 +45,8 @@ public: DeserializeBinaryBulkSettings & settings, DeserializeBinaryBulkStatePtr & state) const override; - void serializeBinary(const Field & field, WriteBuffer & ostr) const override { nested_data_type->serializeBinary(field, ostr); } - void deserializeBinary(Field & field, ReadBuffer & istr) const override { nested_data_type->deserializeBinary(field, istr); } + void serializeBinary(const Field & field, WriteBuffer & ostr) const override; + void deserializeBinary(Field & field, ReadBuffer & istr) const override; void serializeBinary(const IColumn & column, size_t row_num, WriteBuffer & ostr) const override; void deserializeBinary(IColumn & column, ReadBuffer & istr) const override; void serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override; @@ -77,7 +77,7 @@ public: MutableColumnPtr createColumn() const override; - Field getDefault() const override { return Null(); } + Field getDefault() const override; bool equals(const IDataType & rhs) const override; diff --git a/dbms/tests/queries/0_stateless/01016_null_part_minmax.reference b/dbms/tests/queries/0_stateless/01016_null_part_minmax.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/dbms/tests/queries/0_stateless/01016_null_part_minmax.sql b/dbms/tests/queries/0_stateless/01016_null_part_minmax.sql new file mode 100644 index 00000000000..bfc554c90c5 --- /dev/null +++ b/dbms/tests/queries/0_stateless/01016_null_part_minmax.sql @@ -0,0 +1,3 @@ +-- this test checks that null values are correctly serialized inside minmax index (issue #7113) +create table null_01016 (x Nullable(String)) engine MergeTree order by ifNull(x, 'order-null') partition by ifNull(x, 'partition-null'); +insert into null_01016 values (null);