From 6f67e0b56bfa546309df88de156a4f82d21b574e Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 25 Dec 2019 22:19:34 +0200 Subject: [PATCH] lib/metricsq: add ExpandWithExprs --- app/vmselect/promql/exec.go | 11 -------- app/vmselect/promql/exec_test.go | 38 ---------------------------- lib/metricsql/utils.go | 12 +++++++++ lib/metricsql/utils_test.go | 43 ++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 49 deletions(-) create mode 100644 lib/metricsql/utils.go create mode 100644 lib/metricsql/utils_test.go diff --git a/app/vmselect/promql/exec.go b/app/vmselect/promql/exec.go index ca749e8de..1c248d799 100644 --- a/app/vmselect/promql/exec.go +++ b/app/vmselect/promql/exec.go @@ -19,17 +19,6 @@ var logSlowQueryDuration = flag.Duration("search.logSlowQueryDuration", 5*time.S var slowQueries = metrics.NewCounter(`vm_slow_queries_total`) -// ExpandWithExprs expands WITH expressions inside q and returns the resulting -// PromQL without WITH expressions. -func ExpandWithExprs(q string) (string, error) { - e, err := parsePromQLWithCache(q) - if err != nil { - return "", err - } - buf := e.AppendString(nil) - return string(buf), nil -} - // Exec executes q for the given ec. func Exec(ec *EvalConfig, q string, isFirstPointOnly bool) ([]netstorage.Result, error) { if *logSlowQueryDuration > 0 { diff --git a/app/vmselect/promql/exec_test.go b/app/vmselect/promql/exec_test.go index aadbbb33c..a46314e23 100644 --- a/app/vmselect/promql/exec_test.go +++ b/app/vmselect/promql/exec_test.go @@ -8,44 +8,6 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/lib/storage" ) -func TestExpandWithExprsSuccess(t *testing.T) { - f := func(q, qExpected string) { - t.Helper() - for i := 0; i < 3; i++ { - qExpanded, err := ExpandWithExprs(q) - if err != nil { - t.Fatalf("unexpected error when expanding %q: %s", q, err) - } - if qExpanded != qExpected { - t.Fatalf("unexpected expanded expression for %q;\ngot\n%q\nwant\n%q", q, qExpanded, qExpected) - } - } - } - - f(`1`, `1`) - f(`foobar`, `foobar`) - f(`with (x = 1) x+x`, `2`) - f(`with (f(x) = x*x) 3+f(2)+2`, `9`) -} - -func TestExpandWithExprsError(t *testing.T) { - f := func(q string) { - t.Helper() - for i := 0; i < 3; i++ { - qExpanded, err := ExpandWithExprs(q) - if err == nil { - t.Fatalf("expecting non-nil error when expanding %q", q) - } - if qExpanded != "" { - t.Fatalf("unexpected non-empty qExpanded=%q", qExpanded) - } - } - } - - f(``) - f(` with (`) -} - func TestExecSuccess(t *testing.T) { start := int64(1000e3) end := int64(2000e3) diff --git a/lib/metricsql/utils.go b/lib/metricsql/utils.go new file mode 100644 index 000000000..3e3fa8d6e --- /dev/null +++ b/lib/metricsql/utils.go @@ -0,0 +1,12 @@ +package metricsql + +// ExpandWithExprs expands WITH expressions inside q and returns the resulting +// PromQL without WITH expressions. +func ExpandWithExprs(q string) (string, error) { + e, err := Parse(q) + if err != nil { + return "", err + } + buf := e.AppendString(nil) + return string(buf), nil +} diff --git a/lib/metricsql/utils_test.go b/lib/metricsql/utils_test.go new file mode 100644 index 000000000..ce08034a2 --- /dev/null +++ b/lib/metricsql/utils_test.go @@ -0,0 +1,43 @@ +package metricsql + +import ( + "testing" +) + +func TestExpandWithExprsSuccess(t *testing.T) { + f := func(q, qExpected string) { + t.Helper() + for i := 0; i < 3; i++ { + qExpanded, err := ExpandWithExprs(q) + if err != nil { + t.Fatalf("unexpected error when expanding %q: %s", q, err) + } + if qExpanded != qExpected { + t.Fatalf("unexpected expanded expression for %q;\ngot\n%q\nwant\n%q", q, qExpanded, qExpected) + } + } + } + + f(`1`, `1`) + f(`foobar`, `foobar`) + f(`with (x = 1) x+x`, `2`) + f(`with (f(x) = x*x) 3+f(2)+2`, `9`) +} + +func TestExpandWithExprsError(t *testing.T) { + f := func(q string) { + t.Helper() + for i := 0; i < 3; i++ { + qExpanded, err := ExpandWithExprs(q) + if err == nil { + t.Fatalf("expecting non-nil error when expanding %q", q) + } + if qExpanded != "" { + t.Fatalf("unexpected non-empty qExpanded=%q", qExpanded) + } + } + } + + f(``) + f(` with (`) +}