|
30 | 30 | #include "agent/be_exec_version_manager.h" |
31 | 31 | #include "common/exception.h" |
32 | 32 | #include "core/assert_cast.h" |
| 33 | +#include "core/block/block.h" |
33 | 34 | #include "core/column/column_const.h" |
34 | 35 | #include "core/column/column_string.h" |
35 | 36 | #include "core/column/column_varbinary.h" |
36 | 37 | #include "core/data_type/common_data_type_serder_test.h" |
37 | 38 | #include "core/data_type/common_data_type_test.h" |
| 39 | +#include "core/data_type/data_type_array.h" |
38 | 40 | #include "core/data_type/data_type_factory.hpp" |
| 41 | +#include "core/data_type/data_type_map.h" |
| 42 | +#include "core/data_type/data_type_nullable.h" |
39 | 43 | #include "core/data_type/data_type_string.h" |
| 44 | +#include "core/data_type/data_type_struct.h" |
40 | 45 | #include "core/data_type_serde/data_type_serde.h" |
41 | 46 | #include "core/field.h" |
42 | 47 | #include "core/string_buffer.hpp" |
@@ -287,6 +292,81 @@ TEST_F(DataTypeVarbinaryTest, ToProtobufDefaultLen) { |
287 | 292 | EXPECT_EQ(scalar.len(), -1); |
288 | 293 | } |
289 | 294 |
|
| 295 | +TEST_F(DataTypeVarbinaryTest, BlockRoundTripPreservesDeclaredLength) { |
| 296 | + for (int length : {-1, 16, 32}) { |
| 297 | + for (bool nullable : {false, true}) { |
| 298 | + SCOPED_TRACE(::testing::Message() << "length=" << length << ", nullable=" << nullable); |
| 299 | + DataTypePtr type = std::make_shared<DataTypeVarbinary>(length); |
| 300 | + if (nullable) { |
| 301 | + type = make_nullable(type); |
| 302 | + } |
| 303 | + auto column = type->create_column(); |
| 304 | + const std::string value = make_bytes(16); |
| 305 | + column->insert(Field::create_field<TYPE_VARBINARY>(StringView(value))); |
| 306 | + if (nullable) { |
| 307 | + column->insert_default(); |
| 308 | + } |
| 309 | + Block block {{std::move(column), type, "binary_value"}}; |
| 310 | + PBlock serialized; |
| 311 | + size_t uncompressed_bytes = 0; |
| 312 | + size_t compressed_bytes = 0; |
| 313 | + int64_t compress_time = 0; |
| 314 | + ASSERT_TRUE(block.serialize(BeExecVersionManager::get_newest_version(), &serialized, |
| 315 | + &uncompressed_bytes, &compressed_bytes, &compress_time, |
| 316 | + segment_v2::CompressionTypePB::SNAPPY) |
| 317 | + .ok()); |
| 318 | + std::string wire; |
| 319 | + ASSERT_TRUE(serialized.SerializeToString(&wire)); |
| 320 | + PBlock received; |
| 321 | + ASSERT_TRUE(received.ParseFromString(wire)); |
| 322 | + Block restored; |
| 323 | + int64_t decompress_time = 0; |
| 324 | + ASSERT_TRUE(restored.deserialize(received, &uncompressed_bytes, &decompress_time).ok()); |
| 325 | + const auto& entry = restored.get_by_position(0); |
| 326 | + ASSERT_EQ(entry.type->is_nullable(), nullable); |
| 327 | + EXPECT_EQ(assert_cast<const DataTypeVarbinary&>(*remove_nullable(entry.type)).len(), |
| 328 | + length); |
| 329 | + ASSERT_EQ(restored.rows(), block.rows()); |
| 330 | + for (size_t row = 0; row < block.rows(); ++row) { |
| 331 | + EXPECT_EQ((*entry.column)[row], (*block.get_by_position(0).column)[row]); |
| 332 | + } |
| 333 | + } |
| 334 | + } |
| 335 | +} |
| 336 | + |
| 337 | +TEST_F(DataTypeVarbinaryTest, NestedColumnMetadataPreservesDeclaredLength) { |
| 338 | + auto binary16 = std::make_shared<DataTypeVarbinary>(16); |
| 339 | + auto binary32 = std::make_shared<DataTypeVarbinary>(32); |
| 340 | + DataTypeStruct type({std::make_shared<DataTypeArray>(make_nullable(binary16)), |
| 341 | + std::make_shared<DataTypeMap>(binary16, make_nullable(binary32))}, |
| 342 | + {"array_value", "map_value"}); |
| 343 | + PColumnMeta metadata; |
| 344 | + type.to_pb_column_meta(&metadata); |
| 345 | + std::string wire; |
| 346 | + ASSERT_TRUE(metadata.SerializeToString(&wire)); |
| 347 | + PColumnMeta received; |
| 348 | + ASSERT_TRUE(received.ParseFromString(wire)); |
| 349 | + auto restored = DataTypeFactory::instance().create_data_type(received); |
| 350 | + const auto& fields = assert_cast<const DataTypeStruct&>(*restored).get_elements(); |
| 351 | + const auto& array = assert_cast<const DataTypeArray&>(*fields[0]); |
| 352 | + ASSERT_TRUE(array.get_nested_type()->is_nullable()); |
| 353 | + EXPECT_EQ( |
| 354 | + assert_cast<const DataTypeVarbinary&>(*remove_nullable(array.get_nested_type())).len(), |
| 355 | + 16); |
| 356 | + const auto& map = assert_cast<const DataTypeMap&>(*fields[1]); |
| 357 | + EXPECT_EQ(assert_cast<const DataTypeVarbinary&>(*map.get_key_type()).len(), 16); |
| 358 | + ASSERT_TRUE(map.get_value_type()->is_nullable()); |
| 359 | + EXPECT_EQ(assert_cast<const DataTypeVarbinary&>(*remove_nullable(map.get_value_type())).len(), |
| 360 | + 32); |
| 361 | +} |
| 362 | + |
| 363 | +TEST_F(DataTypeVarbinaryTest, LegacyColumnMetadataUsesUnspecifiedLength) { |
| 364 | + PColumnMeta metadata; |
| 365 | + metadata.set_type(PGenericType::VARBINARY); |
| 366 | + auto restored = DataTypeFactory::instance().create_data_type(metadata); |
| 367 | + EXPECT_EQ(assert_cast<const DataTypeVarbinary&>(*restored).len(), -1); |
| 368 | +} |
| 369 | + |
290 | 370 | TEST_F(DataTypeVarbinaryTest, ProtobufPreservesDeclaredLength) { |
291 | 371 | PTypeDesc type; |
292 | 372 | auto* node = type.add_types(); |
|
0 commit comments