From 0fb9e1995eba9e0f648b60fb7bbf1956b6c48fde Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Tue, 15 Apr 2025 23:00:30 +0800 Subject: [PATCH] fix: preserve timestamp precision of irate (#5904) Signed-off-by: Ruihang Xia --- src/promql/src/functions/idelta.rs | 4 +- .../standalone/common/tql/operator.result | 53 +++++++++++++++++++ .../cases/standalone/common/tql/operator.sql | 35 ++++++++++++ 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/src/promql/src/functions/idelta.rs b/src/promql/src/functions/idelta.rs index 0e581d2d2c..c5d1897a3e 100644 --- a/src/promql/src/functions/idelta.rs +++ b/src/promql/src/functions/idelta.rs @@ -138,9 +138,7 @@ impl IDelta { } // else is rate - // TODO(ruihang): "divide 1000" converts the timestamp from millisecond to second. - // it should consider other percisions. - let sampled_interval = (timestamps[len - 1] - timestamps[len - 2]) / 1000; + let sampled_interval = (timestamps[len - 1] - timestamps[len - 2]) as f64 / 1000.0; let last_value = values[len - 1]; let prev_value = values[len - 2]; let result_value = if last_value < prev_value { diff --git a/tests/cases/standalone/common/tql/operator.result b/tests/cases/standalone/common/tql/operator.result index d3b85ac445..6ad97ae88a 100644 --- a/tests/cases/standalone/common/tql/operator.result +++ b/tests/cases/standalone/common/tql/operator.result @@ -78,3 +78,56 @@ drop table trignan; Affected Rows: 0 +-- About irate. Related to issue https://github.com/GreptimeTeam/greptimedb/issues/5880 +CREATE TABLE t( + greptime_timestamp TIMESTAMP(9) TIME INDEX, + greptime_value DOUBLE +); + +Affected Rows: 0 + +INSERT INTO t(greptime_timestamp, greptime_value) +VALUES + ('2025-04-01T00:00:00.5Z', 1), + ('2025-04-01T00:00:01Z', 2), + ('2025-04-01T00:00:01.5Z', 3), + ('2025-04-01T00:00:02Z', 4), + ('2025-04-01T00:00:02.5Z', 5), + ('2025-04-01T00:00:03Z', 6), + ('2025-04-01T00:00:03.5Z', 7), + ('2025-04-01T00:00:04Z', 8), + ('2025-04-01T00:00:04.5Z', 9), + ('2025-04-01T00:00:05Z', 10), + ('2025-04-01T00:00:05.5Z', 11), + ('2025-04-01T00:00:06Z', 12), + ('2025-04-01T00:00:06.5Z', 13), + ('2025-04-01T00:00:07Z', 14), + ('2025-04-01T00:00:07.5Z', 15), + ('2025-04-01T00:00:08Z', 16), + ('2025-04-01T00:00:08.5Z', 17), + ('2025-04-01T00:00:09Z', 18), + ('2025-04-01T00:00:09.5Z', 19), + ('2025-04-01T00:00:10Z', 20); + +Affected Rows: 20 + +tql eval (1743465600.5, 1743465610, '1s') irate(t[2s]); + ++-------------------------+-----------------------------------------------------+ +| greptime_timestamp | prom_irate(greptime_timestamp_range,greptime_value) | ++-------------------------+-----------------------------------------------------+ +| 2025-04-01T00:00:01.500 | 2.0 | +| 2025-04-01T00:00:02.500 | 2.0 | +| 2025-04-01T00:00:03.500 | 2.0 | +| 2025-04-01T00:00:04.500 | 2.0 | +| 2025-04-01T00:00:05.500 | 2.0 | +| 2025-04-01T00:00:06.500 | 2.0 | +| 2025-04-01T00:00:07.500 | 2.0 | +| 2025-04-01T00:00:08.500 | 2.0 | +| 2025-04-01T00:00:09.500 | 2.0 | ++-------------------------+-----------------------------------------------------+ + +drop table t; + +Affected Rows: 0 + diff --git a/tests/cases/standalone/common/tql/operator.sql b/tests/cases/standalone/common/tql/operator.sql index f6c342696b..3a6c2c527b 100644 --- a/tests/cases/standalone/common/tql/operator.sql +++ b/tests/cases/standalone/common/tql/operator.sql @@ -39,3 +39,38 @@ drop table trigx; drop table trigy; drop table trignan; + + +-- About irate. Related to issue https://github.com/GreptimeTeam/greptimedb/issues/5880 +CREATE TABLE t( + greptime_timestamp TIMESTAMP(9) TIME INDEX, + greptime_value DOUBLE +); + +INSERT INTO t(greptime_timestamp, greptime_value) +VALUES + ('2025-04-01T00:00:00.5Z', 1), + ('2025-04-01T00:00:01Z', 2), + ('2025-04-01T00:00:01.5Z', 3), + ('2025-04-01T00:00:02Z', 4), + ('2025-04-01T00:00:02.5Z', 5), + ('2025-04-01T00:00:03Z', 6), + ('2025-04-01T00:00:03.5Z', 7), + ('2025-04-01T00:00:04Z', 8), + ('2025-04-01T00:00:04.5Z', 9), + ('2025-04-01T00:00:05Z', 10), + ('2025-04-01T00:00:05.5Z', 11), + ('2025-04-01T00:00:06Z', 12), + ('2025-04-01T00:00:06.5Z', 13), + ('2025-04-01T00:00:07Z', 14), + ('2025-04-01T00:00:07.5Z', 15), + ('2025-04-01T00:00:08Z', 16), + ('2025-04-01T00:00:08.5Z', 17), + ('2025-04-01T00:00:09Z', 18), + ('2025-04-01T00:00:09.5Z', 19), + ('2025-04-01T00:00:10Z', 20); + +tql eval (1743465600.5, 1743465610, '1s') irate(t[2s]); + +drop table t; +