fix: promql offset direction (#7392)

* fix: promql offset direction

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* sort sqlness result

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* commit forgotten file

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

---------

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
This commit is contained in:
Ruihang Xia
2025-12-12 15:51:35 +08:00
committed by GitHub
parent cbfdeca64c
commit bd3ad60910
7 changed files with 106 additions and 41 deletions

View File

@@ -365,7 +365,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();
@@ -518,11 +518,11 @@ mod test {
"+---------------------+--------+------+\
\n| timestamp | value | path |\
\n+---------------------+--------+------+\
\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| 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+---------------------+--------+------+",
);

View File

@@ -1414,14 +1414,14 @@ impl PromPlanner {
.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,
),
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,
),
None,
@@ -1437,14 +1437,14 @@ impl PromPlanner {
.clone()
.gt_eq(DfExpr::Literal(
ScalarValue::TimestampMillisecond(
Some(timestamp + offset_duration - lookback_delta - range),
Some(timestamp - offset_duration - lookback_delta - range),
None,
),
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,
),
None,

View File

@@ -46,16 +46,6 @@ 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;
+---------------------+-------+---+
| 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 |
+---------------------+------+---+
@@ -63,9 +53,25 @@ tql eval (1500, 1500, '1s') calculate_rate_offset_total offset -10m;
| 1970-01-01T00:25:00 | 60.0 | b |
+---------------------+------+---+
-- 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 (0, 0, '1s') calculate_rate_offset_total offset 10m;
++
++
-- SQLNESS SORT_RESULT 3 1
tql eval (0, 0, '1s') calculate_rate_offset_total offset -10m;
+---------------------+------+---+
| ts | val | x |
+---------------------+------+---+
@@ -73,18 +79,8 @@ tql eval (0, 0, '1s') calculate_rate_offset_total offset 10m;
| 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;
tql eval (3000, 3000, '1s') calculate_rate_offset_total offset 10m;
+---------------------+-------+---+
| ts | val | x |
@@ -93,6 +89,13 @@ tql eval (3000, 3000, '1s') calculate_rate_offset_total offset -10m;
| 1970-01-01T00:50:00 | 80.0 | a |
+---------------------+-------+---+
-- SQLNESS SORT_RESULT 3 1
tql eval (3000, 3000, '1s') calculate_rate_offset_total offset -10m;
++
++
-- SQLNESS SORT_RESULT 3 1
tql eval (3000, 3000, '1s') rate(calculate_rate_window_total[10m]);
++

View File

@@ -42,13 +42,16 @@ 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;
-- SQLNESS SORT_RESULT 3 1
tql eval (0, 0, '1s') calculate_rate_offset_total offset -10m;
-- SQLNESS SORT_RESULT 3 1
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;
-- SQLNESS SORT_RESULT 3 1
tql eval (3000, 3000, '1s') rate(calculate_rate_window_total[10m]);
-- SQLNESS SORT_RESULT 3 1

View File

@@ -0,0 +1,44 @@
-- Regression for offset direction: positive offsets should query past data.
create table offset_direction (
ts timestamp time index,
val double,
host string primary key
);
Affected Rows: 0
insert into offset_direction values
(940000, 10.0, 'a'),
(1000000, 20.0, 'a'),
(1060000, 30.0, 'a');
Affected Rows: 3
tql eval (1000, 1000, '1s') offset_direction;
+---------------------+------+------+
| ts | val | host |
+---------------------+------+------+
| 1970-01-01T00:16:40 | 20.0 | a |
+---------------------+------+------+
tql eval (1000, 1000, '1s') offset_direction offset 60s;
+---------------------+------+------+
| ts | val | host |
+---------------------+------+------+
| 1970-01-01T00:16:40 | 10.0 | a |
+---------------------+------+------+
tql eval (1000, 1000, '1s') offset_direction offset -60s;
+---------------------+------+------+
| ts | val | host |
+---------------------+------+------+
| 1970-01-01T00:16:40 | 30.0 | a |
+---------------------+------+------+
drop table offset_direction;
Affected Rows: 0

View File

@@ -0,0 +1,20 @@
-- Regression for offset direction: positive offsets should query past data.
create table offset_direction (
ts timestamp time index,
val double,
host string primary key
);
insert into offset_direction values
(940000, 10.0, 'a'),
(1000000, 20.0, 'a'),
(1060000, 30.0, 'a');
tql eval (1000, 1000, '1s') offset_direction;
tql eval (1000, 1000, '1s') offset_direction offset 60s;
tql eval (1000, 1000, '1s') offset_direction offset -60s;
drop table offset_direction;

View File

@@ -676,15 +676,10 @@ WITH time_shifted AS (
)
SELECT * FROM time_shifted;
+---------------------+-----+
| 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 |
+---------------------+-----+
+----+-----+
| ts | val |
+----+-----+
+----+-----+
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE (partitioning.*) REDACTED
@@ -702,7 +697,7 @@ SELECT * FROM time_shifted;
| | PromInstantManipulate: range=[0..40000], lookback=[300000], interval=[10000], time index=[ts] |
| | PromSeriesNormalize: offset=[50000], time index=[ts], filter NaN: [false] |
| | PromSeriesDivide: tags=[] |
| | Filter: metric.ts >= TimestampMillisecond(-250000, None) AND metric.ts <= TimestampMillisecond(390000, None) |
| | Filter: metric.ts >= TimestampMillisecond(-350000, None) AND metric.ts <= TimestampMillisecond(290000, None) |
| | TableScan: metric |
| | ]] |
| physical_plan | CooperativeExec |