diff --git a/docs/feature_flags.md b/docs/feature_flags.md index ff6883e033..08981ee931 100644 --- a/docs/feature_flags.md +++ b/docs/feature_flags.md @@ -185,17 +185,25 @@ state is mutex guarded. Cumulative-only OTLP requests are not affected. `--enable-feature=promql-duration-expr` -With this flag, arithmetic expressions can be used in time durations in range queries and offset durations. For example: +With this flag, arithmetic expressions can be used in time durations in range queries and offset durations. In range queries: - rate(http_requests_total[5m * 2]) # 10 minute range - rate(http_requests_total[(5+2) * 1m]) # 7 minute range +``` +rate(http_requests_total[5m * 2]) # 10 minute range +rate(http_requests_total[(5+2) * 1m]) # 7 minute range +``` In offset durations: - http_requests_total offset (1h / 2) # 30 minute offset - http_requests_total offset ((2 ^ 3) * 1m) # 8 minute offset +``` +http_requests_total offset (1h / 2) # 30 minute offset +http_requests_total offset ((2 ^ 3) * 1m) # 8 minute offset +``` -Note: Duration expressions are not supported in the @ timestamp operator. +When using offset with duration expressions, you must wrap the expression in +parentheses. Without parentheses, only the first duration value will be used in +the offset calculation. + +**Note**: Duration expressions are not supported in the @ timestamp operator. The following operators are supported: diff --git a/promql/parser/generated_parser.y b/promql/parser/generated_parser.y index 7128fc250a..fc901374e2 100644 --- a/promql/parser/generated_parser.y +++ b/promql/parser/generated_parser.y @@ -186,7 +186,7 @@ START_METRIC_SELECTOR %type int %type uint %type number series_value signed_number signed_or_unsigned_number -%type 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 duration_expr paren_duration_expr positive_duration_expr +%type 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 duration_expr paren_duration_expr positive_duration_expr offset_duration_expr %start start @@ -467,7 +467,7 @@ positive_duration_expr : duration_expr } ; -offset_expr: expr OFFSET duration_expr +offset_expr: expr OFFSET offset_duration_expr { if numLit, ok := $3.(*NumberLiteral); ok { yylex.(*parser).addOffset($1, time.Duration(math.Round(numLit.Val*float64(time.Second)))) @@ -1052,6 +1052,36 @@ maybe_grouping_labels: /* empty */ { $$ = nil } * Duration expressions. */ +// offset_duration_expr is needed to handle expressions like "foo offset -2^2" correctly. +// Without this rule, such expressions would be parsed as "foo offset (-2^2)" due to operator precedence. +// With this rule, they are parsed as "(foo offset -2)^2", which is the expected behavior without parentheses. +offset_duration_expr : number_duration_literal + { + nl := $1.(*NumberLiteral) + if nl.Val > 1<<63/1e9 || nl.Val < -(1<<63)/1e9 { + yylex.(*parser).addParseErrf(nl.PosRange, "duration out of range") + $$ = &NumberLiteral{Val: 0} + break + } + $$ = nl + } + | unary_op number_duration_literal + { + nl := $2.(*NumberLiteral) + if $1.Typ == SUB { + nl.Val *= -1 + } + if nl.Val > 1<<63/1e9 || nl.Val < -(1<<63)/1e9 { + yylex.(*parser).addParseErrf($1.PositionRange(), "duration out of range") + $$ = &NumberLiteral{Val: 0} + break + } + nl.PosRange.Start = $1.Pos + $$ = nl + } + | duration_expr + ; + duration_expr : number_duration_literal { nl := $1.(*NumberLiteral) diff --git a/promql/parser/generated_parser.y.go b/promql/parser/generated_parser.y.go index 470bf1742a..637cc48433 100644 --- a/promql/parser/generated_parser.y.go +++ b/promql/parser/generated_parser.y.go @@ -340,13 +340,13 @@ var yyExca = [...]int16{ 85, 198, -2, 126, -1, 126, - 41, 250, - 42, 250, - 52, 250, - 53, 250, - 57, 250, + 41, 253, + 42, 253, + 52, 253, + 53, 253, + 57, 253, -2, 20, - -1, 224, + -1, 227, 9, 247, 12, 247, 13, 247, @@ -378,7 +378,7 @@ var yyExca = [...]int16{ 88, 247, 89, 247, -2, 0, - -1, 225, + -1, 228, 9, 247, 12, 247, 13, 247, @@ -414,154 +414,156 @@ var yyExca = [...]int16{ const yyPrivate = 57344 -const yyLast = 892 +const yyLast = 915 var yyAct = [...]int16{ - 166, 359, 357, 169, 364, 251, 39, 216, 301, 52, - 177, 315, 86, 130, 84, 6, 316, 109, 200, 44, - 145, 117, 116, 61, 118, 195, 109, 295, 119, 170, - 371, 372, 373, 374, 120, 109, 222, 121, 223, 224, - 225, 115, 296, 327, 91, 93, 94, 140, 95, 96, - 97, 98, 99, 100, 101, 102, 103, 104, 297, 105, - 106, 108, 92, 93, 123, 202, 125, 124, 105, 126, - 108, 354, 293, 102, 103, 353, 122, 105, 59, 108, - 92, 358, 110, 113, 127, 329, 107, 175, 292, 349, - 318, 285, 132, 114, 112, 107, 327, 115, 111, 174, - 336, 119, 141, 176, 107, 204, 284, 120, 348, 173, - 220, 2, 3, 4, 5, 203, 205, 161, 291, 179, - 180, 181, 182, 183, 184, 185, 190, 163, 194, 163, - 163, 163, 163, 163, 163, 163, 188, 191, 186, 189, - 187, 290, 243, 121, 218, 365, 289, 79, 221, 382, - 208, 206, 35, 226, 227, 228, 229, 230, 231, 232, - 233, 234, 235, 236, 237, 238, 239, 240, 381, 288, - 219, 380, 7, 10, 241, 242, 347, 346, 345, 244, - 245, 344, 343, 81, 163, 163, 164, 109, 164, 164, - 164, 164, 164, 164, 164, 146, 147, 148, 149, 150, - 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, - 342, 132, 287, 212, 91, 93, 94, 139, 95, 96, - 97, 98, 99, 100, 101, 102, 103, 104, 210, 105, - 106, 108, 92, 109, 341, 168, 211, 317, 340, 313, - 314, 339, 174, 164, 164, 294, 178, 338, 337, 50, - 8, 209, 173, 56, 37, 80, 107, 179, 319, 165, - 91, 93, 94, 55, 95, 96, 97, 163, 99, 100, - 101, 102, 103, 104, 175, 105, 106, 108, 92, 248, - 138, 335, 137, 247, 60, 77, 90, 9, 9, 196, - 176, 201, 325, 202, 326, 328, 334, 330, 246, 321, - 113, 78, 107, 333, 331, 332, 56, 36, 110, 113, - 114, 112, 165, 1, 115, 62, 55, 49, 320, 114, - 112, 171, 172, 115, 111, 174, 164, 48, 47, 351, - 46, 352, 144, 204, 45, 173, 43, 383, 77, 360, - 361, 362, 356, 203, 205, 363, 162, 367, 366, 369, - 368, 254, 142, 56, 78, 375, 376, 175, 214, 165, - 377, 264, 174, 55, 192, 217, 379, 270, 133, 220, - 87, 350, 173, 176, 253, 42, 143, 199, 131, 324, - 85, 384, 198, 41, 323, 77, 286, 136, 207, 40, - 51, 88, 135, 88, 175, 197, 266, 267, 215, 322, - 268, 78, 378, 298, 89, 134, 213, 249, 281, 82, - 176, 255, 257, 259, 260, 261, 269, 271, 274, 275, - 276, 277, 278, 282, 283, 254, 370, 256, 258, 262, - 263, 265, 272, 273, 133, 264, 56, 279, 280, 167, - 87, 270, 165, 252, 131, 250, 55, 53, 253, 128, - 85, 174, 129, 0, 0, 0, 0, 88, 83, 0, - 0, 173, 0, 88, 0, 0, 0, 0, 77, 0, - 266, 267, 0, 0, 268, 0, 0, 0, 0, 0, - 0, 0, 281, 175, 78, 255, 257, 259, 260, 261, - 269, 271, 274, 275, 276, 277, 278, 282, 283, 176, - 0, 256, 258, 262, 263, 265, 272, 273, 0, 0, - 56, 279, 280, 54, 79, 0, 57, 300, 0, 22, - 55, 0, 0, 193, 299, 0, 58, 0, 303, 304, - 302, 309, 311, 308, 310, 305, 306, 307, 312, 0, - 0, 0, 77, 0, 0, 0, 0, 0, 18, 19, - 0, 0, 20, 0, 0, 0, 0, 0, 78, 0, - 0, 0, 0, 63, 64, 65, 66, 67, 68, 69, - 70, 71, 72, 73, 74, 75, 76, 0, 0, 0, - 13, 0, 0, 0, 24, 0, 30, 0, 0, 31, - 32, 56, 38, 109, 54, 79, 0, 57, 355, 0, - 22, 55, 0, 0, 0, 0, 0, 58, 0, 303, - 304, 302, 309, 311, 308, 310, 305, 306, 307, 312, - 91, 93, 0, 77, 0, 0, 0, 0, 0, 18, - 19, 102, 103, 20, 0, 105, 106, 108, 92, 78, - 0, 0, 0, 0, 63, 64, 65, 66, 67, 68, - 69, 70, 71, 72, 73, 74, 75, 76, 0, 0, - 0, 13, 107, 0, 0, 24, 0, 30, 0, 56, - 31, 32, 54, 79, 0, 57, 0, 0, 22, 55, - 0, 0, 0, 0, 0, 58, 0, 0, 0, 0, + 167, 363, 361, 170, 368, 255, 39, 219, 305, 52, + 178, 319, 86, 130, 84, 6, 320, 109, 203, 44, + 118, 117, 116, 227, 228, 198, 109, 171, 375, 376, + 377, 378, 119, 121, 225, 109, 226, 145, 120, 61, + 115, 358, 357, 331, 91, 93, 94, 140, 95, 96, + 97, 98, 99, 100, 101, 102, 103, 104, 333, 105, + 106, 108, 92, 93, 123, 133, 125, 124, 105, 126, + 108, 199, 339, 102, 103, 131, 122, 105, 299, 108, + 92, 362, 296, 290, 297, 59, 107, 338, 88, 353, + 110, 113, 132, 300, 337, 107, 322, 119, 331, 176, + 127, 114, 112, 120, 107, 115, 111, 340, 352, 301, + 223, 2, 3, 4, 5, 177, 247, 165, 295, 180, + 183, 184, 185, 186, 187, 188, 141, 163, 197, 181, + 181, 181, 181, 181, 181, 181, 191, 194, 189, 56, + 190, 294, 121, 369, 221, 166, 79, 293, 224, 55, + 211, 209, 215, 229, 230, 231, 232, 233, 234, 235, + 236, 237, 238, 239, 240, 241, 242, 243, 222, 386, + 292, 77, 202, 175, 245, 214, 246, 201, 35, 7, + 248, 249, 351, 174, 244, 350, 181, 78, 385, 213, + 200, 384, 245, 164, 113, 182, 182, 182, 182, 182, + 182, 182, 181, 175, 114, 112, 175, 205, 115, 36, + 349, 348, 212, 174, 132, 291, 174, 347, 387, 146, + 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, + 157, 158, 159, 160, 10, 176, 328, 90, 176, 346, + 321, 327, 317, 318, 81, 109, 298, 207, 110, 113, + 182, 177, 182, 193, 177, 289, 326, 206, 208, 114, + 112, 180, 323, 115, 111, 345, 192, 344, 182, 343, + 288, 181, 91, 93, 94, 342, 95, 96, 97, 98, + 99, 100, 101, 102, 103, 104, 169, 105, 106, 108, + 92, 341, 138, 175, 137, 139, 329, 136, 330, 332, + 252, 334, 135, 174, 251, 1, 217, 161, 335, 336, + 175, 325, 62, 220, 107, 134, 87, 223, 56, 250, + 174, 49, 48, 47, 166, 176, 85, 204, 55, 205, + 324, 46, 8, 355, 210, 356, 37, 182, 133, 88, + 144, 177, 176, 364, 365, 366, 360, 45, 131, 367, + 77, 371, 370, 373, 372, 258, 129, 50, 177, 379, + 380, 88, 43, 80, 381, 268, 78, 142, 195, 207, + 383, 274, 172, 173, 42, 354, 304, 143, 257, 206, + 208, 41, 40, 303, 51, 388, 218, 307, 308, 306, + 313, 315, 312, 314, 309, 310, 311, 316, 382, 60, + 270, 271, 9, 9, 272, 302, 89, 216, 253, 82, + 374, 168, 285, 256, 53, 259, 261, 263, 264, 265, + 273, 275, 278, 279, 280, 281, 282, 286, 287, 258, + 128, 260, 262, 266, 267, 269, 276, 277, 0, 268, + 87, 283, 284, 0, 0, 274, 0, 0, 179, 254, + 85, 0, 257, 0, 0, 56, 0, 0, 83, 0, + 0, 166, 0, 88, 0, 55, 0, 0, 0, 0, + 0, 0, 0, 0, 270, 271, 0, 0, 272, 0, + 0, 0, 0, 0, 0, 0, 285, 77, 0, 259, + 261, 263, 264, 265, 273, 275, 278, 279, 280, 281, + 282, 286, 287, 78, 0, 260, 262, 266, 267, 269, + 276, 277, 0, 0, 56, 283, 284, 54, 79, 0, + 57, 0, 0, 22, 55, 0, 162, 196, 0, 0, + 58, 0, 0, 56, 0, 0, 0, 0, 0, 166, + 0, 0, 0, 55, 0, 0, 77, 0, 0, 0, + 0, 0, 18, 19, 0, 0, 20, 0, 0, 0, + 0, 0, 78, 0, 0, 77, 0, 63, 64, 65, + 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, + 76, 78, 0, 0, 13, 0, 0, 0, 24, 0, + 30, 0, 0, 31, 32, 56, 38, 109, 54, 79, + 0, 57, 359, 0, 22, 55, 0, 0, 0, 0, + 0, 58, 0, 307, 308, 306, 313, 315, 312, 314, + 309, 310, 311, 316, 91, 93, 0, 77, 0, 0, + 0, 0, 0, 18, 19, 102, 103, 20, 0, 105, + 106, 108, 92, 78, 0, 0, 0, 0, 63, 64, + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, + 75, 76, 0, 0, 0, 13, 107, 0, 0, 24, + 0, 30, 0, 56, 31, 32, 54, 79, 0, 57, + 0, 0, 22, 55, 0, 0, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 77, 0, 0, 0, 0, 0, 18, 19, 0, - 0, 20, 0, 0, 0, 17, 79, 78, 0, 0, - 0, 22, 63, 64, 65, 66, 67, 68, 69, 70, - 71, 72, 73, 74, 75, 76, 0, 0, 0, 13, + 0, 0, 0, 0, 0, 77, 0, 0, 0, 0, + 0, 18, 19, 0, 0, 20, 0, 0, 0, 17, + 79, 78, 0, 0, 0, 22, 63, 64, 65, 66, + 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, + 0, 0, 0, 13, 0, 0, 0, 24, 0, 30, + 0, 0, 31, 32, 18, 19, 0, 0, 20, 0, + 0, 0, 17, 35, 0, 0, 0, 0, 22, 11, + 12, 14, 15, 16, 21, 23, 25, 26, 27, 28, + 29, 33, 34, 0, 0, 0, 13, 0, 0, 0, + 24, 0, 30, 0, 0, 31, 32, 18, 19, 0, + 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 11, 12, 14, 15, 16, 21, 23, 25, + 26, 27, 28, 29, 33, 34, 109, 0, 0, 13, 0, 0, 0, 24, 0, 30, 0, 0, 31, 32, - 18, 19, 0, 0, 20, 0, 0, 0, 17, 35, - 0, 0, 0, 0, 22, 11, 12, 14, 15, 16, - 21, 23, 25, 26, 27, 28, 29, 33, 34, 0, - 0, 0, 13, 0, 0, 0, 24, 0, 30, 0, - 0, 31, 32, 18, 19, 0, 0, 20, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 11, 12, - 14, 15, 16, 21, 23, 25, 26, 27, 28, 29, - 33, 34, 109, 0, 0, 13, 0, 0, 0, 24, - 0, 30, 0, 0, 31, 32, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 91, - 93, 94, 0, 95, 96, 0, 0, 99, 100, 0, - 102, 103, 104, 0, 105, 106, 108, 92, 0, 0, + 0, 0, 0, 0, 0, 109, 0, 0, 0, 0, + 0, 0, 0, 91, 93, 94, 0, 95, 96, 97, + 0, 99, 100, 101, 102, 103, 104, 0, 105, 106, + 108, 92, 91, 93, 94, 0, 95, 96, 0, 0, + 99, 100, 0, 102, 103, 104, 0, 105, 106, 108, + 92, 0, 0, 0, 0, 107, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 107, + 0, 0, 0, 0, 107, } var yyPact = [...]int16{ - 13, 162, 746, 746, 582, 703, -1000, -1000, -1000, 139, + 13, 169, 750, 750, 586, 707, -1000, -1000, -1000, 165, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, - -1000, -1000, -1000, -1000, -1000, 438, -1000, 284, -1000, 173, + -1000, -1000, -1000, -1000, -1000, 438, -1000, 235, -1000, 231, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, - -1000, -1000, 41, 22, 128, -1000, -1000, 660, -1000, 660, - 134, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, - -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 432, - -1000, -1000, 385, -1000, -1000, 278, 213, -1000, -1000, 23, - -1000, -58, -58, -58, -58, -58, -58, -58, -58, -58, - -58, -58, -58, -58, -58, -58, -58, 344, 233, 244, - 427, 427, 427, 427, 427, 427, 128, -51, -1000, 124, - 124, 501, -1000, 3, 267, 12, -15, -1000, 375, -1000, - -1000, 289, 61, -1000, -1000, 368, -1000, 226, -1000, 211, - 353, 660, -1000, -46, -41, -1000, 660, 660, 660, 660, - 660, 660, 660, 660, 660, 660, 660, 660, 660, 660, - 660, -1000, -1000, -1000, 427, 427, -1000, 127, -1000, -1000, - -1000, -1000, -1000, -1000, -1000, 90, 90, 277, -1000, 41, - 258, 258, -15, -15, -15, -15, -1000, -1000, -1000, 423, - -1000, -1000, 84, -1000, 173, -1000, -1000, -1000, 366, -1000, - 144, -1000, -1000, -1000, -1000, -1000, 116, -1000, -1000, -1000, - -1000, -1000, -1000, -1000, 62, 46, 1, -1000, -1000, -1000, - 500, 21, 124, 124, 124, 124, 12, 12, 579, 579, - 579, 808, 219, 579, 579, 808, 12, 12, 579, 12, - 21, -15, 267, 68, -1000, -1000, -1000, 297, -1000, 377, + -1000, -1000, 207, 18, 127, -1000, -1000, 664, -1000, 664, + 133, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, + -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 336, + -1000, -1000, 295, -1000, -1000, 290, 291, -1000, -1000, 23, + -1000, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, 524, 284, 446, + 130, 130, 130, 130, 130, 130, 127, -47, -1000, 251, + 251, 505, -1000, 3, 49, 12, -16, -1000, 170, -1000, + -1000, 325, 203, -1000, -1000, 314, -1000, 187, -1000, 150, + 301, 664, -1000, -48, -57, -1000, 664, 664, 664, 664, + 664, 664, 664, 664, 664, 664, 664, 664, 664, 664, + 664, -1000, -1000, -1000, 130, 207, 130, -1000, 101, -1000, + -1000, -1000, -1000, -1000, -1000, -1000, 164, 164, 298, -1000, + 207, -1000, 130, 152, 152, -16, -16, -16, -16, -1000, + -1000, -1000, 427, -1000, -1000, 248, -1000, 231, -1000, -1000, + -1000, 63, -1000, 145, -1000, -1000, -1000, -1000, -1000, 116, + -1000, -1000, -1000, -1000, -1000, -1000, -1000, 56, 58, 52, + -1000, -1000, -1000, 359, 21, 251, 251, 251, 251, 12, + 12, 583, 583, 583, 831, 812, 583, 583, 831, 12, + 12, 583, 12, 21, -1000, -16, 49, 74, -1000, -1000, + -1000, 309, -1000, 234, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, + -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 664, + -1000, -1000, -1000, -1000, -1000, -1000, 79, 79, 32, 79, + 94, 94, 70, 90, -1000, -1000, 285, 269, 263, 261, + 259, 233, 211, 205, 204, 179, 176, -1000, -1000, -1000, + -1000, -1000, -1000, 87, -1000, -1000, -1000, 353, -1000, 231, + -1000, -1000, -1000, 79, -1000, 16, 15, 585, -1000, -1000, + -1000, 24, 194, 194, 194, 164, 129, 129, 24, 129, + 24, -64, -1000, -1000, -1000, -1000, -1000, 79, 79, -1000, + -1000, -1000, 79, -1000, -1000, -1000, -1000, -1000, -1000, 194, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, - -1000, -1000, -1000, -1000, -1000, 660, -1000, -1000, -1000, -1000, - -1000, -1000, 77, 77, 59, 77, 94, 94, 279, 83, - -1000, -1000, 242, 241, 235, 232, 228, 204, 176, 175, - 172, 171, 170, -1000, -1000, -1000, -1000, -1000, -1000, 87, - -1000, -1000, -1000, 349, -1000, 173, -1000, -1000, -1000, 77, - -1000, 49, 45, 581, -1000, -1000, -1000, 24, 442, 442, - 442, 90, 131, 131, 24, 131, 24, -62, -1000, -1000, - -1000, -1000, -1000, 77, 77, -1000, -1000, -1000, 77, -1000, - -1000, -1000, -1000, -1000, -1000, 442, -1000, -1000, -1000, -1000, - -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 147, -1000, - 316, -1000, -1000, -1000, -1000, + -1000, -1000, 167, -1000, 197, -1000, -1000, -1000, -1000, } var yyPgo = [...]int16{ - 0, 449, 13, 447, 5, 18, 443, 284, 78, 439, - 12, 426, 173, 250, 409, 14, 407, 16, 11, 406, - 404, 7, 403, 8, 4, 402, 2, 1, 3, 398, - 29, 0, 390, 389, 22, 102, 383, 376, 6, 375, - 364, 21, 352, 23, 336, 19, 334, 332, 330, 328, - 327, 317, 249, 9, 315, 10, 313, 307, + 0, 430, 13, 414, 5, 18, 413, 399, 85, 411, + 12, 410, 234, 332, 409, 14, 408, 16, 11, 407, + 406, 7, 405, 8, 4, 398, 2, 1, 3, 386, + 27, 0, 384, 382, 22, 126, 381, 377, 6, 374, + 368, 21, 367, 39, 362, 19, 347, 340, 331, 323, + 322, 321, 357, 9, 312, 10, 307, 305, 209, } var yyR1 = [...]int8{ - 0, 56, 56, 56, 56, 56, 56, 56, 38, 38, + 0, 57, 57, 57, 57, 57, 57, 57, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 33, 33, 33, 33, 34, 34, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, @@ -575,7 +577,7 @@ var yyR1 = [...]int8{ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 12, 12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15, - 15, 57, 20, 20, 20, 20, 19, 19, 19, 19, + 15, 58, 20, 20, 20, 20, 19, 19, 19, 19, 19, 19, 19, 19, 19, 29, 29, 29, 21, 21, 21, 21, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 25, 25, @@ -585,8 +587,9 @@ var yyR1 = [...]int8{ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 5, 5, 5, 5, 45, 45, 28, 28, 30, 30, - 31, 31, 27, 26, 26, 49, 10, 18, 18, 53, - 53, 53, 53, 53, 53, 53, 53, 53, 54, + 31, 31, 27, 26, 26, 49, 10, 18, 18, 56, + 56, 56, 53, 53, 53, 53, 53, 53, 53, 53, + 53, 54, } var yyR2 = [...]int8{ @@ -615,14 +618,15 @@ var yyR2 = [...]int8{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 1, 1, 1, 0, 1, 1, - 2, 3, 3, 3, 3, 3, 3, 1, 3, + 2, 1, 1, 2, 3, 3, 3, 3, 3, 3, + 1, 3, } var yyChk = [...]int16{ - -1000, -56, 98, 99, 100, 101, 2, 10, -13, -7, + -1000, -57, 98, 99, 100, 101, 2, 10, -13, -7, -12, 62, 63, 79, 64, 65, 66, 12, 47, 48, 51, 67, 18, 68, 83, 69, 70, 71, 72, 73, - 85, 88, 89, 74, 75, 13, -57, -13, 10, -38, + 85, 88, 89, 74, 75, 13, -58, -13, 10, -38, -33, -36, -39, -44, -45, -46, -48, -49, -50, -51, -52, -32, -53, -3, 12, 19, 9, 15, 25, -8, -7, -43, -54, 62, 63, 64, 65, 66, 67, 68, @@ -635,29 +639,29 @@ var yyChk = [...]int16{ -2, 12, -10, 2, 20, 7, 2, 4, 2, 4, 24, -35, -42, -37, -47, 78, -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, - -35, -53, 2, -45, -8, 15, -31, -9, 2, -28, - -30, 88, 89, 19, 9, 41, 57, -55, 2, -53, - -53, -53, -53, -53, -53, -53, -41, -34, -17, 15, - 2, -17, -40, 22, -38, 22, 22, 20, 7, 2, - -5, 2, 4, 54, 44, 55, -5, 20, -15, 25, - 2, 25, 2, -19, 5, -29, -21, 12, -28, -30, - 16, -38, 82, 84, 80, 81, -38, -38, -38, -38, + -35, -56, 2, -45, -8, -53, 15, -31, -9, 2, + -28, -30, 88, 89, 19, 9, 41, 57, -55, 2, + -53, -45, -8, -53, -53, -53, -53, -53, -53, -41, + -34, -17, 15, 2, -17, -40, 22, -38, 22, 22, + 20, 7, 2, -5, 2, 4, 54, 44, 55, -5, + 20, -15, 25, 2, 25, 2, -19, 5, -29, -21, + 12, -28, -30, 16, -38, 82, 84, 80, 81, -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, - -38, -53, -53, 15, -28, -28, 21, 6, 2, -16, - 22, -4, -6, 25, 2, 62, 78, 63, 79, 64, - 65, 66, 80, 81, 12, 82, 47, 48, 51, 67, - 18, 68, 83, 84, 69, 70, 71, 72, 73, 88, - 89, 59, 74, 75, 22, 7, 20, -2, 25, 2, - 25, 2, 26, 26, -30, 26, 41, 57, -22, 24, - 17, -23, 30, 28, 29, 35, 36, 37, 33, 31, - 34, 32, 38, -17, -17, -18, -17, -18, 22, -55, - 21, 2, 22, 7, 2, -38, -27, 19, -27, 26, - -27, -21, -21, 24, 17, 2, 17, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 21, 2, - 22, -4, -27, 26, 26, 17, -23, -26, 57, -27, - -31, -31, -31, -28, -24, 14, -24, -26, -24, -26, - -11, 92, 93, 94, 95, -27, -27, -27, -25, -31, - 24, 21, 2, 21, -31, + -38, -38, -38, -38, -45, -53, -53, 15, -28, -28, + 21, 6, 2, -16, 22, -4, -6, 25, 2, 62, + 78, 63, 79, 64, 65, 66, 80, 81, 12, 82, + 47, 48, 51, 67, 18, 68, 83, 84, 69, 70, + 71, 72, 73, 88, 89, 59, 74, 75, 22, 7, + 20, -2, 25, 2, 25, 2, 26, 26, -30, 26, + 41, 57, -22, 24, 17, -23, 30, 28, 29, 35, + 36, 37, 33, 31, 34, 32, 38, -17, -17, -18, + -17, -18, 22, -55, 21, 2, 22, 7, 2, -38, + -27, 19, -27, 26, -27, -21, -21, 24, 17, 2, + 17, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, 21, 2, 22, -4, -27, 26, 26, 17, + -23, -26, 57, -27, -31, -31, -31, -28, -24, 14, + -24, -26, -24, -26, -11, 92, 93, 94, 95, -27, + -27, -27, -25, -31, 24, 21, 2, 21, -31, } var yyDef = [...]int16{ @@ -667,7 +671,7 @@ var yyDef = [...]int16{ 122, 123, 124, 125, 126, 0, 2, -2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 0, 109, 234, 235, 0, 245, 0, - 86, 87, 257, -2, -2, -2, -2, -2, -2, -2, + 86, 87, 260, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 228, 229, 0, 5, 101, 0, 129, 132, 0, 136, 140, 246, 141, 145, 44, 44, 44, 44, 44, 44, 44, 44, 44, @@ -677,29 +681,29 @@ var yyDef = [...]int16{ 92, 0, 96, 100, 127, 0, 133, 0, 139, 0, 144, 0, 43, 48, 49, 45, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 70, 71, 249, 0, 0, 72, 0, 74, 240, - 241, 75, 76, 236, 237, 0, 0, 0, 83, 69, - 251, 252, 253, 254, 255, 256, 21, 22, 25, 0, - 55, 26, 0, 64, 66, 68, 258, 88, 0, 93, - 0, 99, 230, 231, 232, 233, 0, 128, 131, 134, - 137, 135, 138, 143, 146, 148, 151, 155, 156, 157, - 0, 27, 0, 0, -2, -2, 28, 29, 30, 31, - 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, - 42, 250, 0, 0, 238, 239, 77, 0, 82, 0, - 54, 57, 59, 60, 61, 199, 200, 201, 202, 203, - 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, - 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, - 224, 225, 226, 227, 63, 67, 89, 91, 94, 98, - 95, 97, 0, 0, 0, 0, 0, 0, 0, 0, - 161, 163, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 46, 47, 50, 248, 51, 73, 0, - 79, 81, 52, 0, 58, 65, 147, 242, 149, 0, - 152, 0, 0, 0, 159, 164, 160, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 78, 80, - 53, 56, 150, 0, 0, 158, 162, 165, 0, 244, - 166, 167, 168, 169, 170, 0, 171, 172, 173, 174, - 175, 181, 182, 183, 184, 153, 154, 243, 0, 179, - 0, 177, 180, 176, 178, + 0, 70, 71, 249, 0, 251, 0, 72, 0, 74, + 240, 241, 75, 76, 236, 237, 0, 0, 0, 83, + 69, 252, 0, 254, 255, 256, 257, 258, 259, 21, + 22, 25, 0, 55, 26, 0, 64, 66, 68, 261, + 88, 0, 93, 0, 99, 230, 231, 232, 233, 0, + 128, 131, 134, 137, 135, 138, 143, 146, 148, 151, + 155, 156, 157, 0, 27, 0, 0, -2, -2, 28, + 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, + 39, 40, 41, 42, 250, 253, 0, 0, 238, 239, + 77, 0, 82, 0, 54, 57, 59, 60, 61, 199, + 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, + 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, + 220, 221, 222, 223, 224, 225, 226, 227, 63, 67, + 89, 91, 94, 98, 95, 97, 0, 0, 0, 0, + 0, 0, 0, 0, 161, 163, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 46, 47, 50, + 248, 51, 73, 0, 79, 81, 52, 0, 58, 65, + 147, 242, 149, 0, 152, 0, 0, 0, 159, 164, + 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 78, 80, 53, 56, 150, 0, 0, 158, + 162, 165, 0, 244, 166, 167, 168, 169, 170, 0, + 171, 172, 173, 174, 175, 181, 182, 183, 184, 153, + 154, 243, 0, 179, 0, 177, 180, 176, 178, } var yyTok1 = [...]int8{ @@ -2048,6 +2052,32 @@ yydefault: yyVAL.node = nl } case 250: + yyDollar = yyS[yypt-2 : yypt+1] + { + nl := yyDollar[2].node.(*NumberLiteral) + if yyDollar[1].item.Typ == SUB { + nl.Val *= -1 + } + if nl.Val > 1<<63/1e9 || nl.Val < -(1<<63)/1e9 { + yylex.(*parser).addParseErrf(yyDollar[1].item.PositionRange(), "duration out of range") + yyVAL.node = &NumberLiteral{Val: 0} + break + } + nl.PosRange.Start = yyDollar[1].item.Pos + yyVAL.node = nl + } + case 252: + yyDollar = yyS[yypt-1 : yypt+1] + { + nl := yyDollar[1].node.(*NumberLiteral) + if nl.Val > 1<<63/1e9 || nl.Val < -(1<<63)/1e9 { + yylex.(*parser).addParseErrf(nl.PosRange, "duration out of range") + yyVAL.node = &NumberLiteral{Val: 0} + break + } + yyVAL.node = nl + } + case 253: yyDollar = yyS[yypt-2 : yypt+1] { switch expr := yyDollar[2].node.(type) { @@ -2080,25 +2110,25 @@ yydefault: break } } - case 251: + case 254: yyDollar = yyS[yypt-3 : yypt+1] { yylex.(*parser).experimentalDurationExpr(yyDollar[1].node.(Expr)) yyVAL.node = &DurationExpr{Op: ADD, LHS: yyDollar[1].node.(Expr), RHS: yyDollar[3].node.(Expr)} } - case 252: + case 255: yyDollar = yyS[yypt-3 : yypt+1] { yylex.(*parser).experimentalDurationExpr(yyDollar[1].node.(Expr)) yyVAL.node = &DurationExpr{Op: SUB, LHS: yyDollar[1].node.(Expr), RHS: yyDollar[3].node.(Expr)} } - case 253: + case 256: yyDollar = yyS[yypt-3 : yypt+1] { yylex.(*parser).experimentalDurationExpr(yyDollar[1].node.(Expr)) yyVAL.node = &DurationExpr{Op: MUL, LHS: yyDollar[1].node.(Expr), RHS: yyDollar[3].node.(Expr)} } - case 254: + case 257: yyDollar = yyS[yypt-3 : yypt+1] { yylex.(*parser).experimentalDurationExpr(yyDollar[1].node.(Expr)) @@ -2109,7 +2139,7 @@ yydefault: } yyVAL.node = &DurationExpr{Op: DIV, LHS: yyDollar[1].node.(Expr), RHS: yyDollar[3].node.(Expr)} } - case 255: + case 258: yyDollar = yyS[yypt-3 : yypt+1] { yylex.(*parser).experimentalDurationExpr(yyDollar[1].node.(Expr)) @@ -2120,13 +2150,13 @@ yydefault: } yyVAL.node = &DurationExpr{Op: MOD, LHS: yyDollar[1].node.(Expr), RHS: yyDollar[3].node.(Expr)} } - case 256: + case 259: yyDollar = yyS[yypt-3 : yypt+1] { yylex.(*parser).experimentalDurationExpr(yyDollar[1].node.(Expr)) yyVAL.node = &DurationExpr{Op: POW, LHS: yyDollar[1].node.(Expr), RHS: yyDollar[3].node.(Expr)} } - case 258: + case 261: yyDollar = yyS[yypt-3 : yypt+1] { yylex.(*parser).experimentalDurationExpr(yyDollar[2].node.(Expr)) diff --git a/promql/parser/parse_test.go b/promql/parser/parse_test.go index a7720a9124..a1b59af8de 100644 --- a/promql/parser/parse_test.go +++ b/promql/parser/parse_test.go @@ -4271,6 +4271,188 @@ var testExpr = []struct { }, }, }, + { + input: `foo offset -1^1`, + expected: &BinaryExpr{ + Op: POW, + LHS: &VectorSelector{ + Name: "foo", + OriginalOffset: -time.Second, + LabelMatchers: []*labels.Matcher{ + MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "foo"), + }, + PosRange: posrange.PositionRange{ + Start: 0, + End: 13, + }, + }, + RHS: &NumberLiteral{ + Val: 1, + PosRange: posrange.PositionRange{ + Start: 14, + End: 15, + }, + }, + }, + }, + { + input: `foo offset -(1^2)`, + expected: &VectorSelector{ + Name: "foo", + OriginalOffset: 0, + OriginalOffsetExpr: &DurationExpr{ + Op: SUB, + RHS: &DurationExpr{ + Op: POW, + LHS: &NumberLiteral{ + Val: 1, + PosRange: posrange.PositionRange{ + Start: 13, + End: 14, + }, + }, + RHS: &NumberLiteral{ + Val: 2, + PosRange: posrange.PositionRange{ + Start: 15, + End: 16, + }, + }, + Wrapped: true, + }, + StartPos: 11, + }, + LabelMatchers: []*labels.Matcher{ + MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "foo"), + }, + PosRange: posrange.PositionRange{ + Start: 0, + End: 17, + }, + }, + }, + { + input: `foo offset (-1^2)`, + expected: &VectorSelector{ + Name: "foo", + OriginalOffset: 0, + OriginalOffsetExpr: &DurationExpr{ + Op: SUB, + RHS: &DurationExpr{ + Op: POW, + LHS: &NumberLiteral{ + Val: 1, + PosRange: posrange.PositionRange{ + Start: 13, + End: 14, + }, + }, + RHS: &NumberLiteral{ + Val: 2, + PosRange: posrange.PositionRange{ + Start: 15, + End: 16, + }, + }, + }, + Wrapped: true, + StartPos: 12, + }, + LabelMatchers: []*labels.Matcher{ + MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "foo"), + }, + PosRange: posrange.PositionRange{ + Start: 0, + End: 17, + }, + }, + }, + { + input: `foo[-2^2]`, + expected: &MatrixSelector{ + VectorSelector: &VectorSelector{ + Name: "foo", + LabelMatchers: []*labels.Matcher{ + MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "foo"), + }, + PosRange: posrange.PositionRange{ + Start: 0, + End: 3, + }, + }, + RangeExpr: &DurationExpr{ + Op: SUB, + LHS: nil, + RHS: &DurationExpr{ + Op: POW, + LHS: &NumberLiteral{ + Val: 2, + PosRange: posrange.PositionRange{ + Start: 5, + End: 6, + }, + }, + RHS: &NumberLiteral{ + Val: 2, + PosRange: posrange.PositionRange{ + Start: 7, + End: 8, + }, + }, + }, + StartPos: 4, + }, + EndPos: 9, + }, + }, + { + input: `foo[0+-2^2]`, + expected: &MatrixSelector{ + VectorSelector: &VectorSelector{ + Name: "foo", + LabelMatchers: []*labels.Matcher{ + MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "foo"), + }, + PosRange: posrange.PositionRange{ + Start: 0, + End: 3, + }, + }, + RangeExpr: &DurationExpr{ + Op: ADD, + LHS: &NumberLiteral{ + Val: 0, + PosRange: posrange.PositionRange{ + Start: 4, + End: 5, + }, + }, + RHS: &DurationExpr{ + Op: SUB, + LHS: nil, + RHS: &DurationExpr{ + Op: POW, + LHS: &NumberLiteral{ + Val: 2, + PosRange: posrange.PositionRange{ + Start: 7, + End: 8, + }, + }, + RHS: &NumberLiteral{ + Val: 2, + PosRange: posrange.PositionRange{ + Start: 9, + End: 10, + }, + }, + }, + StartPos: 6, + }, + }, + EndPos: 11, + }, + }, { input: `foo[5s/0d]`, fail: true, @@ -4397,7 +4579,7 @@ func TestParseExpressions(t *testing.T) { // The FastRegexMatcher is not comparable with a deep equal, so only compare its String() version. if actualVector, ok := expr.(*VectorSelector); ok { - require.IsType(t, &VectorSelector{}, test.expected, "error on input '%s'", test.input) + require.IsType(t, test.expected, actualVector, "error on input '%s'", test.input) expectedVector := test.expected.(*VectorSelector) require.Len(t, actualVector.LabelMatchers, len(expectedVector.LabelMatchers), "error on input '%s'", test.input) @@ -4419,6 +4601,7 @@ func TestParseExpressions(t *testing.T) { require.Equal(t, expected, expr, "error on input '%s'", test.input) } else { + require.Error(t, err) require.ErrorContains(t, err, test.errMsg, "unexpected error on input '%s', expected '%s', got '%s'", test.input, test.errMsg, err.Error()) var errorList ParseErrors diff --git a/promql/promqltest/testdata/duration_expression.test b/promql/promqltest/testdata/duration_expression.test index 251856241a..9a2547c016 100644 --- a/promql/promqltest/testdata/duration_expression.test +++ b/promql/promqltest/testdata/duration_expression.test @@ -118,4 +118,31 @@ eval instant at 1000s metric1_total offset (100 + 2) # Test offset precedence without parentheses: offset 100 + 2 eval instant at 1000s metric1_total offset 100 + 2 - {} 92 \ No newline at end of file + {} 92 + +eval instant at 1002s (metric1_total offset 2) ^ 2 + {} 10000 + +eval instant at 1002s metric1_total offset 2 ^ 2 + {} 10000 + +eval instant at 998s metric1_total offset -2 ^ 2 + {} 10000 + +eval instant at 1000s metric1_total offset (2 ^ 2) + metric1_total{} 99 + +eval instant at 1000s metric1_total offset (2 * 2) + metric1_total{} 99 + +eval instant at 1000s metric1_total offset -2 * 2 + {} 200 + +eval instant at 1000s metric1_total offset (-2 * 2) + metric1_total{} 100 + +eval instant at 1000s metric1_total offset -4 + metric1_total{} 100 + +eval instant at 1000s metric1_total offset (-2 ^ 2) + metric1_total{} 100 \ No newline at end of file