fix: error instead of panic for statement rewrite failure (#21800)

Co-authored-by: Sam Arnold <sarnold@influxdata.com>
pull/21797/head^2
Daniel Moran 2021-07-06 16:35:34 -04:00 committed by GitHub
parent ea5e6eabcb
commit ae9eb3e847
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View File

@ -35,9 +35,10 @@ This release adds an embedded SQLite database for storing metadata required by t
1. [21610](https://github.com/influxdata/influxdb/pull/21610): Avoid rewriting `fields.idx` unnecessarily.
1. [21648](https://github.com/influxdata/influxdb/pull/21648): Change static legend's `hide` to `show` to let users decide if they want it.
1. [21662](https://github.com/influxdata/influxdb/pull/21662): Do not close connection twice in DigestWithOptions
1. [21691](https://github.com/influxdata/influxdb/pull/21691): Remove incorrect optimization for group-by
1. [21747](https://github.com/influxdata/influxdb/pull/21747): Rename arm rpms with yum-compatible names
1. [21662](https://github.com/influxdata/influxdb/pull/21662): Do not close connection twice in DigestWithOptions.
1. [21691](https://github.com/influxdata/influxdb/pull/21691): Remove incorrect optimization for group-by.
1. [21747](https://github.com/influxdata/influxdb/pull/21747): Rename arm rpms with yum-compatible names.
1. [21800](https://github.com/influxdata/influxdb/pull/21800): Return an error instead of panicking when InfluxQL statement rewrites fail.
## v2.0.7 [2021-06-04]

View File

@ -104,7 +104,7 @@ func newCompiler(opt CompileOptions) *compiledStatement {
}
}
func Compile(stmt *influxql.SelectStatement, opt CompileOptions) (Statement, error) {
func Compile(stmt *influxql.SelectStatement, opt CompileOptions) (_ Statement, err error) {
c := newCompiler(opt)
c.stmt = stmt.Clone()
if err := c.preprocess(c.stmt); err != nil {
@ -116,6 +116,17 @@ func Compile(stmt *influxql.SelectStatement, opt CompileOptions) (Statement, err
c.stmt.TimeAlias = c.TimeFieldName
c.stmt.Condition = c.Condition
defer func() {
if e := recover(); e != nil && err == nil {
var ok bool
err, ok = e.(error)
if !ok {
err = fmt.Errorf("panic: %v", e)
}
err = fmt.Errorf("likely malformed statement, unable to rewrite: %w", err)
}
}()
// Convert DISTINCT into a call.
c.stmt.RewriteDistinct()

View File

@ -363,6 +363,7 @@ func TestCompile_Failures(t *testing.T) {
{s: `SELECT atan2(value, 3, 3) FROM cpu`, err: `invalid number of arguments for atan2, expected 2, got 3`},
{s: `SELECT sin(1.3) FROM cpu`, err: `field must contain at least one variable`},
{s: `SELECT nofunc(1.3) FROM cpu`, err: `undefined function nofunc()`},
{s: `SELECT * FROM cpu WHERE ( host =~ /foo/ ^ other AND env =~ /bar/ ) and time >= now()-15m`, err: `likely malformed statement, unable to rewrite: interface conversion: influxql.Expr is *influxql.BinaryExpr, not *influxql.RegexLiteral`},
} {
t.Run(tt.s, func(t *testing.T) {
stmt, err := influxql.ParseStatement(tt.s)