Skip to content

Fix flaky test 02030_function_mapContainsKeyLike - #101538

Merged
Avogar merged 2 commits into
ClickHouse:masterfrom
groeneai:fix/flaky-02030-mapContainsKeyLike
Apr 7, 2026
Merged

Avogar merged 2 commits into
ClickHouse:masterfrom
groeneai:fix/flaky-02030-mapContainsKeyLike

Conversation

@groeneai

@groeneai groeneai commented Apr 1, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

  • Not required

What this PR does

Fixes flaky test 02030_function_mapContainsKeyLike by wrapping Map column output with mapSort() to produce deterministic output regardless of internal Map entry ordering.

The problem: Under rare CI conditions, MergeTree returns Map entries in reversed order (e.g., {'K2':'V2','K1':'V1'} instead of {'K1':'V1','K2':'V2'}), causing test output to differ from the reference file.

CIDB evidence: 5 result-differs failures across 60 days in 4 distinct PRs + 1 master hit. The only consistent setting across all failures is max_insert_threads=3, but the exact trigger could not be isolated after 750+ targeted local reproduction attempts across multiple setting combinations.

PR Check Date
master amd_msan, WasmEdge 2026-03-31
#101158 arm_binary 2026-03-30
#101093 amd_debug 2026-03-29
#91170 amd_debug, s3 2026-03-31
(Fast test) Fast test 2026-02-18

All failures show the same pattern: Map key-value pairs in MergeTree-stored columns are consistently reversed in all rows. Literal Maps (from mapExtractKeyLike) are unaffected.

The fix: mapSort() normalizes Map entry order by sorting keys. This makes the test output deterministic without changing what the test validates — the mapContainsKeyLike and mapExtractKeyLike function correctness is tested through WHERE clause filtering and inline expressions, which are unaffected by this change.

Verification: 100/100 passes with full randomization locally.

🤖 Generated with Claude Code

Version info

  • Merged into: 26.4.1.587

Wrap Map column output with mapSort() to make test output
deterministic regardless of internal Map entry ordering.

The test validates mapContainsKeyLike/mapExtractKeyLike function
correctness via WHERE clause filtering — the Map column display
in SELECT output is incidental. Under rare CI conditions, MergeTree
may return Map entries in non-insertion order, causing result diffs.

CIDB evidence: 5 result-differs failures in 60 days across 4 distinct
PRs + master, all showing consistent Map key reversal (K1,K2 → K2,K1).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@groeneai

groeneai commented Apr 1, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR Validation Gate

Session: cron:clickhouse-ci-task-worker:20260401-164500

a) Deterministic repro?
No — despite 750+ targeted iterations (500 via test runner with full randomization + 250 manual with specific setting combinations), the Map key reversal could not be triggered locally. The failure rate in CI is ~0.001% (5 hits in 60 days). All 5 failures share max_insert_threads=3 but testing that setting alone and in combination with other CI settings (page_cache_inject_eviction, split_ranges_injection_probability, use_skip_indexes_on_data_read, etc.) did not reproduce.

b) Root cause explained?
Partial — CIDB analysis confirms consistent Map entry reversal (K1,K2 → K2,K1) in ALL MergeTree-stored rows, while literal Maps are unaffected. This points to a read/serialization path issue in MergeTree that reorders Map entries under very specific (unidentified) conditions. The trigger involves max_insert_threads=3 (present in all failures) but requires additional unknown conditions.

c) Fix matches root cause?
The fix makes the test robust to Map entry ordering by using mapSort(). Since this test validates mapContainsKeyLike/mapExtractKeyLike function correctness (not Map ordering), this is appropriate. The WHERE clause filtering and inline expressions are unaffected.

d) Test intent preserved?
Yes — the test still validates that mapContainsKeyLike correctly filters rows and that mapExtractKeyLike correctly extracts matching entries. The mapSort() only affects the display column, not the filtering logic.

e) Demonstrated in both directions?
Cannot demonstrate failure direction (see a). Fix direction: 100/100 passes with full randomization.

f) Fix is general, not a narrow patch?
This is a test-only change. No source code modification needed.

@groeneai

groeneai commented Apr 1, 2026

Copy link
Copy Markdown
Collaborator Author

cc @Avogar — could you review this? It wraps Map column output with mapSort() in test 02030_function_mapContainsKeyLike to make the output deterministic — CIDB shows 5 rare result-differs failures in 60 days where MergeTree returns Map entries in reversed key order.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Apr 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [260087f]

Summary: ✅


AI Review

Summary

This PR stabilizes 02030_function_mapContainsKeyLike by sorting Map output with mapSort in two result-bearing queries, removing order-dependent flakiness without changing the tested predicate semantics of mapContainsKeyLike and mapExtractKeyLike. I found no correctness, safety, performance, or compatibility issues in the change scope, and no duplicate bot findings to account for.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ➖
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Apr 1, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@Avogar Avogar self-assigned this Apr 7, 2026
@Avogar
Avogar added this pull request to the merge queue Apr 7, 2026
Merged via the queue into ClickHouse:master with commit 216e974 Apr 7, 2026
159 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 7, 2026
pull Bot pushed a commit to Haofei/ClickHouse that referenced this pull request Jul 9, 2026
The test creates MergeTree tables with a Map(LowCardinality(String), String)
column and reads the Map back to check that CREATE TABLE AS preserves the column
type and that CHECK TABLE passes. It does not check the Map key order.

CI randomizes map_serialization_version_for_zero_level_parts to 'with_buckets',
which stores Map keys in hash-bucket order in zero-level parts instead of
insertion order. Reading the Map back then yields a reordered Map
({'attr_3':'value_3','attr_2':'value_2'} instead of the inserted order), which
differs from the reference.

Pin map_serialization_version and map_serialization_version_for_zero_level_parts
to 'basic' on both tables so the stored order stays the insertion order. Same
approach as the merged fixes for 03399_mapContains_functions (ClickHouse#101541) and
02030_function_mapContainsKeyLike (ClickHouse#101538).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
The test creates MergeTree tables with a Map(LowCardinality(String), String)
column and reads the Map back to check that CREATE TABLE AS preserves the column
type and that CHECK TABLE passes. It does not check the Map key order.

CI randomizes map_serialization_version_for_zero_level_parts to 'with_buckets',
which stores Map keys in hash-bucket order in zero-level parts instead of
insertion order. Reading the Map back then yields a reordered Map
({'attr_3':'value_3','attr_2':'value_2'} instead of the inserted order), which
differs from the reference.

Pin map_serialization_version and map_serialization_version_for_zero_level_parts
to 'basic' on both tables so the stored order stays the insertion order. Same
approach as the merged fixes for 03399_mapContains_functions (ClickHouse#101541) and
02030_function_mapContainsKeyLike (ClickHouse#101538).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants