Skip to content

Match selected tests by exact file name in the flaky and targeted stateless checks - #122059

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:flaky-check-exact-test-selector
Sep 27, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:flaky-check-exact-test-selector

Conversation

@groeneai

@groeneai groeneai commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #117968
Related: #121387

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 into CHANGELOG.md):

...

Description

A failing job this fixes: Stateless tests (amd_debug, flaky check). #117968 changed 03711_top_k_by_skip_index.sql; the job also repeated _dynamic, _handle_ties and _negative, and went red on _handle_ties at 600 s.

Root cause. Targeting.get_changed_tests() appends a bare . to a test's base name ("precisely match this test only") and hands it to clickhouse-test as a positional argument. Those are regexes, searched against the suite file name including the extension (TestSuite.get_selected_tests), so the unescaped, unanchored . also matches <name>_<anything> and a test whose name is a prefix of a sibling's drags its siblings in. Over the suite, 320 of 15,806 test names select more than one test, worst case 22.

Fix. The selector becomes an exact-filename regex at the one boundary where it becomes an argv word, so the producers keep emitting names: ^<escaped name>(?:\.sql\.j2|\.sql|\.sh|\.py|\.expect)$, extensions from _TEST_FILE_EXTENSIONS. Not in get_changed_tests: that value is also consumed as a name (the AST fuzzer's Path(test).stem, the coverage dedup key, the manifest), which a regex breaks. run_tests builds one bash command string, so selectors are shlex.quoted; without that, quote removal deletes the escape and the pattern arrives unchanged.

Validation. Measured end to end, through bash into the real clickhouse-test matcher: for #117968's input the job selects 4 tests before, 1 after. Over the suite the 320 becomes 0: every test is selected alone from each name shape a producer emits (X., X, X.gen, coverage's X.sql), across all five extensions, and none selects 0 (that makes the job exit 1). Dropping the quoting is a bash syntax error rather than a silent reversion, since ( is a metacharacter. Two test_selection_smoke.py methods, each with a negative control.

A hand-written clickhouse-test <regex> keeps loose matching: only a Targeting selection is converted. #121387 fixes the other half of this false red, the 600 s kill of a long test.


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

Version info

  • Merged into: 26.10.1.850-master (included in 26.10 and later)

`Targeting.get_changed_tests()` appended a bare `.` to a test's base name,
commented "precisely match this test only", and that value became a positional
argument of `clickhouse-test`. Those arguments are regexes and
`TestSuite.get_selected_tests` searches them against the suite file name
including its extension, so the unescaped, unanchored `.` also matched
`<name>_<anything>`: every test whose name is a prefix of a sibling's dragged
its siblings into the job. A flaky check then spent its 50 repetitions, its
`--jobs nproc-1` workers and its `--max-failures 5` budget on tests the pull
request never touched, and could report the PR red for one of them
(`03711_top_k_by_skip_index_handle_ties`, killed at 600 s on ClickHouse#117968, which had
changed only `03711_top_k_by_skip_index.sql`). Over the suite, 320 of 15,646
base names selected more than one test, worst case 22.

The conversion to an exact-filename selector happens at the one boundary where
the value becomes a `clickhouse-test` argv word, not in the producers, because
`get_changed_tests()`'s output is also consumed as a *name*: the targeted AST
fuzzer's `Path(test).stem` lookup would miss every changed query, and the
coverage dedup key and selection manifest would record one test twice. One site
also covers the targeted check's previously-failed and coverage arms.

`run_tests` hands one bash command string to `Shell.run`, so the selectors are
`shlex.quote`d there. That half is load-bearing, not decoration: bash quote
removal deletes the backslash of an unquoted word, so escaping alone produces a
pattern byte-identical to the old one at the point of use. With the anchored
form the two halves are welded, since `(` and `)` make an unquoted join a bash
syntax error rather than a silent reversion.

The extension alternation is built from `_TEST_FILE_EXTENSIONS` instead of
spelling `.sql`, which would select nothing for `.sh`, `.sql.j2`, `.py` and
`.expect` tests, and `$` keeps `.sql` from shadowing `.sql.j2`. Measured over
the whole suite: 320 over-selecting names become 0, no name selects 0 (a
selector matching nothing makes the job exit 1), and all five extensions still
select their own test. A hand-written `--test` keeps loose matching.

Co-Authored-By: Claude Opus 5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect labels Sep 24, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding; bounded: 1 full pass

  • 1 fix round + 1 delta recheck), plus my own cold review of the resulting code (3 nits).
