Skip to content

Make 02572_system_logs_materialized_views_ignore_errors ordering total - #121183

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:02572-total-order-by-script-query-number
Sep 20, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:02572-total-order-by-script-query-number

Conversation

@groeneai

@groeneai groeneai commented Sep 20, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #121126
Related: #57088

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

Order the final listing of 02572_system_logs_materialized_views_ignore_errors by script_query_number, type instead of event_time_microseconds, so a same-microsecond pair cannot transpose two rows.

Description

Fast test (arm_darwin) on #121126 failed with two adjacent rows transposed, identical content. No open issue tracks it; #57088 is closed.

order by event_time_microseconds is not total here: the column holds a statement's start time on its QueryStart row and its end time on its QueryFinish row (executeQuery.cpp:557-558 vs :441-442). The reference is statement order, which that key reproduces only while each finish stamp stays below the next statement's start stamp: two clock reads, one client round trip apart. Appending , type or , query only makes the failure deterministic: both sort drop table log_proxy_02572; querystart ahead of system flush logs query_log; queryfinish, the order observed.

system.query_log records the statement order itself: script_query_number is the client's statement counter, the same on a statement's two rows, and type separates those (1 < 2, numeric). The pair is unique per row here (uniqExact = 24 of 24), so the listing no longer reads a clock. .reference is unchanged.

One event in 180 days, so I proved the property instead of waiting for it: degrade a clock column to force the tie, shuffle, sort 50 times. The old key gave 50 distinct outputs, none matching .reference; the new one gave 1, matching on all 50 even with both clock columns destroyed; zeroing script_query_number brings the 50 back, so the probe can fail.

The test's two other recorded failures are rows missing from the listing (2026-07-06, 2026-06-02), a SYSTEM FLUSH LOGS visibility question rather than an ordering one, so they are out of scope here. 01702_system_query_log.sql:137 and 03312_line_numbers.sql:11 carry the same shape with 0 recorded occurrences of this transposition, so they are untouched. script_query_number exists since 25.2 (2e664a00b7238); a branch cut before that needs query_start_time_microseconds, type.

Flakiness data: CIDB query and result
SELECT check_name, count() AS rows_, uniqExact(pull_request_number) AS prs,
       countIf(head_ref = 'master' AND pull_request_number = 0) AS master_rows,
       min(check_start_time) AS first_, max(check_start_time) AS last_
FROM checks
WHERE test_name LIKE '%02572_system_logs_materialized_views_ignore_errors%'
  AND test_status IN ('FAIL','ERROR')
  AND check_start_time > now() - INTERVAL 180 DAY
GROUP BY check_name ORDER BY rows_ DESC;

┌─check_name─────────────┬─rows_─┬─prs─┬─master_rows─┬──────────────first_─┬───────────────last_─┐
│ Fast test              │     3 │   3 │           0 │ 2026-04-01 16:59:37 │ 2026-07-06 12:41:57 │
│ Fast test (arm_darwin) │     2 │   2 │           0 │ 2026-07-03 05:10:40 │ 2026-09-20 09:15:58 │
└────────────────────────┴───────┴─────┴─────────────┴─────────────────────┴─────────────────────┘

Four different mechanisms, each read from that row's test_context_raw:

date check mode
2026-09-20 Fast test (arm_darwin) ordering transposition, fixed here
2026-07-06 Fast test rows missing, trailing set log_queries=0; pair absent
2026-07-03 Fast test (arm_darwin) Timeout! Killing process group, macOS
2026-06-02 Fast test all 24 rows missing
2026-04-01 Fast test Code: 15 DUPLICATE_COLUMN: Virtual column _table already exists, that PR's own change

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

Version info

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

The final listing ordered by `event_time_microseconds`, which holds the
statement's start time on a `QueryStart` row and its end time on a
`QueryFinish` row. The committed reference is statement order, so that key
reproduced it only while every finish timestamp compared strictly less than
the next statement's start timestamp. Those are two independent clock reads
and the only margin between them is one client round trip, so when the margin
collapsed the sort was free to emit the other permutation, which is what the
failure below shows: identical content, two adjacent rows transposed.

