Skip to content

Fix NameError masking test_migration diagnostic on count mismatch - #105159

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-test_migration-broken-nameerror-diagnostic
May 17, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-test_migration-broken-nameerror-diagnostic

Conversation

@groeneai

@groeneai groeneai commented May 17, 2026 •

Copy link
Copy Markdown
Collaborator

The diagnostic branch in test_storage_s3_queue/test_migration.py::test_migration referenced undefined variables total_rows, count1, count2 in its assertion f-string. When the inner wait loop timed out (count didn't converge in 50s), Python raised NameError at f-string evaluation, masking what should have been a clear convergence failure.

CIDB confirms 81 failures of this form over the last 90 days, with a sharp spike to 32 hits on 2026-05-16 across every sanitizer build (amd_msan, amd_asan_ubsan/db disk/old analyzer, amd_tsan, amd_llvm_coverage, arm_binary/distributed plan). All hits are PR-only — pull_request_number > 0 across 6+ unrelated PRs (none of which touch s3_queue), making this a developer-experience pain point.

The diagnostic also computed expected_files using a wrong file naming pattern ({files_path}/test_{x}.csv) — the actual files use {use_prefix}_{i}.csv where use_prefix advances through ["a","b","c","d","e"] across each add_files_and_check call, and i starts at the cumulative start_ind. The resulting missing_files list would always report every expected file as missing, even when convergence was correct.

This change:

  • Computes count1 and count2 from per-node SELECT count() queries (the variables the original author intended).
  • Uses expected_rows[0] instead of the never-defined total_rows.
  • Drops the broken expected_files/missing_files calculation.
  • Reports distinct files processed from the existing processed_files list as a useful proxy without the buggy naming logic.
  • Switches the processed_files query from node (last value from the for node in [node1, node2] loop above) to node1 for clarity.

Demonstration in both directions:

  • Without fix: NameError: name 'total_rows' is not defined
  • With fix: AssertionError: Expected 1000 rows in total, got 300 on node1 and 200 on node2 (500), distinct files processed: 5

CIDB reference: https://play.clickhouse.com/?user=play#U0VMRUNUIHRlc3RfbmFtZSwgY2hlY2tfbmFtZSwgY291bnQoKSBGUk9NIGRlZmF1bHQuY2hlY2tzIFdIRVJFIHRlc3RfbmFtZSBMSUtFICd0ZXN0X3N0b3JhZ2VfczNfcXVldWUvdGVzdF9taWdyYXRpb24ucHklJyBBTkQgdGVzdF9jb250ZXh0X3JhdyBMSUtFICclTmFtZUVycm9yJScgR1JPVVAgQlkgdGVzdF9uYW1lLCBjaGVja19uYW1lIE9SREVSIEJZIGNvdW50KCkgREVTQw==

Past related fix PRs: #97677, #93232.

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):

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.719

