2019-12-05 16:16:12 +00:00
|
|
|
// Copyright 2019 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
%{
|
2020-02-03 16:23:44 +00:00
|
|
|
package parser
|
2019-12-05 16:16:12 +00:00
|
|
|
|
2019-12-21 08:44:12 +00:00
|
|
|
import (
|
|
|
|
"math"
|
2019-12-16 16:38:18 +00:00
|
|
|
"sort"
|
2019-12-21 08:44:12 +00:00
|
|
|
"strconv"
|
2020-01-08 11:04:47 +00:00
|
|
|
"time"
|
2019-12-16 16:38:18 +00:00
|
|
|
|
2019-12-05 16:16:12 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2019-12-21 08:44:12 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/value"
|
|
|
|
)
|
2019-12-05 16:16:12 +00:00
|
|
|
%}
|
|
|
|
|
|
|
|
%union {
|
2019-12-16 13:58:47 +00:00
|
|
|
node Node
|
|
|
|
item Item
|
|
|
|
matchers []*labels.Matcher
|
|
|
|
matcher *labels.Matcher
|
|
|
|
label labels.Label
|
|
|
|
labels labels.Labels
|
2019-12-18 14:18:52 +00:00
|
|
|
strings []string
|
2020-02-03 17:48:27 +00:00
|
|
|
series []SequenceValue
|
2019-12-21 08:44:12 +00:00
|
|
|
uint uint64
|
|
|
|
float float64
|
2020-01-08 11:04:47 +00:00
|
|
|
duration time.Duration
|
2019-12-05 16:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-17 15:16:58 +00:00
|
|
|
%token <item>
|
2020-09-09 10:10:02 +00:00
|
|
|
EQL
|
2020-01-08 11:04:47 +00:00
|
|
|
BLANK
|
|
|
|
COLON
|
|
|
|
COMMA
|
|
|
|
COMMENT
|
|
|
|
DURATION
|
|
|
|
EOF
|
|
|
|
ERROR
|
|
|
|
IDENTIFIER
|
|
|
|
LEFT_BRACE
|
|
|
|
LEFT_BRACKET
|
|
|
|
LEFT_PAREN
|
|
|
|
METRIC_IDENTIFIER
|
|
|
|
NUMBER
|
|
|
|
RIGHT_BRACE
|
|
|
|
RIGHT_BRACKET
|
|
|
|
RIGHT_PAREN
|
|
|
|
SEMICOLON
|
|
|
|
SPACE
|
|
|
|
STRING
|
|
|
|
TIMES
|
2019-12-05 16:16:12 +00:00
|
|
|
|
|
|
|
// Operators.
|
2020-01-08 11:04:47 +00:00
|
|
|
%token operatorsStart
|
|
|
|
%token <item>
|
|
|
|
ADD
|
|
|
|
DIV
|
2020-09-09 10:10:02 +00:00
|
|
|
EQLC
|
2020-01-08 11:04:47 +00:00
|
|
|
EQL_REGEX
|
|
|
|
GTE
|
|
|
|
GTR
|
|
|
|
LAND
|
|
|
|
LOR
|
|
|
|
LSS
|
|
|
|
LTE
|
|
|
|
LUNLESS
|
|
|
|
MOD
|
|
|
|
MUL
|
|
|
|
NEQ
|
|
|
|
NEQ_REGEX
|
|
|
|
POW
|
|
|
|
SUB
|
2021-01-20 10:57:39 +00:00
|
|
|
AT
|
2019-12-05 16:16:12 +00:00
|
|
|
%token operatorsEnd
|
|
|
|
|
|
|
|
// Aggregators.
|
2020-01-08 11:04:47 +00:00
|
|
|
%token aggregatorsStart
|
|
|
|
%token <item>
|
|
|
|
AVG
|
|
|
|
BOTTOMK
|
|
|
|
COUNT
|
|
|
|
COUNT_VALUES
|
2020-06-30 14:51:18 +00:00
|
|
|
GROUP
|
2020-01-08 11:04:47 +00:00
|
|
|
MAX
|
|
|
|
MIN
|
|
|
|
QUANTILE
|
|
|
|
STDDEV
|
|
|
|
STDVAR
|
|
|
|
SUM
|
|
|
|
TOPK
|
2019-12-05 16:16:12 +00:00
|
|
|
%token aggregatorsEnd
|
|
|
|
|
|
|
|
// Keywords.
|
2020-01-08 11:04:47 +00:00
|
|
|
%token keywordsStart
|
|
|
|
%token <item>
|
|
|
|
BOOL
|
|
|
|
BY
|
|
|
|
GROUP_LEFT
|
|
|
|
GROUP_RIGHT
|
|
|
|
IGNORING
|
|
|
|
OFFSET
|
|
|
|
ON
|
|
|
|
WITHOUT
|
2019-12-05 16:16:12 +00:00
|
|
|
%token keywordsEnd
|
|
|
|
|
2021-02-09 16:03:16 +00:00
|
|
|
// Preprocessors.
|
|
|
|
%token preprocessorStart
|
|
|
|
%token <item>
|
|
|
|
START
|
|
|
|
END
|
|
|
|
%token preprocessorEnd
|
|
|
|
|
2019-12-05 16:16:12 +00:00
|
|
|
|
|
|
|
// Start symbols for the generated parser.
|
2020-01-08 11:04:47 +00:00
|
|
|
%token startSymbolsStart
|
|
|
|
%token
|
|
|
|
START_METRIC
|
|
|
|
START_SERIES_DESCRIPTION
|
|
|
|
START_EXPRESSION
|
|
|
|
START_METRIC_SELECTOR
|
2019-12-05 16:16:12 +00:00
|
|
|
%token startSymbolsEnd
|
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
|
|
|
|
// Type definitions for grammar rules.
|
2020-01-14 16:12:15 +00:00
|
|
|
%type <matchers> label_match_list
|
2019-12-05 16:16:12 +00:00
|
|
|
%type <matcher> label_matcher
|
|
|
|
|
2021-02-09 16:03:16 +00:00
|
|
|
%type <item> aggregate_op grouping_label match_op maybe_label metric_identifier unary_op at_modifier_preprocessors
|
2019-12-05 16:16:12 +00:00
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
%type <labels> label_set label_set_list metric
|
|
|
|
%type <label> label_set_item
|
|
|
|
%type <strings> grouping_label_list grouping_labels maybe_grouping_labels
|
|
|
|
%type <series> series_item series_values
|
2019-12-21 08:44:12 +00:00
|
|
|
%type <uint> uint
|
2021-01-20 10:57:39 +00:00
|
|
|
%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
|
2020-01-08 11:04:47 +00:00
|
|
|
%type <duration> duration maybe_duration
|
2019-12-16 13:58:47 +00:00
|
|
|
|
2019-12-05 16:16:12 +00:00
|
|
|
%start start
|
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
// Operators are listed with increasing precedence.
|
|
|
|
%left LOR
|
|
|
|
%left LAND LUNLESS
|
2020-09-09 10:10:02 +00:00
|
|
|
%left EQLC GTE GTR LSS LTE NEQ
|
2020-01-08 11:04:47 +00:00
|
|
|
%left ADD SUB
|
|
|
|
%left MUL DIV MOD
|
|
|
|
%right POW
|
|
|
|
|
|
|
|
// Offset modifiers do not have associativity.
|
|
|
|
%nonassoc OFFSET
|
|
|
|
|
|
|
|
// This ensures that it is always attempted to parse range or subquery selectors when a left
|
|
|
|
// bracket is encountered.
|
|
|
|
%right LEFT_BRACKET
|
|
|
|
|
2019-12-05 16:16:12 +00:00
|
|
|
%%
|
|
|
|
|
2020-01-17 15:16:58 +00:00
|
|
|
start :
|
2020-01-08 11:04:47 +00:00
|
|
|
START_METRIC metric
|
|
|
|
{ yylex.(*parser).generatedParserResult = $2 }
|
2019-12-21 08:44:12 +00:00
|
|
|
| START_SERIES_DESCRIPTION series_description
|
2020-01-08 11:04:47 +00:00
|
|
|
| START_EXPRESSION /* empty */ EOF
|
2020-01-17 15:16:58 +00:00
|
|
|
{ yylex.(*parser).addParseErrf(PositionRange{}, "no expression found in input")}
|
2020-01-08 11:04:47 +00:00
|
|
|
| START_EXPRESSION expr
|
|
|
|
{ yylex.(*parser).generatedParserResult = $2 }
|
2020-01-17 15:16:58 +00:00
|
|
|
| START_METRIC_SELECTOR vector_selector
|
2020-01-08 11:04:47 +00:00
|
|
|
{ yylex.(*parser).generatedParserResult = $2 }
|
2019-12-21 08:44:12 +00:00
|
|
|
| start EOF
|
2019-12-16 13:58:47 +00:00
|
|
|
| error /* If none of the more detailed error messages are triggered, we fall back to this. */
|
2019-12-18 17:36:43 +00:00
|
|
|
{ yylex.(*parser).unexpected("","") }
|
2019-12-05 16:16:12 +00:00
|
|
|
;
|
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
expr :
|
|
|
|
aggregate_expr
|
|
|
|
| binary_expr
|
|
|
|
| function_call
|
|
|
|
| matrix_selector
|
|
|
|
| number_literal
|
|
|
|
| offset_expr
|
|
|
|
| paren_expr
|
|
|
|
| string_literal
|
|
|
|
| subquery_expr
|
|
|
|
| unary_expr
|
|
|
|
| vector_selector
|
2021-01-20 10:57:39 +00:00
|
|
|
| step_invariant_expr
|
2020-01-08 11:04:47 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Aggregations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
aggregate_expr : aggregate_op aggregate_modifier function_call_body
|
|
|
|
{ $$ = yylex.(*parser).newAggregateExpr($1, $2, $3) }
|
|
|
|
| aggregate_op function_call_body aggregate_modifier
|
|
|
|
{ $$ = yylex.(*parser).newAggregateExpr($1, $3, $2) }
|
|
|
|
| aggregate_op function_call_body
|
|
|
|
{ $$ = yylex.(*parser).newAggregateExpr($1, &AggregateExpr{}, $2) }
|
|
|
|
| aggregate_op error
|
2021-01-20 10:57:39 +00:00
|
|
|
{
|
|
|
|
yylex.(*parser).unexpected("aggregation","");
|
2020-03-05 08:03:38 +00:00
|
|
|
$$ = yylex.(*parser).newAggregateExpr($1, &AggregateExpr{}, Expressions{})
|
|
|
|
}
|
2020-01-08 11:04:47 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
aggregate_modifier:
|
|
|
|
BY grouping_labels
|
|
|
|
{
|
|
|
|
$$ = &AggregateExpr{
|
|
|
|
Grouping: $2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
| WITHOUT grouping_labels
|
|
|
|
{
|
|
|
|
$$ = &AggregateExpr{
|
|
|
|
Grouping: $2,
|
|
|
|
Without: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Binary expressions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Operator precedence only works if each of those is listed separately.
|
|
|
|
binary_expr : expr ADD bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
|
|
|
| expr DIV bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
2020-09-09 10:10:02 +00:00
|
|
|
| expr EQLC bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
2020-01-08 11:04:47 +00:00
|
|
|
| expr GTE bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
|
|
|
| expr GTR bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
|
|
|
| expr LAND bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
|
|
|
| expr LOR bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
|
|
|
| expr LSS bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
|
|
|
| expr LTE bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
|
|
|
| expr LUNLESS bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
|
|
|
| expr MOD bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
|
|
|
| expr MUL bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
|
|
|
| expr NEQ bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
|
|
|
| expr POW bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
|
|
|
| expr SUB bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) }
|
|
|
|
;
|
|
|
|
|
2020-01-17 15:16:58 +00:00
|
|
|
// Using left recursion for the modifier rules, helps to keep the parser stack small and
|
2020-01-08 11:04:47 +00:00
|
|
|
// reduces allocations
|
|
|
|
bin_modifier : group_modifiers;
|
|
|
|
|
|
|
|
bool_modifier : /* empty */
|
|
|
|
{ $$ = &BinaryExpr{
|
|
|
|
VectorMatching: &VectorMatching{Card: CardOneToOne},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
| BOOL
|
|
|
|
{ $$ = &BinaryExpr{
|
|
|
|
VectorMatching: &VectorMatching{Card: CardOneToOne},
|
|
|
|
ReturnBool: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
on_or_ignoring : bool_modifier IGNORING grouping_labels
|
|
|
|
{
|
|
|
|
$$ = $1
|
|
|
|
$$.(*BinaryExpr).VectorMatching.MatchingLabels = $3
|
2020-01-17 15:16:58 +00:00
|
|
|
}
|
2020-01-08 11:04:47 +00:00
|
|
|
| bool_modifier ON grouping_labels
|
|
|
|
{
|
|
|
|
$$ = $1
|
|
|
|
$$.(*BinaryExpr).VectorMatching.MatchingLabels = $3
|
|
|
|
$$.(*BinaryExpr).VectorMatching.On = true
|
2020-01-17 15:16:58 +00:00
|
|
|
}
|
2020-01-08 11:04:47 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
group_modifiers: bool_modifier /* empty */
|
|
|
|
| on_or_ignoring /* empty */
|
|
|
|
| on_or_ignoring GROUP_LEFT maybe_grouping_labels
|
|
|
|
{
|
|
|
|
$$ = $1
|
|
|
|
$$.(*BinaryExpr).VectorMatching.Card = CardManyToOne
|
|
|
|
$$.(*BinaryExpr).VectorMatching.Include = $3
|
|
|
|
}
|
|
|
|
| on_or_ignoring GROUP_RIGHT maybe_grouping_labels
|
|
|
|
{
|
|
|
|
$$ = $1
|
|
|
|
$$.(*BinaryExpr).VectorMatching.Card = CardOneToMany
|
|
|
|
$$.(*BinaryExpr).VectorMatching.Include = $3
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2019-12-05 16:16:12 +00:00
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
grouping_labels : LEFT_PAREN grouping_label_list RIGHT_PAREN
|
|
|
|
{ $$ = $2 }
|
|
|
|
| LEFT_PAREN grouping_label_list COMMA RIGHT_PAREN
|
|
|
|
{ $$ = $2 }
|
|
|
|
| LEFT_PAREN RIGHT_PAREN
|
|
|
|
{ $$ = []string{} }
|
|
|
|
| error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("grouping opts", "\"(\""); $$ = nil }
|
2020-01-08 11:04:47 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
grouping_label_list:
|
|
|
|
grouping_label_list COMMA grouping_label
|
|
|
|
{ $$ = append($1, $3.Val) }
|
|
|
|
| grouping_label
|
|
|
|
{ $$ = []string{$1.Val} }
|
|
|
|
| grouping_label_list error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("grouping opts", "\",\" or \")\""); $$ = $1 }
|
2020-01-08 11:04:47 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
grouping_label : maybe_label
|
|
|
|
{
|
|
|
|
if !isLabel($1.Val) {
|
|
|
|
yylex.(*parser).unexpected("grouping opts", "label")
|
|
|
|
}
|
|
|
|
$$ = $1
|
|
|
|
}
|
|
|
|
| error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("grouping opts", "label"); $$ = Item{} }
|
2020-01-08 11:04:47 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Function calls.
|
|
|
|
*/
|
2020-01-17 15:16:58 +00:00
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
function_call : IDENTIFIER function_call_body
|
|
|
|
{
|
|
|
|
fn, exist := getFunction($1.Val)
|
|
|
|
if !exist{
|
2020-01-17 15:16:58 +00:00
|
|
|
yylex.(*parser).addParseErrf($1.PositionRange(),"unknown function with name %q", $1.Val)
|
|
|
|
}
|
2020-01-08 11:04:47 +00:00
|
|
|
$$ = &Call{
|
|
|
|
Func: fn,
|
|
|
|
Args: $2.(Expressions),
|
2020-01-14 16:12:15 +00:00
|
|
|
PosRange: PositionRange{
|
|
|
|
Start: $1.Pos,
|
|
|
|
End: yylex.(*parser).lastClosing,
|
|
|
|
},
|
2020-01-08 11:04:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
function_call_body: LEFT_PAREN function_call_args RIGHT_PAREN
|
|
|
|
{ $$ = $2 }
|
|
|
|
| LEFT_PAREN RIGHT_PAREN
|
|
|
|
{$$ = Expressions{}}
|
|
|
|
;
|
|
|
|
|
|
|
|
function_call_args: function_call_args COMMA expr
|
|
|
|
{ $$ = append($1.(Expressions), $3.(Expr)) }
|
2020-01-17 15:16:58 +00:00
|
|
|
| expr
|
2020-01-08 11:04:47 +00:00
|
|
|
{ $$ = Expressions{$1.(Expr)} }
|
2020-01-16 15:20:20 +00:00
|
|
|
| function_call_args COMMA
|
|
|
|
{
|
2020-01-17 15:16:58 +00:00
|
|
|
yylex.(*parser).addParseErrf($2.PositionRange(), "trailing commas not allowed in function call args")
|
2020-01-16 15:20:20 +00:00
|
|
|
$$ = $1
|
|
|
|
}
|
2020-01-08 11:04:47 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expressions inside parentheses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
paren_expr : LEFT_PAREN expr RIGHT_PAREN
|
2020-01-14 16:12:15 +00:00
|
|
|
{ $$ = &ParenExpr{Expr: $2.(Expr), PosRange: mergeRanges(&$1, &$3)} }
|
2020-01-08 11:04:47 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Offset modifiers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
offset_expr: expr OFFSET duration
|
|
|
|
{
|
|
|
|
yylex.(*parser).addOffset($1, $3)
|
|
|
|
$$ = $1
|
|
|
|
}
|
|
|
|
| expr OFFSET error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("offset", "duration"); $$ = $1 }
|
2020-01-08 11:04:47 +00:00
|
|
|
;
|
2021-01-20 10:57:39 +00:00
|
|
|
/*
|
|
|
|
* @ modifiers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
step_invariant_expr: expr AT signed_or_unsigned_number
|
|
|
|
{
|
|
|
|
yylex.(*parser).setTimestamp($1, $3)
|
|
|
|
$$ = $1
|
|
|
|
}
|
2021-02-09 16:03:16 +00:00
|
|
|
| expr AT at_modifier_preprocessors LEFT_PAREN RIGHT_PAREN
|
|
|
|
{
|
|
|
|
yylex.(*parser).setAtModifierPreprocessor($1, $3)
|
|
|
|
$$ = $1
|
|
|
|
}
|
2021-01-20 10:57:39 +00:00
|
|
|
| expr AT error
|
|
|
|
{ yylex.(*parser).unexpected("@", "timestamp"); $$ = $1 }
|
|
|
|
;
|
2020-01-08 11:04:47 +00:00
|
|
|
|
2021-02-09 16:03:16 +00:00
|
|
|
at_modifier_preprocessors: START | END;
|
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
/*
|
|
|
|
* Subquery and range selectors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
matrix_selector : expr LEFT_BRACKET duration RIGHT_BRACKET
|
|
|
|
{
|
2020-01-15 20:01:49 +00:00
|
|
|
var errMsg string
|
2020-01-08 11:04:47 +00:00
|
|
|
vs, ok := $1.(*VectorSelector)
|
|
|
|
if !ok{
|
2020-01-15 20:01:49 +00:00
|
|
|
errMsg = "ranges only allowed for vector selectors"
|
2021-01-20 10:57:39 +00:00
|
|
|
} else if vs.OriginalOffset != 0{
|
2020-01-15 20:01:49 +00:00
|
|
|
errMsg = "no offset modifiers allowed before range"
|
2021-01-20 10:57:39 +00:00
|
|
|
} else if vs.Timestamp != nil {
|
|
|
|
errMsg = "no @ modifiers allowed before range"
|
2020-01-08 11:04:47 +00:00
|
|
|
}
|
2020-01-15 20:01:49 +00:00
|
|
|
|
|
|
|
if errMsg != ""{
|
|
|
|
errRange := mergeRanges(&$2, &$4)
|
2020-01-17 15:16:58 +00:00
|
|
|
yylex.(*parser).addParseErrf(errRange, errMsg)
|
2020-01-08 11:04:47 +00:00
|
|
|
}
|
2020-01-15 20:01:49 +00:00
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
$$ = &MatrixSelector{
|
2020-01-17 15:16:58 +00:00
|
|
|
VectorSelector: $1.(Expr),
|
2020-01-08 11:04:47 +00:00
|
|
|
Range: $3,
|
2020-01-14 16:12:15 +00:00
|
|
|
EndPos: yylex.(*parser).lastClosing,
|
2020-01-08 11:04:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
subquery_expr : expr LEFT_BRACKET duration COLON maybe_duration RIGHT_BRACKET
|
|
|
|
{
|
|
|
|
$$ = &SubqueryExpr{
|
|
|
|
Expr: $1.(Expr),
|
|
|
|
Range: $3,
|
|
|
|
Step: $5,
|
2020-01-17 15:16:58 +00:00
|
|
|
|
2020-01-14 16:12:15 +00:00
|
|
|
EndPos: $6.Pos + 1,
|
2020-01-08 11:04:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| expr LEFT_BRACKET duration COLON duration error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("subquery selector", "\"]\""); $$ = $1 }
|
2020-01-08 11:04:47 +00:00
|
|
|
| expr LEFT_BRACKET duration COLON error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("subquery selector", "duration or \"]\""); $$ = $1 }
|
2020-01-08 11:04:47 +00:00
|
|
|
| expr LEFT_BRACKET duration error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("subquery or range", "\":\" or \"]\""); $$ = $1 }
|
2020-01-08 11:04:47 +00:00
|
|
|
| expr LEFT_BRACKET error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("subquery selector", "duration"); $$ = $1 }
|
2020-01-08 11:04:47 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unary expressions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
unary_expr :
|
2020-01-08 14:07:10 +00:00
|
|
|
/* gives the rule the same precedence as MUL. This aligns with mathematical conventions */
|
2020-01-17 15:16:58 +00:00
|
|
|
unary_op expr %prec MUL
|
2020-01-08 11:04:47 +00:00
|
|
|
{
|
|
|
|
if nl, ok := $2.(*NumberLiteral); ok {
|
|
|
|
if $1.Typ == SUB {
|
|
|
|
nl.Val *= -1
|
|
|
|
}
|
2020-01-14 16:12:15 +00:00
|
|
|
nl.PosRange.Start = $1.Pos
|
2020-01-08 11:04:47 +00:00
|
|
|
$$ = nl
|
|
|
|
} else {
|
2020-01-14 16:12:15 +00:00
|
|
|
$$ = &UnaryExpr{Op: $1.Typ, Expr: $2.(Expr), StartPos: $1.Pos}
|
2020-01-08 11:04:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Vector selectors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
vector_selector: metric_identifier label_matchers
|
2020-01-17 15:16:58 +00:00
|
|
|
{
|
2020-01-14 16:12:15 +00:00
|
|
|
vs := $2.(*VectorSelector)
|
2020-01-17 15:16:58 +00:00
|
|
|
vs.PosRange = mergeRanges(&$1, vs)
|
2020-01-14 16:12:15 +00:00
|
|
|
vs.Name = $1.Val
|
|
|
|
yylex.(*parser).assembleVectorSelector(vs)
|
|
|
|
$$ = vs
|
|
|
|
}
|
2020-01-17 15:16:58 +00:00
|
|
|
| metric_identifier
|
|
|
|
{
|
2020-01-14 16:12:15 +00:00
|
|
|
vs := &VectorSelector{
|
|
|
|
Name: $1.Val,
|
|
|
|
LabelMatchers: []*labels.Matcher{},
|
|
|
|
PosRange: $1.PositionRange(),
|
|
|
|
}
|
|
|
|
yylex.(*parser).assembleVectorSelector(vs)
|
|
|
|
$$ = vs
|
|
|
|
}
|
2020-01-08 11:04:47 +00:00
|
|
|
| label_matchers
|
2020-01-17 15:16:58 +00:00
|
|
|
{
|
2020-01-14 16:12:15 +00:00
|
|
|
vs := $1.(*VectorSelector)
|
|
|
|
yylex.(*parser).assembleVectorSelector(vs)
|
|
|
|
$$ = vs
|
|
|
|
}
|
2020-01-08 11:04:47 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
label_matchers : LEFT_BRACE label_match_list RIGHT_BRACE
|
2020-01-14 16:12:15 +00:00
|
|
|
{
|
|
|
|
$$ = &VectorSelector{
|
|
|
|
LabelMatchers: $2,
|
|
|
|
PosRange: mergeRanges(&$1, &$3),
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 13:58:47 +00:00
|
|
|
| LEFT_BRACE label_match_list COMMA RIGHT_BRACE
|
2020-01-14 16:12:15 +00:00
|
|
|
{
|
|
|
|
$$ = &VectorSelector{
|
|
|
|
LabelMatchers: $2,
|
|
|
|
PosRange: mergeRanges(&$1, &$4),
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 16:16:12 +00:00
|
|
|
| LEFT_BRACE RIGHT_BRACE
|
2020-01-14 16:12:15 +00:00
|
|
|
{
|
|
|
|
$$ = &VectorSelector{
|
|
|
|
LabelMatchers: []*labels.Matcher{},
|
|
|
|
PosRange: mergeRanges(&$1, &$2),
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 16:16:12 +00:00
|
|
|
;
|
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
label_match_list: label_match_list COMMA label_matcher
|
2020-01-17 15:16:58 +00:00
|
|
|
{
|
2020-01-16 15:20:20 +00:00
|
|
|
if $1 != nil{
|
|
|
|
$$ = append($1, $3)
|
|
|
|
} else {
|
|
|
|
$$ = $1
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 13:58:47 +00:00
|
|
|
| label_matcher
|
|
|
|
{ $$ = []*labels.Matcher{$1}}
|
|
|
|
| label_match_list error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("label matching", "\",\" or \"}\""); $$ = $1 }
|
2019-12-16 13:58:47 +00:00
|
|
|
;
|
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
label_matcher : IDENTIFIER match_op STRING
|
2020-01-16 15:20:20 +00:00
|
|
|
{ $$ = yylex.(*parser).newLabelMatcher($1, $2, $3); }
|
2019-12-05 16:16:12 +00:00
|
|
|
| IDENTIFIER match_op error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("label matching", "string"); $$ = nil}
|
2020-01-17 15:16:58 +00:00
|
|
|
| IDENTIFIER error
|
|
|
|
{ yylex.(*parser).unexpected("label matching", "label matching operator"); $$ = nil }
|
2019-12-16 13:58:47 +00:00
|
|
|
| error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("label matching", "identifier or \"}\""); $$ = nil}
|
2019-12-05 16:16:12 +00:00
|
|
|
;
|
2020-01-17 15:16:58 +00:00
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
/*
|
|
|
|
* Metric descriptions.
|
|
|
|
*/
|
2019-12-05 16:16:12 +00:00
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
metric : metric_identifier label_set
|
2019-12-16 16:38:18 +00:00
|
|
|
{ $$ = append($2, labels.Label{Name: labels.MetricName, Value: $1.Val}); sort.Sort($$) }
|
2020-01-17 15:16:58 +00:00
|
|
|
| label_set
|
2019-12-16 16:38:18 +00:00
|
|
|
{$$ = $1}
|
|
|
|
;
|
|
|
|
|
|
|
|
|
2020-07-08 10:58:12 +00:00
|
|
|
metric_identifier: AVG | BOTTOMK | BY | COUNT | COUNT_VALUES | GROUP | IDENTIFIER | LAND | LOR | LUNLESS | MAX | METRIC_IDENTIFIER | MIN | OFFSET | QUANTILE | STDDEV | STDVAR | SUM | TOPK | WITHOUT;
|
2020-01-08 11:04:47 +00:00
|
|
|
|
|
|
|
label_set : LEFT_BRACE label_set_list RIGHT_BRACE
|
2019-12-16 13:58:47 +00:00
|
|
|
{ $$ = labels.New($2...) }
|
|
|
|
| LEFT_BRACE label_set_list COMMA RIGHT_BRACE
|
|
|
|
{ $$ = labels.New($2...) }
|
|
|
|
| LEFT_BRACE RIGHT_BRACE
|
|
|
|
{ $$ = labels.New() }
|
2019-12-16 16:38:18 +00:00
|
|
|
| /* empty */
|
|
|
|
{ $$ = labels.New() }
|
2019-12-16 13:58:47 +00:00
|
|
|
;
|
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
label_set_list : label_set_list COMMA label_set_item
|
2019-12-16 13:58:47 +00:00
|
|
|
{ $$ = append($1, $3) }
|
|
|
|
| label_set_item
|
|
|
|
{ $$ = []labels.Label{$1} }
|
|
|
|
| label_set_list error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("label set", "\",\" or \"}\"", ); $$ = $1 }
|
2020-01-17 15:16:58 +00:00
|
|
|
|
2019-12-16 13:58:47 +00:00
|
|
|
;
|
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
label_set_item : IDENTIFIER EQL STRING
|
2020-01-17 15:16:58 +00:00
|
|
|
{ $$ = labels.Label{Name: $1.Val, Value: yylex.(*parser).unquoteString($3.Val) } }
|
2019-12-16 13:58:47 +00:00
|
|
|
| IDENTIFIER EQL error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("label set", "string"); $$ = labels.Label{}}
|
2019-12-16 13:58:47 +00:00
|
|
|
| IDENTIFIER error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("label set", "\"=\""); $$ = labels.Label{}}
|
2019-12-16 13:58:47 +00:00
|
|
|
| error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("label set", "identifier or \"}\""); $$ = labels.Label{} }
|
2019-12-16 13:58:47 +00:00
|
|
|
;
|
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
/*
|
|
|
|
* Series descriptions (only used by unit tests).
|
|
|
|
*/
|
2019-12-18 14:18:52 +00:00
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
series_description: metric series_values
|
2019-12-21 08:44:12 +00:00
|
|
|
{
|
|
|
|
yylex.(*parser).generatedParserResult = &seriesDescription{
|
|
|
|
labels: $1,
|
|
|
|
values: $2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
series_values : /*empty*/
|
2020-02-03 17:48:27 +00:00
|
|
|
{ $$ = []SequenceValue{} }
|
2019-12-21 08:44:12 +00:00
|
|
|
| series_values SPACE series_item
|
|
|
|
{ $$ = append($1, $3...) }
|
|
|
|
| series_values SPACE
|
|
|
|
{ $$ = $1 }
|
|
|
|
| error
|
2020-01-16 15:20:20 +00:00
|
|
|
{ yylex.(*parser).unexpected("series values", ""); $$ = nil }
|
2019-12-21 08:44:12 +00:00
|
|
|
;
|
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
series_item : BLANK
|
2020-02-03 17:48:27 +00:00
|
|
|
{ $$ = []SequenceValue{{Omitted: true}}}
|
2019-12-21 08:44:12 +00:00
|
|
|
| BLANK TIMES uint
|
|
|
|
{
|
2020-02-03 17:48:27 +00:00
|
|
|
$$ = []SequenceValue{}
|
2019-12-21 08:44:12 +00:00
|
|
|
for i:=uint64(0); i < $3; i++{
|
2020-02-03 17:48:27 +00:00
|
|
|
$$ = append($$, SequenceValue{Omitted: true})
|
2019-12-21 08:44:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| series_value
|
2020-02-03 17:48:27 +00:00
|
|
|
{ $$ = []SequenceValue{{Value: $1}}}
|
2019-12-21 08:44:12 +00:00
|
|
|
| series_value TIMES uint
|
|
|
|
{
|
2020-02-03 17:48:27 +00:00
|
|
|
$$ = []SequenceValue{}
|
2019-12-21 08:44:12 +00:00
|
|
|
for i:=uint64(0); i <= $3; i++{
|
2020-02-03 17:48:27 +00:00
|
|
|
$$ = append($$, SequenceValue{Value: $1})
|
2019-12-21 08:44:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| series_value signed_number TIMES uint
|
|
|
|
{
|
2020-02-03 17:48:27 +00:00
|
|
|
$$ = []SequenceValue{}
|
2019-12-21 08:44:12 +00:00
|
|
|
for i:=uint64(0); i <= $4; i++{
|
2020-02-03 17:48:27 +00:00
|
|
|
$$ = append($$, SequenceValue{Value: $1})
|
2019-12-21 08:44:12 +00:00
|
|
|
$1 += $2
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 11:04:47 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
series_value : IDENTIFIER
|
|
|
|
{
|
|
|
|
if $1.Val != "stale" {
|
|
|
|
yylex.(*parser).unexpected("series values", "number or \"stale\"")
|
|
|
|
}
|
|
|
|
$$ = math.Float64frombits(value.StaleNaN)
|
|
|
|
}
|
|
|
|
| number
|
|
|
|
| signed_number
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Keyword lists.
|
|
|
|
*/
|
|
|
|
|
2020-06-30 14:51:18 +00:00
|
|
|
aggregate_op : AVG | BOTTOMK | COUNT | COUNT_VALUES | GROUP | MAX | MIN | QUANTILE | STDDEV | STDVAR | SUM | TOPK ;
|
2020-01-08 11:04:47 +00:00
|
|
|
|
|
|
|
// inside of grouping options label names can be recognized as keywords by the lexer. This is a list of keywords that could also be a label name.
|
2020-06-30 14:51:18 +00:00
|
|
|
maybe_label : AVG | BOOL | BOTTOMK | BY | COUNT | COUNT_VALUES | GROUP | GROUP_LEFT | GROUP_RIGHT | IDENTIFIER | IGNORING | LAND | LOR | LUNLESS | MAX | METRIC_IDENTIFIER | MIN | OFFSET | ON | QUANTILE | STDDEV | STDVAR | SUM | TOPK;
|
2020-01-08 11:04:47 +00:00
|
|
|
|
|
|
|
unary_op : ADD | SUB;
|
|
|
|
|
|
|
|
match_op : EQL | NEQ | EQL_REGEX | NEQ_REGEX ;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Literals.
|
|
|
|
*/
|
|
|
|
|
2020-01-17 15:16:58 +00:00
|
|
|
number_literal : NUMBER
|
2020-01-14 16:12:15 +00:00
|
|
|
{
|
|
|
|
$$ = &NumberLiteral{
|
|
|
|
Val: yylex.(*parser).number($1.Val),
|
|
|
|
PosRange: $1.PositionRange(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
2020-01-08 11:04:47 +00:00
|
|
|
|
|
|
|
number : NUMBER { $$ = yylex.(*parser).number($1.Val) } ;
|
|
|
|
|
|
|
|
signed_number : ADD number { $$ = $2 }
|
|
|
|
| SUB number { $$ = -$2 }
|
|
|
|
;
|
|
|
|
|
2021-01-20 10:57:39 +00:00
|
|
|
signed_or_unsigned_number: number | signed_number ;
|
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
uint : NUMBER
|
2019-12-21 08:44:12 +00:00
|
|
|
{
|
|
|
|
var err error
|
|
|
|
$$, err = strconv.ParseUint($1.Val, 10, 64)
|
|
|
|
if err != nil {
|
2020-01-17 15:16:58 +00:00
|
|
|
yylex.(*parser).addParseErrf($1.PositionRange(), "invalid repetition in series values: %s", err)
|
2019-12-21 08:44:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
duration : DURATION
|
2019-12-21 08:44:12 +00:00
|
|
|
{
|
2020-01-08 11:04:47 +00:00
|
|
|
var err error
|
|
|
|
$$, err = parseDuration($1.Val)
|
|
|
|
if err != nil {
|
2020-01-17 15:16:58 +00:00
|
|
|
yylex.(*parser).addParseErr($1.PositionRange(), err)
|
2019-12-21 08:44:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
|
2020-01-14 16:12:15 +00:00
|
|
|
string_literal : STRING
|
|
|
|
{
|
|
|
|
$$ = &StringLiteral{
|
|
|
|
Val: yylex.(*parser).unquoteString($1.Val),
|
|
|
|
PosRange: $1.PositionRange(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
2020-01-08 11:04:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrappers for optional arguments.
|
|
|
|
*/
|
|
|
|
|
|
|
|
maybe_duration : /* empty */
|
|
|
|
{$$ = 0}
|
|
|
|
| duration
|
|
|
|
;
|
2019-12-21 08:44:12 +00:00
|
|
|
|
2020-01-08 11:04:47 +00:00
|
|
|
maybe_grouping_labels: /* empty */ { $$ = nil }
|
|
|
|
| grouping_labels
|
2019-12-21 08:44:12 +00:00
|
|
|
;
|
|
|
|
|
2019-12-09 19:03:31 +00:00
|
|
|
%%
|