Merge pull request #72686 from bigo-sg/enum-excep

Allow unknown values in set that are not present in Enum
This commit is contained in:
Vladimir Cherkasov 2024-12-09 08:29:17 +00:00 committed by GitHub
commit 7003db7efd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 64 additions and 18 deletions

View File

@ -10,7 +10,7 @@ namespace DB
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int UNKNOWN_ELEMENT_OF_ENUM;
}
template <typename T>
@ -38,7 +38,7 @@ public:
{
auto it = value_to_name_map.find(value);
if (it == value_to_name_map.end())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unexpected value {} in enum", toString(value));
throw Exception(ErrorCodes::UNKNOWN_ELEMENT_OF_ENUM, "Unexpected value {} in enum", toString(value));
return it;
}

View File

@ -96,7 +96,9 @@ void SerializationEnum<Type>::deserializeWholeText(IColumn & column, ReadBuffer
{
std::string field_name;
readStringUntilEOF(field_name, istr);
assert_cast<ColumnType &>(column).getData().push_back(ref_enum_values.getValue(StringRef(field_name)));
Type t;
if (ref_enum_values.tryGetValue(t, StringRef(field_name)))
assert_cast<ColumnType &>(column).getData().push_back(t);
}
}

View File

@ -38,6 +38,7 @@ namespace ErrorCodes
extern const int LOGICAL_ERROR;
extern const int TYPE_MISMATCH;
extern const int UNEXPECTED_DATA_AFTER_PARSED_VALUE;
extern const int UNKNOWN_ELEMENT_OF_ENUM;
}
@ -584,6 +585,8 @@ Field convertFieldToTypeImpl(const Field & src, const IDataType & type, const ID
throw;
}
if (col->empty())
return Field(Null());
Field parsed = (*col)[0];
return convertFieldToType(parsed, type, from_type_hint, format_settings);
}
@ -660,7 +663,19 @@ static bool decimalEqualsFloat(Field field, Float64 float_value)
std::optional<Field> convertFieldToTypeStrict(const Field & from_value, const IDataType & from_type, const IDataType & to_type, const FormatSettings & format_settings)
{
Field result_value = convertFieldToType(from_value, to_type, &from_type, format_settings);
//Field result_value = convertFieldToType(from_value, to_type, &from_type, format_settings);
Field result_value;
try
{
result_value = convertFieldToType(from_value, to_type, &from_type, format_settings);
}
catch (Exception & e)
{
if (isEnum(to_type) && e.code() == ErrorCodes::UNKNOWN_ELEMENT_OF_ENUM)
return {};
throw;
}
if (Field::isDecimal(from_value.getType()) && Field::isDecimal(result_value.getType()))
{

View File

@ -43,7 +43,7 @@ SELECT * FROM d;
SELECT '---';
INSERT INTO m VALUES ('b');
SELECT toString(v) FROM (SELECT v FROM d ORDER BY v) FORMAT Null; -- { serverError BAD_ARGUMENTS }
SELECT toString(v) FROM (SELECT v FROM d ORDER BY v) FORMAT Null; -- { serverError UNKNOWN_ELEMENT_OF_ENUM}
DROP TABLE m;

View File

@ -3,4 +3,4 @@ INSERT INTO enum VALUES ('hello');
SELECT count() FROM enum WHERE x = 'hello';
SELECT count() FROM enum WHERE x = 'world';
SELECT count() FROM enum WHERE x = 'xyz'; -- { serverError UNKNOWN_ELEMENT_OF_ENUM }
SELECT count() FROM enum WHERE x = 'xyz';

View File

@ -324,13 +324,13 @@ JSON
"rows": 3,
"exception": "Code: 36. : Unexpected value 99 in enum: While executing JSONRowOutputFormat. (BAD_ARGUMENTS) "
"exception": "Code: 691. : Unexpected value 99 in enum: While executing JSONRowOutputFormat. (UNKNOWN_ELEMENT_OF_ENUM) "
}
JSONEachRow
{"x":1,"s":"str1","y":"a"}
{"x":2,"s":"str2","y":"a"}
{"x":3,"s":"str3","y":"a"}
{"exception": "Code: 36. : Unexpected value 99 in enum: While executing JSONEachRowRowOutputFormat. (BAD_ARGUMENTS) "}
{"exception": "Code: 691. : Unexpected value 99 in enum: While executing JSONEachRowRowOutputFormat. (UNKNOWN_ELEMENT_OF_ENUM) "}
JSONCompact
{
"meta":
@ -358,19 +358,19 @@ JSONCompact
"rows": 3,
"exception": "Code: 36. : Unexpected value 99 in enum: While executing JSONCompactRowOutputFormat. (BAD_ARGUMENTS) "
"exception": "Code: 691. : Unexpected value 99 in enum: While executing JSONCompactRowOutputFormat. (UNKNOWN_ELEMENT_OF_ENUM) "
}
JSONCompactEachRow
[1, "str1", "a"]
[2, "str2", "a"]
[3, "str3", "a"]
["Code: 36. : Unexpected value 99 in enum: While executing JSONCompactEachRowRowOutputFormat. (BAD_ARGUMENTS) "]
["Code: 691. : Unexpected value 99 in enum: While executing JSONCompactEachRowRowOutputFormat. (UNKNOWN_ELEMENT_OF_ENUM) "]
JSONObjectEachRow
{
"row_1": {"x":1,"s":"str1","y":"a"},
"row_2": {"x":2,"s":"str2","y":"a"},
"row_3": {"x":3,"s":"str3","y":"a"},
"exception": "Code: 36. : Unexpected value 99 in enum: While executing JSONObjectEachRowRowOutputFormat. (BAD_ARGUMENTS) "
"exception": "Code: 691. : Unexpected value 99 in enum: While executing JSONObjectEachRowRowOutputFormat. (UNKNOWN_ELEMENT_OF_ENUM) "
}
XML
<?xml version='1.0' encoding='UTF-8' ?>
@ -409,7 +409,7 @@ XML
</row>
</data>
<rows>3</rows>
<exception>Code: 36. : Unexpected value 99 in enum: While executing XMLRowOutputFormat. (BAD_ARGUMENTS) </exception>
<exception>Code: 691. : Unexpected value 99 in enum: While executing XMLRowOutputFormat. (UNKNOWN_ELEMENT_OF_ENUM) </exception>
</result>
With parallel formatting
JSON
@ -697,10 +697,10 @@ JSON
"rows": 0,
"exception": "Code: 36. : Unexpected value 99 in enum: While executing JSONRowOutputFormat. (BAD_ARGUMENTS) "
"exception": "Code: 691. : Unexpected value 99 in enum: While executing JSONRowOutputFormat. (UNKNOWN_ELEMENT_OF_ENUM) "
}
JSONEachRow
{"exception": "Code: 36. : Unexpected value 99 in enum: While executing JSONEachRowRowOutputFormat. (BAD_ARGUMENTS) "}
{"exception": "Code: 691. : Unexpected value 99 in enum: While executing JSONEachRowRowOutputFormat. (UNKNOWN_ELEMENT_OF_ENUM) "}
JSONCompact
{
"meta":
@ -726,13 +726,13 @@ JSONCompact
"rows": 0,
"exception": "Code: 36. : Unexpected value 99 in enum: While executing JSONCompactRowOutputFormat. (BAD_ARGUMENTS) "
"exception": "Code: 691. : Unexpected value 99 in enum: While executing JSONCompactRowOutputFormat. (UNKNOWN_ELEMENT_OF_ENUM) "
}
JSONCompactEachRow
["Code: 36. : Unexpected value 99 in enum: While executing JSONCompactEachRowRowOutputFormat. (BAD_ARGUMENTS) "]
["Code: 691. : Unexpected value 99 in enum: While executing JSONCompactEachRowRowOutputFormat. (UNKNOWN_ELEMENT_OF_ENUM) "]
JSONObjectEachRow
{
"exception": "Code: 36. : Unexpected value 99 in enum: While executing JSONObjectEachRowRowOutputFormat. (BAD_ARGUMENTS) "
"exception": "Code: 691. : Unexpected value 99 in enum: While executing JSONObjectEachRowRowOutputFormat. (UNKNOWN_ELEMENT_OF_ENUM) "
}
XML
<?xml version='1.0' encoding='UTF-8' ?>
@ -756,7 +756,7 @@ XML
<data>
</data>
<rows>0</rows>
<exception>Code: 36. : Unexpected value 99 in enum: While executing XMLRowOutputFormat. (BAD_ARGUMENTS) </exception>
<exception>Code: 691. : Unexpected value 99 in enum: While executing XMLRowOutputFormat. (UNKNOWN_ELEMENT_OF_ENUM) </exception>
</result>
With parallel formatting
JSON

