Skip to content

Commit

Permalink
Merge pull request #210 from 1261385937/master
Browse files Browse the repository at this point in the history
fix CreateColumnFromAst
  • Loading branch information
Enmk authored Aug 22, 2022
2 parents 66e9c54 + 1ec61ff commit 3dd998e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 6 additions & 2 deletions clickhouse/columns/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,16 @@ static ColumnRef CreateColumnFromAst(const TypeAst& ast, CreateColumnByTypeSetti

case TypeAst::Enum: {
std::vector<Type::EnumItem> 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) {
Expand Down
16 changes: 14 additions & 2 deletions ut/types_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 3dd998e

Please sign in to comment.