`script_query_number` is the client's 1-based statement counter and the
QueryFinish log element is a copy of the QueryStart one, so both rows of a
statement carry the same value and `type` separates them. In this result set
`(script_query_number, type)` is unique per row, so the listing is ordered
without reading a clock at all. The reference is unchanged, which is the
cheapest proof the ordering semantics were preserved rather than redefined.

Failure this fixes, Fast test (arm_darwin):
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=121126&sha=239316397592d4fd59cfac529b039ba88282c3be&name_0=PR&name_1=Fast%20test%20%28arm_darwin%29
ClickHouse#121126
@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 20, 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; 0 findings; one full pass over the
change, the reference, the test runner and the sibling query-log tests). My own cold review of the
resulting tree, done before that gate and without the implementation notes, raised 2 nits and no
blocker or major.

# Sev Finding Verdict Evidence / action
1 💡 The description ran to the top of its length budget for a one-line test change AGREE Cut before publishing. An open-ended offer to widen the scope went, and one verified sentence took its place: why appending , type or , query to the old key would only make the failing order deterministic
2 💡 Two literal names are not in code formatting in the second paragraph of the commit message AGREE Left as it is: re-punctuating a commit body costs a rebuild and a second review round for nothing a reader sees

Severity: 💡 nit. Nothing was deferred. The ordering claims were checked against
src/Core/QueryLogElementType.h, src/Client/ClientBase.cpp, tests/clickhouse-test and the
committed 03312_line_numbers.reference rather than taken from the implementation notes.

Session id: cron:clickhouse-review-slot-9:20260920-114000

@clickhouse-gh clickhouse-gh Bot closed this Sep 20, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Sep 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c47e745]

Summary: ✅


AI Review

Summary

This PR makes 02572_system_logs_materialized_views_ignore_errors deterministic by replacing a mixed QueryStart / QueryFinish timestamp order with the client-side statement counter. That fix is correct for this test, but the same non-total ordering contract is still present in another system.query_log reference test, so I would request one sibling fix before merge.

Findings

⚠️ Majors

  • [tests/queries/0_stateless/03312_line_numbers.sql:11] 03312_line_numbers.sql still orders mixed QueryStart / QueryFinish rows by event_time_microseconds, type, even though this PR's root cause is that a statement's finish timestamp can tie or overtake the next statement's start timestamp. Its reference expects the same per-statement pair ordering, so the middle two rows can transpose there for the same reason. Switch that query to ORDER BY script_query_number, type (it already selects script_query_number) so both query-log reference tests rely on statement order rather than clock order.
Final Verdict

Status: ⚠️ Request changes

  • Update 03312_line_numbers.sql to use a total statement-order key instead of event_time_microseconds, type.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Sep 20, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes at the defect's own level: forced-tie + shuffled-input probe, 50 runs, on demand, reproduced twice. The whole-test CI red is one event in 180 days and does NOT reproduce here (0 ties and 0 inversions in 2200 statement-boundary pairs); stated, not papered over.
