diff --git a/clickhouse/columns/factory.cpp b/clickhouse/columns/factory.cpp index b2e368ce..d2bea1ae 100644 --- a/clickhouse/columns/factory.cpp +++ b/clickhouse/columns/factory.cpp @@ -25,6 +25,8 @@ #include "../exceptions.h" +#include +#include #include #include @@ -157,6 +159,53 @@ static ColumnRef CreateTerminalColumn(const TypeAst& ast) { } } +// Kept out of CreateColumnFromAst, which calls itself once per nesting level. The locals here would make +// every level of that recursion use more stack in a build without optimization. +static ColumnRef CreateEnumColumn(const TypeAst& ast) { + // The parser gives one flat list. Each quoted name is followed by its number if the name has one. + std::vector>> items; + for (const auto& element : ast.elements) { + if (element.meta == TypeAst::Terminal && element.code == Type::String && element.name.empty()) { + items.emplace_back(element.value_string, std::nullopt); + } else if (element.meta == TypeAst::Number && !items.empty() && !items.back().second.has_value()) { + items.back().second = element.value; + } else { + throw ValidationError(ast.name + " content is not correct"); + } + } + + // Do the same as the server. A name without a number gets the number of the first name plus its position. + // If the first name has no number, the count starts at 1. The names after the first must all have a number + // or all have no number. + const int64_t min_value = ast.code == Type::Enum8 ? std::numeric_limits::min() : std::numeric_limits::min(); + const int64_t max_value = ast.code == Type::Enum8 ? std::numeric_limits::max() : std::numeric_limits::max(); + std::vector enum_items; + enum_items.reserve(items.size()); + for (size_t i = 0; i < items.size(); ++i) { + if (i > 1 && items[i].second.has_value() != items[1].second.has_value()) { + throw ValidationError(ast.name + " content is not correct"); + } + // When i was 0, the range check ran for the first name, so this sum cannot overflow. + const int64_t value = items[i].second ? *items[i].second : items[0].second.value_or(1) + static_cast(i); + if (value < min_value || value > max_value) { + throw ValidationError("Value " + std::to_string(value) + " for '" + items[i].first + "' is out of range for " + + ast.name); + } + enum_items.emplace_back(items[i].first, static_cast(value)); + } + + if (ast.code == Type::Enum8) { + return std::make_shared( + Type::CreateEnum8(enum_items) + ); + } else if (ast.code == Type::Enum16) { + return std::make_shared( + Type::CreateEnum16(enum_items) + ); + } + return nullptr; +} + static ColumnRef CreateColumnFromAst(const TypeAst& ast, CreateColumnByTypeSettings settings) { switch (ast.meta) { case TypeAst::Array: { @@ -202,32 +251,9 @@ static ColumnRef CreateColumnFromAst(const TypeAst& ast, CreateColumnByTypeSetti return std::make_shared(columns); } - 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"); - } + case TypeAst::Enum: + return CreateEnumColumn(ast); - 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, - static_cast(ast.elements[i + 1].value) - }); - } - - if (ast.code == Type::Enum8) { - return std::make_shared( - Type::CreateEnum8(enum_items) - ); - } else if (ast.code == Type::Enum16) { - return std::make_shared( - Type::CreateEnum16(enum_items) - ); - } - break; - } case TypeAst::LowCardinality: { const auto& nested = GetASTChildElement(ast, 0); if (settings.low_cardinality_as_wrapped_column) { diff --git a/ut/CreateColumnByType_ut.cpp b/ut/CreateColumnByType_ut.cpp index b84d6e0d..0ae70bce 100644 --- a/ut/CreateColumnByType_ut.cpp +++ b/ut/CreateColumnByType_ut.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -306,6 +307,58 @@ TEST(CreateColumnByType, EnumEscapedNames) { ASSERT_TRUE(col->Type()->IsEqual(Type::CreateEnum8(enum_items))); } +// Enum type names in which some or all names have no number. The expected type name is what the server +// gives for the same text with SELECT toTypeName(defaultValueOfTypeName(type_name)). +class CreateColumnByEnumTypeWithImplicitValues + : public ::testing::TestWithParam> {}; + +TEST_P(CreateColumnByEnumTypeWithImplicitValues, CreateColumnByType) { + const auto& [type_name, expected_name] = GetParam(); + const auto col = CreateColumnByType(type_name); + ASSERT_NE(nullptr, col) << "CreateColumnByType returned nullptr for " << type_name; + EXPECT_EQ(expected_name, col->GetType().GetName()); +} + +INSTANTIATE_TEST_SUITE_P(Enum, CreateColumnByEnumTypeWithImplicitValues, + ::testing::Values(std::make_tuple("Enum8('ONE')", "Enum8('ONE' = 1)"), + std::make_tuple("Enum8('a', 'b')", "Enum8('a' = 1, 'b' = 2)"), + std::make_tuple("Enum8('a', 'b', 'c')", "Enum8('a' = 1, 'b' = 2, 'c' = 3)"), + std::make_tuple("Enum16('a', 'b')", "Enum16('a' = 1, 'b' = 2)"), + std::make_tuple("Enum8('ONE'=1,'TWO')", "Enum8('ONE' = 1, 'TWO' = 2)"), + std::make_tuple("Enum8('a' = 5, 'b', 'c')", "Enum8('a' = 5, 'b' = 6, 'c' = 7)"), + std::make_tuple("Enum8('a' = -3, 'b')", "Enum8('a' = -3, 'b' = -2)"), + std::make_tuple("Enum8('a' = -3, 'b', 'c')", "Enum8('a' = -3, 'b' = -2, 'c' = -1)"), + std::make_tuple("Enum8('a' = 0, 'b')", "Enum8('a' = 0, 'b' = 1)"), + std::make_tuple("Enum8('a', 'b' = 5)", "Enum8('a' = 1, 'b' = 5)"), + std::make_tuple("Enum8('a', 'b' = -1)", "Enum8('b' = -1, 'a' = 1)"), + std::make_tuple("Enum8('a' = 126, 'b')", "Enum8('a' = 126, 'b' = 127)"), + std::make_tuple("Enum8('a' = -128, 'b')", "Enum8('a' = -128, 'b' = -127)"), + std::make_tuple("Enum16('a' = 127, 'b')", "Enum16('a' = 127, 'b' = 128)"), + std::make_tuple("Enum16('a' = 32766, 'b')", "Enum16('a' = 32766, 'b' = 32767)"), + std::make_tuple("Enum8('A', 'a')", "Enum8('A' = 1, 'a' = 2)"), + std::make_tuple("Enum8('x=y', 'b')", "Enum8('x=y' = 1, 'b' = 2)"), + std::make_tuple("Enum8('a' = 1, 'x=y')", "Enum8('a' = 1, 'x=y' = 2)"), + std::make_tuple("Enum8('it\\'s', 'b')", "Enum8('it\\'s' = 1, 'b' = 2)"), + std::make_tuple("Nullable(Enum8('a', 'b'))", "Nullable(Enum8('a' = 1, 'b' = 2))"), + std::make_tuple("Array(Enum8('a', 'b'))", "Array(Enum8('a' = 1, 'b' = 2))"), + std::make_tuple("Map(String, Enum8('a', 'b'))", "Map(String, Enum8('a' = 1, 'b' = 2))"), + std::make_tuple("Tuple(Enum8('a', 'b'), Enum16('c' = 300, 'd'))", + "Tuple(Enum8('a' = 1, 'b' = 2), Enum16('c' = 300, 'd' = 301))"))); + +TEST(CreateColumnByType, EnumImplicitValuesAppend) { + const auto col = CreateColumnByType("Enum8('a' = -3, 'b', 'c')"); + ASSERT_NE(nullptr, col); + const auto e = col->As(); + ASSERT_NE(nullptr, e); + + e->Append(std::string("c")); + e->Append(std::string("b")); + EXPECT_EQ(-1, e->At(0)); + EXPECT_EQ("c", e->NameAt(0)); + EXPECT_EQ(-2, e->At(1)); + EXPECT_EQ("b", e->NameAt(1)); +} + // Round-trip: build a Type from raw values, render it via GetName() (escape), // re-parse the rendered name via CreateColumnByType (unescape) and verify the // raw values survive the render->parse cycle. Uses only escape symbols that are diff --git a/ut/client_ut.cpp b/ut/client_ut.cpp index 79ee9ca8..af57e54b 100644 --- a/ut/client_ut.cpp +++ b/ut/client_ut.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "clickhouse/base/socket.h" #include "clickhouse/types/bignum.h" @@ -970,6 +971,41 @@ TEST_P(ClientCase, Enum) { EXPECT_EQ(sizeof(TEST_DATA)/sizeof(TEST_DATA[0]), row); } +// The table and the client column use the same Enum type name, and some names in it have no number. +// The client must give each name the same number as the server. +TEST_P(ClientCase, Enum_ImplicitValues) { + const std::string type_name = "Enum8('a' = -3, 'b', 'c')"; + client_->Execute("DROP TEMPORARY TABLE IF EXISTS test_clickhouse_cpp_enum_implicit_values;"); + client_->Execute("CREATE TEMPORARY TABLE IF NOT EXISTS test_clickhouse_cpp_enum_implicit_values (id UInt64, e " + type_name + ")"); + + const std::vector names = {"c", "a", "b"}; + const std::vector numbers = {-1, -3, -2}; + { + auto id = std::make_shared(); + auto e = CreateColumnByType(type_name); + ASSERT_NE(nullptr, e); + for (size_t i = 0; i < names.size(); ++i) { + id->Append(static_cast(i)); + e->As()->Append(names[i]); + } + + Block block; + block.AppendColumn("id", id); + block.AppendColumn("e", e); + client_->Insert("test_clickhouse_cpp_enum_implicit_values", block); + } + + size_t row = 0; + client_->Select("SELECT id, e, CAST(e AS Int8) FROM test_clickhouse_cpp_enum_implicit_values ORDER BY id", [&](const Block& block) { + for (size_t i = 0; i < block.GetRowCount(); ++i, ++row) { + ASSERT_LT(row, names.size()); + EXPECT_EQ(names[row], block[1]->As()->NameAt(i)); + EXPECT_EQ(numbers[row], block[2]->As()->At(i)); + } + }); + EXPECT_EQ(names.size(), row); +} + // Live-server round-trip of escape sequences in an Enum type: hand-write a // CREATE TABLE whose Enum labels contain escape sequences, insert matching // values via the streaming BeginInsert/SendInsertBlock/EndInsert API (so the diff --git a/ut/types_ut.cpp b/ut/types_ut.cpp index 6dbe6408..37933abf 100644 --- a/ut/types_ut.cpp +++ b/ut/types_ut.cpp @@ -250,9 +250,18 @@ TEST(TypesCase, IsEqual) { TEST(TypesCase, ErrorEnumContent) { const std::string type_names[] = { "Enum8()", - "Enum8('ONE')", - "Enum8('ONE'=1,'TWO')", "Enum16('TWO'=,'TWO')", + // Some names after the first have a number and some do not. + "Enum8('a' = 5, 'b', 'c' = 10, 'd')", + "Enum8('a' = 1, 'b' = 2, 'c', 'd')", + "Enum8('a', 'b', 'c' = 5)", + "Enum8('a', 'b' = 5, 'c')", + // A name without quotes. + "Enum8(aa, 1)", + // An explicit or implicit number out of the range of the type. + "Enum8('a' = 128)", + "Enum8('a' = 126, 'b', 'c')", + "Enum16('a' = 32766, 'b', 'c')", }; for (const auto& type_name : type_names) {