From a03e16fcb1668102c8a54bc0488659f7022e3149 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Tue, 19 Dec 2023 17:06:22 +0800 Subject: [PATCH] Support enum with index 0 (#8533) (#8549) close pingcap/tiflash#8311 --- dbms/src/DataTypes/DataTypeEnum.h | 6 ++++- dbms/src/Flash/Coprocessor/ArrowColCodec.cpp | 2 +- .../ddl/alter_column_enum.test | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/dbms/src/DataTypes/DataTypeEnum.h b/dbms/src/DataTypes/DataTypeEnum.h index d2251e85662..7a510e57f8c 100644 --- a/dbms/src/DataTypes/DataTypeEnum.h +++ b/dbms/src/DataTypes/DataTypeEnum.h @@ -81,11 +81,15 @@ class DataTypeEnum final : public IDataTypeEnum std::string getName() const override { return name; } const char * getFamilyName() const override; - const StringRef & getNameForValue(const FieldType & value) const + StringRef getNameForValue(const FieldType & value) const { const auto it = value_to_name_map.find(value); if (it == std::end(value_to_name_map)) + { + if (!value) + return {}; throw Exception{"Unexpected value " + toString(value) + " for type " + getName(), ErrorCodes::LOGICAL_ERROR}; + } return it->second; } diff --git a/dbms/src/Flash/Coprocessor/ArrowColCodec.cpp b/dbms/src/Flash/Coprocessor/ArrowColCodec.cpp index c5c16028b1b..ed8b900cf6d 100644 --- a/dbms/src/Flash/Coprocessor/ArrowColCodec.cpp +++ b/dbms/src/Flash/Coprocessor/ArrowColCodec.cpp @@ -280,7 +280,7 @@ void flashEnumColToArrowCol( } } auto enum_value = static_cast(flash_col->getElement(i)); - if (enum_value == 0 || enum_value > enum_value_size) + if (enum_value > enum_value_size) throw TiFlashException("number of enum overflow enum boundary", Errors::Coprocessor::Internal); TiDBEnum ti_enum(enum_value, enum_type->getNameForValue(static_cast(enum_value))); dag_column.append(ti_enum); diff --git a/tests/fullstack-test2/ddl/alter_column_enum.test b/tests/fullstack-test2/ddl/alter_column_enum.test index e3bf0016a01..90759cce7f7 100644 --- a/tests/fullstack-test2/ddl/alter_column_enum.test +++ b/tests/fullstack-test2/ddl/alter_column_enum.test @@ -41,4 +41,28 @@ mysql> set session tidb_isolation_read_engines='tiflash'; select * from test.a1 | 4 | D | +----+------+ +mysql> SET SESSION SQL_MODE = ''; insert into test.a1 values (5,''); + +mysql> set session tidb_isolation_read_engines='tiflash'; select * from test.a1 order by id; ++----+------+ +| id | name | ++----+------+ +| 1 | A | +| 2 | B | +| 3 | C | +| 4 | D | +| 5 | | ++----+------+ + +mysql> set session tidb_isolation_read_engines='tiflash'; select name+0 from test.a1 order by id; ++--------+ +| name+0 | ++--------+ +| 1 | +| 2 | +| 3 | +| 4 | +| 0 | ++--------+ + mysql> drop table if exists test.a1