View File

@ -0,0 +1,6 @@
a
a
a
a
a
a

View File

@ -0,0 +1,22 @@
DROP TABLE IF EXISTS t_enum_in_unknown_value;
CREATE TABLE t_enum_in_unknown_value (e Enum('a'=1, 'b'=2)) ENGINE=Memory;
INSERT INTO t_enum_in_unknown_value VALUES ('a');
SELECT * FROM t_enum_in_unknown_value;
SELECT * FROM t_enum_in_unknown_value WHERE e IN ('a');
SELECT * FROM t_enum_in_unknown_value WHERE e NOT IN ('a');
SELECT * FROM t_enum_in_unknown_value WHERE e IN ('a', 'b');
SELECT * FROM t_enum_in_unknown_value WHERE e NOT IN ('a', 'b');
SELECT * FROM t_enum_in_unknown_value WHERE e IN ('a', 'c');
SELECT * FROM t_enum_in_unknown_value WHERE e NOT IN ('a', 'c');
SELECT * FROM t_enum_in_unknown_value WHERE e IN ('a', 'b', 'c');
SELECT * FROM t_enum_in_unknown_value WHERE e NOT IN ('a', 'b', 'c');
SELECT * FROM t_enum_in_unknown_value WHERE e IN ('c');
SELECT * FROM t_enum_in_unknown_value WHERE e NOT IN ('c');