Skip to content

Commit

Permalink
Allow math functions to be used in the condition
Browse files Browse the repository at this point in the history
  • Loading branch information
jsternberg committed Apr 10, 2018
1 parent 529c028 commit 1f9227e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
40 changes: 35 additions & 5 deletions tsdb/engine/tsm1/iterator.gen.go
Expand Up @@ -270,7 +270,13 @@ func (itr *floatIterator) Next() (*query.FloatPoint, error) {
}

// Evaluate condition, if one exists. Retry if it fails.
if itr.opt.Condition != nil && !influxql.EvalBool(itr.opt.Condition, itr.m) {
valuer := influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}
if itr.opt.Condition != nil && !valuer.EvalBool(itr.opt.Condition) {
continue
}

Expand Down Expand Up @@ -742,7 +748,13 @@ func (itr *integerIterator) Next() (*query.IntegerPoint, error) {
}

// Evaluate condition, if one exists. Retry if it fails.
if itr.opt.Condition != nil && !influxql.EvalBool(itr.opt.Condition, itr.m) {
valuer := influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}
if itr.opt.Condition != nil && !valuer.EvalBool(itr.opt.Condition) {
continue
}

Expand Down Expand Up @@ -1214,7 +1226,13 @@ func (itr *unsignedIterator) Next() (*query.UnsignedPoint, error) {
}

// Evaluate condition, if one exists. Retry if it fails.
if itr.opt.Condition != nil && !influxql.EvalBool(itr.opt.Condition, itr.m) {
valuer := influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}
if itr.opt.Condition != nil && !valuer.EvalBool(itr.opt.Condition) {
continue
}

Expand Down Expand Up @@ -1686,7 +1704,13 @@ func (itr *stringIterator) Next() (*query.StringPoint, error) {
}

// Evaluate condition, if one exists. Retry if it fails.
if itr.opt.Condition != nil && !influxql.EvalBool(itr.opt.Condition, itr.m) {
valuer := influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}
if itr.opt.Condition != nil && !valuer.EvalBool(itr.opt.Condition) {
continue
}

Expand Down Expand Up @@ -2158,7 +2182,13 @@ func (itr *booleanIterator) Next() (*query.BooleanPoint, error) {
}

// Evaluate condition, if one exists. Retry if it fails.
if itr.opt.Condition != nil && !influxql.EvalBool(itr.opt.Condition, itr.m) {
valuer := influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}
if itr.opt.Condition != nil && !valuer.EvalBool(itr.opt.Condition) {
continue
}

Expand Down
8 changes: 7 additions & 1 deletion tsdb/engine/tsm1/iterator.gen.go.tmpl
Expand Up @@ -268,7 +268,13 @@ func (itr *{{.name}}Iterator) Next() (*query.{{.Name}}Point, error) {
}

// Evaluate condition, if one exists. Retry if it fails.
if itr.opt.Condition != nil && !influxql.EvalBool(itr.opt.Condition, itr.m) {
valuer := influxql.ValuerEval{
Valuer: influxql.MultiValuer(
query.MathValuer{},
influxql.MapValuer(itr.m),
),
}
if itr.opt.Condition != nil && !valuer.EvalBool(itr.opt.Condition) {
continue
}

Expand Down
17 changes: 13 additions & 4 deletions tsdb/index.go
Expand Up @@ -1850,7 +1850,13 @@ func (is IndexSet) seriesByBinaryExprIterator(name []byte, n *influxql.BinaryExp
if !ok {
key, ok = n.RHS.(*influxql.VarRef)
if !ok {
return nil, fmt.Errorf("invalid expression: %s", n.String())
// This is an expression we do not know how to evaluate. Let the
// query engine take care of this.
itr, err := is.measurementSeriesIDIterator(name)
if err != nil {
return nil, err
}
return newSeriesIDExprIterator(itr, n), nil
}
value = n.LHS
}
Expand Down Expand Up @@ -1882,10 +1888,13 @@ func (is IndexSet) seriesByBinaryExprIterator(name []byte, n *influxql.BinaryExp
case *influxql.VarRef:
return is.seriesByBinaryExprVarRefIterator(name, []byte(key.Val), value, n.Op)
default:
if n.Op == influxql.NEQ || n.Op == influxql.NEQREGEX {
return is.measurementSeriesIDIterator(name)
// We do not know how to evaluate this expression so pass it
// on to the query engine.
itr, err := is.measurementSeriesIDIterator(name)
if err != nil {
return nil, err
}
return nil, nil
return newSeriesIDExprIterator(itr, n), nil
}
}

Expand Down
11 changes: 6 additions & 5 deletions tsdb/index/inmem/meta.go
Expand Up @@ -632,7 +632,9 @@ func (m *measurement) idsForExpr(n *influxql.BinaryExpr) (seriesIDs, influxql.Ex
if !ok {
name, ok = n.RHS.(*influxql.VarRef)
if !ok {
return nil, nil, fmt.Errorf("invalid expression: %s", n.String())
// This is an expression we do not know how to evaluate. Let the
// query engine take care of this.
return m.SeriesIDs(), n, nil
}
value = n.LHS
}
Expand Down Expand Up @@ -771,10 +773,9 @@ func (m *measurement) idsForExpr(n *influxql.BinaryExpr) (seriesIDs, influxql.Ex
return ids, nil, nil
}

if n.Op == influxql.NEQ || n.Op == influxql.NEQREGEX {
return m.SeriesIDs(), nil, nil
}
return nil, nil, nil
// We do not know how to evaluate this expression so pass it
// on to the query engine.
return m.SeriesIDs(), n, nil
}

// FilterExprs represents a map of series IDs to filter expressions.
Expand Down

0 comments on commit 1f9227e

Please sign in to comment.