From 2992e7039338ac22588bac4d841b5a5a819610fe Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Tue, 12 Aug 2025 20:20:47 -0700 Subject: [PATCH] fix: correct offset's symbol (#6728) Signed-off-by: Ruihang Xia --- src/promql/src/extension_plan/normalize.rs | 12 +- src/query/src/promql/planner.rs | 8 +- .../standalone/common/promql/offset.result | 114 ++++++++++++++++++ .../cases/standalone/common/promql/offset.sql | 57 +++++++++ .../standalone/common/tql/tql-cte.result | 13 +- 5 files changed, 190 insertions(+), 14 deletions(-) create mode 100644 tests/cases/standalone/common/promql/offset.result create mode 100644 tests/cases/standalone/common/promql/offset.sql diff --git a/src/promql/src/extension_plan/normalize.rs b/src/promql/src/extension_plan/normalize.rs index c9bce90d6a..78f809491c 100644 --- a/src/promql/src/extension_plan/normalize.rs +++ b/src/promql/src/extension_plan/normalize.rs @@ -307,7 +307,7 @@ impl SeriesNormalizeStream { Arc::new(ts_column.clone()) as _ } else { Arc::new(TimestampMillisecondArray::from_iter( - ts_column.iter().map(|ts| ts.map(|ts| ts + self.offset)), + ts_column.iter().map(|ts| ts.map(|ts| ts - self.offset)), )) }; let mut columns = input.columns().to_vec(); @@ -457,11 +457,11 @@ mod test { "+---------------------+--------+------+\ \n| timestamp | value | path |\ \n+---------------------+--------+------+\ - \n| 1970-01-01T00:01:01 | 0.0 | foo |\ - \n| 1970-01-01T00:02:01 | 1.0 | foo |\ - \n| 1970-01-01T00:00:01 | 10.0 | foo |\ - \n| 1970-01-01T00:00:31 | 100.0 | foo |\ - \n| 1970-01-01T00:01:31 | 1000.0 | foo |\ + \n| 1970-01-01T00:00:59 | 0.0 | foo |\ + \n| 1970-01-01T00:01:59 | 1.0 | foo |\ + \n| 1969-12-31T23:59:59 | 10.0 | foo |\ + \n| 1970-01-01T00:00:29 | 100.0 | foo |\ + \n| 1970-01-01T00:01:29 | 1000.0 | foo |\ \n+---------------------+--------+------+", ); diff --git a/src/query/src/promql/planner.rs b/src/query/src/promql/planner.rs index b7d91c600b..60c092699b 100644 --- a/src/query/src/promql/planner.rs +++ b/src/query/src/promql/planner.rs @@ -1360,12 +1360,12 @@ impl PromPlanner { let single_time_range = time_index_expr .clone() .gt_eq(DfExpr::Literal(ScalarValue::TimestampMillisecond( - Some(self.ctx.start - offset_duration - self.ctx.lookback_delta - range), + Some(self.ctx.start + offset_duration - self.ctx.lookback_delta - range), None, ))) .and( time_index_expr.lt_eq(DfExpr::Literal(ScalarValue::TimestampMillisecond( - Some(self.ctx.end - offset_duration + self.ctx.lookback_delta), + Some(self.ctx.end + offset_duration + self.ctx.lookback_delta), None, ))), ); @@ -1379,12 +1379,12 @@ impl PromPlanner { time_index_expr .clone() .gt_eq(DfExpr::Literal(ScalarValue::TimestampMillisecond( - Some(timestamp - offset_duration - lookback_delta - range), + Some(timestamp + offset_duration - lookback_delta - range), None, ))) .and(time_index_expr.clone().lt_eq(DfExpr::Literal( ScalarValue::TimestampMillisecond( - Some(timestamp - offset_duration + lookback_delta), + Some(timestamp + offset_duration + lookback_delta), None, ), ))), diff --git a/tests/cases/standalone/common/promql/offset.result b/tests/cases/standalone/common/promql/offset.result new file mode 100644 index 0000000000..f5787928d8 --- /dev/null +++ b/tests/cases/standalone/common/promql/offset.result @@ -0,0 +1,114 @@ +-- Referenced from https://github.com/prometheus/prometheus/blob/a48d348811619ba4e8eae9c3eaae4857749a1578/promql/promqltest/testdata/functions.test#L172-L185 +create table calculate_rate_offset_total ( + ts timestamp time index, + val double, + x string primary key +); + +Affected Rows: 0 + +insert into calculate_rate_offset_total values + (0, 0.0, 'a'), + (300000, 10.0, 'a'), + (600000, 20.0, 'a'), + (900000, 30.0, 'a'), + (1200000, 40.0, 'a'), + (1500000, 50.0, 'a'), + (1800000, 60.0, 'a'), + (2100000, 70.0, 'a'), + (2400000, 80.0, 'a'), + (2700000, 90.0, 'a'), + (3000000, 100.0, 'a'), + (0, 0.0, 'b'), + (300000, 20.0, 'b'), + (600000, 40.0, 'b'), + (900000, 60.0, 'b'), + (1200000, 80.0, 'b'), + (1500000, 100.0, 'b'), + (1800000, 120.0, 'b'), + (2100000, 140.0, 'b'), + (2400000, 160.0, 'b'), + (2700000, 180.0, 'b'), + (3000000, 200.0, 'b'); + +Affected Rows: 22 + +-- SQLNESS SORT_RESULT 3 1 +tql eval (1500, 1500, '1s') calculate_rate_offset_total; + ++---------------------+-------+---+ +| ts | val | x | ++---------------------+-------+---+ +| 1970-01-01T00:25:00 | 100.0 | b | +| 1970-01-01T00:25:00 | 50.0 | a | ++---------------------+-------+---+ + +-- SQLNESS SORT_RESULT 3 1 +tql eval (1500, 1500, '1s') calculate_rate_offset_total offset 10m; + ++---------------------+-------+---+ +| ts | val | x | ++---------------------+-------+---+ +| 1970-01-01T00:25:00 | 140.0 | b | +| 1970-01-01T00:25:00 | 70.0 | a | ++---------------------+-------+---+ + +-- SQLNESS SORT_RESULT 3 1 +tql eval (1500, 1500, '1s') calculate_rate_offset_total offset -10m; + ++---------------------+------+---+ +| ts | val | x | ++---------------------+------+---+ +| 1970-01-01T00:25:00 | 30.0 | a | +| 1970-01-01T00:25:00 | 60.0 | b | ++---------------------+------+---+ + +-- SQLNESS SORT_RESULT 3 1 +tql eval (0, 0, '1s') calculate_rate_offset_total offset 10m; + ++---------------------+------+---+ +| ts | val | x | ++---------------------+------+---+ +| 1970-01-01T00:00:00 | 20.0 | a | +| 1970-01-01T00:00:00 | 40.0 | b | ++---------------------+------+---+ + +tql eval (0, 0, '1s') calculate_rate_offset_total offset -10m; + +++ +++ + +tql eval (3000, 3000, '1s') calculate_rate_offset_total offset 10m; + +++ +++ + +-- SQLNESS SORT_RESULT 3 1 +tql eval (3000, 3000, '1s') calculate_rate_offset_total offset -10m; + ++---------------------+-------+---+ +| ts | val | x | ++---------------------+-------+---+ +| 1970-01-01T00:50:00 | 160.0 | b | +| 1970-01-01T00:50:00 | 80.0 | a | ++---------------------+-------+---+ + +tql eval (3000, 3000, '1s') rate(calculate_rate_window_total[10m]); + +++ +++ + +-- SQLNESS SORT_RESULT 3 1 +tql eval (3000, 3000, '1s') rate(calculate_rate_offset_total[10m] offset 5m); + ++---------------------+------------------------------------------+---+ +| ts | prom_rate(ts_range,val,ts,Int64(600000)) | x | ++---------------------+------------------------------------------+---+ +| 1970-01-01T00:50:00 | 0.03333333333333333 | a | +| 1970-01-01T00:50:00 | 0.06666666666666667 | b | ++---------------------+------------------------------------------+---+ + +drop table calculate_rate_offset_total; + +Affected Rows: 0 + diff --git a/tests/cases/standalone/common/promql/offset.sql b/tests/cases/standalone/common/promql/offset.sql new file mode 100644 index 0000000000..37981e7929 --- /dev/null +++ b/tests/cases/standalone/common/promql/offset.sql @@ -0,0 +1,57 @@ +-- Referenced from https://github.com/prometheus/prometheus/blob/a48d348811619ba4e8eae9c3eaae4857749a1578/promql/promqltest/testdata/functions.test#L172-L185 + +create table calculate_rate_offset_total ( + ts timestamp time index, + val double, + x string primary key +); + +insert into calculate_rate_offset_total values + (0, 0.0, 'a'), + (300000, 10.0, 'a'), + (600000, 20.0, 'a'), + (900000, 30.0, 'a'), + (1200000, 40.0, 'a'), + (1500000, 50.0, 'a'), + (1800000, 60.0, 'a'), + (2100000, 70.0, 'a'), + (2400000, 80.0, 'a'), + (2700000, 90.0, 'a'), + (3000000, 100.0, 'a'), + (0, 0.0, 'b'), + (300000, 20.0, 'b'), + (600000, 40.0, 'b'), + (900000, 60.0, 'b'), + (1200000, 80.0, 'b'), + (1500000, 100.0, 'b'), + (1800000, 120.0, 'b'), + (2100000, 140.0, 'b'), + (2400000, 160.0, 'b'), + (2700000, 180.0, 'b'), + (3000000, 200.0, 'b'); + +-- SQLNESS SORT_RESULT 3 1 +tql eval (1500, 1500, '1s') calculate_rate_offset_total; + +-- SQLNESS SORT_RESULT 3 1 +tql eval (1500, 1500, '1s') calculate_rate_offset_total offset 10m; + +-- SQLNESS SORT_RESULT 3 1 +tql eval (1500, 1500, '1s') calculate_rate_offset_total offset -10m; + +-- SQLNESS SORT_RESULT 3 1 +tql eval (0, 0, '1s') calculate_rate_offset_total offset 10m; + +tql eval (0, 0, '1s') calculate_rate_offset_total offset -10m; + +tql eval (3000, 3000, '1s') calculate_rate_offset_total offset 10m; + +-- SQLNESS SORT_RESULT 3 1 +tql eval (3000, 3000, '1s') calculate_rate_offset_total offset -10m; + +tql eval (3000, 3000, '1s') rate(calculate_rate_window_total[10m]); + +-- SQLNESS SORT_RESULT 3 1 +tql eval (3000, 3000, '1s') rate(calculate_rate_offset_total[10m] offset 5m); + +drop table calculate_rate_offset_total; diff --git a/tests/cases/standalone/common/tql/tql-cte.result b/tests/cases/standalone/common/tql/tql-cte.result index 943e7e30a3..c651a8d361 100644 --- a/tests/cases/standalone/common/tql/tql-cte.result +++ b/tests/cases/standalone/common/tql/tql-cte.result @@ -228,10 +228,15 @@ WITH time_shifted AS ( ) SELECT * FROM time_shifted; -+----+-----+ -| ts | val | -+----+-----+ -+----+-----+ ++---------------------+-----+ +| ts | val | ++---------------------+-----+ +| 1970-01-01T00:00:00 | 3.0 | +| 1970-01-01T00:00:10 | 3.0 | +| 1970-01-01T00:00:20 | 3.0 | +| 1970-01-01T00:00:30 | 3.0 | +| 1970-01-01T00:00:40 | 3.0 | ++---------------------+-----+ -- TQL CTE with JOIN between TQL and regular table -- SQLNESS SORT_RESULT 3 1