diff --git a/clickhouse/columns/factory.cpp b/clickhouse/columns/factory.cpp index 47c3feeb..38b02e1d 100644 --- a/clickhouse/columns/factory.cpp +++ b/clickhouse/columns/factory.cpp @@ -137,12 +137,16 @@ static ColumnRef CreateColumnFromAst(const TypeAst& ast, CreateColumnByTypeSetti case TypeAst::Enum: { std::vector enum_items; + //ast.elements.size() minimum is 1. + if ((ast.elements.size() % 2) != 0) { + throw ValidationError(ast.name + " content is not correct"); + } enum_items.reserve(ast.elements.size() / 2); for (size_t i = 0; i < ast.elements.size(); i += 2) { enum_items.push_back( - Type::EnumItem{ast.elements[i].value_string, - (int16_t)ast.elements[i + 1].value}); + Type::EnumItem{ ast.elements[i].value_string, + (int16_t)ast.elements[i + 1].value }); } if (ast.code == Type::Enum8) { diff --git a/ut/types_ut.cpp b/ut/types_ut.cpp index 8e355ee1..c5922d0e 100644 --- a/ut/types_ut.cpp +++ b/ut/types_ut.cpp @@ -86,8 +86,6 @@ TEST(TypesCase, IsEqual) { "DateTime64(3, 'UTC')", "Decimal(9,3)", "Decimal(18,3)", - "Enum8()", - "Enum16()", "Enum8('ONE' = 1)", "Enum8('ONE' = 1, 'TWO' = 2)", "Enum16('ONE' = 1, 'TWO' = 2, 'THREE' = 3, 'FOUR' = 4)", @@ -127,3 +125,17 @@ TEST(TypesCase, IsEqual) { } } } + +TEST(TypesCase, ErrorEnumContent) { + const std::string type_names[] = { + "Enum8()", + "Enum8('ONE')", + "Enum8('ONE'=1,'TWO')", + "Enum16('TWO'=,'TWO')", + }; + + for (const auto& type_name : type_names) { + SCOPED_TRACE(type_name); + EXPECT_THROW(clickhouse::CreateColumnByType(type_name)->Type(), ValidationError); + } +} \ No newline at end of file