From 2e4f5e8cfc966fe29cac87dac337acd70ac3e15c Mon Sep 17 00:00:00 2001 From: ADITYA TIWARI Date: Mon, 15 Dec 2025 12:38:12 +0000 Subject: [PATCH] promql/parser: consolidate label quoting logic refactors binary expression formatting to reuse writeLabels() instead of maintaining separate joinLabels() function. adds comprehensive UTF-8 label tests for all expression types Signed-off-by: ADITYA TIWARI --- promql/parser/printer.go | 32 +++++++++++++++++--------------- promql/parser/printer_test.go | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/promql/parser/printer.go b/promql/parser/printer.go index 67b13eaf12..cca04ae222 100644 --- a/promql/parser/printer.go +++ b/promql/parser/printer.go @@ -145,21 +145,9 @@ func (node *BinaryExpr) ShortString() string { return node.Op.String() + node.returnBool() + node.getMatchingStr() } -// joinLabels joins label names, quoting them if they are not valid legacy label names. -func joinLabels(labels []string) string { - quoted := make([]string, 0, len(labels)) - for _, label := range labels { - if model.LegacyValidation.IsValidLabelName(label) { - quoted = append(quoted, label) - } else { - quoted = append(quoted, strconv.Quote(label)) - } - } - return strings.Join(quoted, ", ") -} - func (node *BinaryExpr) getMatchingStr() string { matching := "" + var b bytes.Buffer vm := node.VectorMatching if vm != nil { if len(vm.MatchingLabels) > 0 || vm.On || vm.Card == CardManyToOne || vm.Card == CardOneToMany { @@ -167,7 +155,14 @@ func (node *BinaryExpr) getMatchingStr() string { if vm.On { vmTag = "on" } - matching = fmt.Sprintf(" %s (%s)", vmTag, joinLabels(vm.MatchingLabels)) + // Use writeLabels() instead of joinLabels() + b.Reset() + b.WriteString(" ") + b.WriteString(vmTag) + b.WriteString(" (") + writeLabels(&b, vm.MatchingLabels) + b.WriteString(")") + matching = b.String() } if vm.Card == CardManyToOne || vm.Card == CardOneToMany { @@ -175,7 +170,14 @@ func (node *BinaryExpr) getMatchingStr() string { if vm.Card == CardManyToOne { vmCard = "left" } - matching += fmt.Sprintf(" group_%s (%s)", vmCard, joinLabels(vm.Include)) + // Use writeLabels() instead of joinLabels() + b.Reset() + b.WriteString(" group_") + b.WriteString(vmCard) + b.WriteString(" (") + writeLabels(&b, vm.Include) + b.WriteString(")") + matching += b.String() } } return matching diff --git a/promql/parser/printer_test.go b/promql/parser/printer_test.go index c9a3cb35e8..31e707ee96 100644 --- a/promql/parser/printer_test.go +++ b/promql/parser/printer_test.go @@ -273,6 +273,14 @@ func TestExprString(t *testing.T) { in: `sum by("üüü") (foo)`, out: `sum by ("üüü") (foo)`, }, + { + in: `sum without("äää") (foo)`, + out: `sum without ("äää") (foo)`, + }, + { + in: `count by("ööö", job) (foo)`, + out: `count by ("ööö", job) (foo)`, + }, } EnableExtendedRangeSelectors = true @@ -410,11 +418,21 @@ func TestBinaryExprUTF8Labels(t *testing.T) { input: `foo / on("äää") bar`, expected: `foo / on ("äää") bar`, }, + { + name: "UTF-8 labels in ignoring clause", + input: `foo / ignoring("üüü") bar`, + expected: `foo / ignoring ("üüü") bar`, + }, { name: "UTF-8 labels in group_left clause", input: `foo / on("äää") group_left("ööö") bar`, expected: `foo / on ("äää") group_left ("ööö") bar`, }, + { + name: "UTF-8 labels in group_right clause", + input: `foo / on("äää") group_right("ööö") bar`, + expected: `foo / on ("äää") group_right ("ööö") bar`, + }, { name: "Mixed legacy and UTF-8 labels", input: `foo / on(legacy, "üüü") bar`,