diff --git a/promql/parser/printer.go b/promql/parser/printer.go index 0b6368ed2..c5f80eb0b 100644 --- a/promql/parser/printer.go +++ b/promql/parser/printer.go @@ -116,8 +116,10 @@ func (node *MatrixSelector) String() string { // Copy the Vector selector before changing the offset vecSelector := *node.VectorSelector.(*VectorSelector) offset := "" - if vecSelector.OriginalOffset != time.Duration(0) { + if vecSelector.OriginalOffset > time.Duration(0) { offset = fmt.Sprintf(" offset %s", model.Duration(vecSelector.OriginalOffset)) + } else if vecSelector.OriginalOffset < time.Duration(0) { + offset = fmt.Sprintf(" offset -%s", model.Duration(-vecSelector.OriginalOffset)) } at := "" if vecSelector.Timestamp != nil { @@ -147,8 +149,10 @@ func (node *SubqueryExpr) String() string { step = model.Duration(node.Step).String() } offset := "" - if node.OriginalOffset != time.Duration(0) { + if node.OriginalOffset > time.Duration(0) { offset = fmt.Sprintf(" offset %s", model.Duration(node.OriginalOffset)) + } else if node.OriginalOffset < time.Duration(0) { + offset = fmt.Sprintf(" offset -%s", model.Duration(-node.OriginalOffset)) } at := "" if node.Timestamp != nil { @@ -187,8 +191,10 @@ func (node *VectorSelector) String() string { labelStrings = append(labelStrings, matcher.String()) } offset := "" - if node.OriginalOffset != time.Duration(0) { + if node.OriginalOffset > time.Duration(0) { offset = fmt.Sprintf(" offset %s", model.Duration(node.OriginalOffset)) + } else if node.OriginalOffset < time.Duration(0) { + offset = fmt.Sprintf(" offset -%s", model.Duration(-node.OriginalOffset)) } at := "" if node.Timestamp != nil { diff --git a/promql/parser/printer_test.go b/promql/parser/printer_test.go index 850026a68..1ad48c3d4 100644 --- a/promql/parser/printer_test.go +++ b/promql/parser/printer_test.go @@ -77,12 +77,18 @@ func TestExprString(t *testing.T) { { in: `a offset 1m`, }, + { + in: `a offset -7m`, + }, { in: `a{c="d"}[5m] offset 1m`, }, { in: `a[5m] offset 1m`, }, + { + in: `a[12m] offset -3m`, + }, { in: `a[1h:5m] offset 1m`, },