Fix formatting of range vector selectors with smoothed/anchored modifier

The modifiers were already printed as part of the VectorSelector, so for:

`foo[5m] anchored`

...the output was:

`foo anchored[5m] anchored`

Similar to how it was already done for `@` and `offset`, I now removed these
modifiers in the copy of the vector selector that is used to print the matrix
selector. I also removed some unused code that restored the copy of the vector
selector after overwriting its fields. AFAICS there was no use in doing that,
since it was a copy already that would just be thrown away after printing, and
the original selector wasn't affected. I also removed an erroneous comment in
`atOffset()` where no actual copying took place and no fields were overwritten.

Signed-off-by: Julius Volz <julius.volz@gmail.com>
This commit is contained in:
Julius Volz 2025-10-17 12:17:32 +02:00 committed by György Krajcsovits
parent d06c96136d
commit a1000efe55
No known key found for this signature in database
GPG Key ID: 47A8F9CE80FD7C7F
2 changed files with 38 additions and 8 deletions

View File

@ -229,7 +229,6 @@ func (node *Call) ShortString() string {
}
func (node *MatrixSelector) atOffset() (string, string) {
// Copy the Vector selector before changing the offset
vecSelector := node.VectorSelector.(*VectorSelector)
offset := ""
switch {
@ -254,20 +253,21 @@ func (node *MatrixSelector) atOffset() (string, string) {
func (node *MatrixSelector) String() string {
at, offset := node.atOffset()
// Copy the Vector selector before changing the offset
// Copy the Vector selector so we can modify it to not print @, offset, and other modifiers twice.
vecSelector := *node.VectorSelector.(*VectorSelector)
// Do not print the @ and offset twice.
offsetVal, offsetExprVal, atVal, preproc := vecSelector.OriginalOffset, vecSelector.OriginalOffsetExpr, vecSelector.Timestamp, vecSelector.StartOrEnd
anchored, smoothed := vecSelector.Anchored, vecSelector.Smoothed
vecSelector.OriginalOffset = 0
vecSelector.OriginalOffsetExpr = nil
vecSelector.Timestamp = nil
vecSelector.StartOrEnd = 0
vecSelector.Anchored = false
vecSelector.Smoothed = false
extendedAttribute := ""
switch {
case vecSelector.Anchored:
case anchored:
extendedAttribute = " anchored"
case vecSelector.Smoothed:
case smoothed:
extendedAttribute = " smoothed"
}
rangeStr := model.Duration(node.Range).String()
@ -276,8 +276,6 @@ func (node *MatrixSelector) String() string {
}
str := fmt.Sprintf("%s[%s]%s%s%s", vecSelector.String(), rangeStr, extendedAttribute, at, offset)
vecSelector.OriginalOffset, vecSelector.OriginalOffsetExpr, vecSelector.Timestamp, vecSelector.StartOrEnd = offsetVal, offsetExprVal, atVal, preproc
return str
}

View File

@ -115,6 +115,36 @@ func TestExprString(t *testing.T) {
{
in: `a[1h:5m] offset 1m`,
},
{
in: `a anchored`,
},
{
in: `a[5m] anchored`,
},
{
in: `a{b="c"}[5m] anchored`,
},
{
in: `a{b="c"}[5m] anchored offset 1m`,
},
{
in: `a{b="c"}[5m] anchored @ start() offset 1m`,
},
{
in: `a smoothed`,
},
{
in: `a[5m] smoothed`,
},
{
in: `a{b="c"}[5m] smoothed`,
},
{
in: `a{b="c"}[5m] smoothed offset 1m`,
},
{
in: `a{b="c"}[5m] smoothed @ start() offset 1m`,
},
{
in: `{__name__="a"}`,
},
@ -222,6 +252,8 @@ func TestExprString(t *testing.T) {
},
}
EnableExtendedRangeSelectors = true
for _, test := range inputs {
t.Run(test.in, func(t *testing.T) {
expr, err := ParseExpr(test.in)