Repository navigation
Add locality_hash to the TimeSeries samples table and use it in PromQL queries - #111697
nikitamikhaylov wants to merge 5 commits into
Conversation
…L queries The samples inner table now has locality_hash UInt64 (= xxHash64(metric_name)) as the first column of its primary key, ORDER BY (locality_hash, id, timestamp), so samples of the same metric are stored close to each other; the generated engine also uses index_granularity = 32768. The tags inner table stores the same hash as a MATERIALIZED column, so the queries generated for evaluating PromQL selectors (and Prometheus remote read) read it instead of calculating hashes: they filter the samples table with (locality_hash, id) IN (SELECT locality_hash, timeSeriesStoreTags(...) FROM tags WHERE <matchers>), letting the primary key index prune granules. Both write paths (TimeSeriesSink and the Prometheus remote-write protocol) calculate the hash per time series and replicate it per sample row; timeSeriesLocalityHash.h is the single source of truth for the invariant. The column is required and validated at CREATE time: UInt64 in both tables, the tags column must be declared MATERIALIZED (or DEFAULT) xxHash64(metric_name) (any other stored value would make queries silently return no data), SummingMergeTree samples engines must keep the column in the sorting key, and locality_hash is a reserved name for tags_to_columns. Older tables lack the column; once the TimeSeries schema versioning is merged, TimeSeriesVersion::LATEST and MIN_SUPPORTED_BY_PROMQL must be bumped to 2. Depends on: #111204 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [b9d577b] Summary: ❌
AI ReviewSummaryThis PR adds optional Findings❌ Blockers
Final Verdict❌ Changes requested. The compatibility fixes for old tables look good, but the |
| return; | ||
|
|
||
| /// An explicit list of columns to sum is the user's choice, e.g. SummingMergeTree((value)). | ||
| if (storage.engine->arguments && !storage.engine->arguments->children.empty()) |
There was a problem hiding this comment.
This return is too broad for the SummingMergeTree family. ReplicatedSummingMergeTree('/zk', '{replica}') (and similar replicated/shared variants) always has non-empty engine->arguments even when the user did not provide an explicit columns-to-sum list, so ORDER BY (id, timestamp) slips through here. The first background merge will then sum locality_hash and selectors will silently miss those rows. Please distinguish replication parameters from the optional summing-columns argument instead of treating any arg list as safe.
| if (kind == ViewTarget::Samples) | ||
| { | ||
| if (auto * samples_engine = create_query.getTargetInnerEngine(kind)) | ||
| checkSamplesEngineWontSumLocalityHash(*samples_engine, table_id); |
There was a problem hiding this comment.
We only enforce the SummingMergeTree / locality_hash invariant for inline inner samples engines. SAMPLES ext_samples still goes through the external-target branch above, where we only check column types. That means CREATE TABLE ... ENGINE = TimeSeries SAMPLES ext_samples backed by SummingMergeTree ORDER BY (id, timestamp) is accepted even though merges will later sum locality_hash and make PromQL selectors miss data. The PR description says this invariant is validated at CREATE time, so external samples targets need the same engine check.
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
The auto-created `timestamp` and `value` samples columns now carry `CODEC(DoubleDelta, ZSTD(1))` and `CODEC(Gorilla, ZSTD(1))`, so the `extract` pattern `\(([^)]*)\)` truncated the `SAMPLES INNER COLUMNS` block at the first closing parenthesis inside the codec. Anchor the extraction at `) SAMPLES INNER ENGINE` instead and update the reference. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111697&sha=ec9704295fe958b3049ff9e841cf248d8a907345&name_0=PR&name_1=Fast%20test PR: #111697 Co-Authored-By: Claude Fable 5 <[email protected]>
| /// `SummingMergeTree` sums every numeric column outside the sorting key when it collapses rows, | ||
| /// which would corrupt the `locality_hash` column during merges and silently hide the affected | ||
| /// samples from PromQL queries. Reject such engines unless `locality_hash` is part of the | ||
| /// sorting key or the columns to sum are given explicitly. |
There was a problem hiding this comment.
SummingMergeTree correctness here is stricter than "keep locality_hash out of the summed columns". The samples table still has to preserve the full sample identity across merges. For example, SAMPLES INNER ENGINE = SummingMergeTree ORDER BY (locality_hash, id) passes this check today, but the first background merge will collapse every timestamp for a series into one row; ORDER BY (locality_hash, timestamp) does the same across different ids. An explicit columns_to_sum list does not rescue those cases because the incomplete sorting key still groups distinct samples together. This validator needs to require id and timestamp in the sorting key as well, not only locality_hash.
a74d317 to
dab0bd6
Compare
The auto-created `timestamp` and `value` samples columns now carry `CODEC(DoubleDelta, ZSTD(1))` and `CODEC(Gorilla, ZSTD(1))`, so the `extract` pattern `\(([^)]*)\)` truncated the `SAMPLES INNER COLUMNS` block at the first closing parenthesis inside the codec. Anchor the extraction at `) SAMPLES INNER ENGINE` instead and update the reference. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111697&sha=ec9704295fe958b3049ff9e841cf248d8a907345&name_0=PR&name_1=Fast%20test PR: #111697 Co-Authored-By: Claude Fable 5 <[email protected]>
…eSeries tables The test test_prometheus_protocols/test_upgrade_from_prealpha.py failed in #111697 because tables created by older versions of ClickHouse don't have the `locality_hash` column while the read and write paths assumed it exists. The original plan was to reject such tables via the TimeSeries schema versioning (PR #111204); that dependency is dropped in favor of presence-based compatibility: - `StorageTimeSeriesSelector` generates the `(locality_hash, id) IN (...)` condition only when both the samples and the tags target tables have the column, otherwise it falls back to the previous `id IN (...)` filter; - `TimeSeriesSink` and the Prometheus remote-write protocol fill the column only if the samples table has it; - `checkTargetTable` validates the column (`UInt64` type, and the `MATERIALIZED xxHash64(metric_name)` expression in the tags table) only when the column is present. Newly created `TimeSeries` tables still always get the column because `normalizeInnerTableColumns` adds it to the inner tables at CREATE time. The new test `04628_timeseries_locality_hash_compat` covers tables without the column and the mixed cases (only samples / only tags have it). Co-Authored-By: Claude Fable 5 <[email protected]>
The samples inner table of a newly created TimeSeries table now also has the `locality_hash` column, so the test added on master in #112110 lists one more column. Co-Authored-By: Claude Fable 5 <[email protected]>
|
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 275/281 (97.86%) · Uncovered code |
|
I think if we allow multi-component identifiers (#112799) it will get us actually the same idea of storing samples of the same metrics closer to each others without any compatibility issues. Also multi-component identifiers seems to be a good idea anyway, so I implemented them. |
|
I moved switching default granularity for the |
Closes: #100906
Related: #111204
The samples inner table of a
TimeSeriestable now has alocality_hash UInt64column (xxHash64(metric_name)) as the first column of its primary key (ORDER BY (locality_hash, id, timestamp)) and usesindex_granularity = 32768, so samples of the same metric are stored close to each other. The tags inner table stores the same hash as aMATERIALIZEDcolumn, and the queries generated for evaluating PromQL selectors filter the samples table with(locality_hash, id) IN (SELECT locality_hash, timeSeriesStoreTags(...) FROM tags WHERE <matchers>), pruning granules by the primary key index. Both write paths (INSERTand Prometheus remote write) calculate the hash per time series, and the invariant is validated atCREATEtime. Old tables lack the column; once #111204 is merged,TimeSeriesVersion::LATESTandMIN_SUPPORTED_BY_PROMQLwill be bumped to 2 in a follow-up commit on this PR.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The samples inner table of a
TimeSeriestable now stores alocality_hashcolumn (xxHash64(metric_name)) as the first column of its primary key and usesindex_granularity = 32768; the queries evaluating PromQL selectors use that column to prune granules.🤖 Generated with Claude Code