b Root cause explained? event_time_microseconds is a start time on QueryStart rows and an end time on QueryFinish rows (executeQuery.cpp:557-558 vs :441-442), so the key reproduces the committed reference only while each finish stamp stays below the next statement's start stamp: two clock reads, one client round trip apart.
c Fix matches root cause? Yes. The key becomes the recorded statement order itself, so it reads no clock. No widened bound, no no-random-* tag, no data reduction, no retry. The obvious tiebreak event_time_microseconds, type was rejected because the collapsed pair spans a statement boundary, so it makes the FAILING order deterministic.
d Test intent preserved / new tests added? Preserved exactly: same 24 rows, same content, and .reference is byte-for-byte unchanged, which is the cheapest proof nothing was neutered. No new test is owed (this is the test; no source change).
e Both directions demonstrated? Yes: old key 50 distinct outputs / 0 matching the reference; new key exactly 1 / 50 matching with both clock columns degraded; negative control (zeroing script_query_number) 50 / 0, proving the probe can redden.
f Fix is general across code paths? No code path involved (test-only). The two in-tree listings with the same latent shape, 01702_system_query_log.sql:137 and 03312_line_numbers.sql:11, are named in the description with the measurement that scopes them out: 0 recorded occurrences each in 180 days.
g Fix generalizes across inputs (params/datatypes/wrappers)? The key's input space is enumerated, not sampled: script_query_number is 1..12 here (200/200 runs) and 0 for any non-multiquery row, which cannot appear under current_database = currentDatabase(); type is a 4-value Enum8 whose other two values sort after their own statement's rows. No wrapper matrix is reachable: both are fixed non-nullable system-table columns.
h Backward compatible? (maintainer-approved exception only) Yes, nothing is owed: no setting, no default, no format, no SettingsChangesHistory.cpp entry. One constraint recorded for a backport: script_query_number exists since 25.2 (2e664a00b7238), so an older branch needs query_start_time_microseconds, type.
i Invariants and contracts preserved? The invariant "the final listing is in statement order" now rests on the column that records statement order; (script_query_number, type) uniqueness is measured (uniqExact = 24 of 24), not argued. No lock, concurrency or trust boundary is touched. This test's own comment-placement invariant is respected: a comment above a statement running under log_queries=1 joins that statement's logged query text, so the new comment sits inside the final SELECT, which runs after set log_queries=0;.

Session id: cron:clickhouse-impl-slot-4:20260920-110500

where event_date >= yesterday() AND event_time >= now() - 600 AND current_database = currentDatabase()
order by event_time_microseconds
-- `script_query_number` is the client's statement counter, the same on a statement's `QueryStart` and `QueryFinish` rows.
order by script_query_number, type

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.

03312_line_numbers.sql still has the same non-total ordering that this PR is fixing here: it emits QueryStart / QueryFinish pairs from system.query_log, sorts them by event_time_microseconds, type, and its reference expects the two rows of statement 1 to stay ahead of statement 2. Once a QueryFinish timestamp ties or overtakes the next QueryStart, the middle pair can transpose there for the same reason 02572 just did.

Since that test already selects script_query_number, please switch it to ORDER BY script_query_number, type in the same change so we do not leave the sibling query_log reference test with the identical latent flake.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Verified, and the mechanism is real: 03312_line_numbers.sql:11 does order by event_time_microseconds, type, script_query_number is already in its projection, and its reference does depend on cross-statement order, so a tie would transpose the middle pair there for exactly the reason it did here (QUERY_START = 1 sorts ahead of QUERY_FINISH = 2, so statement 2's start would overtake statement 1's finish).

I am not folding it in, on the measurement. Over 180 days 03312 produced 535,754 OK results, and this transposition is not among its failures: of its 31 recorded failures, 4 have extra QueryStart/QueryFinish 3 10 SYSTEM FLUSH LOGS ... rows, 7 have the text_log Ok rows missing, and 20 are non-zero exits (158/159/241) or a runner artifact. In all 11 of those reference diffs the four QueryStart/QueryFinish rows are unchanged context lines, in reference order. The one observed tie is on Fast test (arm_darwin), and 03312 is -- Tags: no-fasttest: it ran there once in 180 days against 23,949 skips, where 02572 executes 23,545 times. So the tie has never been sampled on a platform 03312 actually runs on.

That makes it a latent nit rather than a fix, and widening a PR that is already queued for review is the reviewer's call rather than mine. The change itself would be one line and safe: ascending (script_query_number, type) is (1,1), (1,2), (2,1), (2,2) there, which is the committed reference order. If a reviewer prefers it here, I will add it in one push.

@clickhouse-gh clickhouse-gh Bot added the comp-testing Test content only (no production code). label Sep 20, 2026
@alexey-milovidov alexey-milovidov self-assigned this Sep 20, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 20, 2026
Merged via the queue into ClickHouse:master with commit 7d5fa47 Sep 20, 2026
176 of 349 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 Sep 21, 2026
@clickhouse-gh clickhouse-gh Bot added comp-logging Logging/monitoring/observability (logs, metrics, tracing, instrumentation). and removed comp-testing Test content only (no production code). labels Nov 22, 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 comp-logging Logging/monitoring/observability (logs, metrics, tracing, instrumentation). groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect 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