Skip to content

Fix flaky tests 03632_skip_indices_logged and 04039_skip_index_query_log - #100984

Merged
shankar-iyer merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/skip-indices-query-log-flaky
Mar 28, 2026
Merged

shankar-iyer merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/skip-indices-query-log-flaky

Conversation

@groeneai

@groeneai groeneai commented Mar 28, 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):

  • none

Description

Fixes flaky tests 03632_skip_indices_logged_in_query_log and 04039_skip_index_query_log_actually_applied which verify that query_log.skip_indices is populated correctly.

Two root causes:

  1. auto_statistics_types=minmax (randomized MergeTree setting) creates MinMax statistics that perform part-level pruning before skip indices can perform granule-level filtering. When statistics prune all parts, skip indices see 0 granules, drop 0, and skip_indices stays empty.

    Fix: Pin auto_statistics_types = '' in CREATE TABLE SETTINGS for all tables in both tests.

  2. use_skip_indexes_on_data_read=1 in test 03632 evaluates skip indices during data reading, but query_log.skip_indices only tracks indices evaluated during mark selection (use_skip_indexes_on_data_read=0).

    Fix: Change to use_skip_indexes_on_data_read=0 in test 03632 (test 04039 already used =0). Update the .reference file accordingly.

Reproduction (root cause 1):

clickhouse client --allow_merge_tree_settings --auto_statistics_types minmax -q "
  CREATE TABLE t (a UInt64, INDEX idx_a a TYPE minmax GRANULARITY 1)
  ENGINE = MergeTree ORDER BY tuple() SETTINGS index_granularity = 8192;
  -- ... skip indices will be empty in query_log
"

Reproduction (root cause 2):

-- use_skip_indexes_on_data_read=1 → skip_indices empty
-- use_skip_indexes_on_data_read=0 → skip_indices populated

Verified: 50/50 passes on both tests with full randomization (query + MergeTree settings).

🤖 Generated with Claude Code

Version info

  • Merged into: 26.4.1.348

Two root causes for skip_indices being empty in query_log:

1. auto_statistics_types=minmax (randomized MergeTree setting) creates
   MinMax statistics that prune parts BEFORE skip indices evaluate.
   When statistics prune all parts, skip indices see 0 granules, drop 0,
   and nothing gets logged. Fix: pin auto_statistics_types='' in
   CREATE TABLE SETTINGS for all three tables.

2. use_skip_indexes_on_data_read=1 evaluates skip indices during data
   reading, but query_log.skip_indices only tracks indices evaluated
   during mark selection. Fix: change to use_skip_indexes_on_data_read=0
   in test 03632 (test 04039 already used =0).

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

Copy link
Copy Markdown
Collaborator Author

Pre-PR Validation (session cron:clickhouse-ci-task-worker:20260328-051500)

a) Deterministic repro? YES — --allow_merge_tree_settings --auto_statistics_types minmax on the CLI deterministically causes skip_indices to be empty for any table with minmax skip indices, because MinMax statistics prune parts before skip indices evaluate.

b) Root cause explained? auto_statistics_types=minmax → MinMaxStatistics auto-created on columns → statistics prune ALL parts (part-level) → skip indices see 0 parts with 0 granules → index_dropped_granules stays at 0 → nothing logged in query_log.skip_indices. For 03632: use_skip_indexes_on_data_read=1 evaluates skip indices during data reading, but the logging code at MergeTreeDataSelectExecutor.cpp:1180-1188 only tracks indices evaluated during mark selection (=0).

c) Fix matches root cause? YES — pinning auto_statistics_types='' prevents statistics from pre-empting skip indices. Changing use_skip_indexes_on_data_read from 1→0 ensures indices are evaluated in the logged code path.

d) Test intent preserved? YES — both tests verify skip index logging in query_log. The fix ensures skip indices can actually evaluate and get logged. No assertions weakened.

e) Both directions demonstrated? YES — Without fix: 03632 fails deterministically (even without randomization), 04039 fails ~45% with randomization. With fix: both pass 50/50 with full randomization.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @shankar-iyer — could you review this? It fixes the skip_indices query_log tests you may have seen failing: auto_statistics_types=minmax (randomized) prunes parts before skip indices evaluate, and use_skip_indexes_on_data_read=1 evaluates indices in a code path that doesn't log to query_log.

@clickhouse-gh

clickhouse-gh Bot commented Mar 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2475bb6]

Summary: ✅


AI Review

Summary

This PR stabilizes two flaky stateless tests around query_log.skip_indices by (1) pinning auto_statistics_types = '' in test tables so automatic minmax part-pruning does not hide skip-index behavior, and (2) switching test 03632 to use_skip_indexes_on_data_read = 0, which matches how skip_indices is populated. I did not find correctness, safety, or performance issues in the proposed test-only changes.

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 Mar 28, 2026
@shankar-iyer

Copy link
Copy Markdown
Member

cc @shankar-iyer — could you review this? It fixes the skip_indices query_log tests you may have seen failing: auto_statistics_types=minmax (randomized) prunes parts before skip indices evaluate, and use_skip_indexes_on_data_read=1 evaluates indices in a code path that doesn't log to query_log.

Changes look good. How much effort is required to log the skip indexes used into query_log when use_skip_indexes_on_data_read=1 ? You can check the code in MergeTreeIndexReadResultPool.cpp

@shankar-iyer shankar-iyer added the can be tested Allows running workflows for external contributors label Mar 28, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Thanks for the review!

I looked into MergeTreeIndexReadResultPool.cpp and MergeTreeReaderIndex.cpp. The data-reading path already tracks which granules pass filtering via SkipIndexReadResult::granules_selected and MergeTreeReaderIndex::canSkipMark(), but these statistics aren't aggregated back to the query context for query_log logging.

The effort is medium (~1-2 days):

  1. Add atomic counters to track skipped granules per index during data reading (low — similar to existing index_dropped_granules in the mark selection path)
  2. Thread the statistics from canSkipMark() back through MergeTreeRangeReader to the executor context (medium — main complexity, multi-threaded reads need careful aggregation)
  3. Call context->addSkipIndexAccessInfo() after data reading completes (low — infrastructure already exists)

Key files: MergeTreeReaderIndex.cpp, MergeTreeRangeReader.cpp, MergeTreeDataSelectExecutor.cpp.

This is a worthwhile enhancement but separate from this flaky test fix — happy to do it as a follow-up PR.

@shankar-iyer
shankar-iyer added this pull request to the merge queue Mar 28, 2026
Merged via the queue into ClickHouse:master with commit 2402f55 Mar 28, 2026
297 of 298 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 28, 2026
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.

3 participants