Skip to content

Respect validate_enum_literals_in_operators in index analysis and notEquals - #122672

Merged
nihalzp merged 4 commits into
masterfrom
vdimir/enum-literal-key-condition
Oct 2, 2026
Merged

nihalzp merged 4 commits into
masterfrom
vdimir/enum-literal-key-condition

Conversation

@vdimir

@vdimir vdimir commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Closes: #122235
Closes: #111545
Related: #121723

With validate_enum_literals_in_operators = 0, enum_col = 'unknown' is false and enum_col != 'unknown' is true for every row, but three paths still threw UNKNOWN_ELEMENT_OF_ENUM: KeyCondition converted the literal to the key type whenever the enum column was in the primary or partition key, so the same query failed only on key columns; the bloom_filter skip index did the same conversion; and an inverted guard in executeWithConstString made notEquals throw while the ordering operators silently returned false. KeyCondition and the bloom filter now fold such comparisons to constants, and notEquals takes the same shortcut as equals. <, <=, >, >= against an unknown element keep throwing, as there is no position to compare against.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Comparing an Enum column with a string literal that is not one of its elements no longer throws UNKNOWN_ELEMENT_OF_ENUM when validate_enum_literals_in_operators is disabled (the default) and the column is part of the primary key, the partition key, or a bloom_filter index. != with such a literal now returns true for every row instead of throwing. Closes #122235, #111545.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/122672]

Version info

  • Merged into: 26.10.1.1266-master (included in 26.10 and later)
  • Backported to: 26.9.12.10, 26.8.22.14, 26.7.25.7

…n `notEquals`

With the setting off, comparing an `Enum` column with a string literal that
names none of its elements must not throw: `equals` is false and `notEquals`
is true for every row. Three paths ignored that.

`KeyCondition` converted the literal to the key type and threw
`UNKNOWN_ELEMENT_OF_ENUM` whenever the enum column was part of the primary or
partition key. It now folds `equals` to `ALWAYS_FALSE` and `notEquals` to
`ALWAYS_TRUE`, declines the other operators, and reads the setting once in
the constructor.

The `bloom_filter` skip index did the same conversion in `traverseTreeEquals`.
`equals` now folds to `ALWAYS_FALSE` there, including the
`arrayJoin(col) = const` form.

The operator guard in `executeWithConstString` was inverted, so `notEquals`
fell through to the throwing conversion while the ordering operators silently
returned false. Only `equals` and `notEquals` take the shortcut now; `<`, `<=`,
`>`, `>=` against an unknown element keep throwing.

Add `isUnknownEnumElement` to `DataTypeEnum`, shared by both index paths.

Closes: #122235
Closes: #111545
Related: #121723

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a52b196]

Summary: ✅


AI Review

Summary

This PR fixes the KeyCondition, bloom_filter equals, and != cases for unknown Enum literals when validate_enum_literals_in_operators = 0, and the new IN coverage closes the previously reported set-conversion gap. I still see one blocker in the remaining bloom_filter search adapters: tables with a bloom_filter over Array(Enum) or Map(... Enum ...) can still turn a query that should evaluate to a simple miss into UNKNOWN_ELEMENT_OF_ENUM, so the physical design can still change query validity.

Findings
  • ❌ Blockers
    • src/Storages/MergeTree/MergeTreeIndexBloomFilter.cpp:1040 The new guard only covers the equals-derived branches. The other FunctionArrayIndex-based carriers from the same contract still feed an unknown enum label into a throwing conversion: has(arr, 'missing') / indexOf(arr, 'missing') != 0 still go through convertConstantForArrayIndexFunction here, and the sibling mapContains* / has(map, ...) path still does the same at src/Storages/MergeTree/MergeTreeIndexBloomFilter.cpp:1185. Runtime semantics already tolerate a miss here — tests/queries/0_stateless/02893_array_enum_has_hasAny.sql:8 expects has(Array(Enum), 'x') to return no rows, not throw — so a table-level bloom_filter can still reintroduce UNKNOWN_ELEMENT_OF_ENUM even after this fix.
    • Suggested fix: apply the same unknown-enum fold/decline logic to every bloom_filter carrier that reuses convertConstantForArrayIndexFunction / createColumnFromConstantArray for Array(Enum) and Map(... Enum ...), then add focused coverage for has, hasAny/hasAll, and mapContains* with and without skip indexes.
Final Verdict
  • Status: ❌ Block
  • Minimum required actions: finish the bloom_filter fix for the remaining array-search and map-search carriers, and add regression coverage proving that enabling skip indexes does not change the result from a simple miss into UNKNOWN_ELEMENT_OF_ENUM.

LLVM Coverage Report

Measured on commit a52b196.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 51/55 (92.73%) · Uncovered code

Full report · Diff report


/// With validation off, `equals` against a name the enum lacks is constant false for every row,
/// so the conversion below must not throw; `KeyCondition` folds the same way.
if (function_name == "equals" && !validate_enum_literals_in_operators && isUnknownEnumElement(*actual_type, value_field))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

equals is now guarded, but the other bloom_filter derivations from #111545 still feed the missing enum label into a throwing conversion. has(arr, 'c') goes through convertConstantForArrayIndexFunction at 1040, hasAny/hasAll go through createColumnFromConstantArray at 1067, and mapContains* reaches the same conversion at 1184. With validate_enum_literals_in_operators = 0, the runtime functions already tolerate an unknown label (for example tests/queries/0_stateless/02893_array_enum_has_hasAny.sql expects has(value, 'x') to return no rows, not throw), so a table-level bloom_filter index still changes those queries back into UNKNOWN_ELEMENT_OF_ENUM. If this PR is closing #111545, these carriers need the same setting-aware fold/skip logic and coverage too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Show repro which current patch does not cover

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concrete repro on the current PR head:

DROP TABLE IF EXISTS t_enum_bf_has;
CREATE TABLE t_enum_bf_has
(
    arr Array(Enum8('a' = 1, 'b' = 2)),
    INDEX bf_arr arr TYPE bloom_filter GRANULARITY 1
)
ENGINE = MergeTree
ORDER BY tuple()
SETTINGS index_granularity = 1, index_granularity_bytes = 0, min_bytes_for_wide_part = 0;

INSERT INTO t_enum_bf_has VALUES (['a']), (['b']);

SELECT count() FROM t_enum_bf_has WHERE has(arr, 'missing')
    SETTINGS validate_enum_literals_in_operators = 0, use_skip_indexes = 0; -- 0
SELECT count() FROM t_enum_bf_has WHERE has(arr, 'missing')
    SETTINGS validate_enum_literals_in_operators = 0, use_skip_indexes = 1; -- Code: 691

The runtime contract is already covered by tests/queries/0_stateless/02893_array_enum_has_hasAny.sql:8, which expects has(Array(Enum), 'x') to return no rows. The index path still reaches convertConstantForArrayIndexFunction at src/Storages/MergeTree/MergeTreeIndexBloomFilter.cpp:1040, so it calls convertFieldToType(... Enum ...) and throws before scan fallback can happen. The same untouched conversion is also reused by hasAny/hasAll at 1067 and by mapContains* / has(map, ...) at 1185.

@nihalzp nihalzp self-assigned this Sep 28, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Sep 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a52b196e5 with master be50c1b5e (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

560 translation units recompiled, 4876 s compile time in total, 560 of them have a recent master baseline.

Job report

@vdimir

vdimir commented Oct 1, 2026 •

Copy link
Copy Markdown
Member Author

@nihalzp feel free to review whenever you have a chance

@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 6, 2026
robot-clickhouse added a commit that referenced this pull request Oct 6, 2026
Cherry pick #122672 to 26.7: Respect validate_enum_literals_in_operators in index analysis and notEquals
robot-clickhouse added a commit that referenced this pull request Oct 6, 2026
robot-ch-test-poll4 added a commit that referenced this pull request Oct 6, 2026
Cherry pick #122672 to 26.8: Respect validate_enum_literals_in_operators in index analysis and notEquals
robot-clickhouse added a commit that referenced this pull request Oct 6, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 6, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 6, 2026
Backport #122672 to 26.9: Respect validate_enum_literals_in_operators in index analysis and notEquals
PedroTadim added a commit that referenced this pull request Oct 8, 2026
Backport #122672 to 26.7: Respect validate_enum_literals_in_operators in index analysis and notEquals
@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

@vdimir the 26.7 backport (#124247, merged) and the open 26.8 one (#124276) fail 05243_enum_unknown_literal_key_condition at SELECT count() FROM t_enum_bf WHERE e != 'c' with UNKNOWN_ELEMENT_OF_ENUM, deterministically (26.7 tip, #124276), so Stateless tests (amd_asan_ubsan, distributed plan, parallel) is red on 26.7 since the merge.

26.7 and 26.8 predate #116544, so MergeTreeIndexConditionBloomFilter::traverseFunction there still sends notEquals to traverseTreeEquals, where the new guard covers only equals and the convertFieldToType right after it throws. Master and 26.9 never build a notEquals atom for this index.

The client stops at that statement, so the rest of the file has not run on those branches yet. One more difference there: they also predate #117803, which adds validate_enum_literals_in_operators to the query condition cache key, and the validate_enum_literals_in_operators = 1 tail of the file re-runs t_enum_nokey WHERE e = 'c'.

For 26.7/26.8, do you prefer (a) extending that bloom filter guard to notEquals in both backports (one branch-only hunk), or (b) v26.7-must-backport on #116544 (which also makes force_data_skipping_indices report INDEX_NOT_USED for a !=-only condition)?

kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 9, 2026
…_operators in index analysis and notEquals
@vdimir

vdimir commented Oct 9, 2026 •

Copy link
Copy Markdown
Member Author

Will revert backport #124876 or fix #124878

clickhouse-gh Bot added a commit that referenced this pull request Oct 9, 2026
Backport #122672 to 26.8: Respect validate_enum_literals_in_operators in index analysis and notEquals
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 10, 2026
The row pinned `CAST('a', 'Enum8(\'a\' = 1)') < 'zz'` at 0 to show this
change leaves Enum targets alone. Since ClickHouse#122672 master rejects a
non-member literal in an ordering comparison with
UNKNOWN_ELEMENT_OF_ENUM before the conversion this change touches, so
the row can no longer reach that code and master's own
05243_enum_unknown_literal_key_condition covers the new behaviour.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

analyzer-important comp-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.4-must-backport

Projects

None yet

7 participants