# Sev Finding Verdict Evidence / action
1 ⚠️ The two new smoke methods call the helpers directly, so removing the rebinding in main or restoring the plain join in run_tests would leave them green and silently disable the fix (ci/jobs/scripts/test_selection_smoke.py) DISAGREE Not silent for run_tests: the selector contains (, so a plain join aborts the command with rc 2, syntax error near unexpected token '(', before clickhouse-test starts (measured through bash). For main, this suite cannot reach :894: main resolves and chmod +xes a binary at :752-772 and raises FileNotFoundError without one, and nothing in the repository imports ci.jobs.functional_tests, so there is no harness to extend. Moving the conversion into run_tests to make it reachable would weaken the invariant, since the rebinding is upstream of all four run_tests call sites and a future call site inherits exact selection instead of opting in. The production path is measured end to end in both directions instead, through the command string, bash, and the real TestSuite.get_selected_tests: 4 tests before, 1 after. Residual: the rebinding has no automated guard.
2 💡 test_selection_args_survive_the_shell models bash quote removal with shlex.split rather than with bash DISAGREE Deliberate: this suite runs as a strict gate before selection (functional_tests.py:866), so a subprocess dependency there could fail a job for environmental reasons. The bash arm is measured separately (rc 0 quoted, rc 2 unquoted). The method is still reddened by its own mutation control.
3 💡 The dotted-sibling fixture pair in the new test is constructed, not taken from the suite DISAGREE Intentional. Exactly 1 of 15,646 stateless sources has a dotted base name, and it has no prefix sibling, so the real suite cannot express the case. A hermetic fixture also keeps the test from breaking when a test is renamed.
4 💡 The diagnostics command at functional_tests.py:1443 still joins its test names unquoted and unanchored DISAGREE Out of scope and unreachable here: the DIAGNOSTICS stage is removed for flaky, targeted, bugfix-validation and local runs (:794-801), its inputs are result-derived names without shell metacharacters, and run_diagnose_from_artifacts is gated on --diagnose-random-settings, which only that separate command passes.

Both load-bearing measurements were re-run cold rather than trusted: the new selector selects
exactly its own file for all 15,646 test sources (0 selecting nothing, 0 selecting more than one,
all five extensions preserved), and the "320 names, worst case 22" figure reproduces exactly
(318 prefix siblings plus 2 the old selector also caught because it is unanchored at both ends,
02969_archive_seek inside 04545_02969_archive_seek_stored).

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-9:20260924-171100

@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Sep 24, 2026
@clickhouse-gh clickhouse-gh Bot closed this Sep 24, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Sep 24, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes, 100%, no build and no server. The real clickhouse-test matcher is module-loaded and asked what 03711_top_k_by_skip_index. selects: 4 test sources (.sql, _dynamic, _handle_ties, _negative), every run.
b Root cause explained? The selector is a regex consumed by TestSuite.get_selected_tests, which searches it against the suite file name including the extension. The appended . is unescaped and unanchored, so it also matches <name>_<anything>: any test whose name is a prefix of a sibling's drags the sibling into the job.
c Fix matches root cause? The selector is made exact (^<escaped name>(?:<known extension>)$) in the language of the process that parses it, at the single place it becomes an argv word. No tag, no widened bound, no guard at the symptom.
d Test intent preserved / new tests added? Two methods added to the existing ci/jobs/scripts/test_selection_smoke.py, which CI already runs with strict=True. Each was given a mutation control: replacing the pattern with the bare-dot form, and replacing the quoting with a plain join, each reddens its own method; both reverted, 15/15 green. No existing test weakened.
e Both directions demonstrated? Yes, through the production path: the command string run_tests builds, executed by bash, feeding the real matcher. Before 4 selected tests, after 1. The whole suite: 320 of 15,646 base names selecting more than one test becomes 0.
f Fix is general across code paths? The conversion sits at the one argv boundary, so the flaky, bugfix-validation and targeted (changed / previously-failed / coverage) arms and all four run_tests call sites are covered by one line. Deliberately not in the producers: their output is also consumed as a name, measured (Path(<selector>).stem and _test_exists(<selector>) both break on a regex).
g Fix generalizes across inputs? All five extensions measured over the whole suite (62 .expect, 9 .py, 3846 .sh, 11540 .sql, 189 .sql.j2, no misses), plus the .gen and .gen.sql report spellings, a dotted base name, a name that is the prefix of a dotted sibling, and the empty/None selection (byte-identical to the previous behaviour).
h Backward compatible? ci/ job scripts only: no setting, no SettingsChangesHistory.cpp, no format, nothing user-visible. A hand-written --test keeps its free-form regex semantics by construction.
i Invariants and contracts preserved? The invariant is that a CI-derived selector selects exactly its own test file and reaches argv verbatim. Eleven carriers checked by measurement: every producer gates on the source file existing (0 of 15,646 mismatches, so no selector can select nothing and make the job exit 1); explicit_test_selectors_matched still records the selector, so the runner's exit-1 path is unchanged; the name consumers (AST fuzzer, coverage dedup, selection manifest, _test_exists) are upstream of the conversion; the human-facing logs still print names.

Session id: cron:clickhouse-impl-slot-4:20260924-164100

@groeneai
groeneai requested review from fm4v and maxknv September 24, 2026 18:00
@clickhouse-gh

clickhouse-gh Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c704a20]

