Skip to content

Commit

Permalink
Support enum with index 0 (pingcap#8533) (pingcap#8549)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Dec 19, 2023
1 parent a5a9e99 commit a03e16f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
6 changes: 5 additions & 1 deletion dbms/src/DataTypes/DataTypeEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Flash/Coprocessor/ArrowColCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ void flashEnumColToArrowCol(
}
}
auto enum_value = static_cast<UInt64>(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<const DataTypeEnum16::FieldType>(enum_value)));
dag_column.append(ti_enum);
Expand Down
24 changes: 24 additions & 0 deletions tests/fullstack-test2/ddl/alter_column_enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a03e16f

Please sign in to comment.