diff --git a/reader/traceql/traceql_ir/hints_test.go b/reader/traceql/traceql_ir/hints_test.go index cd863af0..7b702433 100644 --- a/reader/traceql/traceql_ir/hints_test.go +++ b/reader/traceql/traceql_ir/hints_test.go @@ -14,6 +14,7 @@ func TestSampleFraction(t *testing.T) { }{ {``, 0, false}, {` with(sample=0.1)`, 0.1, true}, + {` with(sample=.1)`, 0.1, true}, {` with(exemplars=2, sample=0.25)`, 0.25, true}, {` with(sample=true)`, 0, false}, {` with(sample=false)`, 0, false}, diff --git a/reader/traceql/traceql_ir/lower_test.go b/reader/traceql/traceql_ir/lower_test.go index cde96ce7..b5b04539 100644 --- a/reader/traceql/traceql_ir/lower_test.go +++ b/reader/traceql/traceql_ir/lower_test.go @@ -45,7 +45,8 @@ func TestLowerFilters(t *testing.T) { {`{span.x != 200}`, cmp("span.x", "!=", ValueNumber, "200")}, {`{.a > -3}`, cmp(".a", ">", ValueNumber, "-3")}, {`{.a > - -3}`, cmp(".a", ">", ValueNumber, "3")}, - {`{.a < 1 . 5}`, cmp(".a", "<", ValueNumber, "1.5")}, + {`{.a < 1.5}`, cmp(".a", "<", ValueNumber, "1.5")}, + {`{.a = .95}`, cmp(".a", "=", ValueNumber, ".95")}, {`{duration > 1.5ms}`, cmp("duration", ">", ValueDuration, "1.5ms")}, {`{duration > -1ms}`, cmp("duration", ">", ValueDuration, "-1ms")}, {`{.a = true}`, cmp(".a", "=", ValueBool, "true")}, @@ -328,3 +329,9 @@ func TestHasStructural(t *testing.T) { require.Equal(t, want, mustParse(t, query).HasStructural(), query) } } + +func TestNumberLiteralTypes(t *testing.T) { + for text, want := range map[string]FieldType{`.95`: TypeFloat, `5.`: TypeFloat, `1e3`: TypeFloat, `.5e2`: TypeFloat, `5`: TypeInt} { + require.Equal(t, want, TypeOf(&Static{Kind: ValueNumber, Text: text}), text) + } +} diff --git a/reader/traceql/traceql_parser/lexer_rules v2.go b/reader/traceql/traceql_parser/lexer_rules v2.go index d640659e..78f4d33b 100644 --- a/reader/traceql/traceql_parser/lexer_rules v2.go +++ b/reader/traceql/traceql_parser/lexer_rules v2.go @@ -41,6 +41,9 @@ var TraceQLLexerRulesV2 = []lexer.SimpleRule{ {"UnionSibling", `&~`}, {"Sibling", `~`}, + // A digit right after a dot makes a number. + {"Float", `([0-9]+\.[0-9]*|\.[0-9]+)([eE][-+]?[0-9]+)?|[0-9]+[eE][-+]?[0-9]+`}, + // Label_name supports dotted names and colon intrinsics (span:duration). // Keywords like true/false/nil are matched as Label_name and handled in the grammar. {"Label_name", `(\.[a-zA-Z_][.:a-zA-Z0-9_-]*|[a-zA-Z_][.:a-zA-Z0-9_-]*)`}, @@ -60,8 +63,6 @@ var TraceQLLexerRulesV2 = []lexer.SimpleRule{ {"Div", `/`}, {"Mod", `%`}, {"Pow", `\^`}, - // Float must come before Integer to correctly lex 3.14 as a single token - {"Float", `[0-9]+\.[0-9]+`}, {"Integer", "[0-9]+"}, {"space", `\s+`}, diff --git a/reader/traceql/traceql_parser/lexer_rules_test.go b/reader/traceql/traceql_parser/lexer_rules_test.go new file mode 100644 index 00000000..70f7f8b8 --- /dev/null +++ b/reader/traceql/traceql_parser/lexer_rules_test.go @@ -0,0 +1,64 @@ +package traceql_parser + +import ( + "strings" + "testing" + + "github.com/alecthomas/participle/v2/lexer" + "github.com/stretchr/testify/require" +) + +// lexTokens returns "Rule:value" for each token of q, without whitespace. +func lexTokens(t *testing.T, q string) []string { + t.Helper() + names := map[lexer.TokenType]string{} + for name, tokenType := range TraceQLLexerDefinition.Symbols() { + names[tokenType] = name + } + lex, err := TraceQLLexerDefinition.Lex("", strings.NewReader(q)) + require.NoError(t, err) + tokens, err := lexer.ConsumeAll(lex) + require.NoError(t, err) + var got []string + for _, tok := range tokens { + if tok.EOF() || names[tok.Type] == "space" { + continue + } + got = append(got, names[tok.Type]+":"+tok.Value) + } + return got +} + +func TestLexNumbersAndAttributes(t *testing.T) { + for _, tc := range []struct { + query string + want []string + }{ + {`.95`, []string{"Float:.95"}}, + {`.5`, []string{"Float:.5"}}, + {`5.`, []string{"Float:5."}}, + {`1.5`, []string{"Float:1.5"}}, + {`1e3`, []string{"Float:1e3"}}, + {`1E+3`, []string{"Float:1E+3"}}, + {`1.5e-3`, []string{"Float:1.5e-3"}}, + {`.5e2`, []string{"Float:.5e2"}}, + {`5`, []string{"Integer:5"}}, + {`-.5`, []string{"Minus:-", "Float:.5"}}, + {`.5s`, []string{"Float:.5", "Label_name:s"}}, + {`5.s`, []string{"Float:5.", "Label_name:s"}}, + {`.5xx`, []string{"Float:.5", "Label_name:xx"}}, + {`.5.5`, []string{"Float:.5", "Float:.5"}}, + {`1.2.3`, []string{"Float:1.2", "Float:.3"}}, + {`1e`, []string{"Integer:1", "Label_name:e"}}, + {`5 . 5`, []string{"Integer:5", "Dot:.", "Integer:5"}}, + {`.foo.95`, []string{"Label_name:.foo.95"}}, + {`span.5xx`, []string{"Label_name:span.5xx"}}, + {`.foo-.5`, []string{"Label_name:.foo-.5"}}, + {`.foo - .5`, []string{"Label_name:.foo", "Minus:-", "Float:.5"}}, + {`.foo * .5`, []string{"Label_name:.foo", "Mul:*", "Float:.5"}}, + } { + t.Run(tc.query, func(t *testing.T) { + require.Equal(t, tc.want, lexTokens(t, tc.query)) + }) + } +} diff --git a/reader/traceql/traceql_parser/model_v2.go b/reader/traceql/traceql_parser/model_v2.go index 2697c961..8ed35dda 100644 --- a/reader/traceql/traceql_parser/model_v2.go +++ b/reader/traceql/traceql_parser/model_v2.go @@ -252,7 +252,7 @@ func (a Aggregate) String() string { // ScalarLiteral is a number, with its duration unit if any. type ScalarLiteral struct { - Num string `@Minus? (@Float | @Integer @Dot? @Integer?)` + Num string `@Minus? (@Float | @Integer)` Unit string `@("ns"|"us"|"ms"|"s"|"m"|"h"|"d")?` } @@ -314,7 +314,7 @@ func (a FieldAtom) String() string { // NumberLiteral is an unsigned number, with its duration unit if any. type NumberLiteral struct { - Num string `(@Float | @Integer @Dot? @Integer?)` + Num string `(@Float | @Integer)` Unit string `@("ns"|"us"|"ms"|"s"|"m"|"h"|"d")?` } diff --git a/reader/traceql/traceql_parser/parser_test.go b/reader/traceql/traceql_parser/parser_test.go index 4d9dcdcb..f6cca56c 100644 --- a/reader/traceql/traceql_parser/parser_test.go +++ b/reader/traceql/traceql_parser/parser_test.go @@ -113,7 +113,17 @@ func TestParseSelectors(t *testing.T) { {`{.a !~ "x"}`, term(".a", "!~", str(`"x"`))}, {`{.a > -3}`, term(".a", ">", unary("-", num("3")))}, {`{.a >= 1.5}`, term(".a", ">=", num("1.5"))}, - {`{.a < 1 . 5}`, term(".a", "<", num("1.5"))}, + {`{.a = .95}`, term(".a", "=", num(".95"))}, + {`{.a = .5}`, term(".a", "=", num(".5"))}, + {`{.a = 5.}`, term(".a", "=", num("5."))}, + {`{.a = 1e3}`, term(".a", "=", num("1e3"))}, + {`{.a = 1.5e-3}`, term(".a", "=", num("1.5e-3"))}, + {`{.a = .5e2}`, term(".a", "=", num(".5e2"))}, + {`{.a = -.5}`, term(".a", "=", unary("-", num(".5")))}, + {`{duration > .5s}`, term("duration", ">", dur(".5", "s"))}, + {`{duration > 5.s}`, term("duration", ">", dur("5.", "s"))}, + {`{.foo.95 = 1}`, term(".foo.95", "=", num("1"))}, + {`{span.5xx = 1}`, term("span.5xx", "=", num("1"))}, {`{.a <= 0}`, term(".a", "<=", num("0"))}, {`{duration > 1.5ms}`, term("duration", ">", dur("1.5", "ms"))}, {`{span:duration < 2s}`, term("span:duration", "<", dur("2", "s"))}, @@ -152,6 +162,10 @@ func TestParseFieldExpressions(t *testing.T) { {`{.a - .b > 3}`, field(name(".a"), "-", name(".b"), ">", num("3"))}, {`{.a -.b > 3}`, field(name(".a"), "-", name(".b"), ">", num("3"))}, {`{.a-1 = 2}`, term(".a-1", "=", num("2"))}, + {`{.foo-.5 = 2}`, term(".foo-.5", "=", num("2"))}, + {`{.foo - .5 > 1}`, field(name(".foo"), "-", num(".5"), ">", num("1"))}, + {`{ .95 = 1 }`, field(num(".95"), "=", num("1"))}, + {`{.foo * .5 > 1}`, field(name(".foo"), "*", num(".5"), ">", num("1"))}, {`{.a + .b * .c / .d % .e ^ .f = 1}`, field(name(".a"), "+", name(".b"), "*", name(".c"), "/", name(".d"), "%", name(".e"), "^", name(".f"), "=", num("1"))}, {`{.a+.b*2>3}`, field(name(".a"), "+", name(".b"), "*", num("2"), ">", num("3"))}, @@ -179,7 +193,11 @@ func TestParseFieldExpressions(t *testing.T) { require.Equal(t, script(expr(sel(nil)), filter(scalar(ScalarAtom{Aggregate: &Aggregate{Fn: "avg", Arg: arg}}), ">", lit("2", ""))), got) }) - for _, q := range []string{`{.a = }`, `{.a + }`, `{!}`, `{.a = 1 !}`, `{.a ** 2}`, `{.a = ()}`} { + for _, q := range []string{ + `{.a = }`, `{.a + }`, `{!}`, `{.a = 1 !}`, `{.a ** 2}`, `{.a = ()}`, + `{ .5xx = 1 }`, `{ .a = .5xx }`, `{ .a = .5.5 }`, `{ .a = 1.2.3 }`, `{ .a = 1e }`, `{ .a = .5x }`, + `{ .a = 0x10 }`, `{ .a = 1_000 }`, `{ .a < 1 . 5 }`, `{ .a = . }`, + } { t.Run("rejects "+q, func(t *testing.T) { _, err := Parse(q) require.Error(t, err) @@ -290,7 +308,10 @@ func TestParseScalarFilters(t *testing.T) { {`{} | max(span.x) < -3`, script(expr(sel(nil)), filter(agg("max", "span.x"), "<", lit("-3", "")))}, {`{} | min(.x) != 0`, script(expr(sel(nil)), filter(agg("min", ".x"), "!=", lit("0", "")))}, {`{} | sum(resource.x) <= 10s`, script(expr(sel(nil)), filter(agg("sum", "resource.x"), "<=", lit("10", "s")))}, - {`{} | avg(span:duration) > 1 . 5`, script(expr(sel(nil)), filter(agg("avg", "span:duration"), ">", lit("1.5", "")))}, + {`{} | avg(span:duration) > 1.5`, script(expr(sel(nil)), filter(agg("avg", "span:duration"), ">", lit("1.5", "")))}, + {`{} | avg(.foo) > .5`, script(expr(sel(nil)), filter(agg("avg", ".foo"), ">", lit(".5", "")))}, + {`{} | count() > -.5`, script(expr(sel(nil)), filter(agg("count", ""), ">", lit("-.5", "")))}, + {`{} | avg(duration) > .5s`, script(expr(sel(nil)), filter(agg("avg", "duration"), ">", lit(".5", "s")))}, // A scalar filter applies to the whole expression before it. {`{.a = "b"} && {} | count() > 1`, script(expr(sel(a), then("&&", sel(nil))), filter(agg("count", ""), ">", lit("1", "")))}, @@ -342,6 +363,8 @@ func TestParseScalarFilters(t *testing.T) { `{} | count(.a) > 2`, `{} | avg() > 2`, `{} | count() > 2 | count() >`, + `{} | count() > 5 . 5`, + `{} | count() > .5x`, } { t.Run("rejects "+q, func(t *testing.T) { _, err := Parse(q) @@ -468,6 +491,8 @@ func TestParseMetricsFunctions(t *testing.T) { &MetricsPipelineStage{Fn: "count_over_time", By: &ByClause{Labels: []string{"span.x", "name"}}})}, {`{} | quantile_over_time(duration, 0.9)`, metrics(empty(), &MetricsPipelineStage{Fn: "quantile_over_time", Attr: "duration", Percentile: f64(0.9)})}, + {`{} | quantile_over_time(duration, .95)`, metrics(empty(), + &MetricsPipelineStage{Fn: "quantile_over_time", Attr: "duration", Percentile: f64(0.95)})}, {`{} | histogram_over_time(duration)`, metrics(empty(), &MetricsPipelineStage{Fn: "histogram_over_time", Attr: "duration"})}, {`{} | compare({.a = "b"}, 10, 1, 2)`, metrics(empty(), &MetricsPipelineStage{Fn: "compare", CompSel: &CompareSelector{ @@ -517,6 +542,9 @@ func TestParseWithHintsAndSecondStage(t *testing.T) { {`{} | rate() with(sample=0.1, exemplars=false)`, rate(&TraceQLScript{ WithHints: &WithClause{Hints: []WithHint{{Key: "sample", Value: "0.1"}, {Key: "exemplars", Value: "false"}}}, })}, + {`{} | rate() with(sample=.1)`, rate(&TraceQLScript{ + WithHints: &WithClause{Hints: []WithHint{{Key: "sample", Value: ".1"}}}, + })}, {`{} | rate() with(sample=true, exemplars=5, most_recent=x)`, rate(&TraceQLScript{ WithHints: &WithClause{Hints: []WithHint{ {Key: "sample", Value: "true"}, {Key: "exemplars", Value: "5"}, {Key: "most_recent", Value: "x"}, diff --git a/reader/traceql/traceql_transpiler/clickhouse_transpiler/search_plan_test.go b/reader/traceql/traceql_transpiler/clickhouse_transpiler/search_plan_test.go index b729d7bc..b54c4191 100644 --- a/reader/traceql/traceql_transpiler/clickhouse_transpiler/search_plan_test.go +++ b/reader/traceql/traceql_transpiler/clickhouse_transpiler/search_plan_test.go @@ -236,3 +236,21 @@ func TestSearchReadsDistributedTablesOnClusters(t *testing.T) { }) } } + +func TestSearchRendersNumberLiterals(t *testing.T) { + for _, tc := range []struct{ query, want string }{ + {`{ .foo = .95 }`, `0.950000`}, + {`{ .foo = -.5 }`, `-0.500000`}, + {`{ .foo = 5. }`, `5.000000`}, + {`{ .foo = 1e3 }`, `1000.000000`}, + {`{ duration > .5s }`, `duration) > (500000000)`}, + {`{ duration > 5.s }`, `duration) > (5000000000)`}, + {`{} | count() > -.5`, `-0.5`}, + } { + t.Run(tc.query, func(t *testing.T) { + if res := renderPlan(t, planSearch(t, tc.query), testPlannerContext(7)); !strings.Contains(res, tc.want) { + t.Errorf("want %s in %s", tc.want, res) + } + }) + } +}