From b3a3e9990f5dac28beb3d8c0f5fe62a0b251b674 Mon Sep 17 00:00:00 2001
From: Aliaksandr Valialkin <valyala@victoriametrics.com>
Date: Mon, 11 Apr 2022 18:30:53 +0300
Subject: [PATCH] vendor: update github.com/VictoriaMetrics/metricsql from
 v0.40.0 to v0.41.0

This allows using built-in function names as with template names
---
 docs/CHANGELOG.md                               |  1 +
 go.mod                                          |  2 +-
 go.sum                                          |  4 ++--
 .../VictoriaMetrics/metricsql/parser.go         | 17 +++++++++--------
 vendor/modules.txt                              |  2 +-
 5 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 1a179177f..e4b1c9a27 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -18,6 +18,7 @@ The following tip changes can be tested by building VictoriaMetrics components f
 FEATURE: [vmalert](https://docs.victoriametrics.com/vmalert.html): add support for `alert_relabel_configs` option at `-notifier.config`. This option allows configuring relabeling rules for alerts before sending them to configured notifiers. See [these docs](https://docs.victoriametrics.com/vmalert.html#notifier-configuration-file) for details.
 
 BUGFIX: [Graphite Render API](https://docs.victoriametrics.com/#graphite-render-api-usage): properly calculate [hitCount](https://graphite.readthedocs.io/en/latest/functions.html#graphite.render.functions.hitcount) function. Previously it could return empty results if there were no original samples in some parts of the selected time range.
+BUGFIX: [MetricsQL](https://docs.victoriametrics.com/MetricsQL.html): allow overriding built-in function names inside [WITH templates](https://play.victoriametrics.com/promql/expand-with-exprs). For example, `WITH (sum(a,b) = a + b + 1) sum(x,y)` now expands into `x + y + 1`. Previously such a query would fail with `cannot use reserved name` error. See [this bugreport](https://github.com/VictoriaMetrics/metricsql/issues/5).
 
 
 ## [v1.76.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.76.0)
diff --git a/go.mod b/go.mod
index 8f6a1fa0d..d168811e2 100644
--- a/go.mod
+++ b/go.mod
@@ -10,7 +10,7 @@ require (
 	// like https://github.com/valyala/fasthttp/commit/996610f021ff45fdc98c2ce7884d5fa4e7f9199b
 	github.com/VictoriaMetrics/fasthttp v1.1.0
 	github.com/VictoriaMetrics/metrics v1.18.1
-	github.com/VictoriaMetrics/metricsql v0.40.0
+	github.com/VictoriaMetrics/metricsql v0.41.0
 	github.com/aws/aws-sdk-go v1.43.34
 	github.com/cespare/xxhash/v2 v2.1.2
 	github.com/cheggaaa/pb/v3 v3.0.8
diff --git a/go.sum b/go.sum
index dc8c16532..9e1db8bb3 100644
--- a/go.sum
+++ b/go.sum
@@ -119,8 +119,8 @@ github.com/VictoriaMetrics/fasthttp v1.1.0 h1:3crd4YWHsMwu60GUXRH6OstowiFvqrwS4a
 github.com/VictoriaMetrics/fasthttp v1.1.0/go.mod h1:/7DMcogqd+aaD3G3Hg5kFgoFwlR2uydjiWvoLp5ZTqQ=
 github.com/VictoriaMetrics/metrics v1.18.1 h1:OZ0+kTTto8oPfHnVAnTOoyl0XlRhRkoQrD2n2cOuRw0=
 github.com/VictoriaMetrics/metrics v1.18.1/go.mod h1:ArjwVz7WpgpegX/JpB0zpNF2h2232kErkEnzH1sxMmA=
-github.com/VictoriaMetrics/metricsql v0.40.0 h1:QDzuhzsP2cZJyrijIptDJ6gnxd3qWGzQi4Fhj8mOLHo=
-github.com/VictoriaMetrics/metricsql v0.40.0/go.mod h1:6pP1ZeLVJHqJrHlF6Ij3gmpQIznSsgktEcZgsAWYel0=
+github.com/VictoriaMetrics/metricsql v0.41.0 h1:fhWnSE9ZXVEbiXXGFY73YPLdovTaDRaDaFdxC3TTRZs=
+github.com/VictoriaMetrics/metricsql v0.41.0/go.mod h1:6pP1ZeLVJHqJrHlF6Ij3gmpQIznSsgktEcZgsAWYel0=
 github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
 github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
 github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
diff --git a/vendor/github.com/VictoriaMetrics/metricsql/parser.go b/vendor/github.com/VictoriaMetrics/metricsql/parser.go
index 403c40d09..ac64fc438 100644
--- a/vendor/github.com/VictoriaMetrics/metricsql/parser.go
+++ b/vendor/github.com/VictoriaMetrics/metricsql/parser.go
@@ -306,9 +306,6 @@ func (p *parser) parseWithArgExpr() (*withArgExpr, error) {
 		return nil, fmt.Errorf(`withArgExpr: unexpected token %q; want "ident"`, p.lex.Token)
 	}
 	wa.Name = unescapeIdent(p.lex.Token)
-	if isAggrFunc(wa.Name) || IsRollupFunc(wa.Name) || IsTransformFunc(wa.Name) || isWith(wa.Name) {
-		return nil, fmt.Errorf(`withArgExpr: cannot use reserved name %q`, wa.Name)
-	}
 	if err := p.lex.Next(); err != nil {
 		return nil, err
 	}
@@ -682,17 +679,21 @@ func expandWithExpr(was []*withArgExpr, e Expr) (Expr, error) {
 			return nil, err
 		}
 		wa := getWithArgExpr(was, t.Name)
-		if wa == nil {
-			fe := *t
-			fe.Args = args
-			return &fe, nil
+		if wa != nil {
+			return expandWithExprExt(was, wa, args)
 		}
-		return expandWithExprExt(was, wa, args)
+		fe := *t
+		fe.Args = args
+		return &fe, nil
 	case *AggrFuncExpr:
 		args, err := expandWithArgs(was, t.Args)
 		if err != nil {
 			return nil, err
 		}
+		wa := getWithArgExpr(was, t.Name)
+		if wa != nil {
+			return expandWithExprExt(was, wa, args)
+		}
 		modifierArgs, err := expandModifierArgs(was, t.Modifier.Args)
 		if err != nil {
 			return nil, err
diff --git a/vendor/modules.txt b/vendor/modules.txt
index 6ad174986..f743002b5 100644
--- a/vendor/modules.txt
+++ b/vendor/modules.txt
@@ -27,7 +27,7 @@ github.com/VictoriaMetrics/fasthttp/stackless
 # github.com/VictoriaMetrics/metrics v1.18.1
 ## explicit; go 1.12
 github.com/VictoriaMetrics/metrics
-# github.com/VictoriaMetrics/metricsql v0.40.0
+# github.com/VictoriaMetrics/metricsql v0.41.0
 ## explicit; go 1.13
 github.com/VictoriaMetrics/metricsql
 github.com/VictoriaMetrics/metricsql/binaryop