Repository navigation
Do not throw on a comparison of an Enum with a non-member string literal - #118398
alexey-milovidov wants to merge 6 commits into
Conversation
…teral With the default `validate_enum_literals_in_operators = 0`, comparing an `Enum` column with a string literal that is not a member of the enum threw `UNKNOWN_ELEMENT_OF_ENUM` in two cases, while the setting documents that such a literal must not throw: - for `!=` on any column: the operator filter in `FunctionComparison::executeWithConstString` read `!equals && not_equals`, so the tolerant path was taken for every operator *except* the one it was meant to cover, and `!=` fell through to `convertFieldToType`, which throws; - for every comparison operator on a column that belongs to the sorting or partition key: `KeyCondition` converts the literal to the key type before the predicate is ever evaluated, so the same query over the same data answered or failed depending on the physical layout of the table. A literal that converts to no value of the compared type is `false` for `=`, `<`, `>`, `<=`, `>=` and `true` for `!=`, as `toUInt8(1) = '257'` already is. Enum comparison now follows that rule for every operator, and key analysis declines the atom instead of converting it, leaving the predicate to be evaluated. With `validate_enum_literals_in_operators = 1` the comparison still throws, now for a key and a non-key column alike. Closes: #111545 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [dcda1cc] Summary: ❌
AI ReviewSummaryThis PR extends the enum-non-member literal fix into Findings❌ Blockers
Final VerdictChanges requested. LLVM Coverage ReportMeasured on commit dcda1cc.
Changed lines: Changed C/C++ lines covered: 81/89 (91.01%) · Uncovered code |
|
TL;DR: the 1. The remaining throw CREATE TABLE t_bf (e Enum8('a' = 1, 'b' = 2), INDEX ibf e TYPE bloom_filter GRANULARITY 1)
ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t_bf VALUES ('a'), ('b');
SELECT count() FROM t_bf WHERE e = '4';
-- Code: 691. DB::Exception: Unknown element '4' for enum ... (UNKNOWN_ELEMENT_OF_ENUM)
-- returns 0 without the indexMeasured on 26.9.1.1; the diff does not touch this path. The stack is 2. Verified the rest of the index coverage both ways The 3. A bonus this PR quietly delivers, probed correct
For reference, the earlier fix attempt #114035 for the same issue also patches the |
Build profile diff (arm_release)Commit See the job log for details. |
…eral-comparison-111545
`KeyCondition` now declines an atom whose string constant names no member of the enum, but the `bloom_filter` skip index builds its own RPN: `MergeTreeIndexConditionBloomFilter::traverseTreeEquals` converts the constant to the indexed type to hash it, and `bloom_filter` indexes accept `Enum` columns. So with `INDEX ... TYPE bloom_filter` on an enum column (or on an `Array(Enum)`, or on `mapValues` of a map with an enum value type), `e = '4'`, `e != '4'`, `has(a, '4')`, `indexOf(a, '4')`, `hasAny`/`hasAll`, `mapContainsValue` and an `ARRAY JOIN`ed `x = '4'` still threw `UNKNOWN_ELEMENT_OF_ENUM` with the default `validate_enum_literals_in_operators = 0`, while the same query over the same data answered without the index - the outcome depended on the physical layout again. Apply the same rule there: decline the atom and let the predicate be evaluated. The check moved from `KeyCondition.cpp` to `stringConstantIsNotAnEnumMember` next to `convertFieldToType`, which is what it guards, and is applied at every site of the bloom-filter condition builder that converts a comparison constant, including the per-element conversion for a constant array. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`m['k'] = c` and `m['k'] IN (...)` over an `INDEX ... mapKeys(m)` or `mapValues(m)` never converted the comparison constant to the indexed type: they hashed the `Field` as it arrived, and `BloomFilterHash::hashWithField` reads it as the indexed type. So an `Enum`, a `DateTime` or a `UUID` compared with a string literal - which the comparison itself leaves a `String`, see `FunctionComparison::executeWithConstString` - threw `Bad get: has String, requested Int64` (`BAD_GET`) rather than answering, and threw it for a literal naming a *member* too, not only for the non-member literal the rest of this branch is about. The same holds for an `Enum` map key in the `arrayElement` form, where the key literal is not deserialized with the key type as it is for a `map.key_<key>` subcolumn. Both branches now go through one `hashMapConstant`, which declines a string literal naming no member of an `Enum`, converts the constant to the indexed type, declines when it converts to no value of that type, and only then hashes it. Declining is not what makes the answers right: a constant that does convert is still hashed and the index still prunes, which `05183_bloom_filter_map_constant_conversion` asserts with `EXPLAIN indexes = 1`. `05101_enum_non_member_string_literal` gains the enum map cases in both `optimize_functions_to_subcolumns` forms; the new test covers the `DateTime`, `UUID` and `Enum`-key cases against an unindexed control table.
…eral-comparison-111545
…`Enum` member `m['k']` throws `UNKNOWN_ELEMENT_OF_ENUM` when `k` names no member of the `Enum` key type of `m`. An index on `mapValues(m)` ignores the key, so for `m['missing'] = 'x'` or `m['missing'] IN (...)` it could prune every granule by the value alone, and the query returned `0` instead of throwing. Decline the atom in `tryResolveMapInfoFromNode`, which covers both the `equals` and the `IN` branches. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
|
||
| Field string_value = left_const ? left_const->getField() : right_const->getField(); | ||
|
|
||
| /// A string literal that is not a member of the enum converts to no value of the compared type, so it |
There was a problem hiding this comment.
This still misses wrapped enum columns. type_to_compare here is the original argument type, so for Nullable(Enum8(...)) or LowCardinality(Enum8(...)) both typeid_cast<const DataTypeEnum8 *>(type_to_compare.get()) and typeid_cast<const DataTypeEnum16 *>(type_to_compare.get()) return nullptr. The new fast path is then skipped, and the next convertFieldToType(string_value, *type_to_compare, ...) unwraps those wrappers and still throws UNKNOWN_ELEMENT_OF_ENUM, so comparisons like e = '4' and e != '4' keep failing at pure defaults on wrapped enum columns even though the PR/changelog says these comparisons no longer throw. Please run the check on removeLowCardinalityAndNullable(type_to_compare) (or have stringConstantIsNotAnEnumMember unwrap wrappers) and add a regression test for Nullable(Enum) / LowCardinality(Enum).
|
Will be fixed in #122672 |
Comparing an
Enumcolumn with a string literal that is not a member of the enum threwUNKNOWN_ELEMENT_OF_ENUMin three cases, even with the defaultvalidate_enum_literals_in_operators = 0,which documents that such a literal must not throw:
!=on any column: the operator filter inFunctionComparison::executeWithConstStringreads!equals && not_equals, so the tolerant path was taken for every operator except the one it was meantto cover, and
!=fell through toconvertFieldToType, which throws;KeyConditionconverts the literal to the key type before the predicate is ever evaluated, so the same query over the
same data answered or failed depending on the physical layout of the table;
bloom_filterskip index: that index builds its own RPN, andMergeTreeIndexConditionBloomFilter::traverseTreeEqualsconverts the constant to the indexed type tohash it.
bloom_filterindexes acceptEnumcolumns, soe = '4',e != '4',has(a, '4'),indexOf(a, '4'),hasAny/hasAll,mapContainsValueand anARRAY JOINedx = '4'threw thereas well.
A literal that converts to no value of the compared type is
falsefor=,<,>,<=,>=andtruefor
!=, astoUInt8(1) = '257'already is. Enum comparison now follows that rule for every operator, andboth key analysis and the
bloom_filtercondition builder decline the atom instead of converting it,leaving the predicate to be evaluated. With
validate_enum_literals_in_operators = 1the comparison stillthrows, now the same way whatever the physical layout is.
The map branches of the same
bloom_filtercondition builder -m['k'] = candm['k'] IN (...)over anINDEX ... mapKeys(m)ormapValues(m)- turned out never to convert the constant at all: they hashed theFieldas it arrived, andBloomFilterHash::hashWithFieldreads it as the indexed type. So the enum literalthrew
Bad get: has String, requested Int64(BAD_GET) there rather thanUNKNOWN_ELEMENT_OF_ENUM, and itthrew for a literal naming a member too. Every type whose string literal the comparison itself leaves a
Stringhit the same wall, soMap(String, DateTime)andMap(String, UUID)threw alike, as did anEnummap key in the
arrayElementform, where the key literal is not deserialized with the key type the way amap.key_<key>subcolumn's is. Both branches now convert the constant to the indexed type before hashing itand decline the atom when it converts to no value of that type.
The map atom is also declined when its key is a string naming no member of the map's
Enumkey type.m['missing']throwsUNKNOWN_ELEMENT_OF_ENUMthere, but an index onmapValues(m)ignores the key, so itcould prune every granule by the value alone and turn that exception into an empty result.
Closes: #111545
Related: #72686
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Comparing an
Enumcolumn with a string literal that is not a member of the enum no longer throwsUNKNOWN_ELEMENT_OF_ENUMwith the defaultvalidate_enum_literals_in_operators = 0. Previously!=threwon any column, and every comparison operator threw when the column belonged to the sorting or partition key
or was covered by a
bloom_filterskip index. Also fixed comparing a key or a value of aMapcolumncovered by a
bloom_filterindex onmapKeysormapValueswith a string literal, which threwBAD_GETfor an
Enum, aDateTimeor aUUID.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/118398]