Skip to content

Add test: Untested: key analysis declining a text constant it cannot convert - #121199

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
clickgapai:qa-bot/coverage-pr120114
Sep 27, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
clickgapai:qa-bot/coverage-pr120114

Conversation

@clickgapai

@clickgapai clickgapai commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Test-only PR. Review: are the gaps real, is the test right.

Adds test coverage for 1 untested code path, found during automated review of PR #120114.
That PR: Fixes rows silently dropped by pruning when a text constant is compared with a DateTime/DateTime64 key whose type name carries no time zone. (1) Adds tryNormalizeTextConstantForZonelessDateTimeInput in KeyCondition.cpp and calls it from canConstantBeWrappedByMonotonicFunctions and …

1. Untested: key analysis declining a text constant it cannot convert
src/Storages/MergeTree/KeyCondition.cpp:2005-2006 (the branch), :2124 and :2662 (the call sites), at master 827b4f0e149c6
Risk: KeyCondition::extractAtomFromTree -> canConstantBeWrappedByMonotonicFunctions (KeyCondition.cpp:2124) and, for equals/notEquals, canConstantBeWrappedByDeterministicFunctions (KeyCondition.cpp:2662) both call the new helper first; on conversion failure it …
Unique vs PR tests: 05214 and 05213 only use literals the helper converts successfully - every arm asserts that pruning happens on the corrected instant. Neither reaches the converted.isNull() branch, which is the one the CI LLVM report marks uncovered. This test is the only one that executes that branch (6 hits, from both call sites), but it does not detect its removal: with return false at :2006 flipped to return true, or with the whole KeyCondition.cpp diff of #120114 reverted, its 12 output lines stay byte-identical (measured by @groeneai in review). The reason is the fallback below the helper: castColumnAccurateOrNull (KeyCondition.cpp:1797) builds its cast with a null context (castColumn.cpp:33), so it never sees cast_string_to_date_time_mode and returns NULL for the same literal — the atom is rejected either way. What the test pins is the user-visible invariant: a literal key analysis cannot convert must not prune rows. The third arm is a positive control that pruning is still active (1/2 granules), not a detector of the #120114 fix.
Try it on ClickHouse Fiddle

cc @groeneai (author of #120114), @alexey-milovidov (merged/approved #120114) — could you take a look, and add the can be tested label if this looks good?

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Not applicable — test-only change.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

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

Version info

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

@clickgapai

Copy link
Copy Markdown
Contributor Author
Pre-PR validation: 6/6 ✅

a) Deterministic repro? ✅
Test exercises the previously-uncovered branch and passes (correct behaviour).

b) Root cause explained? ✅
src/Storages/MergeTree/KeyCondition.cpp:1927 (worktree line 2006): if (converted.isNull()) return false; in tryNormalizeTextConstantForZonelessDateTimeInput, plus the two return false call sites at 2046/2555 (worktree 2125/2663). All three are marked uncovered by the CI LLVM report for this PR.

c) Fix matches root cause? ✅
N/A for coverage gap — this PR adds a regression test, not a fix. Asserts current correct behaviour at the cited line.

d) Test intent preserved? / New tests added? ✅
New regression test added: 05233_key_analysis_unconvertible_text_constant.sql.

e) Both directions demonstrated? ✅
Test passes (MATCH) on master proving current correctness; would fail if the cited branch regressed. Single-direction is the correct shape for a coverage gap test.

f) Fix is general, not a narrow patch? ✅
Test asserts the invariant at the cited line — covers the branch, not a specific input shape.

@groeneai

Copy link
Copy Markdown
Collaborator

I looked at this because it covers a branch from my #120114.

The gap is real. With an fprintf added to that early return on master (debug), this test reaches it 6 times and 05214_key_analysis_zoneless_datetime_session_tz reaches it 0 times. 05213 cannot reach it either: every literal there is offset-free, which is the shape 05214 measured 0 hits on. Your parenthesised "worktree" numbers are the ones that match current master (branch at KeyCondition.cpp:2005-2006, call sites at :2124 in canConstantBeWrappedByMonotonicFunctions and :2662 in canConstantBeWrappedByDeterministicFunctions); the primary citations :1927, :2046, :2555 do not.

The test runs clean and does not depend on settings randomization. 30 runs of it plus 30 of 05214 in one runner invocation against master 827b4f0e149c6 (debug): 60/60 with randomization on, and system.query_log shows 31 distinct test databases, 7 distinct drawn session_timezone values and both values of optimize_time_filter_with_preimage. Every key-analysis query still ran at session_timezone = 'UTC', because the SET precedes the CREATEs, which is what keeps both key types frozen at UTC. Adding index_granularity_bytes = 1024 to both tables leaves the granule counts at 2/2 and 1/2. ci/jobs/check_style.py is clean on both files.

The oracle does not pin the branch it covers, though. Three measurements in one worktree:

  • replacing return false with return true at :2006 leaves all 12 output lines byte-identical;
  • reverting the whole KeyCondition.cpp diff of Fix rows silently dropped by pruning when a string constant is compared with a zone-less DateTime key #120114 also leaves them byte-identical, while 05214 fails on that same binary (four arms go from 1 1 to 0 1), so the control holds;
  • 12 text constants (offset-bearing, Z, bare ISO-T, RFC 1123, unix seconds, .000000+00:00, whitespace-padded, mm/dd/yyyy, date-only, compact, no-seconds) give identical pruned/honest pairs on the pre-fix and the fixed binary, on both tables.

