From bd4ea118e9bdc41bb79c1c1967d7674389a63987 Mon Sep 17 00:00:00 2001 From: darshanime Date: Fri, 15 Mar 2024 00:18:51 +0530 Subject: [PATCH] Allow durations for number rule Signed-off-by: darshanime --- promql/parser/generated_parser.y | 22 +- promql/parser/generated_parser.y.go | 463 ++++++++++---------- promql/parser/lex.go | 2 +- promql/parser/parse_test.go | 30 +- promql/promqltest/testdata/at_modifier.test | 8 + 5 files changed, 288 insertions(+), 237 deletions(-) diff --git a/promql/parser/generated_parser.y b/promql/parser/generated_parser.y index 940526845..b99e67424 100644 --- a/promql/parser/generated_parser.y +++ b/promql/parser/generated_parser.y @@ -428,7 +428,7 @@ offset_expr: expr OFFSET number_duration_literal $$ = $1 } | expr OFFSET error - { yylex.(*parser).unexpected("offset", "integer or duration"); $$ = $1 } + { yylex.(*parser).unexpected("offset", "number or duration"); $$ = $1 } ; /* * @ modifiers. @@ -504,11 +504,11 @@ subquery_expr : expr LEFT_BRACKET number_duration_literal COLON number_duratio | expr LEFT_BRACKET number_duration_literal COLON number_duration_literal error { yylex.(*parser).unexpected("subquery selector", "\"]\""); $$ = $1 } | expr LEFT_BRACKET number_duration_literal COLON error - { yylex.(*parser).unexpected("subquery selector", "number/duration or \"]\""); $$ = $1 } + { yylex.(*parser).unexpected("subquery selector", "number or duration or \"]\""); $$ = $1 } | expr LEFT_BRACKET number_duration_literal error { yylex.(*parser).unexpected("subquery or range", "\":\" or \"]\""); $$ = $1 } | expr LEFT_BRACKET error - { yylex.(*parser).unexpected("subquery selector", "number/duration"); $$ = $1 } + { yylex.(*parser).unexpected("subquery selector", "number or duration"); $$ = $1 } ; /* @@ -902,7 +902,21 @@ number_duration_literal : NUMBER } ; -number : NUMBER { $$ = yylex.(*parser).number($1.Val) } ; +number : NUMBER + { + $$ = yylex.(*parser).number($1.Val) + } + | DURATION + { + var err error + var dur time.Duration + dur, err = parseDuration($1.Val) + if err != nil { + yylex.(*parser).addParseErr($1.PositionRange(), err) + } + $$ = dur.Seconds() + } + ; signed_number : ADD number { $$ = $2 } | SUB number { $$ = -$2 } diff --git a/promql/parser/generated_parser.y.go b/promql/parser/generated_parser.y.go index 8b8d9f400..423082daf 100644 --- a/promql/parser/generated_parser.y.go +++ b/promql/parser/generated_parser.y.go @@ -323,198 +323,198 @@ var yyExca = [...]int16{ 78, 188, 84, 188, -2, 124, - -1, 199, - 9, 236, - 12, 236, - 13, 236, - 18, 236, - 19, 236, - 25, 236, - 40, 236, - 46, 236, - 47, 236, - 50, 236, - 56, 236, - 61, 236, - 62, 236, - 63, 236, - 64, 236, - 65, 236, - 66, 236, - 67, 236, - 68, 236, - 69, 236, - 70, 236, - 71, 236, - 72, 236, - 73, 236, - 74, 236, - 78, 236, - 82, 236, - 84, 236, - 87, 236, - 88, 236, - -2, 0, -1, 200, - 9, 236, - 12, 236, - 13, 236, - 18, 236, - 19, 236, - 25, 236, - 40, 236, - 46, 236, - 47, 236, - 50, 236, - 56, 236, - 61, 236, - 62, 236, - 63, 236, - 64, 236, - 65, 236, - 66, 236, - 67, 236, - 68, 236, - 69, 236, - 70, 236, - 71, 236, - 72, 236, - 73, 236, - 74, 236, - 78, 236, - 82, 236, - 84, 236, - 87, 236, - 88, 236, + 9, 237, + 12, 237, + 13, 237, + 18, 237, + 19, 237, + 25, 237, + 40, 237, + 46, 237, + 47, 237, + 50, 237, + 56, 237, + 61, 237, + 62, 237, + 63, 237, + 64, 237, + 65, 237, + 66, 237, + 67, 237, + 68, 237, + 69, 237, + 70, 237, + 71, 237, + 72, 237, + 73, 237, + 74, 237, + 78, 237, + 82, 237, + 84, 237, + 87, 237, + 88, 237, + -2, 0, + -1, 201, + 9, 237, + 12, 237, + 13, 237, + 18, 237, + 19, 237, + 25, 237, + 40, 237, + 46, 237, + 47, 237, + 50, 237, + 56, 237, + 61, 237, + 62, 237, + 63, 237, + 64, 237, + 65, 237, + 66, 237, + 67, 237, + 68, 237, + 69, 237, + 70, 237, + 71, 237, + 72, 237, + 73, 237, + 74, 237, + 78, 237, + 82, 237, + 84, 237, + 87, 237, + 88, 237, -2, 0, } const yyPrivate = 57344 -const yyLast = 727 +const yyLast = 728 var yyAct = [...]int16{ - 155, 330, 328, 274, 335, 152, 225, 39, 191, 44, - 288, 287, 156, 118, 82, 177, 55, 106, 6, 53, - 77, 109, 56, 133, 108, 22, 54, 110, 107, 171, - 159, 197, 57, 198, 199, 200, 60, 111, 325, 151, - 324, 301, 320, 307, 265, 154, 55, 75, 128, 105, - 159, 160, 349, 18, 19, 290, 54, 20, 306, 308, - 105, 319, 159, 76, 113, 305, 114, 161, 61, 62, + 155, 331, 329, 275, 336, 152, 226, 39, 192, 44, + 289, 288, 156, 118, 82, 178, 55, 106, 6, 53, + 77, 109, 56, 133, 108, 22, 54, 110, 107, 172, + 300, 198, 57, 199, 200, 201, 60, 111, 326, 151, + 325, 302, 321, 308, 266, 154, 55, 75, 128, 105, + 291, 300, 160, 18, 19, 309, 54, 20, 307, 218, + 105, 320, 159, 76, 113, 306, 114, 330, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, - 73, 74, 112, 160, 179, 13, 87, 89, 264, 24, - 101, 30, 104, 150, 31, 32, 115, 98, 99, 161, - 109, 101, 102, 104, 88, 195, 110, 2, 3, 4, - 5, 263, 258, 149, 84, 162, 55, 103, 159, 172, - 166, 169, 299, 181, 83, 165, 54, 257, 103, 193, - 157, 158, 184, 180, 182, 164, 183, 196, 299, 185, - 217, 194, 201, 202, 203, 204, 205, 206, 207, 208, - 209, 210, 211, 212, 213, 214, 215, 129, 348, 329, - 216, 218, 219, 55, 38, 121, 53, 77, 268, 56, - 111, 336, 22, 54, 121, 119, 266, 347, 77, 57, - 346, 176, 269, 259, 119, 7, 175, 35, 122, 260, - 160, 262, 117, 178, 75, 179, 318, 122, 270, 174, - 18, 19, 317, 267, 20, 316, 161, 187, 285, 286, - 76, 86, 289, 315, 261, 61, 62, 63, 64, 65, - 66, 67, 68, 69, 70, 71, 72, 73, 74, 10, - 186, 291, 13, 314, 181, 313, 24, 312, 30, 79, - 311, 31, 32, 310, 180, 182, 134, 135, 136, 137, - 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, - 148, 309, 36, 127, 293, 126, 297, 298, 300, 296, - 302, 55, 222, 189, 295, 84, 221, 125, 303, 304, - 192, 54, 124, 292, 195, 83, 168, 159, 1, 294, - 227, 220, 50, 81, 163, 123, 49, 48, 78, 167, - 237, 55, 322, 323, 47, 46, 243, 132, 160, 327, - 321, 54, 332, 333, 334, 45, 331, 43, 130, 338, - 337, 340, 339, 170, 161, 59, 341, 342, 9, 9, - 42, 343, 8, 131, 239, 240, 37, 345, 241, 41, - 40, 51, 190, 344, 271, 85, 254, 350, 188, 228, - 230, 232, 233, 234, 242, 244, 247, 248, 249, 250, - 251, 255, 256, 223, 80, 229, 231, 235, 236, 238, - 245, 246, 120, 153, 55, 252, 253, 53, 77, 58, - 56, 226, 52, 22, 54, 116, 0, 0, 0, 0, - 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 227, 0, 0, 0, 0, 75, 0, 0, 0, 0, - 237, 18, 19, 0, 0, 20, 243, 0, 0, 0, - 224, 76, 0, 0, 0, 0, 61, 62, 63, 64, + 73, 74, 112, 161, 180, 13, 87, 89, 265, 24, + 101, 30, 104, 150, 31, 32, 115, 98, 99, 162, + 109, 101, 102, 104, 88, 349, 110, 2, 3, 4, + 5, 264, 196, 149, 111, 163, 160, 103, 337, 173, + 167, 170, 84, 182, 348, 166, 159, 347, 103, 194, + 157, 158, 83, 181, 183, 165, 184, 197, 77, 186, + 185, 195, 202, 203, 204, 205, 206, 207, 208, 209, + 210, 211, 212, 213, 214, 215, 216, 129, 269, 263, + 217, 160, 219, 220, 55, 38, 35, 53, 77, 267, + 56, 159, 270, 22, 54, 121, 297, 188, 7, 259, + 57, 296, 262, 161, 319, 119, 318, 317, 271, 179, + 261, 180, 161, 260, 258, 75, 295, 84, 122, 162, + 187, 18, 19, 316, 268, 20, 315, 83, 162, 286, + 287, 76, 314, 290, 313, 81, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, - 0, 0, 0, 13, 239, 240, 0, 24, 241, 30, - 0, 0, 31, 32, 0, 0, 254, 105, 0, 228, - 230, 232, 233, 234, 242, 244, 247, 248, 249, 250, - 251, 255, 256, 0, 0, 229, 231, 235, 236, 238, - 245, 246, 17, 77, 89, 252, 253, 0, 22, 0, - 0, 326, 0, 0, 98, 99, 0, 0, 101, 0, - 104, 88, 276, 277, 275, 282, 284, 281, 283, 278, - 279, 280, 17, 35, 0, 0, 18, 19, 22, 0, - 20, 0, 0, 0, 0, 103, 0, 0, 0, 0, - 0, 11, 12, 14, 15, 16, 21, 23, 25, 26, - 27, 28, 29, 33, 34, 0, 18, 19, 13, 0, - 20, 0, 24, 0, 30, 0, 0, 31, 32, 0, - 0, 11, 12, 14, 15, 16, 21, 23, 25, 26, - 27, 28, 29, 33, 34, 105, 0, 0, 13, 0, - 0, 0, 24, 173, 30, 0, 0, 31, 32, 0, - 0, 0, 0, 0, 105, 0, 0, 0, 0, 0, - 0, 87, 89, 90, 0, 91, 92, 93, 94, 95, - 96, 97, 98, 99, 100, 0, 101, 102, 104, 88, - 87, 89, 90, 0, 91, 92, 93, 94, 95, 96, - 97, 98, 99, 100, 273, 101, 102, 104, 88, 105, - 0, 272, 0, 103, 0, 276, 277, 275, 282, 284, - 281, 283, 278, 279, 280, 0, 0, 0, 105, 0, - 0, 0, 103, 0, 0, 87, 89, 90, 0, 91, - 92, 93, 0, 95, 96, 97, 98, 99, 100, 0, - 101, 102, 104, 88, 87, 89, 90, 0, 91, 92, - 0, 0, 95, 96, 0, 98, 99, 100, 0, 101, - 102, 104, 88, 0, 0, 0, 0, 103, 0, 0, + 182, 86, 292, 13, 55, 10, 312, 24, 311, 30, + 181, 183, 31, 32, 54, 79, 134, 135, 136, 137, + 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, + 148, 310, 127, 36, 126, 1, 121, 298, 299, 301, + 164, 303, 49, 48, 190, 294, 119, 55, 160, 304, + 305, 193, 55, 160, 117, 196, 223, 54, 159, 122, + 222, 228, 54, 159, 293, 350, 50, 47, 46, 169, + 132, 238, 78, 323, 324, 221, 45, 244, 43, 161, + 328, 322, 168, 333, 334, 335, 130, 332, 171, 177, + 339, 338, 341, 340, 176, 162, 125, 342, 343, 42, + 59, 124, 344, 9, 9, 240, 241, 175, 346, 242, + 131, 8, 41, 40, 123, 37, 51, 255, 351, 191, + 229, 231, 233, 234, 235, 243, 245, 248, 249, 250, + 251, 252, 256, 257, 345, 272, 230, 232, 236, 237, + 239, 246, 247, 85, 189, 55, 253, 254, 53, 77, + 224, 56, 80, 120, 22, 54, 153, 58, 227, 52, + 116, 57, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 228, 0, 0, 0, 0, 75, 0, 0, 0, + 0, 238, 18, 19, 0, 0, 20, 244, 0, 0, + 0, 225, 76, 0, 0, 0, 0, 61, 62, 63, + 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, + 74, 0, 0, 0, 13, 240, 241, 0, 24, 242, + 30, 0, 0, 31, 32, 0, 0, 255, 105, 0, + 229, 231, 233, 234, 235, 243, 245, 248, 249, 250, + 251, 252, 256, 257, 0, 0, 230, 232, 236, 237, + 239, 246, 247, 17, 77, 89, 253, 254, 0, 22, + 0, 0, 327, 0, 0, 98, 99, 0, 0, 101, + 0, 104, 88, 277, 278, 276, 283, 285, 282, 284, + 279, 280, 281, 17, 35, 0, 0, 18, 19, 22, + 0, 20, 0, 0, 0, 0, 103, 0, 0, 0, + 0, 0, 11, 12, 14, 15, 16, 21, 23, 25, + 26, 27, 28, 29, 33, 34, 0, 18, 19, 13, + 0, 20, 0, 24, 0, 30, 0, 0, 31, 32, + 0, 0, 11, 12, 14, 15, 16, 21, 23, 25, + 26, 27, 28, 29, 33, 34, 105, 0, 0, 13, + 0, 0, 0, 24, 174, 30, 0, 0, 31, 32, + 0, 0, 0, 0, 0, 105, 0, 0, 0, 0, + 0, 0, 87, 89, 90, 0, 91, 92, 93, 94, + 95, 96, 97, 98, 99, 100, 0, 101, 102, 104, + 88, 87, 89, 90, 0, 91, 92, 93, 94, 95, + 96, 97, 98, 99, 100, 274, 101, 102, 104, 88, + 105, 0, 273, 0, 103, 0, 277, 278, 276, 283, + 285, 282, 284, 279, 280, 281, 0, 0, 0, 105, + 0, 0, 0, 103, 0, 0, 87, 89, 90, 0, + 91, 92, 93, 0, 95, 96, 97, 98, 99, 100, + 0, 101, 102, 104, 88, 87, 89, 90, 0, 91, + 92, 0, 0, 95, 96, 0, 98, 99, 100, 0, + 101, 102, 104, 88, 0, 0, 0, 0, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 103, + 0, 0, 0, 0, 0, 0, 0, 103, } var yyPact = [...]int16{ - 16, 175, 500, 500, 154, 470, -1000, -1000, -1000, 174, + 16, 168, 501, 501, 155, 471, -1000, -1000, -1000, 153, -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, 273, -1000, 209, -1000, 580, + -1000, -1000, -1000, -1000, -1000, 195, -1000, 229, -1000, 581, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, - -1000, -1000, 22, 155, -1000, -1000, 365, -1000, 365, 165, + -1000, -1000, 22, 99, -1000, -1000, 366, -1000, 366, 125, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, - -1000, -1000, -1000, -1000, -1000, -1000, -1000, 172, -1000, -1000, - 275, -1000, -1000, 261, -1000, 24, -1000, -54, -54, -54, + -1000, -1000, -1000, -1000, -1000, -1000, -1000, 264, -1000, -1000, + 324, -1000, -1000, 260, -1000, 24, -1000, -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, - -54, -54, -54, 37, 43, 292, 155, -57, -1000, 284, - 284, 7, -1000, 561, 35, -1000, 179, -1000, -1000, 191, - 80, -1000, -1000, -1000, 112, -1000, 205, -1000, 268, 365, - -1000, -50, -45, -1000, 365, 365, 365, 365, 365, 365, - 365, 365, 365, 365, 365, 365, 365, 365, 365, -1000, - 107, -1000, -1000, 125, -1000, -1000, -1000, -1000, -1000, -1000, - 99, 99, 270, -1000, -1000, -1000, -1000, 398, -1000, -1000, - 105, -1000, 580, -1000, -1000, 163, -1000, 189, -1000, -1000, - -1000, -1000, -1000, 86, -1000, -1000, -1000, -1000, -1000, 18, - 150, 142, -1000, -1000, -1000, 617, 443, 284, 284, 284, - 284, 35, 35, 46, 46, 46, 644, 625, 46, 46, - 644, 35, 35, 46, 35, 443, -1000, 33, -1000, -1000, - -1000, 262, -1000, 267, -1000, -1000, -1000, -1000, -1000, -1000, + -54, -54, -54, 37, 43, 268, 99, -57, -1000, 297, + 297, 7, -1000, 562, 35, -1000, 317, -1000, -1000, 187, + 80, -1000, -1000, -1000, 120, -1000, 175, -1000, 269, 366, + -1000, -50, -45, -1000, 366, 366, 366, 366, 366, 366, + 366, 366, 366, 366, 366, 366, 366, 366, 366, -1000, + 225, -1000, -1000, 44, -1000, -1000, -1000, -1000, -1000, -1000, + -1000, 107, 107, 284, -1000, -1000, -1000, -1000, 399, -1000, + -1000, 172, -1000, 581, -1000, -1000, 173, -1000, 157, -1000, + -1000, -1000, -1000, -1000, 86, -1000, -1000, -1000, -1000, -1000, + 18, 143, 132, -1000, -1000, -1000, 618, 444, 297, 297, + 297, 297, 35, 35, 46, 46, 46, 645, 626, 46, + 46, 645, 35, 35, 46, 35, 444, -1000, 28, -1000, + -1000, -1000, 273, -1000, 174, -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, 365, -1000, - -1000, -1000, -1000, -1000, -1000, 119, 119, 15, 119, 89, - 89, 41, 42, -1000, -1000, 255, 237, 234, 231, 229, - 227, 207, 199, 196, 190, -1000, -1000, -1000, -1000, -1000, - -1000, 40, -1000, -1000, -1000, 288, -1000, 580, -1000, -1000, - -1000, 119, -1000, 14, 12, 474, -1000, -1000, -1000, 103, - 11, 99, 99, 99, 157, 157, 103, 157, 103, -1000, - -1000, -1000, -1000, -1000, 119, 119, -1000, -1000, -1000, 119, - -1000, -1000, -1000, -1000, -1000, -1000, 99, -1000, -1000, -1000, - -1000, -1000, -1000, -1000, 156, -1000, 31, -1000, -1000, -1000, - -1000, + -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 366, + -1000, -1000, -1000, -1000, -1000, -1000, 32, 32, 15, 32, + 96, 96, 41, 38, -1000, -1000, 255, 232, 230, 208, + 206, 200, 197, 181, 180, 178, -1000, -1000, -1000, -1000, + -1000, -1000, 40, -1000, -1000, -1000, 289, -1000, 581, -1000, + -1000, -1000, 32, -1000, 14, 12, 475, -1000, -1000, -1000, + 11, 152, 107, 107, 107, 104, 104, 11, 104, 11, + -1000, -1000, -1000, -1000, -1000, 32, 32, -1000, -1000, -1000, + 32, -1000, -1000, -1000, -1000, -1000, -1000, 107, -1000, -1000, + -1000, -1000, -1000, -1000, -1000, 103, -1000, 274, -1000, -1000, + -1000, -1000, } var yyPgo = [...]int16{ - 0, 385, 13, 382, 6, 15, 381, 325, 379, 373, - 372, 229, 332, 364, 14, 363, 10, 11, 348, 345, - 8, 344, 3, 4, 343, 2, 1, 0, 342, 12, - 5, 341, 340, 17, 157, 339, 333, 7, 330, 323, - 28, 318, 36, 317, 9, 315, 307, 305, 304, 297, - 296, 292, 288, 262, + 0, 390, 13, 389, 6, 15, 388, 330, 387, 386, + 383, 235, 341, 382, 14, 380, 10, 11, 374, 373, + 8, 365, 3, 4, 364, 2, 1, 0, 349, 12, + 5, 346, 343, 17, 157, 342, 340, 7, 329, 318, + 28, 316, 36, 308, 9, 306, 300, 298, 297, 273, + 272, 296, 265, 263, } var yyR1 = [...]int8{ @@ -540,8 +540,8 @@ var yyR1 = [...]int8{ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 8, - 5, 5, 5, 5, 44, 44, 27, 29, 29, 30, - 30, 26, 25, 25, 48, 10, 17, 17, + 5, 5, 5, 5, 44, 44, 27, 27, 29, 29, + 30, 30, 26, 25, 25, 48, 10, 17, 17, } var yyR2 = [...]int8{ @@ -567,8 +567,8 @@ var yyR2 = [...]int8{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 1, 1, 1, 1, 1, 1, 1, 2, 2, + 1, 1, 1, 2, 1, 1, 1, 0, 1, } var yyChk = [...]int16{ @@ -588,26 +588,26 @@ var yyChk = [...]int16{ -41, -36, -46, 77, -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, -44, 56, 2, -30, -9, 2, -27, -29, 87, 88, 19, - 40, 56, -44, 2, -40, -33, -16, 15, 2, -16, - -39, 22, -37, 22, 20, 7, 2, -5, 2, 4, - 53, 43, 54, -5, 20, -14, 25, 2, -18, 5, - -28, -20, 12, -27, -29, 16, -37, 81, 83, 79, - 80, -37, -37, -37, -37, -37, -37, -37, -37, -37, - -37, -37, -37, -37, -37, -37, -44, 15, -27, -27, - 21, 6, 2, -15, 22, -4, -6, 2, 61, 77, - 62, 78, 63, 64, 65, 79, 80, 12, 81, 46, - 47, 50, 66, 18, 67, 82, 83, 68, 69, 70, - 71, 72, 87, 88, 58, 73, 74, 22, 7, 20, - -2, 25, 2, 25, 2, 26, 26, -29, 26, 40, - 56, -21, 24, 17, -22, 30, 28, 29, 35, 36, - 37, 33, 31, 34, 32, -16, -16, -17, -16, -17, - 22, -44, 21, 2, 22, 7, 2, -37, -26, 19, - -26, 26, -26, -20, -20, 24, 17, 2, 17, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 21, - 2, 22, -4, -26, 26, 26, 17, -22, -25, 56, - -26, -30, -27, -27, -27, -23, 14, -23, -25, -23, - -25, -26, -26, -26, -24, -27, 24, 21, 2, 21, - -27, + 9, 40, 56, -44, 2, -40, -33, -16, 15, 2, + -16, -39, 22, -37, 22, 20, 7, 2, -5, 2, + 4, 53, 43, 54, -5, 20, -14, 25, 2, -18, + 5, -28, -20, 12, -27, -29, 16, -37, 81, 83, + 79, 80, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -44, 15, -27, + -27, 21, 6, 2, -15, 22, -4, -6, 2, 61, + 77, 62, 78, 63, 64, 65, 79, 80, 12, 81, + 46, 47, 50, 66, 18, 67, 82, 83, 68, 69, + 70, 71, 72, 87, 88, 58, 73, 74, 22, 7, + 20, -2, 25, 2, 25, 2, 26, 26, -29, 26, + 40, 56, -21, 24, 17, -22, 30, 28, 29, 35, + 36, 37, 33, 31, 34, 32, -16, -16, -17, -16, + -17, 22, -44, 21, 2, 22, 7, 2, -37, -26, + 19, -26, 26, -26, -20, -20, 24, 17, 2, 17, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 21, 2, 22, -4, -26, 26, 26, 17, -22, -25, + 56, -26, -30, -27, -27, -27, -23, 14, -23, -25, + -23, -25, -26, -26, -26, -24, -27, 24, 21, 2, + 21, -27, } var yyDef = [...]int16{ @@ -616,37 +616,37 @@ var yyDef = [...]int16{ 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 0, 2, -2, 3, 4, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, - 18, 19, 0, 107, 224, 225, 0, 234, 0, 84, + 18, 19, 0, 107, 224, 225, 0, 235, 0, 84, 85, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 218, 219, 0, 5, 99, 0, 127, 130, 0, 135, 136, 140, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 0, 0, 0, 0, 22, 23, 0, 0, 0, 60, 0, 82, 83, 0, 88, 90, 0, - 94, 98, 235, 125, 0, 131, 0, 134, 139, 0, + 94, 98, 236, 125, 0, 131, 0, 134, 139, 0, 42, 47, 48, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, - 0, 69, 70, 0, 72, 229, 230, 73, 74, 226, - 0, 0, 0, 81, 20, 21, 24, 0, 54, 25, - 0, 62, 64, 66, 86, 0, 91, 0, 97, 220, - 221, 222, 223, 0, 126, 129, 132, 133, 138, 141, - 143, 146, 150, 151, 152, 0, 26, 0, 0, -2, - -2, 27, 28, 29, 30, 31, 32, 33, 34, 35, - 36, 37, 38, 39, 40, 41, 68, 0, 227, 228, - 75, 0, 80, 0, 53, 56, 58, 59, 189, 190, - 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, - 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, - 211, 212, 213, 214, 215, 216, 217, 61, 65, 87, - 89, 92, 96, 93, 95, 0, 0, 0, 0, 0, - 0, 0, 0, 156, 158, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 45, 46, 49, 237, 50, - 71, 0, 77, 79, 51, 0, 57, 63, 142, 231, - 144, 0, 147, 0, 0, 0, 154, 159, 155, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, - 78, 52, 55, 145, 0, 0, 153, 157, 160, 0, - 233, 161, 162, 163, 164, 165, 0, 166, 167, 168, - 169, 148, 149, 232, 0, 173, 0, 171, 174, 170, - 172, + 0, 69, 70, 0, 72, 230, 231, 73, 74, 226, + 227, 0, 0, 0, 81, 20, 21, 24, 0, 54, + 25, 0, 62, 64, 66, 86, 0, 91, 0, 97, + 220, 221, 222, 223, 0, 126, 129, 132, 133, 138, + 141, 143, 146, 150, 151, 152, 0, 26, 0, 0, + -2, -2, 27, 28, 29, 30, 31, 32, 33, 34, + 35, 36, 37, 38, 39, 40, 41, 68, 0, 228, + 229, 75, 0, 80, 0, 53, 56, 58, 59, 189, + 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, + 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, + 210, 211, 212, 213, 214, 215, 216, 217, 61, 65, + 87, 89, 92, 96, 93, 95, 0, 0, 0, 0, + 0, 0, 0, 0, 156, 158, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 45, 46, 49, 238, + 50, 71, 0, 77, 79, 51, 0, 57, 63, 142, + 232, 144, 0, 147, 0, 0, 0, 154, 159, 155, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 76, 78, 52, 55, 145, 0, 0, 153, 157, 160, + 0, 234, 161, 162, 163, 164, 165, 0, 166, 167, + 168, 169, 148, 149, 233, 0, 173, 0, 171, 174, + 170, 172, } var yyTok1 = [...]int8{ @@ -1308,7 +1308,7 @@ yydefault: case 69: yyDollar = yyS[yypt-3 : yypt+1] { - yylex.(*parser).unexpected("offset", "integer or duration") + yylex.(*parser).unexpected("offset", "number or duration") yyVAL.node = yyDollar[1].node } case 70: @@ -1386,7 +1386,7 @@ yydefault: case 79: yyDollar = yyS[yypt-5 : yypt+1] { - yylex.(*parser).unexpected("subquery selector", "number/duration or \"]\"") + yylex.(*parser).unexpected("subquery selector", "number or duration or \"]\"") yyVAL.node = yyDollar[1].node } case 80: @@ -1398,7 +1398,7 @@ yydefault: case 81: yyDollar = yyS[yypt-3 : yypt+1] { - yylex.(*parser).unexpected("subquery selector", "number/duration") + yylex.(*parser).unexpected("subquery selector", "number or duration") yyVAL.node = yyDollar[1].node } case 82: @@ -1847,16 +1847,27 @@ yydefault: yyVAL.float = yylex.(*parser).number(yyDollar[1].item.Val) } case 227: - yyDollar = yyS[yypt-2 : yypt+1] + yyDollar = yyS[yypt-1 : yypt+1] { - yyVAL.float = yyDollar[2].float + var err error + var dur time.Duration + dur, err = parseDuration(yyDollar[1].item.Val) + if err != nil { + yylex.(*parser).addParseErr(yyDollar[1].item.PositionRange(), err) + } + yyVAL.float = dur.Seconds() } case 228: + yyDollar = yyS[yypt-2 : yypt+1] + { + yyVAL.float = yyDollar[2].float + } + case 229: yyDollar = yyS[yypt-2 : yypt+1] { yyVAL.float = -yyDollar[2].float } - case 231: + case 232: yyDollar = yyS[yypt-1 : yypt+1] { var err error @@ -1865,17 +1876,17 @@ yydefault: yylex.(*parser).addParseErrf(yyDollar[1].item.PositionRange(), "invalid repetition in series values: %s", err) } } - case 232: + case 233: yyDollar = yyS[yypt-2 : yypt+1] { yyVAL.int = -int64(yyDollar[2].uint) } - case 233: + case 234: yyDollar = yyS[yypt-1 : yypt+1] { yyVAL.int = int64(yyDollar[1].uint) } - case 234: + case 235: yyDollar = yyS[yypt-1 : yypt+1] { yyVAL.node = &StringLiteral{ @@ -1883,7 +1894,7 @@ yydefault: PosRange: yyDollar[1].item.PositionRange(), } } - case 235: + case 236: yyDollar = yyS[yypt-1 : yypt+1] { yyVAL.item = Item{ @@ -1892,7 +1903,7 @@ yydefault: Val: yylex.(*parser).unquoteString(yyDollar[1].item.Val), } } - case 236: + case 237: yyDollar = yyS[yypt-0 : yypt+1] { yyVAL.strings = nil diff --git a/promql/parser/lex.go b/promql/parser/lex.go index 0e06ca525..18abd49ea 100644 --- a/promql/parser/lex.go +++ b/promql/parser/lex.go @@ -969,7 +969,7 @@ func (l *Lexer) scanNumber() bool { // Handle digits at the end since we already consumed before this loop. l.acceptRun(digitPattern) } - // empty string is not a valid number + // Empty string is not a valid number. if l.pos == initialPos { return false } diff --git a/promql/parser/parse_test.go b/promql/parser/parse_test.go index 8ef5775a6..35c5f54a1 100644 --- a/promql/parser/parse_test.go +++ b/promql/parser/parse_test.go @@ -2278,6 +2278,24 @@ var testExpr = []struct { EndPos: 7, }, }, + { + input: `some_metric[5m] @ 1m`, + expected: &MatrixSelector{ + VectorSelector: &VectorSelector{ + Name: "some_metric", + Timestamp: makeInt64Pointer(60000), + LabelMatchers: []*labels.Matcher{ + MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "some_metric"), + }, + PosRange: posrange.PositionRange{ + Start: 0, + End: 11, + }, + }, + Range: 5 * time.Minute, + EndPos: 20, + }, + }, { input: `foo[5mm]`, fail: true, @@ -2335,18 +2353,13 @@ var testExpr = []struct { { input: `some_metric[5m] OFFSET`, fail: true, - errMsg: "unexpected end of input in offset, expected integer or duration", + errMsg: "unexpected end of input in offset, expected number or duration", }, { input: `some_metric OFFSET 1m[5m]`, fail: true, errMsg: "1:22: parse error: no offset modifiers allowed before range", }, - { - input: `some_metric[5m] @ 1m`, - fail: true, - errMsg: "1:19: parse error: unexpected duration \"1m\" in @, expected timestamp", - }, { input: `some_metric[5m] @`, fail: true, @@ -3031,6 +3044,11 @@ var testExpr = []struct { errMsg: "illegal character U+002E '.' in escape sequence", }, // Subquery. + { + input: `foo{bar="baz"}[`, + fail: true, + errMsg: `1:16: parse error: bad number or duration syntax: ""`, + }, { input: `foo{bar="baz"}[10m:6s]`, expected: &SubqueryExpr{ diff --git a/promql/promqltest/testdata/at_modifier.test b/promql/promqltest/testdata/at_modifier.test index e11640ae0..35f90ee67 100644 --- a/promql/promqltest/testdata/at_modifier.test +++ b/promql/promqltest/testdata/at_modifier.test @@ -10,6 +10,14 @@ eval instant at 10s metric @ 100 metric{job="1"} 10 metric{job="2"} 20 +eval instant at 10s metric @ 100s + metric{job="1"} 10 + metric{job="2"} 20 + +eval instant at 10s metric @ 1m40s + metric{job="1"} 10 + metric{job="2"} 20 + eval instant at 10s metric @ 100 offset 50s metric{job="1"} 5 metric{job="2"} 10