fix(observability-proxy): refuse any minus right after an offset again

Telling a compound duration from a subtraction by a duration-part
pattern missed forms metricsql accepts after the minus: offset 5m-1.h
is 55 minutes forward and offset 5m-.5h is 25, yet both read as five
minutes back, and the lookup silently missed pods created in between.
A pattern that followed scanSingleDuration exactly would still be a
second copy of the grammar to keep in step, so any minus right after
an offset's duration is refused again; offset 1h-1 and offset 5m-y now
answer 422 as well, and metricsql cannot parse the latter anyway.

Assisted-by: LLM
Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
ZverGuy committed 2026-11-10 04:03:24 +07:00
1 parent 56fc64575a
commit b876368a3f
2 files changed
+11 -9

No files matched your search

@@ -49,10 +49,6 @@ var (
querySeconds = regexp.MustCompile(`^\d+(?:\.\d+)?$`)
)
// negativeDurationPart is what metricsql's scanSingleDuration accepts
// after a minus inside one duration token.
var negativeDurationPart = regexp.MustCompile(`^(?:\d+(?:\.\d+)?(?:ms|s|m|h|d|w|y|i)|\$__interval)`)
var queryDurationUnits = map[string]time.Duration{
"ms": time.Millisecond, "s": time.Second, "m": time.Minute, "h": time.Hour,
"d": 24 * time.Hour, "w": 7 * 24 * time.Hour, "y": 365 * 24 * time.Hour,
@@ -216,8 +212,10 @@ func scanOffsets(lower string) (back, ahead float64, ok bool) {
// metricsql lexes 1h-30m as one duration whose parts after the
// first negative one all subtract (DurationValue), so -1h-30m is
// -90m and 1h-2h is -1h; the scan does not read such compounds.
// A minus not followed by a duration part is an operator.
if end < len(arg) && arg[end] == '-' && negativeDurationPart.MatchString(arg[end+1:]) {
// metricsql continues a duration after a minus in more forms than
// the scan can tell from a subtraction (-1.h and -.5h among them),
// so any minus right after the duration is refused.
if end < len(arg) && arg[end] == '-' {
return 0, 0, false
}
d, ok := parseQueryDuration(arg[:end])
@@ -450,9 +450,6 @@ func TestQueryLookahead(t *testing.T) {
{`x offset 1h - 30m`, 0},
{`x offset -1h + 30m`, time.Hour},
{`x offset -1h30m`, 90 * time.Minute},
// A minus not followed by a duration is an operator.
{`x offset 1h-1`, 0},
{`x offset 5m-y`, 0},
}
for _, tt := range tests {
t.Run(tt.query, func(t *testing.T) {
@@ -477,6 +474,12 @@ func TestQueryLookahead(t *testing.T) {
`x offset - 1h-30m`,
`x offset 1h-2h`,
`x offset 1h-30m`,
// metricsql continues a duration with -1.h or -.5h too, so any minus
// right after the duration is refused rather than told apart.
`x offset 5m-1.h`,
`x offset 5m-.5h`,
`x offset 1h-1`,
`x offset 5m-y`,
`rate(x[5m] offset 1h-30m)`,
// vmselect evaluates rollup_candlestick one step ahead
// (evalRollupFuncWithoutAt), whatever the offset.
@@ -498,6 +501,7 @@ func TestQueryLookahead(t *testing.T) {
// values back onto earlier points, so a series that starts after the
// evaluation time can reach the answer.
func TestQueryLookaheadSubquery(t *testing.T) {
for _, q := range []string{`max_over_time(range_last(x)[1h:1h])`, `max_over_time(keep_next_value(x)[1h:1h])`} {
got, ok := queryLookahead(q, time.Minute)
if !ok || got < 2*time.Hour {