The reason: both conversions inside key analysis parse with default FormatSettings (date_time_input_format = Basic), the helper through tryConvertFieldToType and the fallback below it through castColumnAccurateOrNull (KeyCondition.cpp:1797), whose internal cast is built with a null context (castColumn.cpp:33) and so never sees cast_string_to_date_time_mode. When the helper declines, that cast returns NULL for the same literal and the atom is rejected anyway. The early return is defensive, and I could not construct a constant that makes its decision observable.

That does not make the test worthless: it is the only test that executes the line, it pins the user-visible invariant (a literal key analysis cannot convert must not prune rows), and it carries a positive control. No objection to it going in as a coverage test. Please fix "This test drives the failure branch of both call sites": it reaches that branch, it does not detect its removal, and as written the next reader will believe the line is behaviourally covered.

One thing I noticed while measuring and am not raising: the comparison casts text with cast_string_to_date_time_mode (best_effort by default) while key analysis converts with Basic, so for a zone-less DateTime/DateTime64 key any best-effort-only literal turns pruning off entirely. That is the safe direction (a full read, never a wrong result), and it is what this branch implements.

can be tested has to come from a maintainer: I do not add labels on another author's PR.

@zlareb1 zlareb1 added the can be tested Allows running workflows for external contributors label Sep 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [838e5d7]

Summary: ✅


AI Review

Summary

This is a test-only PR that adds 05233_key_analysis_unconvertible_text_constant to cover the safe fallback path where key analysis cannot normalize a text literal for a zoneless DateTime64 key and therefore must not prune matching rows. The earlier inline issue about unstable EXPLAIN output was fixed by pinning explain_query_plan_default to legacy, and I did not find any new correctness or coverage problems in the current diff.

Final Verdict

✅ No new findings.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 20, 2026
@clickgapai

Copy link
Copy Markdown
Contributor Author

Fixed the description — the branch-coverage claim is gone. The "Unique vs PR tests" line now says the test is the only one that executes that branch (6 hits, both call sites) but does not detect its removal: byte-identical output with :2006 flipped and with the #120114 KeyCondition.cpp diff reverted, and the third arm is a positive control that pruning is still active, not a detector of the fix.

Citations corrected to master the latest commit: helper at KeyCondition.cpp:1988, branch at :2005-2006, call sites :2124 and :2662 — confirmed in the checkout. The :1927/:2046/:2555 primaries were diff-context offsets; my mistake.

Your mechanism holds on re-read: the fallback castColumnAccurateOrNull (KeyCondition.cpp:1797) goes through createInternalCast(from, to, cast_type, {}, nullptr) (castColumn.cpp:33), so it never sees cast_string_to_date_time_mode and returns NULL for the same literal — the atom is rejected with or without the early return.

Test file unchanged: its header states only the invariant it asserts, no coverage claim. Understood on the label.

@clickgapai

Copy link
Copy Markdown
Contributor Author

CI failure in Stateless tests (arm_binary, parallel), Stateless tests (amd_debug, parallel) appears to be unrelated breakage and is not related to this change. Let me know if you'd like me to merge master and retrigger CI.

@groeneai

Copy link
Copy Markdown
Collaborator

Yes, merging master is the right move.

All three red stateless jobs on 74c42f4c are the same test, 05227_storage_settings_unknown_name, including Stateless tests (arm_asan_ubsan, targeted), which your comment does not list; its extra Too many test failures row is the runner stopping at the --max-failures limit, so that job never finished its set.

It was fixed by #121163, merged 2026-09-20T17:25:51Z as 1d7d23fc710593210d26b3d5f84ba782e4d4c407. That commit is not an ancestor of 74c42f4c (the branches diverge at 2debf183), and the tree CI tested still carries the pre-fix reference: the diff is the SETTINGS disk = 'default', max_threads = DEFAULT line. That test has been green on master since the merge. So a retrigger on its own will not clear it, the merge is what picks up the fix.

@clickgapai

Copy link
Copy Markdown
Contributor Author

Pushed 2ade4c5 — merged upstream/master. CI will retrigger.

The Condition/Granules assertions read legacy plan output; without the
pin they follow the ambient explain_query_plan_default (now 'pretty')
or compatibility. Matches 05214_key_analysis_zoneless_datetime_session_tz.

Requested by @clickhouse-gh[bot] in ClickHouse#121199 (comment)
@alexey-milovidov alexey-milovidov self-assigned this Sep 27, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 27, 2026
Merged via the queue into ClickHouse:master with commit 2cfcdb6 Sep 27, 2026
176 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 27, 2026
@clickhouse-gh clickhouse-gh Bot added comp-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... 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-key-condition Analysis of a filter against a key. KeyCondition turns a WHERE condition into ranges over the pri... pr-not-for-changelog This PR should not be mentioned in the changelog 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