Skip to content

Commit

Permalink
Forbid wildcards in binary expressions
Browse files Browse the repository at this point in the history
When rewriting fields, wildcards within binary expressions were skipped.
This now throws an error whenever it finds a wildcard within a binary
expression in order to prevent the panic that occurs.
  • Loading branch information
jsternberg committed Mar 15, 2017
1 parent a18a634 commit 42de18f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
- [#7821](https://github.com/influxdata/influxdb/issues/7821): Expose some configuration settings via SHOW DIAGNOSTICS
- [#8025](https://github.com/influxdata/influxdb/issues/8025): Support single and multiline comments in InfluxQL.

### Bugfixes

- [#8064](https://github.com/influxdata/influxdb/issues/8064): Forbid wildcards in binary expressions.

## v1.2.2 [2017-03-14]

### Release Notes
Expand Down
20 changes: 20 additions & 0 deletions influxql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,26 @@ func (s *SelectStatement) RewriteFields(m FieldMapper) (*SelectStatement, error)
Alias: fmt.Sprintf("%s_%s", f.Name(), ref.Val),
})
}
case *BinaryExpr:
// Search for regexes or wildcards within the binary
// expression. If we find any, throw an error indicating that
// it's illegal.
var regex, wildcard bool
WalkFunc(expr, func(n Node) {
switch n.(type) {
case *RegexLiteral:
regex = true
case *Wildcard:
wildcard = true
}
})

if wildcard {
return nil, fmt.Errorf("unsupported expression with wildcard: %s", f.Expr)
} else if regex {
return nil, fmt.Errorf("unsupported expression with regex field: %s", f.Expr)
}
rwFields = append(rwFields, f)
default:
rwFields = append(rwFields, f)
}
Expand Down
50 changes: 44 additions & 6 deletions influxql/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func TestSelectStatement_RewriteFields(t *testing.T) {
var tests = []struct {
stmt string
rewrite string
err string
}{
// No wildcards
{
Expand Down Expand Up @@ -462,6 +463,35 @@ func TestSelectStatement_RewriteFields(t *testing.T) {
stmt: `SELECT * FROM (SELECT mean(value1) FROM cpu GROUP BY host) GROUP BY *`,
rewrite: `SELECT mean::float FROM (SELECT mean(value1::float) FROM cpu GROUP BY host) GROUP BY host`,
},

// Invalid queries that can't be rewritten should return an error (to
// avoid a panic in the query engine)
{
stmt: `SELECT count(*) / 2 FROM cpu`,
err: `unsupported expression with wildcard: count(*) / 2`,
},

{
stmt: `SELECT * / 2 FROM (SELECT count(*) FROM cpu)`,
err: `unsupported expression with wildcard: * / 2`,
},

{
stmt: `SELECT count(/value/) / 2 FROM cpu`,
err: `unsupported expression with regex field: count(/value/) / 2`,
},

// This one should be possible though since there's no wildcard in the
// binary expression.
{
stmt: `SELECT value1 + value2, * FROM cpu`,
rewrite: `SELECT value1::float + value2::integer, host::tag, region::tag, value1::float, value2::integer FROM cpu`,
},

{
stmt: `SELECT value1 + value2, /value/ FROM cpu`,
rewrite: `SELECT value1::float + value2::integer, value1::float, value2::integer FROM cpu`,
},
}

for i, tt := range tests {
Expand Down Expand Up @@ -496,12 +526,20 @@ func TestSelectStatement_RewriteFields(t *testing.T) {

// Rewrite statement.
rw, err := stmt.(*influxql.SelectStatement).RewriteFields(&ic)
if err != nil {
t.Errorf("%d. %q: error: %s", i, tt.stmt, err)
} else if rw == nil {
t.Errorf("%d. %q: unexpected nil statement", i, tt.stmt)
} else if rw := rw.String(); tt.rewrite != rw {
t.Errorf("%d. %q: unexpected rewrite:\n\nexp=%s\n\ngot=%s\n\n", i, tt.stmt, tt.rewrite, rw)
if tt.err != "" {
if err != nil && err.Error() != tt.err {
t.Errorf("%d. %q: unexpected error: %s != %s", i, tt.stmt, err.Error(), tt.err)
} else if err == nil {
t.Errorf("%d. %q: expected error", i, tt.stmt)
}
} else {
if err != nil {
t.Errorf("%d. %q: error: %s", i, tt.stmt, err)
} else if rw == nil && tt.err == "" {
t.Errorf("%d. %q: unexpected nil statement", i, tt.stmt)
} else if rw := rw.String(); tt.rewrite != rw {
t.Errorf("%d. %q: unexpected rewrite:\n\nexp=%s\n\ngot=%s\n\n", i, tt.stmt, tt.rewrite, rw)
}
}
}
}
Expand Down

0 comments on commit 42de18f

Please sign in to comment.