The diagnostic branch in `test_storage_s3_queue/test_migration.py::test_migration`
referenced undefined variables `total_rows`, `count1`, `count2` in its assertion
f-string. When the inner wait loop timed out (count didn't converge in 50s),
Python raised `NameError` at f-string evaluation, masking what should have
been a clear convergence failure.

CIDB confirms 81 failures of this form over the last 90 days, with a sharp
spike to 32 hits on 2026-05-16 across every sanitizer build (`amd_msan`,
`amd_asan_ubsan/db disk/old analyzer`, `amd_tsan`, `amd_llvm_coverage`,
`arm_binary/distributed plan`). All hits are PR-only — `pull_request_number > 0`
across 6+ unrelated PRs.

The diagnostic also computed `expected_files` using a wrong file naming
pattern (`{files_path}/test_{x}.csv`) — the actual files use `{use_prefix}_{i}.csv`
where `use_prefix` advances through `["a","b","c","d","e"]` across each
`add_files_and_check` call, and `i` starts at the cumulative `start_ind`.
The resulting `missing_files` list would always report every expected file
as missing, even when convergence was correct.

This change:
- Computes `count1` and `count2` from per-node `SELECT count()` queries
  (the variables the original author intended)
- Uses `expected_rows[0]` instead of the never-defined `total_rows`
- Drops the broken `expected_files`/`missing_files` calculation
- Reports `distinct files processed` from the existing `processed_files` list,
  which is a useful proxy for diagnostic without the buggy naming logic
- Switches the `processed_files` query from `node` (last value from a for-loop)
  to `node1` for clarity

Demonstration in both directions:
- Without fix: `NameError: name 'total_rows' is not defined`
- With fix: `AssertionError: Expected 1000 rows in total, got 300 on node1
  and 200 on node2 (500), distinct files processed: 5`

CIDB reference (last seen 2026-05-16 06:55 UTC):
https://play.clickhouse.com/?user=play&query=SELECT+test_name%2C+check_name%2C+count%28%29+FROM+default.checks+WHERE+test_name+LIKE+%27test_storage_s3_queue%2Ftest_migration.py%25%27+AND+test_context_raw+LIKE+%27%25NameError%25%27+GROUP+BY+test_name%2C+check_name+ORDER+BY+count%28%29+DESC

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

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (worker session cron:clickhouse-ci-task-worker:20260517-153000)

  • (a) Deterministic repro? ✅ NameError fires whenever expected_rows[0] != get_count() after 50 retries — 81 CIDB-confirmed hits over the last 90 days, spike to 32 on 2026-05-16 alone.
  • (b) Root cause? ✅ f-string at line 199 referenced total_rows, count1, count2 — none of which were ever defined in add_files_and_check. Python raised NameError at f-string evaluation when the diagnostic branch was reached, masking the real convergence failure.
  • (c) Fix matches root cause? ✅ The fix defines count1/count2 via direct per-node SELECT count() queries (the variables the author originally intended), uses expected_rows[0] for the total, and drops the broken expected_files/missing_files calculation. No widening of bounds, no silencing of randomization.
  • (d) Test intent preserved? ✅ The assertion still fires on the same condition (expected_rows[0] != get_count() after 50 retries). The retry budget is unchanged. The diagnostic now reports what the original author intended (per-node split + cluster total) plus a count of distinct files actually processed.
  • (e) Demonstrated in both directions? ✅
    • Without fix: NameError: name 'total_rows' is not defined
    • With fix (mismatch): AssertionError: Expected 1000 rows in total, got 300 on node1 and 200 on node2 (500), distinct files processed: 5
    • With fix (convergence): no assertion triggered
  • (f) Fix is general? N/A — test-only Python fix, no symmetric C++ paths.

Additional notes:

  • The expected_files = [f"{files_path}/test_{x}.csv" ...] calculation that was removed had two latent bugs even before NameError fired: it used the wrong file-name prefix (test_ instead of {use_prefix}_) and ignored prefix rotation across add_files_and_check calls, so missing_files would always have reported every expected file as missing.
  • The CIDB confirms this failure pattern is PR-only (0 master hits), distributed across 6+ unrelated PRs and 6 sanitizer/coverage build flavors, never reproducing on actual master. Fixing the diagnostic restores the test's debuggability on those PR runs.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @kssenii @alexey-milovidov — could you review this? It fixes the NameError-masked diagnostic in test_storage_s3_queue/test_migration.py (introduced by #97677 / commit 85202e8d8b3f): the assertion f-string referenced undefined total_rows/count1/count2 so any count-convergence failure surfaced as NameError instead of useful per-node info, fully masking the actual failure mode in PR-only CI runs across all sanitizer builds.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label May 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [da6f3ae]

Summary: ✅


AI Review

Summary

This PR fixes the failure diagnostic in test_storage_s3_queue/test_migration.py so a row-count convergence timeout now raises a clear AssertionError instead of a masking NameError from undefined variables. The changed logic is consistent with the intended diagnostic behavior and I did not find any unresolved correctness, safety, or contract issues in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label May 17, 2026
@alexey-milovidov alexey-milovidov self-assigned this May 17, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 17, 2026
Merged via the queue into ClickHouse:master with commit 41537f7 May 17, 2026
167 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label May 17, 2026
alexey-milovidov added a commit that referenced this pull request Jun 18, 2026
Configures ruff via [tool.ruff] in pyproject.toml and wires it into
the existing style-test job in ci/jobs/check_style.py. The initial
rule set is narrow on purpose - only rules whose every hit is a real
bug or unreachable reference:

- F402 import-shadowed-by-loop-var
- F522/F523 unused .format args
- F601 duplicate dict-literal keys
- F632 is comparison with a literal
- F811 redefinition of an unused name (silently shadowed tests,
  duplicate route handlers, etc.)
- F821 undefined name (the PR #105159 class - undefined variable
  inside an f-string evaluated only on an error/diagnostic branch,
  masking the original failure with NameError)

Stylistic rules (F401, F541, F841, F403, F405) are intentionally
left off for follow-up cleanups so the initial signal is bug-class
only.

Bugs found and fixed by the initial run:

  F821 (same pattern as PR #105159):
  - ci/jobs/scripts/check_ci.py - commit_status_data referenced in
    an exception branch but never assigned
  - tests/fuzz/runner.py - PIPE referenced without
    `from subprocess import PIPE`
  - tests/integration/test_backup_restore_on_cluster_with_checksum_data_file_name/test.py
    - os.path.join with os not imported
  - tests/integration/test_database_iceberg/test.py - BASE_URL_DOCKER
    typo for BASE_URL
  - tests/integration/test_keeper_remove_acl/test.py - node_zk
    referenced in a finally cleanup that was unreachable; the bare
    except silently swallowed the NameError on every run
  - tests/integration/test_storage_delta_disks/test.py - missing
    `from helpers.config_cluster import minio_secret_key`

  F811 silently-shadowed tests (later def overwrites earlier):
  - test_disk_types::test_different_types
  - test_storage_s3::test_empty_file
  - test_executable_table_function - one of the two same-named
    functions renamed to test_executable_storage_*
  - test_storage_delta::test_partition_columns_3 - second def
    renamed to test_partition_columns_jumbled

  F811 dead route handler:
  - test_merge_tree_s3_failover endpoint had two `def fail_request`;
    the throttle one was unreachable, renamed to throttle_request

  F601 duplicate dict-literal keys with divergent values in
  test_storage_s3_queue/test_5.py - same setting specified twice
  with different values; cleaned up to make the intended value
  explicit

  F522/F523 .format-string drift - templates updated without
  dropping the corresponding args (test_jbod_ha,
  test_manipulate_statistics, test_mysql_protocol,
  test_storage_kafka, helpers/dictionary)

  F632 `assert error is ""` -> `==` in test_allow_feature_tier

  F402 loop variable named `field` shadowing
  `from dataclasses import field` in praktika/issue.py

  Plus auto-fixed duplicate imports across CI scripts and
  integration tests.

Two cases use `# noqa: F821` for legitimate self-referential type
annotations (TypeVar bound and nested dataclass forward reference)
in praktika/utils.py and praktika/gh.py.

Reference: #105159

Co-Authored-By: Claude Opus 4.7 (1M context) <[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.

3 participants