mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-26 12:44:10 +00:00
* fix(promql): collapse duplicate samples at one timestamp before range functions A read path that does not deduplicate, such as an append-mode region, can hand PromQL two rows for one series at one timestamp. Every range function reads its samples from RangeManipulate, so the duplicate entries were treated as independent samples: count_over_time counted both, idelta and irate returned NULL because the last two entries shared a timestamp, and delta/increase/rate deflated because the average sampling interval divides the observed span by sample_count - 1. Prometheus sample identity is one sample per series per timestamp, so its range functions can never see this input. Restore that identity once, at the operator every range function shares, instead of special-casing each function's arithmetic. The collapse compares native ticks, never the millisecond payload, so distinct sub-millisecond samples that share a millisecond stay distinct as calculate_range documents and tests. Batches without duplicate timestamps take a single adjacency scan and are returned untouched; only when duplicates exist is a take built, keeping tag and value columns aligned. Keeping the last row of a group matches MergeMode::LastRow. The normalize test that fed three rows at one timestamp now uses distinct timestamps: it exercises histogram start-timestamp offsetting, and its repeated timestamp was incidental, not asserted behavior. Signed-off-by: discord9 <discord9@163.com> * test(promql): cover duplicate samples end to end and keep the newer value The operator now keeps the first row of a tied timestamp group, which is the row a deduplicating read keeps: storage orders equal primary key and timestamp by sequence descending, and FlatDedupReader takes the first row of each group. The end-to-end case pins that choice by writing 100 and then 101 at the same timestamp: the window reports increase 6, while keeping the stale 100 would report 8. The sqlness case writes duplicate rows into an append_mode region, which is the read path that does not deduplicate, and asserts every range function plus the unaffected ordinary table in both the standalone and distributed environments. Signed-off-by: discord9 <discord9@163.com> * docs(promql): qualify duplicate-timestamp conflict selection Signed-off-by: discord9 <discord9@163.com> * perf(promql): avoid copying tags when collapsing duplicate samples Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com>
73 lines
3.1 KiB
SQL
73 lines
3.1 KiB
SQL
-- A duplicate row set for one series (same tags, same timestamp) is what an append-mode
|
|
-- region legitimately keeps, and what a retried write leaves behind. Prometheus sample
|
|
-- identity is (series, timestamp), so its range functions can never see two samples at one
|
|
-- timestamp; GreptimeDB must not let them see one either.
|
|
--
|
|
-- Two samples per window, each written twice. The window is (0, 60s] with the first sample at
|
|
-- 29.6s and the last at 59.6s, so the pre-fix arithmetic is visible in the results below:
|
|
-- duplicates: n=4, average interval 10s, threshold 11s, leading gap clamped to 5s
|
|
-- factor = (30 + 5 + 0.4) / 30 = 1.18 => increase 2 * 1.18 = 2.36
|
|
-- deduplicated: n=2, average interval 30s, threshold 33s, leading gap kept at 29.6s
|
|
-- factor = (30 + 29.6 + 0.4) / 30 = 2 => increase 2 * 2 = 4
|
|
CREATE TABLE duplicate_samples (
|
|
ts TIMESTAMP TIME INDEX,
|
|
greptime_value DOUBLE,
|
|
series STRING PRIMARY KEY
|
|
) engine=mito with('append_mode'='true');
|
|
|
|
INSERT INTO duplicate_samples VALUES
|
|
(29600, 100.0, 'a'),
|
|
(29600, 100.0, 'a'),
|
|
(59600, 102.0, 'a'),
|
|
(59600, 102.0, 'a');
|
|
|
|
-- Counting duplicate entries would report 4 samples for a window that holds two.
|
|
TQL EVAL (60, 60, '1m') count_over_time(duplicate_samples[1m]);
|
|
|
|
-- Both extrapolated functions must keep the same leading extrapolation.
|
|
TQL EVAL (60, 60, '1m') increase(duplicate_samples[1m]);
|
|
TQL EVAL (60, 60, '1m') delta(duplicate_samples[1m]);
|
|
TQL EVAL (60, 60, '1m') rate(duplicate_samples[1m]);
|
|
|
|
-- The last two samples are 30s apart, not two entries of one timestamp.
|
|
TQL EVAL (60, 60, '1m') idelta(duplicate_samples[1m]);
|
|
TQL EVAL (60, 60, '1m') irate(duplicate_samples[1m]);
|
|
|
|
-- A duplicate row whose value differs from the first write is the same sample updated. One sample
|
|
-- remains for that timestamp, and it carries the value a deduplicating read would return, so the
|
|
-- later write wins: the window holds 101 and 104, giving an observed delta of 3 and an
|
|
-- extrapolation factor of 2, hence 6. Keeping the stale 100 would instead report 8.
|
|
CREATE TABLE duplicate_samples_conflict (
|
|
ts TIMESTAMP TIME INDEX,
|
|
greptime_value DOUBLE,
|
|
series STRING PRIMARY KEY
|
|
) engine=mito with('append_mode'='true');
|
|
|
|
INSERT INTO duplicate_samples_conflict VALUES
|
|
(29600, 100.0, 'a'),
|
|
(29600, 101.0, 'a'),
|
|
(59600, 104.0, 'a');
|
|
|
|
TQL EVAL (60, 60, '1m') count_over_time(duplicate_samples_conflict[1m]);
|
|
TQL EVAL (60, 60, '1m') increase(duplicate_samples_conflict[1m]);
|
|
|
|
-- A deduplicating region keeps its single row, so ordinary tables are unaffected.
|
|
CREATE TABLE duplicate_samples_deduped (
|
|
ts TIMESTAMP TIME INDEX,
|
|
greptime_value DOUBLE,
|
|
series STRING PRIMARY KEY
|
|
);
|
|
|
|
INSERT INTO duplicate_samples_deduped VALUES
|
|
(29600, 100.0, 'a'),
|
|
(29600, 100.0, 'a'),
|
|
(59600, 102.0, 'a'),
|
|
(59600, 102.0, 'a');
|
|
|
|
TQL EVAL (60, 60, '1m') count_over_time(duplicate_samples_deduped[1m]);
|
|
TQL EVAL (60, 60, '1m') increase(duplicate_samples_deduped[1m]);
|
|
|
|
DROP TABLE duplicate_samples;
|
|
DROP TABLE duplicate_samples_conflict;
|
|
DROP TABLE duplicate_samples_deduped;
|