From 5ea79f65c326b6504e5247085288ffabfe03e1f5 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 1 Sep 2023 11:28:54 +0200 Subject: [PATCH] vendor: return back the working github.com/VictoriaMetrics/metricsql release after 4a338d32ceb648dd3728a4b398473727f1eb0d40 --- go.mod | 2 +- go.sum | 7 +- .../VictoriaMetrics/metricsql/lexer.go | 19 +- .../VictoriaMetrics/metricsql/optimizer.go | 24 +- .../VictoriaMetrics/metricsql/parser.go | 742 +++++------------- .../VictoriaMetrics/metricsql/prettifier.go | 201 ----- .../VictoriaMetrics/metricsql/rollup.go | 1 - vendor/modules.txt | 2 +- 8 files changed, 195 insertions(+), 803 deletions(-) delete mode 100644 vendor/github.com/VictoriaMetrics/metricsql/prettifier.go diff --git a/go.mod b/go.mod index 7b0b86629..b9f3549d4 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( // like https://github.com/valyala/fasthttp/commit/996610f021ff45fdc98c2ce7884d5fa4e7f9199b github.com/VictoriaMetrics/fasthttp v1.2.0 github.com/VictoriaMetrics/metrics v1.24.0 - github.com/VictoriaMetrics/metricsql v0.64.0 + github.com/VictoriaMetrics/metricsql v0.56.2 github.com/aws/aws-sdk-go-v2 v1.21.0 github.com/aws/aws-sdk-go-v2/config v1.18.38 github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.82 diff --git a/go.sum b/go.sum index a5bee3901..f9ae6b983 100644 --- a/go.sum +++ b/go.sum @@ -68,10 +68,11 @@ github.com/VictoriaMetrics/fastcache v1.12.1 h1:i0mICQuojGDL3KblA7wUNlY5lOK6a4bw github.com/VictoriaMetrics/fastcache v1.12.1/go.mod h1:tX04vaqcNoQeGLD+ra5pU5sWkuxnzWhEzLwhP9w653o= github.com/VictoriaMetrics/fasthttp v1.2.0 h1:nd9Wng4DlNtaI27WlYh5mGXCJOmee/2c2blTJwfyU9I= github.com/VictoriaMetrics/fasthttp v1.2.0/go.mod h1:zv5YSmasAoSyv8sBVexfArzFDIGGTN4TfCKAtAw7IfE= +github.com/VictoriaMetrics/metrics v1.18.1/go.mod h1:ArjwVz7WpgpegX/JpB0zpNF2h2232kErkEnzH1sxMmA= github.com/VictoriaMetrics/metrics v1.24.0 h1:ILavebReOjYctAGY5QU2F9X0MYvkcrG3aEn2RKa1Zkw= github.com/VictoriaMetrics/metrics v1.24.0/go.mod h1:eFT25kvsTidQFHb6U0oa0rTrDRdz4xTYjpL8+UPohys= -github.com/VictoriaMetrics/metricsql v0.64.0 h1:uty6AXQFY3OpQ+eopo1jDjCcTctuqkqYLnRbQVhukW8= -github.com/VictoriaMetrics/metricsql v0.64.0/go.mod h1:k4UaP/+CjuZslIjd+kCigNG9TQmUqh5v0TP/nMEy90I= +github.com/VictoriaMetrics/metricsql v0.56.2 h1:quBAbYOlWMhmdgzFSCr1yjtVcdZYZrVQJ7nR9zor7ZM= +github.com/VictoriaMetrics/metricsql v0.56.2/go.mod h1:6pP1ZeLVJHqJrHlF6Ij3gmpQIznSsgktEcZgsAWYel0= github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -633,8 +634,6 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/vendor/github.com/VictoriaMetrics/metricsql/lexer.go b/vendor/github.com/VictoriaMetrics/metricsql/lexer.go index da6a47e68..f0dec0a82 100644 --- a/vendor/github.com/VictoriaMetrics/metricsql/lexer.go +++ b/vendor/github.com/VictoriaMetrics/metricsql/lexer.go @@ -37,11 +37,6 @@ func (lex *lexer) Init(s string) { lex.sTail = s } -func (lex *lexer) PushBack(currToken, sHead string) { - lex.Token = currToken - lex.sTail = sHead + lex.sTail -} - func (lex *lexer) Next() error { if lex.err != nil { return lex.err @@ -233,7 +228,7 @@ func scanPositiveNumber(s string) (string, error) { } return s[:i], nil } - for i < len(s) && isDecimalCharOrUnderscore(s[i]) { + for i < len(s) && isDecimalChar(s[i]) { i++ } @@ -258,7 +253,7 @@ func scanPositiveNumber(s string) (string, error) { // Scan fractional part. It cannot be empty. i++ j := i - for j < len(s) && isDecimalCharOrUnderscore(s[j]) { + for j < len(s) && isDecimalChar(s[j]) { j++ } i = j @@ -566,8 +561,10 @@ func DurationValue(s string, step int64) (int64, error) { func parseSingleDuration(s string, step int64) (float64, error) { s = strings.ToLower(s) numPart := s[:len(s)-1] - // Strip trailing m if the duration is in ms - numPart = strings.TrimSuffix(numPart, "m") + if strings.HasSuffix(numPart, "m") { + // Duration in ms + numPart = numPart[:len(numPart)-1] + } f, err := strconv.ParseFloat(numPart, 64) if err != nil { return 0, fmt.Errorf("cannot parse duration %q: %s", s, err) @@ -673,10 +670,6 @@ func isDecimalChar(ch byte) bool { return ch >= '0' && ch <= '9' } -func isDecimalCharOrUnderscore(ch byte) bool { - return isDecimalChar(ch) || ch == '_' -} - func isHexChar(ch byte) bool { return isDecimalChar(ch) || ch >= 'a' && ch <= 'f' || ch >= 'A' && ch <= 'F' } diff --git a/vendor/github.com/VictoriaMetrics/metricsql/optimizer.go b/vendor/github.com/VictoriaMetrics/metricsql/optimizer.go index 8072667cd..ff5ad3524 100644 --- a/vendor/github.com/VictoriaMetrics/metricsql/optimizer.go +++ b/vendor/github.com/VictoriaMetrics/metricsql/optimizer.go @@ -78,7 +78,7 @@ func optimizeInplace(e Expr) { func getCommonLabelFilters(e Expr) []LabelFilter { switch t := e.(type) { case *MetricExpr: - return getCommonLabelFiltersWithoutMetricName(t.LabelFilterss) + return getLabelFiltersWithoutMetricName(t.LabelFilters) case *RollupExpr: return getCommonLabelFilters(t.Expr) case *FuncExpr: @@ -180,21 +180,6 @@ func TrimFiltersByGroupModifier(lfs []LabelFilter, be *BinaryOpExpr) []LabelFilt } } -func getCommonLabelFiltersWithoutMetricName(lfss [][]LabelFilter) []LabelFilter { - if len(lfss) == 0 { - return nil - } - lfsA := getLabelFiltersWithoutMetricName(lfss[0]) - for _, lfs := range lfss[1:] { - if len(lfsA) == 0 { - return nil - } - lfsB := getLabelFiltersWithoutMetricName(lfs) - lfsA = intersectLabelFilters(lfsA, lfsB) - } - return lfsA -} - func getLabelFiltersWithoutMetricName(lfs []LabelFilter) []LabelFilter { lfsNew := make([]LabelFilter, 0, len(lfs)) for _, lf := range lfs { @@ -228,11 +213,8 @@ func pushdownBinaryOpFiltersInplace(e Expr, lfs []LabelFilter) { } switch t := e.(type) { case *MetricExpr: - for i, lfsLocal := range t.LabelFilterss { - lfsLocal = unionLabelFilters(lfsLocal, lfs) - sortLabelFilters(lfsLocal) - t.LabelFilterss[i] = lfsLocal - } + t.LabelFilters = unionLabelFilters(t.LabelFilters, lfs) + sortLabelFilters(t.LabelFilters) case *RollupExpr: pushdownBinaryOpFiltersInplace(t.Expr, lfs) case *FuncExpr: diff --git a/vendor/github.com/VictoriaMetrics/metricsql/parser.go b/vendor/github.com/VictoriaMetrics/metricsql/parser.go index bc3f83e7f..57f63bcb0 100644 --- a/vendor/github.com/VictoriaMetrics/metricsql/parser.go +++ b/vendor/github.com/VictoriaMetrics/metricsql/parser.go @@ -311,7 +311,7 @@ func (p *parser) parseWithArgExpr() (*withArgExpr, error) { } if p.lex.Token == "(" { // Parse func args. - args, err := p.parseIdentList(false) + args, err := p.parseIdentList() if err != nil { return nil, fmt.Errorf(`withArgExpr: cannot parse args for %q: %s`, wa.Name, err) } @@ -365,26 +365,16 @@ func (p *parser) parseExpr() (Expr, error) { } } if isBinaryOpGroupModifier(p.lex.Token) { - if err := p.parseModifierExpr(&be.GroupModifier, false); err != nil { + if err := p.parseModifierExpr(&be.GroupModifier); err != nil { return nil, err } if isBinaryOpJoinModifier(p.lex.Token) { if isBinaryOpLogicalSet(be.Op) { return nil, fmt.Errorf(`modifier %q cannot be applied to %q`, p.lex.Token, be.Op) } - if err := p.parseModifierExpr(&be.JoinModifier, true); err != nil { + if err := p.parseModifierExpr(&be.JoinModifier); err != nil { return nil, err } - if isPrefixModifier(p.lex.Token) { - if err := p.lex.Next(); err != nil { - return nil, fmt.Errorf("cannot read prefix for %s: %w", be.JoinModifier.AppendString(nil), err) - } - se, err := p.parseStringExpr() - if err != nil { - return nil, fmt.Errorf("cannot parse prefix for %s: %w", be.JoinModifier.AppendString(nil), err) - } - be.JoinModifierPrefix = se - } } } e2, err := p.parseSingleExpr() @@ -392,12 +382,6 @@ func (p *parser) parseExpr() (Expr, error) { return nil, err } be.Right = e2 - if isKeepMetricNames(p.lex.Token) { - be.KeepMetricNames = true - if err := p.lex.Next(); err != nil { - return nil, err - } - } e = balanceBinaryOp(&be) } } @@ -583,14 +567,6 @@ func (p *parser) parseParensExpr() (*parensExpr, error) { if err := p.lex.Next(); err != nil { return nil, err } - if len(exprs) == 1 { - if be, ok := exprs[0].(*BinaryOpExpr); ok && isKeepMetricNames(p.lex.Token) { - if err := p.lex.Next(); err != nil { - return nil, err - } - be.KeepMetricNames = true - } - } pe := parensExpr(exprs) return &pe, nil } @@ -618,7 +594,7 @@ funcPrefixLabel: if !isAggrFuncModifier(p.lex.Token) { return nil, fmt.Errorf(`AggrFuncExpr: unexpected token %q; want aggregate func modifier`, p.lex.Token) } - if err := p.parseModifierExpr(&ae.Modifier, false); err != nil { + if err := p.parseModifierExpr(&ae.Modifier); err != nil { return nil, err } } @@ -633,7 +609,7 @@ funcArgsLabel: // Verify whether func suffix exists. if ae.Modifier.Op == "" && isAggrFuncModifier(p.lex.Token) { - if err := p.parseModifierExpr(&ae.Modifier, false); err != nil { + if err := p.parseModifierExpr(&ae.Modifier); err != nil { return nil, err } } @@ -675,18 +651,6 @@ func expandWithExpr(was []*withArgExpr, e Expr) (Expr, error) { if err != nil { return nil, err } - var joinModifierPrefix *StringExpr - if t.JoinModifierPrefix != nil { - jmp, err := expandWithExpr(was, t.JoinModifierPrefix) - if err != nil { - return nil, err - } - se, ok := jmp.(*StringExpr) - if !ok { - return nil, fmt.Errorf("unexpected prefix for %s; want quoted string; got %s", t.JoinModifier.AppendString(nil), jmp.AppendString(nil)) - } - joinModifierPrefix = se - } if t.Op == "+" { lse, lok := left.(*StringExpr) rse, rok := right.(*StringExpr) @@ -702,7 +666,6 @@ func expandWithExpr(was []*withArgExpr, e Expr) (Expr, error) { be.Right = right be.GroupModifier.Args = groupModifierArgs be.JoinModifier.Args = joinModifierArgs - be.JoinModifierPrefix = joinModifierPrefix pe := parensExpr{&be} return &pe, nil case *FuncExpr: @@ -784,18 +747,6 @@ func expandWithExpr(was []*withArgExpr, e Expr) (Expr, error) { } re := *t re.Expr = eNew - re.Window, err = expandDuration(was, re.Window) - if err != nil { - return nil, fmt.Errorf("cannot parse window for %s: %w", re.Expr.AppendString(nil), err) - } - re.Step, err = expandDuration(was, re.Step) - if err != nil { - return nil, fmt.Errorf("cannot parse step in %s: %w", re.Expr.AppendString(nil), err) - } - re.Offset, err = expandDuration(was, re.Offset) - if err != nil { - return nil, fmt.Errorf("cannot parse offset in %s: %w", re.Expr.AppendString(nil), err) - } if t.At != nil { atNew, err := expandWithExpr(was, t.At) if err != nil { @@ -814,71 +765,59 @@ func expandWithExpr(was []*withArgExpr, e Expr) (Expr, error) { } return eNew, nil case *MetricExpr: - if len(t.labelFilterss) == 0 { + if len(t.LabelFilters) > 0 { // Already expanded. return t, nil } { var me MetricExpr - // Populate me.LabelFilterss - for _, lfes := range t.labelFilterss { - var lfsNew []LabelFilter - for _, lfe := range lfes { - if lfe.Value == nil { - // Expand lfe.Label into lfsNew. - wa := getWithArgExpr(was, lfe.Label) - if wa == nil { - return nil, fmt.Errorf("cannot find WITH template for %q inside %q", lfe.Label, t.AppendString(nil)) - } - eNew, err := expandWithExprExt(was, wa, []Expr{}) - if err != nil { - return nil, err - } - wme, ok := eNew.(*MetricExpr) - if !ok || wme.getMetricName() != "" { - return nil, fmt.Errorf("WITH template %q inside %q must be {...}; got %q", - lfe.Label, t.AppendString(nil), eNew.AppendString(nil)) - } - if len(wme.labelFilterss) > 0 { - panic(fmt.Errorf("BUG: wme.labelFilterss must be empty after WITH template expansion; got %s", wme.AppendString(nil))) - } - lfssSrc := wme.LabelFilterss - if len(lfssSrc) > 1 { - return nil, fmt.Errorf("WITH template %q at %q must be {...} without 'or'; got %s", - lfe.Label, t.AppendString(nil), wme.AppendString(nil)) - } - if len(lfssSrc) == 1 { - lfsNew = append(lfsNew, lfssSrc[0]...) - } - continue + // Populate me.LabelFilters + for _, lfe := range t.labelFilters { + if lfe.Value == nil { + // Expand lfe.Label into []LabelFilter. + wa := getWithArgExpr(was, lfe.Label) + if wa == nil { + return nil, fmt.Errorf("missing %q value inside %q", lfe.Label, t.AppendString(nil)) } - - // convert lfe to LabelFilter. - se, err := expandWithExpr(was, lfe.Value) + eNew, err := expandWithExprExt(was, wa, nil) if err != nil { return nil, err } - var lfeNew labelFilterExpr - lfeNew.Label = lfe.Label - lfeNew.Value = se.(*StringExpr) - lfeNew.IsNegative = lfe.IsNegative - lfeNew.IsRegexp = lfe.IsRegexp - lf, err := lfeNew.toLabelFilter() - if err != nil { - return nil, err + wme, ok := eNew.(*MetricExpr) + if !ok || wme.hasNonEmptyMetricGroup() { + return nil, fmt.Errorf("%q must be filters expression inside %q; got %q", lfe.Label, t.AppendString(nil), eNew.AppendString(nil)) } - lfsNew = append(lfsNew, *lf) + if len(wme.labelFilters) > 0 { + panic(fmt.Errorf("BUG: wme.labelFilters must be empty; got %s", wme.labelFilters)) + } + me.LabelFilters = append(me.LabelFilters, wme.LabelFilters...) + continue } - lfsNew = removeDuplicateLabelFilters(lfsNew) - me.LabelFilterss = append(me.LabelFilterss, lfsNew) + + // convert lfe to LabelFilter. + se, err := expandWithExpr(was, lfe.Value) + if err != nil { + return nil, err + } + var lfeNew labelFilterExpr + lfeNew.Label = lfe.Label + lfeNew.Value = se.(*StringExpr) + lfeNew.IsNegative = lfe.IsNegative + lfeNew.IsRegexp = lfe.IsRegexp + lf, err := lfeNew.toLabelFilter() + if err != nil { + return nil, err + } + me.LabelFilters = append(me.LabelFilters, *lf) } + me.LabelFilters = removeDuplicateLabelFilters(me.LabelFilters) t = &me } - metricName := t.getMetricName() - if metricName == "" { + if !t.hasNonEmptyMetricGroup() { return t, nil } - wa := getWithArgExpr(was, metricName) + k := t.LabelFilters[0].Value + wa := getWithArgExpr(was, k) if wa == nil { return t, nil } @@ -894,51 +833,25 @@ func expandWithExpr(was []*withArgExpr, e Expr) (Expr, error) { wme, _ = eNew.(*MetricExpr) } if wme == nil { - if t.isOnlyMetricName() { - return eNew, nil + if !t.isOnlyMetricGroup() { + return nil, fmt.Errorf("cannot expand %q to non-metric expression %q", t.AppendString(nil), eNew.AppendString(nil)) } - return nil, fmt.Errorf("cannot expand %q to non-metric expression %q", t.AppendString(nil), eNew.AppendString(nil)) + return eNew, nil } - if len(wme.labelFilterss) > 0 { - panic(fmt.Errorf("BUG: wme.labelFilterss must be empty after WITH templates expansion; got %s", wme.AppendString(nil))) - } - lfssSrc := wme.LabelFilterss - var lfssNew [][]LabelFilter - if len(lfssSrc) != 1 { - // template_name{filters} where template_name is {... or ...} - if t.isOnlyMetricName() { - // {filters} is empty. Return {... or ...} - return eNew, nil - } - if len(t.LabelFilterss) != 1 { - // {filters} contain {... or ...}. It cannot be merged with {... or ...} - return nil, fmt.Errorf("%q mustn't contain 'or' filters; got %s", metricName, wme.AppendString(nil)) - } - // {filters} doesn't contain `or`. Merge it with {... or ...} into {...,filters or ...,filters} - for _, lfs := range lfssSrc { - lfsNew := append([]LabelFilter{}, lfs...) - lfsNew = append(lfsNew, t.LabelFilterss[0][1:]...) - lfsNew = removeDuplicateLabelFilters(lfsNew) - lfssNew = append(lfssNew, lfsNew) - } - } else { - // template_name{... or ...} where template_name is an ordinary {filters} without 'or'. - // Merge it into {filters,... or filters,...} - for _, lfs := range t.LabelFilterss { - lfsNew := append([]LabelFilter{}, lfssSrc[0]...) - lfsNew = append(lfsNew, lfs[1:]...) - lfsNew = removeDuplicateLabelFilters(lfsNew) - lfssNew = append(lfssNew, lfsNew) - } - } - me := &MetricExpr{ - LabelFilterss: lfssNew, + if len(wme.labelFilters) > 0 { + panic(fmt.Errorf("BUG: wme.labelFilters must be empty; got %s", wme.labelFilters)) } + + var me MetricExpr + me.LabelFilters = append(me.LabelFilters, wme.LabelFilters...) + me.LabelFilters = append(me.LabelFilters, t.LabelFilters[1:]...) + me.LabelFilters = removeDuplicateLabelFilters(me.LabelFilters) + if re == nil { - return me, nil + return &me, nil } reNew := *re - reNew.Expr = me + reNew.Expr = &me return &reNew, nil default: return e, nil @@ -957,38 +870,6 @@ func expandWithArgs(was []*withArgExpr, args []Expr) ([]Expr, error) { return dstArgs, nil } -func expandDuration(was []*withArgExpr, d *DurationExpr) (*DurationExpr, error) { - if d == nil { - return nil, nil - } - if !d.needsParsing { - return d, nil - } - wa := getWithArgExpr(was, d.s) - if wa == nil { - return nil, fmt.Errorf("cannot find WITH template for %q", d.s) - } - e, err := expandWithExprExt(was, wa, []Expr{}) - if err != nil { - return nil, err - } - switch t := e.(type) { - case *DurationExpr: - if t.needsParsing { - panic(fmt.Errorf("BUG: DurationExpr %q must be already parsed", t.s)) - } - return t, nil - case *NumberExpr: - // Convert number of seconds to DurationExpr - de := &DurationExpr{ - s: t.s, - } - return de, nil - default: - return nil, fmt.Errorf("unexpected value for WITH template %q; got %s; want duration", d.s, e.AppendString(nil)) - } -} - func expandModifierArgs(was []*withArgExpr, args []string) ([]string, error) { if len(args) == 0 { return nil, nil @@ -1008,22 +889,22 @@ func expandModifierArgs(was []*withArgExpr, args []string) ([]string, error) { } me, ok := wa.Expr.(*MetricExpr) if ok { - if !me.isOnlyMetricName() { + if !me.isOnlyMetricGroup() { return nil, fmt.Errorf("cannot use %q instead of %q in %s", me.AppendString(nil), arg, args) } - metricName := me.getMetricName() - dstArgs = append(dstArgs, metricName) + dstArg := me.LabelFilters[0].Value + dstArgs = append(dstArgs, dstArg) continue } pe, ok := wa.Expr.(*parensExpr) if ok { for _, pArg := range *pe { me, ok := pArg.(*MetricExpr) - if !ok || !me.isOnlyMetricName() { + if !ok || !me.isOnlyMetricGroup() { return nil, fmt.Errorf("cannot use %q instead of %q in %s", pe.AppendString(nil), arg, args) } - metricName := me.getMetricName() - dstArgs = append(dstArgs, metricName) + dstArg := me.LabelFilters[0].Value + dstArgs = append(dstArgs, dstArg) } continue } @@ -1045,9 +926,7 @@ func expandModifierArgs(was []*withArgExpr, args []string) ([]string, error) { func expandWithExprExt(was []*withArgExpr, wa *withArgExpr, args []Expr) (Expr, error) { if len(wa.Args) != len(args) { if args == nil { - // This case is possible if metric name clashes with one of the WITH template name. - // - // In this case just return MetricExpr with the wa.Name name. + // Just return MetricExpr with the wa.Name name. return newMetricExpr(wa.Name), nil } return nil, fmt.Errorf("invalid number of args for %q; got %d; want %d", wa.Name, len(args), len(wa.Args)) @@ -1070,14 +949,10 @@ func expandWithExprExt(was []*withArgExpr, wa *withArgExpr, args []Expr) (Expr, func newMetricExpr(name string) *MetricExpr { return &MetricExpr{ - LabelFilterss: [][]LabelFilter{ - { - { - Label: "__name__", - Value: name, - }, - }, - }, + LabelFilters: []LabelFilter{{ + Label: "__name__", + Value: name, + }}, } } @@ -1151,7 +1026,7 @@ func isKeepMetricNames(token string) bool { return token == "keep_metric_names" } -func (p *parser) parseModifierExpr(me *ModifierExpr, allowStar bool) error { +func (p *parser) parseModifierExpr(me *ModifierExpr) error { if !isIdentPrefix(p.lex.Token) { return fmt.Errorf(`ModifierExpr: unexpected token %q; want "ident"`, p.lex.Token) } @@ -1165,40 +1040,25 @@ func (p *parser) parseModifierExpr(me *ModifierExpr, allowStar bool) error { // join modifier may miss ident list. return nil } - args, err := p.parseIdentList(allowStar) + args, err := p.parseIdentList() if err != nil { - return fmt.Errorf("ModifierExpr: %w", err) + return err } me.Args = args return nil } -func (p *parser) parseIdentList(allowStar bool) ([]string, error) { +func (p *parser) parseIdentList() ([]string, error) { if p.lex.Token != "(" { return nil, fmt.Errorf(`identList: unexpected token %q; want "("`, p.lex.Token) } - if err := p.lex.Next(); err != nil { - return nil, err - } - if allowStar && p.lex.Token == "*" { - if err := p.lex.Next(); err != nil { - return nil, err - } - if p.lex.Token != ")" { - return nil, fmt.Errorf(`identList: unexpected token %q after "*"; want ")"`, p.lex.Token) - } - if err := p.lex.Next(); err != nil { - return nil, err - } - return []string{"*"}, nil - } var idents []string for { + if err := p.lex.Next(); err != nil { + return nil, err + } if p.lex.Token == ")" { - if err := p.lex.Next(); err != nil { - return nil, err - } - return idents, nil + goto closeParensLabel } if !isIdentPrefix(p.lex.Token) { return nil, fmt.Errorf(`identList: unexpected token %q; want "ident"`, p.lex.Token) @@ -1209,15 +1069,19 @@ func (p *parser) parseIdentList(allowStar bool) ([]string, error) { } switch p.lex.Token { case ",": - if err := p.lex.Next(); err != nil { - return nil, err - } - case ")": continue + case ")": + goto closeParensLabel default: return nil, fmt.Errorf(`identList: unexpected token %q; want ",", ")"`, p.lex.Token) } } + +closeParensLabel: + if err := p.lex.Next(); err != nil { + return nil, err + } + return idents, nil } func (p *parser) parseArgListExpr() ([]Expr, error) { @@ -1266,70 +1130,39 @@ func getWithArgExpr(was []*withArgExpr, name string) *withArgExpr { return nil } -func (p *parser) parseLabelFilterss(mf *labelFilterExpr) ([][]*labelFilterExpr, error) { +func (p *parser) parseLabelFilters() ([]*labelFilterExpr, error) { if p.lex.Token != "{" { return nil, fmt.Errorf(`labelFilters: unexpected token %q; want "{"`, p.lex.Token) } - if err := p.lex.Next(); err != nil { - return nil, err - } - if p.lex.Token == "}" { + + var lfes []*labelFilterExpr + for { if err := p.lex.Next(); err != nil { return nil, err } - if mf != nil { - return [][]*labelFilterExpr{{mf}}, nil + if p.lex.Token == "}" { + goto closeBracesLabel } - return nil, nil - } - - var lfess [][]*labelFilterExpr - for { - lfes, err := p.parseLabelFilters(mf) - if err != nil { - return nil, err - } - lfess = append(lfess, lfes) - switch strings.ToLower(p.lex.Token) { - case "}": - if err := p.lex.Next(); err != nil { - return nil, err - } - return lfess, nil - case "or": - if err := p.lex.Next(); err != nil { - return nil, err - } - } - } -} - -func (p *parser) parseLabelFilters(mf *labelFilterExpr) ([]*labelFilterExpr, error) { - var lfes []*labelFilterExpr - if mf != nil { - lfes = append(lfes, mf) - } - for { lfe, err := p.parseLabelFilterExpr() if err != nil { return nil, err } lfes = append(lfes, lfe) - switch strings.ToLower(p.lex.Token) { + switch p.lex.Token { case ",": - if err := p.lex.Next(); err != nil { - return nil, err - } - if p.lex.Token == "}" { - return lfes, nil - } continue - case "or", "}": - return lfes, nil + case "}": + goto closeBracesLabel default: - return nil, fmt.Errorf(`labelFilters: unexpected token %q; want ",", "or", "}"`, p.lex.Token) + return nil, fmt.Errorf(`labelFilters: unexpected token %q; want ",", "}"`, p.lex.Token) } } + +closeBracesLabel: + if err := p.lex.Next(); err != nil { + return nil, err + } + return lfes, nil } func (p *parser) parseLabelFilterExpr() (*labelFilterExpr, error) { @@ -1342,7 +1175,7 @@ func (p *parser) parseLabelFilterExpr() (*labelFilterExpr, error) { return nil, err } - switch strings.ToLower(p.lex.Token) { + switch p.lex.Token { case "=": // Nothing to do. case "!=": @@ -1352,10 +1185,10 @@ func (p *parser) parseLabelFilterExpr() (*labelFilterExpr, error) { case "!~": lfe.IsNegative = true lfe.IsRegexp = true - case ",", "}", "or": + case ",", "}": return &lfe, nil default: - return nil, fmt.Errorf(`labelFilterExpr: unexpected token %q; want "=", "!=", "=~", "!~", ",", "or", "}"`, p.lex.Token) + return nil, fmt.Errorf(`labelFilterExpr: unexpected token %q; want "=", "!=", "=~", "!~", ",", "}"`, p.lex.Token) } if err := p.lex.Next(); err != nil { @@ -1379,24 +1212,8 @@ type labelFilterExpr struct { IsNegative bool } -func (lfe *labelFilterExpr) AppendString(dst []byte) []byte { - dst = appendEscapedIdent(dst, lfe.Label) - if lfe.Value == nil { - return dst - } - dst = appendLabelFilterOp(dst, lfe.IsNegative, lfe.IsRegexp) - tokens := lfe.Value.tokens - if len(tokens) == 0 { - dst = strconv.AppendQuote(dst, lfe.Value.S) - return dst - } - for i, token := range tokens { - dst = append(dst, token...) - if i+1 < len(tokens) { - dst = append(dst, '+') - } - } - return dst +func (lfe *labelFilterExpr) String() string { + return fmt.Sprintf("[label=%q, value=%+v, isRegexp=%v, isNegative=%v]", lfe.Label, lfe.Value, lfe.IsRegexp, lfe.IsNegative) } func (lfe *labelFilterExpr) toLabelFilter() (*LabelFilter, error) { @@ -1512,28 +1329,13 @@ func (p *parser) parseDuration() (*DurationExpr, error) { func (p *parser) parsePositiveDuration() (*DurationExpr, error) { s := p.lex.Token - if isIdentPrefix(s) { - n := strings.IndexByte(s, ':') - if n >= 0 { - p.lex.PushBack(s[:n], s[n:]) - s = s[:n] - } - if err := p.lex.Next(); err != nil { - return nil, err - } - de := &DurationExpr{ - s: s, - needsParsing: true, - } - return de, nil - } if isPositiveDuration(s) { if err := p.lex.Next(); err != nil { return nil, err } } else { if !isPositiveNumberPrefix(s) { - return nil, fmt.Errorf(`duration: unexpected token %q; want valid duration`, s) + return nil, fmt.Errorf(`duration: unexpected token %q; want "duration"`, s) } // Verify the duration in seconds without explicit suffix. if _, err := p.parsePositiveNumberExpr(); err != nil { @@ -1553,9 +1355,6 @@ func (p *parser) parsePositiveDuration() (*DurationExpr, error) { // DurationExpr contains the duration type DurationExpr struct { s string - - // needsParsing is set to true if s isn't parsed yet with expandWithExpr() - needsParsing bool } // AppendString appends string representation of de to dst and returns the result. @@ -1563,31 +1362,14 @@ func (de *DurationExpr) AppendString(dst []byte) []byte { if de == nil { return dst } - if de.needsParsing { - panic(fmt.Errorf("BUG: duration %q must be already parsed with expandWithExpr()", de.s)) - } return append(dst, de.s...) } -// NonNegativeDuration returns non-negative duration for de in milliseconds. -// -// Error is returned if the duration is negative. -func (de *DurationExpr) NonNegativeDuration(step int64) (int64, error) { - d := de.Duration(step) - if d < 0 { - return 0, fmt.Errorf("unexpected negative duration %dms", d) - } - return d, nil -} - // Duration returns the duration from de in milliseconds. func (de *DurationExpr) Duration(step int64) int64 { if de == nil { return 0 } - if de.needsParsing { - panic(fmt.Errorf("BUG: duration %q must be already parsed", de.s)) - } d, err := DurationValue(de.s, step) if err != nil { panic(fmt.Errorf("BUG: cannot parse duration %q: %s", de.s, err)) @@ -1633,28 +1415,26 @@ func (p *parser) parseIdentExpr() (Expr, error) { } func (p *parser) parseMetricExpr() (*MetricExpr, error) { - var mf *labelFilterExpr var me MetricExpr if isIdentPrefix(p.lex.Token) { - mf = &labelFilterExpr{ - Label: "__name__", - Value: &StringExpr{ - tokens: []string{strconv.Quote(unescapeIdent(p.lex.Token))}, - }, + var lfe labelFilterExpr + lfe.Label = "__name__" + lfe.Value = &StringExpr{ + tokens: []string{strconv.Quote(unescapeIdent(p.lex.Token))}, } + me.labelFilters = append(me.labelFilters[:0], &lfe) if err := p.lex.Next(); err != nil { return nil, err } if p.lex.Token != "{" { - me.labelFilterss = append(me.labelFilterss[:0], []*labelFilterExpr{mf}) return &me, nil } } - lfess, err := p.parseLabelFilterss(mf) + lfes, err := p.parseLabelFilters() if err != nil { return nil, err } - me.labelFilterss = append(me.labelFilterss, lfess...) + me.labelFilters = append(me.labelFilters, lfes...) return &me, nil } @@ -1712,9 +1492,6 @@ type StringExpr struct { // AppendString appends string representation of se to dst and returns the result. func (se *StringExpr) AppendString(dst []byte) []byte { - if len(se.tokens) > 0 { - panic(fmt.Errorf("BUG: StringExpr=%q must be already parsed with expandWithExpr()", se.tokens)) - } return strconv.AppendQuote(dst, se.S) } @@ -1760,14 +1537,6 @@ type BinaryOpExpr struct { // JoinModifier contains modifier such as "group_left" or "group_right". JoinModifier ModifierExpr - // JoinModifierPrefix is an optional prefix to add to labels specified inside group_left() or group_right() lists. - // - // The syntax is `group_left(foo,bar) prefix "abc"` - JoinModifierPrefix *StringExpr - - // If KeepMetricNames is set to true, then the operation should keep metric names. - KeepMetricNames bool - // Left contains left arg for the `left op right` expression. Left Expr @@ -1777,59 +1546,17 @@ type BinaryOpExpr struct { // AppendString appends string representation of be to dst and returns the result. func (be *BinaryOpExpr) AppendString(dst []byte) []byte { - if be.KeepMetricNames { + if _, ok := be.Left.(*BinaryOpExpr); ok { dst = append(dst, '(') - dst = be.appendStringNoKeepMetricNames(dst) - dst = append(dst, ") keep_metric_names"...) - } else { - dst = be.appendStringNoKeepMetricNames(dst) - } - return dst -} - -func (be *BinaryOpExpr) appendStringNoKeepMetricNames(dst []byte) []byte { - if be.needLeftParens() { - dst = appendArgInParens(dst, be.Left) + dst = be.Left.AppendString(dst) + dst = append(dst, ')') } else { dst = be.Left.AppendString(dst) } dst = append(dst, ' ') - dst = be.appendModifiers(dst) - dst = append(dst, ' ') - if be.needRightParens() { - dst = appendArgInParens(dst, be.Right) - } else { - dst = be.Right.AppendString(dst) - } - return dst -} - -func (be *BinaryOpExpr) needLeftParens() bool { - return needBinaryOpArgParens(be.Left) -} - -func (be *BinaryOpExpr) needRightParens() bool { - if needBinaryOpArgParens(be.Right) { - return true - } - switch t := be.Right.(type) { - case *MetricExpr: - metricName := t.getMetricName() - return isReservedBinaryOpIdent(metricName) - case *FuncExpr: - if isReservedBinaryOpIdent(t.Name) { - return true - } - return t.KeepMetricNames || be.KeepMetricNames - default: - return false - } -} - -func (be *BinaryOpExpr) appendModifiers(dst []byte) []byte { dst = append(dst, be.Op...) if be.Bool { - dst = append(dst, "bool"...) + dst = append(dst, " bool"...) } if be.GroupModifier.Op != "" { dst = append(dst, ' ') @@ -1838,40 +1565,15 @@ func (be *BinaryOpExpr) appendModifiers(dst []byte) []byte { if be.JoinModifier.Op != "" { dst = append(dst, ' ') dst = be.JoinModifier.AppendString(dst) - if prefix := be.JoinModifierPrefix; prefix != nil { - dst = append(dst, " prefix "...) - dst = prefix.AppendString(dst) - } } - return dst -} - -func needBinaryOpArgParens(arg Expr) bool { - switch t := arg.(type) { - case *BinaryOpExpr: - return true - case *RollupExpr: - if be, ok := t.Expr.(*BinaryOpExpr); ok && be.KeepMetricNames { - return true - } - return t.Offset != nil || t.At != nil - default: - return false + dst = append(dst, ' ') + if _, ok := be.Right.(*BinaryOpExpr); ok { + dst = append(dst, '(') + dst = be.Right.AppendString(dst) + dst = append(dst, ')') + } else { + dst = be.Right.AppendString(dst) } -} - -func isReservedBinaryOpIdent(s string) bool { - return isBinaryOpGroupModifier(s) || isBinaryOpJoinModifier(s) || isBinaryOpBoolModifier(s) || isPrefixModifier(s) -} - -func isPrefixModifier(s string) bool { - return strings.ToLower(s) == "prefix" -} - -func appendArgInParens(dst []byte, arg Expr) []byte { - dst = append(dst, '(') - dst = arg.AppendString(dst) - dst = append(dst, ')') return dst } @@ -1887,15 +1589,11 @@ type ModifierExpr struct { // AppendString appends string representation of me to dst and returns the result. func (me *ModifierExpr) AppendString(dst []byte) []byte { dst = append(dst, me.Op...) - dst = append(dst, '(') + dst = append(dst, " ("...) for i, arg := range me.Args { - if arg == "*" { - dst = append(dst, '*') - } else { - dst = appendEscapedIdent(dst, arg) - } + dst = appendEscapedIdent(dst, arg) if i+1 < len(me.Args) { - dst = append(dst, ',') + dst = append(dst, ", "...) } } dst = append(dst, ')') @@ -1930,10 +1628,6 @@ type FuncExpr struct { func (fe *FuncExpr) AppendString(dst []byte) []byte { dst = appendEscapedIdent(dst, fe.Name) dst = appendStringArgListExpr(dst, fe.Args) - return fe.appendModifiers(dst) -} - -func (fe *FuncExpr) appendModifiers(dst []byte) []byte { if fe.KeepMetricNames { dst = append(dst, " keep_metric_names"...) } @@ -1962,10 +1656,6 @@ type AggrFuncExpr struct { func (ae *AggrFuncExpr) AppendString(dst []byte) []byte { dst = appendEscapedIdent(dst, ae.Name) dst = appendStringArgListExpr(dst, ae.Args) - return ae.appendModifiers(dst) -} - -func (ae *AggrFuncExpr) appendModifiers(dst []byte) []byte { if ae.Modifier.Op != "" { dst = append(dst, ' ') dst = ae.Modifier.AppendString(dst) @@ -2065,7 +1755,18 @@ func (re *RollupExpr) ForSubquery() bool { // AppendString appends string representation of re to dst and returns the result. func (re *RollupExpr) AppendString(dst []byte) []byte { - needParens := re.needParens() + needParens := func() bool { + if _, ok := re.Expr.(*RollupExpr); ok { + return true + } + if _, ok := re.Expr.(*BinaryOpExpr); ok { + return true + } + if ae, ok := re.Expr.(*AggrFuncExpr); ok && ae.Modifier.Op != "" { + return true + } + return false + }() if needParens { dst = append(dst, '(') } @@ -2073,10 +1774,6 @@ func (re *RollupExpr) AppendString(dst []byte) []byte { if needParens { dst = append(dst, ')') } - return re.appendModifiers(dst) -} - -func (re *RollupExpr) appendModifiers(dst []byte) []byte { if re.Window != nil || re.InheritStep || re.Step != nil { dst = append(dst, '[') dst = re.Window.AppendString(dst) @@ -2106,17 +1803,6 @@ func (re *RollupExpr) appendModifiers(dst []byte) []byte { return dst } -func (re *RollupExpr) needParens() bool { - switch t := re.Expr.(type) { - case *RollupExpr, *BinaryOpExpr: - return true - case *AggrFuncExpr: - return t.Modifier.Op != "" - default: - return false - } -} - // LabelFilter represents MetricsQL label filter like `foo="bar"`. type LabelFilter struct { // Label contains label name for the filter. @@ -2135,143 +1821,77 @@ type LabelFilter struct { // AppendString appends string representation of me to dst and returns the result. func (lf *LabelFilter) AppendString(dst []byte) []byte { dst = appendEscapedIdent(dst, lf.Label) - dst = appendLabelFilterOp(dst, lf.IsNegative, lf.IsRegexp) + var op string + if lf.IsNegative { + if lf.IsRegexp { + op = "!~" + } else { + op = "!=" + } + } else { + if lf.IsRegexp { + op = "=~" + } else { + op = "=" + } + } + dst = append(dst, op...) dst = strconv.AppendQuote(dst, lf.Value) return dst } -func appendLabelFilterOp(dst []byte, isNegative, isRegexp bool) []byte { - if isNegative { - if isRegexp { - return append(dst, "!~"...) - } - return append(dst, "!="...) - } - if isRegexp { - return append(dst, "=~"...) - } - return append(dst, '=') -} - // MetricExpr represents MetricsQL metric with optional filters, i.e. `foo{...}`. -// -// Curly braces may contain or-delimited list of filters. For example: -// -// x{job="foo",instance="bar" or job="x",instance="baz"} -// -// In this case the filter returns all the series, which match at least one of the following filters: -// -// x{job="foo",instance="bar"} -// x{job="x",instance="baz"} -// -// This allows using or-delimited list of filters inside rollup functions. For example, -// the following query calculates rate per each matching series for the given or-delimited filters: -// -// rate(x{job="foo",instance="bar" or job="x",instance="baz"}[5m]) type MetricExpr struct { - // LabelFilters contains a list of or-delimited groups of label filters from curly braces. - // Filter for metric name (aka __name__ label) must go first in every group. - LabelFilterss [][]LabelFilter + // LabelFilters contains a list of label filters from curly braces. + // Filter or metric name must be the first if present. + LabelFilters []LabelFilter - // labelFilters contain non-expanded label filters joined by 'or' operator. - // // labelFilters must be expanded to LabelFilters by expandWithExpr. - labelFilterss [][]*labelFilterExpr -} - -func appendLabelFilterss(dst []byte, lfss [][]*labelFilterExpr) []byte { - dst = append(dst, '{') - for i, lfs := range lfss { - for j, lf := range lfs { - dst = lf.AppendString(dst) - if j+1 < len(lfs) { - dst = append(dst, ',') - } - } - if i+1 < len(lfss) { - dst = append(dst, " or "...) - } - } - dst = append(dst, '}') - return dst + labelFilters []*labelFilterExpr } // AppendString appends string representation of me to dst and returns the result. func (me *MetricExpr) AppendString(dst []byte) []byte { - if len(me.labelFilterss) > 0 { - return appendLabelFilterss(dst, me.labelFilterss) + lfs := me.LabelFilters + if len(lfs) > 0 { + lf := &lfs[0] + if lf.Label == "__name__" && !lf.IsNegative && !lf.IsRegexp { + dst = appendEscapedIdent(dst, lf.Value) + lfs = lfs[1:] + } } - lfss := me.LabelFilterss - if len(lfss) == 0 { + if len(lfs) > 0 { + dst = append(dst, '{') + for i := range lfs { + dst = lfs[i].AppendString(dst) + if i+1 < len(lfs) { + dst = append(dst, ", "...) + } + } + dst = append(dst, '}') + } else if len(me.LabelFilters) == 0 { dst = append(dst, "{}"...) - return dst - } - offset := 0 - metricName := me.getMetricName() - if metricName != "" { - offset = 1 - dst = appendEscapedIdent(dst, metricName) - } - if len(lfss) == 1 && len(lfss[0]) == offset { - return dst - } - dst = append(dst, '{') - lfs := lfss[0] - dst = appendLabelFilters(dst, lfs[offset:]) - for _, lfs := range lfss[1:] { - dst = append(dst, " or "...) - dst = appendLabelFilters(dst, lfs[offset:]) - } - dst = append(dst, '}') - return dst -} - -func appendLabelFilters(dst []byte, lfs []LabelFilter) []byte { - if len(lfs) == 0 { - return dst - } - dst = lfs[0].AppendString(dst) - lfs = lfs[1:] - for i := range lfs { - dst = append(dst, ',') - dst = lfs[i].AppendString(dst) } return dst } // IsEmpty returns true of me equals to `{}`. func (me *MetricExpr) IsEmpty() bool { - return len(me.LabelFilterss) == 0 + return len(me.LabelFilters) == 0 } -func (me *MetricExpr) isOnlyMetricName() bool { - if me.getMetricName() == "" { +func (me *MetricExpr) isOnlyMetricGroup() bool { + if !me.hasNonEmptyMetricGroup() { return false } - for _, lfs := range me.LabelFilterss { - if len(lfs) > 1 { - return false - } - } - return true + return len(me.LabelFilters) == 1 } -func (me *MetricExpr) getMetricName() string { - lfss := me.LabelFilterss - if len(lfss) == 0 { - return "" +func (me *MetricExpr) hasNonEmptyMetricGroup() bool { + if len(me.LabelFilters) == 0 { + return false } - lfs := lfss[0] - if len(lfs) == 0 || !lfs[0].isMetricNameFilter() { - return "" - } - metricName := lfs[0].Value - for _, lfs := range lfss[1:] { - if len(lfs) == 0 || !lfs[0].isMetricNameFilter() || lfs[0].Value != metricName { - return "" - } - } - return metricName + return me.LabelFilters[0].isMetricNameFilter() } func (lf *LabelFilter) isMetricNameFilter() bool { diff --git a/vendor/github.com/VictoriaMetrics/metricsql/prettifier.go b/vendor/github.com/VictoriaMetrics/metricsql/prettifier.go deleted file mode 100644 index 486a22492..000000000 --- a/vendor/github.com/VictoriaMetrics/metricsql/prettifier.go +++ /dev/null @@ -1,201 +0,0 @@ -package metricsql - -// Prettify returns prettified representation of MetricsQL query q. -func Prettify(q string) (string, error) { - e, err := Parse(q) - if err != nil { - return "", err - } - b := appendPrettifiedExpr(nil, e, 0, false) - return string(b), nil -} - -// maxPrettifiedLineLen is the maximum length of a single line returned by Prettify(). -// -// Actual lines may exceed the maximum length in some cases. -const maxPrettifiedLineLen = 80 - -func appendPrettifiedExpr(dst []byte, e Expr, indent int, needParens bool) []byte { - dstLen := len(dst) - - // Try appending e to dst and check whether its length exceeds the maximum allowed line length. - dst = appendIndent(dst, indent) - if needParens { - dst = append(dst, '(') - } - dst = e.AppendString(dst) - if needParens { - dst = append(dst, ')') - } - if len(dst)-dstLen <= maxPrettifiedLineLen { - // There is no need in splitting the e string representation, since its' length doesn't exceed. - return dst - } - - // The e string representation exceeds maxPrettifiedLineLen. Split it into multiple lines - dst = dst[:dstLen] - if needParens { - dst = appendIndent(dst, indent) - dst = append(dst, "(\n"...) - indent++ - } - switch t := e.(type) { - case *BinaryOpExpr: - // Split: - // - // a op b - // - // into: - // - // foo - // op - // bar - if t.KeepMetricNames { - dst = appendIndent(dst, indent) - dst = append(dst, "(\n"...) - indent++ - } - dst = appendPrettifiedExpr(dst, t.Left, indent, t.needLeftParens()) - dst = append(dst, '\n') - dst = appendIndent(dst, indent+1) - dst = t.appendModifiers(dst) - dst = append(dst, '\n') - dst = appendPrettifiedExpr(dst, t.Right, indent, t.needRightParens()) - if t.KeepMetricNames { - indent-- - dst = append(dst, '\n') - dst = appendIndent(dst, indent) - dst = append(dst, ") keep_metric_names"...) - } - case *RollupExpr: - // Split: - // - // q[d:s] offset off @ x - // - // into: - // - // ( - // q - // )[d:s] offset off @ x - dst = appendPrettifiedExpr(dst, t.Expr, indent, t.needParens()) - dst = t.appendModifiers(dst) - case *AggrFuncExpr: - // Split: - // - // aggr_func(arg1, ..., argN) modifiers - // - // into: - // - // aggr_func( - // arg1, - // ... - // argN - // ) modifiers - dst = appendIndent(dst, indent) - dst = appendEscapedIdent(dst, t.Name) - dst = appendPrettifiedFuncArgs(dst, indent, t.Args) - dst = t.appendModifiers(dst) - case *FuncExpr: - // Split: - // - // func(arg1, ..., argN) modifiers - // - // into: - // - // func( - // arg1, - // ... - // argN - // ) modifiers - dst = appendIndent(dst, indent) - dst = appendEscapedIdent(dst, t.Name) - dst = appendPrettifiedFuncArgs(dst, indent, t.Args) - dst = t.appendModifiers(dst) - case *MetricExpr: - // Split: - // - // metric{filters1 or ... or filtersN} - // - // into: - // - // metric{ - // filters1 - // or - // ... - // or - // filtersN - // } - offset := 0 - metricName := t.getMetricName() - if metricName != "" { - offset = 1 - } - dst = appendIndent(dst, indent) - dst = appendEscapedIdent(dst, metricName) - dst = append(dst, "{\n"...) - lfss := t.LabelFilterss - for i, lfs := range lfss { - dst = appendPrettifiedLabelFilters(dst, indent+1, lfs[offset:]) - dst = append(dst, '\n') - if i+1 < len(lfss) { - dst = appendIndent(dst, indent+2) - dst = append(dst, "or\n"...) - } - } - dst = appendIndent(dst, indent) - dst = append(dst, '}') - default: - // marshal other expressions as is - dst = t.AppendString(dst) - } - if needParens { - indent-- - dst = append(dst, '\n') - dst = appendIndent(dst, indent) - dst = append(dst, ')') - } - return dst -} - -func appendPrettifiedFuncArgs(dst []byte, indent int, args []Expr) []byte { - dst = append(dst, "(\n"...) - for i, arg := range args { - dst = appendPrettifiedExpr(dst, arg, indent+1, false) - if i+1 < len(args) { - dst = append(dst, ',') - } - dst = append(dst, '\n') - } - dst = appendIndent(dst, indent) - dst = append(dst, ')') - return dst -} - -func appendPrettifiedLabelFilters(dst []byte, indent int, lfs []LabelFilter) []byte { - dstLen := len(dst) - - // Try marshaling lfs into a single line - dst = appendIndent(dst, indent) - dst = appendLabelFilters(dst, lfs) - if len(dst)-dstLen <= maxPrettifiedLineLen { - return dst - } - - // Too long line - split it into multiple lines - dst = dst[:dstLen] - for i := range lfs { - dst = appendIndent(dst, indent) - dst = lfs[i].AppendString(dst) - if i+1 < len(lfs) { - dst = append(dst, ",\n"...) - } - } - return dst -} - -func appendIndent(dst []byte, indent int) []byte { - for i := 0; i < indent; i++ { - dst = append(dst, " "...) - } - return dst -} diff --git a/vendor/github.com/VictoriaMetrics/metricsql/rollup.go b/vendor/github.com/VictoriaMetrics/metricsql/rollup.go index da3204adc..b04950895 100644 --- a/vendor/github.com/VictoriaMetrics/metricsql/rollup.go +++ b/vendor/github.com/VictoriaMetrics/metricsql/rollup.go @@ -64,7 +64,6 @@ var rollupFuncs = map[string]bool{ "scrape_interval": true, "share_gt_over_time": true, "share_le_over_time": true, - "share_eq_over_time": true, "stale_samples_over_time": true, "stddev_over_time": true, "stdvar_over_time": true, diff --git a/vendor/modules.txt b/vendor/modules.txt index b62145581..0f014e8a6 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -99,7 +99,7 @@ github.com/VictoriaMetrics/fasthttp/stackless # github.com/VictoriaMetrics/metrics v1.24.0 ## explicit; go 1.20 github.com/VictoriaMetrics/metrics -# github.com/VictoriaMetrics/metricsql v0.64.0 +# github.com/VictoriaMetrics/metricsql v0.56.2 ## explicit; go 1.13 github.com/VictoriaMetrics/metricsql github.com/VictoriaMetrics/metricsql/binaryop