Summary: ✅


AI Review

Summary

This PR fixes the stateless flaky and targeted selectors by turning Targeting-produced test names into exact clickhouse-test regexes and by shell-quoting those selectors before bash parses the command line. The selector logic itself looks sound on the current tree, but the added regression coverage still does not exercise the production branch that applies exact selection, so the proof is weaker than the contract the PR is claiming.

Findings

⚠️ Majors

  • [ci/jobs/functional_tests.py:938] The exact-selection invariant is enforced here, but the new smoke suite only calls Targeting.selection_pattern / selection_args directly and never reaches this rebinding. Targeting.SELECTION_SOURCES also still omits ci/jobs/functional_tests.py, so a future edit that removes this conversion would not rerun the smoke first either. That means deleting this line would silently reintroduce sibling over-selection while the new regression tests stay green. Factor the "Targeting-produced vs hand-written --test selectors" decision into a pure helper and cover that helper, or add a harness that executes this branch.
Final Verdict

Changes requested. The regex fix itself looks correct, but the new tests are still weaker than the user-visible contract because they do not cover the production path that makes Targeting selections exact.

LLVM Coverage Report

⚠️ No coverage measurement for commit c704a20: incomplete coverage measurement: 16 of 21 shard profiles are missing: LLVM_COVERAGE_FILE_ft_2.profdata, LLVM_COVERAGE_FILE_ft_3.profdata, LLVM_COVERAGE_FILE_ft_s3_async_sequential.profdata, LLVM_COVERAGE_FILE_ft_s3_db_repl_parallel_2.profdata, LLVM_COVERAGE_FILE_ft_s3_db_repl_parallel_3.profdata, LLVM_COVERAGE_FILE_ft_s3_db_repl_sequential_1.profdata, LLVM_COVERAGE_FILE_ft_s3_db_repl_sequential_2.profdata, LLVM_COVERAGE_FILE_ft_s3_sequential.profdata, LLVM_COVERAGE_FILE_it_1.profdata, LLVM_COVERAGE_FILE_it_2.profdata, LLVM_COVERAGE_FILE_it_3.profdata, LLVM_COVERAGE_FILE_it_4.profdata, LLVM_COVERAGE_FILE_it_5.profdata, LLVM_COVERAGE_FILE_it_6.profdata, LLVM_COVERAGE_FILE_it_7.profdata, LLVM_COVERAGE_FILE_it_8.profdata.

@clickhouse-gh clickhouse-gh Bot added pr-ci comp-ci-infrastructure CI/CD pipelines (GitHub Actions, CI scripts, runners). labels Sep 24, 2026
@alexey-milovidov alexey-milovidov self-assigned this Sep 26, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, fix conflicts.

…est-selector

Master reverted ClickHouse#118223 in ClickHouse#122110, and this branch was built on top of it:

- ci/jobs/scripts/test_selection_smoke.py: deleted on master, modified here.
  Take the deletion. The two selector checks that were added to it go with the
  file; nothing on master runs a unit suite for find_tests.py.
