Skip to content

Commit 6f9f551

Browse files
authored
fix: error instead of panic for statement rewrite failure (#21792) (#21809)
(cherry picked from commit 98361e2)
1 parent d38f5f5 commit 6f9f551

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

query/compile.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func newCompiler(opt CompileOptions) *compiledStatement {
103103
}
104104
}
105105

106-
func Compile(stmt *influxql.SelectStatement, opt CompileOptions) (Statement, error) {
106+
func Compile(stmt *influxql.SelectStatement, opt CompileOptions) (_ Statement, err error) {
107107
c := newCompiler(opt)
108108
c.stmt = stmt.Clone()
109109
if err := c.preprocess(c.stmt); err != nil {
@@ -115,6 +115,17 @@ func Compile(stmt *influxql.SelectStatement, opt CompileOptions) (Statement, err
115115
c.stmt.TimeAlias = c.TimeFieldName
116116
c.stmt.Condition = c.Condition
117117

118+
defer func() {
119+
if e := recover(); e != nil && err == nil {
120+
var ok bool
121+
err, ok = e.(error)
122+
if !ok {
123+
err = fmt.Errorf("panic: %v", e)
124+
}
125+
err = fmt.Errorf("likely malformed statement, unable to rewrite: %w", err)
126+
}
127+
}()
128+
118129
// Convert DISTINCT into a call.
119130
c.stmt.RewriteDistinct()
120131

query/compile_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ func TestCompile_Failures(t *testing.T) {
361361
{s: `SELECT atan2(value, 3, 3) FROM cpu`, err: `invalid number of arguments for atan2, expected 2, got 3`},
362362
{s: `SELECT sin(1.3) FROM cpu`, err: `field must contain at least one variable`},
363363
{s: `SELECT nofunc(1.3) FROM cpu`, err: `undefined function nofunc()`},
364+
{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`},
364365
} {
365366
t.Run(tt.s, func(t *testing.T) {
366367
stmt, err := influxql.ParseStatement(tt.s)

0 commit comments

Comments
 (0)