mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/metricsq: add ExpandWithExprs
This commit is contained in:
parent
1925ee038d
commit
6f67e0b56b
4 changed files with 55 additions and 49 deletions
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
12
lib/metricsql/utils.go
Normal file
12
lib/metricsql/utils.go
Normal file
|
@ -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
|
||||
}
|
43
lib/metricsql/utils_test.go
Normal file
43
lib/metricsql/utils_test.go
Normal file
|
@ -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 (`)
|
||||
}
|
Loading…
Reference in a new issue