- ci/jobs/scripts/find_tests.py: merged cleanly as text, but selection_pattern
  called selection_test_name, which the revert removed. The name is now
  normalized in selection_pattern itself: normalize_stateless_test_name for the
  .gen / .gen.sql report names, then the trailing dot, then one file extension,
  because per-test coverage names a test by its case file name (00001_x.sql).

Re-validated on the merged tree: every one of the 15,747 stateless test sources
is selected alone by its selector, from every name shape a producer emits (X.,
X, X.gen, X.gen.sql, X<ext>), through the real run_tests command line, bash,
and the tests/clickhouse-test matcher. Without the extension strip all 15,558
coverage-shaped names would select nothing.

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

Copy link
Copy Markdown
Collaborator Author

Merged master in 4cb9282. #122110 removed selection_test_name and test_selection_smoke.py, so selection_pattern now normalizes the test name itself, and the two smoke checks went with the file.

Comment thread ci/jobs/scripts/find_tests.py Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, one more time, please.

…est-selector

Master re-landed ClickHouse#118223 (74c90e9, ClickHouse#122111), which restores
Targeting.selection_test_name and ci/jobs/scripts/test_selection_smoke.py and
drops DIRECT_CALL_MAX_DEPTH:

- ci/jobs/scripts/find_tests.py: selection_pattern builds on
  selection_test_name again, as before the revert, instead of repeating its
  normalization. DIRECT_CALL_MAX_DEPTH stays removed.
- ci/jobs/scripts/test_selection_smoke.py: the two selector checks are back.
  The targeted job runs this suite when find_tests.py changes.

Re-validated on the merged tree: the smoke suite passes 16/16 and each selector
check fails under its mutation; every one of the 15,806 stateless test sources
is selected alone by its selector from every name shape a producer emits,
through the real run_tests command line, bash and the tests/clickhouse-test
matcher.

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

Copy link
Copy Markdown
Collaborator Author

Merged master in c704a20. The re-land in #122111 brought back selection_test_name and test_selection_smoke.py, so selection_pattern builds on selection_test_name again and the two smoke checks are back. The PR's change is now identical to the version first approved.

# A selection made by `Targeting` names whole tests, so it becomes an exact
# selector. A hand-written `--test` stays the free-form regex it was typed as.
if tests and not args.test:
tests = [Targeting.selection_pattern(test) for test in tests]

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.

The exact-selection contract now depends on this rebinding, but the new smoke coverage never reaches it: test_selection_smoke.py only exercises Targeting.selection_pattern / selection_args directly, and Targeting.SELECTION_SOURCES still omits ci/jobs/functional_tests.py.

That leaves the regression evidence weaker than the behavior this PR is claiming here. If a later edit drops this conversion, sibling over-selection comes back silently while the new smoke suite stays green. Please factor the "Targeting-produced vs hand-written --test selectors" decision into a small pure helper and cover that helper, or add a harness that actually executes this branch.

@fm4v

fm4v commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

@groeneai

Select only the changed test in the flaky and targeted stateless checks

Could you explain why you decided not to select tests based on coverage?

@groeneai groeneai changed the title Select only the changed test in the flaky and targeted stateless checks Match selected tests by exact file name in the flaky and targeted stateless checks Sep 27, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

I did not drop coverage selection. The old title read that way, so I changed it to "Match selected tests by exact file name in the flaky and targeted stateless checks".

Which tests are selected is unchanged: the flaky check runs only the changed tests, as on master (functional_tests.py#L869-L873), and the targeted selection is still the union of changed, previously failed and coverage tests (find_tests.py#L884-L906). The PR changes only how each selected name is passed to clickhouse-test (functional_tests.py#L935-L938).

Coverage hits had the same problem. protect_selection normalizes every selected test to <name>., and as a clickhouse-test regex that bare dot also selects every test whose name starts with <name>. On master a coverage hit on 03711_top_k_by_skip_index.sql runs all four 03711_top_k_by_skip_index* tests; with this PR it runs that one. A sibling that covers the changed lines is still selected by coverage, under its own name.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 27, 2026
Merged via the queue into ClickHouse:master with commit d640c70 Sep 27, 2026
172 checks passed
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 comp-ci-infrastructure CI/CD pipelines (GitHub Actions, CI scripts, runners). groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect manual approve Manual approve required to run CI 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.

4 participants