Skip to content

Commit 28b4930

Browse files
committed
fix
1 parent 022559c commit 28b4930

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

cpp-ch/local-engine/Common/CHUtil.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ BlockUtil::convertColumnAsNecessary(const DB::ColumnWithTypeAndName & column, co
151151
DB::JoinCommon::convertColumnToNullable(nullable_column);
152152
return {nullable_column.column, sample_column.type, sample_column.name};
153153
}
154+
else if (!sample_column.type->isNullable() && column.type->isNullable() && sample_column.type->equals(*DB::removeNullable(column.type)))
155+
{
156+
const auto * nullable_column = DB::checkAndGetColumn<DB::ColumnNullable>(column.column.get());
157+
if (!nullable_column || std::ranges::any_of(nullable_column->getNullMapData(), [](UInt8 is_null) { return is_null != 0; }))
158+
throw DB::Exception(
159+
DB::ErrorCodes::LOGICAL_ERROR,
160+
"Cannot convert nullable column with NULL values to non-nullable type. original:{} expected:{}",
161+
column.dumpStructure(),
162+
sample_column.dumpStructure());
163+
return {nullable_column->getNestedColumnPtr(), sample_column.type, sample_column.name};
164+
}
154165
else
155166
throw DB::Exception(
156167
DB::ErrorCodes::LOGICAL_ERROR,

cpp-ch/local-engine/tests/gtest_local_engine.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
#include <iostream>
1919
#include <incbin.h>
2020

21+
#include <Columns/ColumnNullable.h>
22+
#include <Columns/ColumnsNumber.h>
2123
#include <Disks/DiskLocal.h>
24+
#include <DataTypes/DataTypeNullable.h>
25+
#include <DataTypes/DataTypeString.h>
2226
#include <Formats/FormatFactory.h>
2327
#include <Interpreters/Context.h>
2428
#include <Interpreters/registerInterpreters.h>
@@ -59,6 +63,36 @@ TEST(TESTUtil, TestByteToLong)
5963
ASSERT_EQ(expected, result);
6064
}
6165

66+
TEST(BlockUtil, ConvertNullableColumnAsNecessary)
67+
{
68+
auto string_type = std::make_shared<DataTypeString>();
69+
auto nullable_string_type = std::make_shared<DataTypeNullable>(string_type);
70+
71+
auto nested = string_type->createColumn();
72+
nested->insert("A");
73+
nested->insert("B");
74+
auto null_map = ColumnUInt8::create(2, 0);
75+
ColumnWithTypeAndName nullable_column(
76+
ColumnNullable::create(std::move(nested), std::move(null_map)), nullable_string_type, "col_1");
77+
ColumnWithTypeAndName sample_column(string_type, "broadcast_right_col_1");
78+
79+
auto converted = BlockUtil::convertColumnAsNecessary(nullable_column, sample_column);
80+
ASSERT_TRUE(converted.type->equals(*string_type));
81+
ASSERT_EQ("broadcast_right_col_1", converted.name);
82+
ASSERT_FALSE(converted.column->isNullable());
83+
84+
auto nested_with_null = string_type->createColumn();
85+
nested_with_null->insert("A");
86+
nested_with_null->insertDefault();
87+
auto null_map_with_null = ColumnUInt8::create();
88+
null_map_with_null->insertValue(0);
89+
null_map_with_null->insertValue(1);
90+
ColumnWithTypeAndName nullable_column_with_null(
91+
ColumnNullable::create(std::move(nested_with_null), std::move(null_map_with_null)), nullable_string_type, "col_1");
92+
93+
ASSERT_THROW(BlockUtil::convertColumnAsNecessary(nullable_column_with_null, sample_column), DB::Exception);
94+
}
95+
6296
TEST(ReadBufferFromFile, seekBackwards)
6397
{
6498
static constexpr size_t N = 256;
@@ -110,4 +144,4 @@ int main(int argc, char ** argv)
110144

111145
::testing::InitGoogleTest(&argc, argv);
112146
return RUN_ALL_TESTS();
113-
}
147+
}

0 commit comments

Comments
 (0)