Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid wildcards in binary expressions #8110

Merged
merged 1 commit into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
### Bugfixes

- [#8091](https://github.com/influxdata/influxdb/issues/8091): Do not increment the continuous query statistic if no query is run.
- [#8064](https://github.com/influxdata/influxdb/issues/8064): Forbid wildcards in binary expressions.

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

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