Skip to content

Commit

Permalink
Support using field regex comparisons in the WHERE clause
Browse files Browse the repository at this point in the history
Fixes #2715.
  • Loading branch information
jsternberg committed Mar 9, 2016
1 parent 425ef2c commit 4ba3386
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [#5844](https://github.com/influxdata/influxdb/pull/5844): Tag TSM engine stats with database and retention policy
- [#5593](https://github.com/influxdata/influxdb/issues/5593): Modify `SHOW TAG VALUES` output for the new query engine to normalize the output.
- [#5862](https://github.com/influxdata/influxdb/pull/5862): Make Admin UI dynamically fetch both client and server versions
- [#2715](https://github.com/influxdata/influxdb/issues/2715): Support using field regex comparisons in the WHERE clause

### Bugfixes

Expand Down
15 changes: 12 additions & 3 deletions influxql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3485,6 +3485,8 @@ func Eval(expr Expr, m map[string]interface{}) interface{} {
return expr.Val
case *ParenExpr:
return Eval(expr.Expr, m)
case *RegexLiteral:
return expr.Val
case *StringLiteral:
return expr.Val
case *VarRef:
Expand Down Expand Up @@ -3570,12 +3572,19 @@ func evalBinaryExpr(expr *BinaryExpr, m map[string]interface{}) interface{} {
return lhs / rhs
}
case string:
rhs, _ := rhs.(string)
switch expr.Op {
case EQ:
return lhs == rhs
rhs, ok := rhs.(string)
return ok && lhs == rhs
case NEQ:
return lhs != rhs
rhs, ok := rhs.(string)
return ok && lhs != rhs
case EQREGEX:
rhs, ok := rhs.(*regexp.Regexp)
return ok && rhs.MatchString(lhs)
case NEQREGEX:
rhs, ok := rhs.(*regexp.Regexp)
return ok && !rhs.MatchString(lhs)
}
}
return nil
Expand Down
8 changes: 8 additions & 0 deletions influxql/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,11 +1014,19 @@ func TestEval(t *testing.T) {
{in: `'foo' = 'bar'`, out: false},
{in: `'foo' = 'foo'`, out: true},

// Regex literals.
{in: `'foo' =~ /f.*/`, out: true},
{in: `'foo' =~ /b.*/`, out: false},
{in: `'foo' !~ /f.*/`, out: false},
{in: `'foo' !~ /b.*/`, out: true},

// Variable references.
{in: `foo`, out: "bar", data: map[string]interface{}{"foo": "bar"}},
{in: `foo = 'bar'`, out: true, data: map[string]interface{}{"foo": "bar"}},
{in: `foo = 'bar'`, out: nil, data: map[string]interface{}{"foo": nil}},
{in: `foo <> 'bar'`, out: true, data: map[string]interface{}{"foo": "xxx"}},
{in: `foo =~ /b.*/`, out: true, data: map[string]interface{}{"foo": "bar"}},
{in: `foo !~ /b.*/`, out: false, data: map[string]interface{}{"foo": "bar"}},
} {
// Evaluate expression.
out := influxql.Eval(MustParseExpr(tt.in), tt.data)
Expand Down

0 comments on commit 4ba3386

Please sign in to comment.