Skip to content

Commit

Permalink
feat(influxql): rewrite regex conditions in subqueries (#20962)
Browse files Browse the repository at this point in the history

Co-authored-by: Tecker.Yu <[email protected]>
  • Loading branch information
danxmoran and yujiahaol68 authored Mar 17, 2021
1 parent 0f86200 commit 8e3d35f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ the endpoint has been removed. Use the `/metrics` endpoint to collect system sta
1. [20846](https://github.com/influxdata/influxdb/pull/20846): Add `--compression` option to `influx write` to support GZIP inputs.
1. [20845](https://github.com/influxdata/influxdb/pull/20845): Add `influx task retry-failed` command to rerun failed runs.
1. [20965](https://github.com/influxdata/influxdb/pull/20965): Add `--metrics-disabled` option to `influxd` to disable exposing Prometheus metrics over HTTP.
1. [20962](https://github.com/influxdata/influxdb/pull/20962): Rewrite regex conditions in InfluxQL subqueries for performance. Thanks @yujiahaol68!

### Bug Fixes

Expand Down
1 change: 1 addition & 0 deletions influxql/query/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func (c *compiledStatement) compile(stmt *influxql.SelectStatement) error {
if err := c.subquery(source.Statement); err != nil {
return err
}
source.Statement.RewriteRegexConditions()
}
}
return nil
Expand Down
24 changes: 24 additions & 0 deletions influxql/query/compile_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package query

import (
"testing"

"github.com/influxdata/influxql"
"github.com/stretchr/testify/require"
)

func TestCompile_RewriteSubqueryRegex(t *testing.T) {
q := `SELECT top(mean, 10), host FROM (SELECT mean(value) FROM cpu WHERE id =~ /^(server-1|server-2|server-3)$/ GROUP BY host)`
stmt, err := influxql.ParseStatement(q)
require.NoError(t, err)
s := stmt.(*influxql.SelectStatement)

compiled, err := Compile(s, CompileOptions{})
require.NoError(t, err)

c := compiled.(*compiledStatement)
require.Len(t, c.stmt.Sources, 1)

subquery := c.stmt.Sources[0]
require.Equal(t, `(SELECT mean(value) FROM cpu WHERE id = 'server-1' OR id = 'server-2' OR id = 'server-3' GROUP BY host)`, subquery.String())
}
1 change: 1 addition & 0 deletions influxql/query/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func TestCompile_Success(t *testing.T) {
`SELECT max(value) FROM cpu WHERE time >= now() - 1m GROUP BY time(10s, '2000-01-01T00:00:05Z')`,
`SELECT max(value) FROM cpu WHERE time >= now() - 1m GROUP BY time(10s, now())`,
`SELECT max(mean) FROM (SELECT mean(value) FROM cpu GROUP BY host)`,
`SELECT top(mean, 10), host FROM (SELECT mean(value) FROM cpu WHERE id =~ /^(server-1|server-2|server-3)$/ GROUP BY host)`,
`SELECT max(derivative) FROM (SELECT derivative(mean(value)) FROM cpu) WHERE time >= now() - 1m GROUP BY time(10s)`,
`SELECT max(value) FROM (SELECT value + total FROM cpu) WHERE time >= now() - 1m GROUP BY time(10s)`,
`SELECT value FROM cpu WHERE time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T01:00:00Z'`,
Expand Down

0 comments on commit 8e3d35f

Please sign in to comment.