Deprecate duration token

Signed-off-by: darshanime <deathbullet@gmail.com>
This commit is contained in:
darshanime 2024-03-04 19:27:28 +05:30 committed by beorn7
parent 8c8860d2d6
commit cfad8ff3b2
5 changed files with 557 additions and 458 deletions

View File

@ -43,7 +43,6 @@ import (
int int64 int int64
uint uint64 uint uint64
float float64 float float64
duration time.Duration
} }
@ -176,8 +175,7 @@ START_METRIC_SELECTOR
%type <int> int %type <int> int
%type <uint> uint %type <uint> uint
%type <float> number series_value signed_number signed_or_unsigned_number %type <float> number series_value signed_number signed_or_unsigned_number
%type <node> step_invariant_expr aggregate_expr aggregate_modifier bin_modifier binary_expr bool_modifier expr function_call function_call_args function_call_body group_modifiers label_matchers matrix_selector number_literal offset_expr on_or_ignoring paren_expr string_literal subquery_expr unary_expr vector_selector %type <node> step_invariant_expr aggregate_expr aggregate_modifier bin_modifier binary_expr bool_modifier expr function_call function_call_args function_call_body group_modifiers label_matchers matrix_selector number_duration_literal offset_expr on_or_ignoring paren_expr string_literal subquery_expr unary_expr vector_selector
%type <duration> duration maybe_duration
%start start %start start
@ -218,7 +216,7 @@ expr :
| binary_expr | binary_expr
| function_call | function_call
| matrix_selector | matrix_selector
| number_literal | number_duration_literal
| offset_expr | offset_expr
| paren_expr | paren_expr
| string_literal | string_literal
@ -415,18 +413,22 @@ paren_expr : LEFT_PAREN expr RIGHT_PAREN
* Offset modifiers. * Offset modifiers.
*/ */
offset_expr: expr OFFSET duration offset_expr: expr OFFSET number_duration_literal
{ {
yylex.(*parser).addOffset($1, $3) numLit, _ := $3.(*NumberLiteral)
$$ = $1 dur := time.Duration(numLit.Val * 1000) * time.Millisecond
yylex.(*parser).addOffset($1, dur)
$$ = $1
} }
| expr OFFSET SUB duration | expr OFFSET SUB number_duration_literal
{ {
yylex.(*parser).addOffset($1, -$4) numLit, _ := $4.(*NumberLiteral)
$$ = $1 dur := time.Duration(numLit.Val * 1000) * time.Millisecond
yylex.(*parser).addOffset($1, -dur)
$$ = $1
} }
| expr OFFSET error | expr OFFSET error
{ yylex.(*parser).unexpected("offset", "duration"); $$ = $1 } { yylex.(*parser).unexpected("offset", "integer or duration"); $$ = $1 }
; ;
/* /*
* @ modifiers. * @ modifiers.
@ -452,7 +454,7 @@ at_modifier_preprocessors: START | END;
* Subquery and range selectors. * Subquery and range selectors.
*/ */
matrix_selector : expr LEFT_BRACKET duration RIGHT_BRACKET matrix_selector : expr LEFT_BRACKET number_duration_literal RIGHT_BRACKET
{ {
var errMsg string var errMsg string
vs, ok := $1.(*VectorSelector) vs, ok := $1.(*VectorSelector)
@ -469,32 +471,44 @@ matrix_selector : expr LEFT_BRACKET duration RIGHT_BRACKET
yylex.(*parser).addParseErrf(errRange, errMsg) yylex.(*parser).addParseErrf(errRange, errMsg)
} }
numLit, _ := $3.(*NumberLiteral)
$$ = &MatrixSelector{ $$ = &MatrixSelector{
VectorSelector: $1.(Expr), VectorSelector: $1.(Expr),
Range: $3, Range: time.Duration(numLit.Val * 1000) * time.Millisecond,
EndPos: yylex.(*parser).lastClosing, EndPos: yylex.(*parser).lastClosing,
} }
} }
; ;
subquery_expr : expr LEFT_BRACKET duration COLON maybe_duration RIGHT_BRACKET subquery_expr : expr LEFT_BRACKET number_duration_literal COLON number_duration_literal RIGHT_BRACKET
{ {
numLitRange, _ := $3.(*NumberLiteral)
numLitStep, _ := $5.(*NumberLiteral)
$$ = &SubqueryExpr{ $$ = &SubqueryExpr{
Expr: $1.(Expr), Expr: $1.(Expr),
Range: $3, Range: time.Duration(numLitRange.Val * 1000) * time.Millisecond,
Step: $5, Step: time.Duration(numLitStep.Val * 1000) * time.Millisecond,
EndPos: $6.Pos + 1, EndPos: $6.Pos + 1,
} }
} }
| expr LEFT_BRACKET duration COLON duration error | expr LEFT_BRACKET number_duration_literal COLON RIGHT_BRACKET
{
numLitRange, _ := $3.(*NumberLiteral)
$$ = &SubqueryExpr{
Expr: $1.(Expr),
Range: time.Duration(numLitRange.Val * 1000) * time.Millisecond,
Step: 0,
EndPos: $5.Pos + 1,
}
}
| expr LEFT_BRACKET number_duration_literal COLON number_duration_literal error
{ yylex.(*parser).unexpected("subquery selector", "\"]\""); $$ = $1 } { yylex.(*parser).unexpected("subquery selector", "\"]\""); $$ = $1 }
| expr LEFT_BRACKET duration COLON error | expr LEFT_BRACKET number_duration_literal COLON error
{ yylex.(*parser).unexpected("subquery selector", "duration or \"]\""); $$ = $1 } { yylex.(*parser).unexpected("subquery selector", "number/duration or \"]\""); $$ = $1 }
| expr LEFT_BRACKET duration error | expr LEFT_BRACKET number_duration_literal error
{ yylex.(*parser).unexpected("subquery or range", "\":\" or \"]\""); $$ = $1 } { yylex.(*parser).unexpected("subquery or range", "\":\" or \"]\""); $$ = $1 }
| expr LEFT_BRACKET error | expr LEFT_BRACKET error
{ yylex.(*parser).unexpected("subquery selector", "duration"); $$ = $1 } { yylex.(*parser).unexpected("subquery selector", "number/duration"); $$ = $1 }
; ;
/* /*
@ -866,12 +880,25 @@ match_op : EQL | NEQ | EQL_REGEX | NEQ_REGEX ;
* Literals. * Literals.
*/ */
number_literal : NUMBER number_duration_literal : NUMBER
{ {
$$ = &NumberLiteral{ $$ = &NumberLiteral{
Val: yylex.(*parser).number($1.Val), Val: yylex.(*parser).number($1.Val),
PosRange: $1.PositionRange(), PosRange: $1.PositionRange(),
}
} }
| DURATION
{
var err error
var dur time.Duration
dur, err = parseDuration($1.Val)
if err != nil {
yylex.(*parser).addParseErr($1.PositionRange(), err)
}
$$ = &NumberLiteral{
Val: dur.Seconds(),
PosRange: $1.PositionRange(),
}
} }
; ;
@ -897,21 +924,6 @@ int : SUB uint { $$ = -int64($2) }
| uint { $$ = int64($1) } | uint { $$ = int64($1) }
; ;
duration : DURATION
{
var err error
$$, err = parseDuration($1.Val)
if err != nil {
yylex.(*parser).addParseErr($1.PositionRange(), err)
}
}
| number
{
$$ = yylex.(*parser).parseNumberLiteral($1);
}
;
string_literal : STRING string_literal : STRING
{ {
$$ = &StringLiteral{ $$ = &StringLiteral{
@ -935,11 +947,6 @@ string_identifier : STRING
* Wrappers for optional arguments. * Wrappers for optional arguments.
*/ */
maybe_duration : /* empty */
{$$ = 0}
| duration
;
maybe_grouping_labels: /* empty */ { $$ = nil } maybe_grouping_labels: /* empty */ { $$ = nil }
| grouping_labels | grouping_labels
; ;

File diff suppressed because it is too large Load Diff

View File

@ -849,10 +849,6 @@ func parseDuration(ds string) (time.Duration, error) {
return time.Duration(dur), nil return time.Duration(dur), nil
} }
func (p *parser) parseNumberLiteral(ts float64) time.Duration {
return time.Duration(ts * float64(time.Second))
}
// parseGenerated invokes the yacc generated parser. // parseGenerated invokes the yacc generated parser.
// The generated parser gets the provided startSymbol injected into // The generated parser gets the provided startSymbol injected into
// the lexer stream, based on which grammar will be used. // the lexer stream, based on which grammar will be used.

View File

@ -2152,6 +2152,96 @@ var testExpr = []struct {
EndPos: 27, EndPos: 27,
}, },
}, },
{
input: `foo[3ms] @ 2.345`,
expected: &MatrixSelector{
VectorSelector: &VectorSelector{
Name: "foo",
Timestamp: makeInt64Pointer(2345),
LabelMatchers: []*labels.Matcher{
MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "foo"),
},
PosRange: posrange.PositionRange{
Start: 0,
End: 3,
},
},
Range: 3 * time.Millisecond,
EndPos: 16,
},
},
{
input: `foo[4s180ms] @ 2.345`,
expected: &MatrixSelector{
VectorSelector: &VectorSelector{
Name: "foo",
Timestamp: makeInt64Pointer(2345),
LabelMatchers: []*labels.Matcher{
MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "foo"),
},
PosRange: posrange.PositionRange{
Start: 0,
End: 3,
},
},
Range: 4*time.Second + 180*time.Millisecond,
EndPos: 20,
},
},
{
input: `foo[4.18] @ 2.345`,
expected: &MatrixSelector{
VectorSelector: &VectorSelector{
Name: "foo",
Timestamp: makeInt64Pointer(2345),
LabelMatchers: []*labels.Matcher{
MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "foo"),
},
PosRange: posrange.PositionRange{
Start: 0,
End: 3,
},
},
Range: 4*time.Second + 180*time.Millisecond,
EndPos: 17,
},
},
{
input: `foo[4s18ms] @ 2.345`,
expected: &MatrixSelector{
VectorSelector: &VectorSelector{
Name: "foo",
Timestamp: makeInt64Pointer(2345),
LabelMatchers: []*labels.Matcher{
MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "foo"),
},
PosRange: posrange.PositionRange{
Start: 0,
End: 3,
},
},
Range: 4*time.Second + 18*time.Millisecond,
EndPos: 19,
},
},
{
input: `foo[4.018] @ 2.345`,
expected: &MatrixSelector{
VectorSelector: &VectorSelector{
Name: "foo",
Timestamp: makeInt64Pointer(2345),
LabelMatchers: []*labels.Matcher{
MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "foo"),
},
PosRange: posrange.PositionRange{
Start: 0,
End: 3,
},
},
Range: 4*time.Second + 18*time.Millisecond,
EndPos: 18,
},
},
{ {
input: `test{a="b"}[5y] @ 1603774699`, input: `test{a="b"}[5y] @ 1603774699`,
expected: &MatrixSelector{ expected: &MatrixSelector{
@ -2245,7 +2335,7 @@ var testExpr = []struct {
{ {
input: `some_metric[5m] OFFSET`, input: `some_metric[5m] OFFSET`,
fail: true, fail: true,
errMsg: "unexpected end of input in offset, expected duration", errMsg: "unexpected end of input in offset, expected integer or duration",
}, },
{ {
input: `some_metric OFFSET 1m[5m]`, input: `some_metric OFFSET 1m[5m]`,

View File

@ -244,10 +244,16 @@ eval instant at 50m deriv(testcounter_reset_middle[100m])
eval instant at 50m predict_linear(testcounter_reset_middle[50m], 3600) eval instant at 50m predict_linear(testcounter_reset_middle[50m], 3600)
{} 76.81818181818181 {} 76.81818181818181
eval instant at 50m predict_linear(testcounter_reset_middle[50m], 1h)
{} 76.81818181818181
# intercept at t = 3000+3600 = 6600 # intercept at t = 3000+3600 = 6600
eval instant at 50m predict_linear(testcounter_reset_middle[50m] @ 3000, 3600) eval instant at 50m predict_linear(testcounter_reset_middle[50m] @ 3000, 3600)
{} 76.81818181818181 {} 76.81818181818181
eval instant at 50m predict_linear(testcounter_reset_middle[50m] @ 3000, 1h)
{} 76.81818181818181
# intercept at t = 600+3600 = 4200 # intercept at t = 600+3600 = 4200
eval instant at 10m predict_linear(testcounter_reset_middle[50m] @ 3000, 3600) eval instant at 10m predict_linear(testcounter_reset_middle[50m] @ 3000, 3600)
{} 51.36363636363637 {} 51.36363636363637