fix: correct offset's symbol (#6728)

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
This commit is contained in:
Ruihang Xia
2025-08-12 20:20:47 -07:00
committed by GitHub
parent 8a44137f37
commit 2992e70393
5 changed files with 190 additions and 14 deletions

View File

@@ -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+---------------------+--------+------+",
);

View File

@@ -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,
),
))),

View File

@@ -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

View File

@@ -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;

View File

@@ -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