diff --git a/query/compile.go b/query/compile.go index d06c54762a2..acb6e4c0534 100644 --- a/query/compile.go +++ b/query/compile.go @@ -320,6 +320,22 @@ func (c *compiledField) compileExpr(expr influxql.Expr) error { return errors.New("unimplemented") } +// compileNestedExpr ensures that the expression is compiled as if it were +// a nested expression. +func (c *compiledField) compileNestedExpr(expr influxql.Expr) error { + // Intercept the distinct call so we can pass nested as true. + switch expr := expr.(type) { + case *influxql.Call: + if expr.Name == "distinct" { + return c.compileDistinct(expr.Args, true) + } + case *influxql.Distinct: + call := expr.NewCall() + return c.compileDistinct(call.Args, true) + } + return c.compileExpr(expr) +} + func (c *compiledField) compileSymbol(name string, field influxql.Expr) error { // Must be a variable reference, wildcard, or regexp. switch field.(type) { @@ -431,7 +447,7 @@ func (c *compiledField) compileDerivative(args []influxql.Expr, isNonNegative bo if c.global.Interval.IsZero() { return fmt.Errorf("%s aggregate requires a GROUP BY interval", name) } - return c.compileExpr(arg0) + return c.compileNestedExpr(arg0) default: if !c.global.Interval.IsZero() { return fmt.Errorf("aggregate function required inside the call to %s", name) @@ -464,7 +480,7 @@ func (c *compiledField) compileElapsed(args []influxql.Expr) error { if c.global.Interval.IsZero() { return fmt.Errorf("elapsed aggregate requires a GROUP BY interval") } - return c.compileExpr(arg0) + return c.compileNestedExpr(arg0) default: if !c.global.Interval.IsZero() { return fmt.Errorf("aggregate function required inside the call to elapsed") @@ -490,7 +506,7 @@ func (c *compiledField) compileDifference(args []influxql.Expr, isNonNegative bo if c.global.Interval.IsZero() { return fmt.Errorf("%s aggregate requires a GROUP BY interval", name) } - return c.compileExpr(arg0) + return c.compileNestedExpr(arg0) default: if !c.global.Interval.IsZero() { return fmt.Errorf("aggregate function required inside the call to %s", name) @@ -511,7 +527,7 @@ func (c *compiledField) compileCumulativeSum(args []influxql.Expr) error { if c.global.Interval.IsZero() { return fmt.Errorf("cumulative_sum aggregate requires a GROUP BY interval") } - return c.compileExpr(arg0) + return c.compileNestedExpr(arg0) default: if !c.global.Interval.IsZero() { return fmt.Errorf("aggregate function required inside the call to cumulative_sum") @@ -541,7 +557,7 @@ func (c *compiledField) compileMovingAverage(args []influxql.Expr) error { if c.global.Interval.IsZero() { return fmt.Errorf("moving_average aggregate requires a GROUP BY interval") } - return c.compileExpr(arg0) + return c.compileNestedExpr(arg0) default: if !c.global.Interval.IsZero() { return fmt.Errorf("aggregate function required inside the call to moving_average") @@ -602,7 +618,7 @@ func (c *compiledField) compileHoltWinters(args []influxql.Expr, withFit bool) e } else if c.global.Interval.IsZero() { return fmt.Errorf("%s aggregate requires a GROUP BY interval", name) } - return c.compileExpr(call) + return c.compileNestedExpr(call) } func (c *compiledField) compileDistinct(args []influxql.Expr, nested bool) error { diff --git a/query/compile_test.go b/query/compile_test.go index 6f5a3884dd5..8cf2d60ac2a 100644 --- a/query/compile_test.go +++ b/query/compile_test.go @@ -81,6 +81,10 @@ func TestCompile_Success(t *testing.T) { `SELECT value FROM cpu WHERE time >= '2000-01-01T00:00:00Z' AND time <= '2000-01-01T01:00:00Z'`, `SELECT value FROM (SELECT value FROM cpu) ORDER BY time DESC`, `SELECT count(distinct(value)), max(value) FROM cpu`, + `SELECT derivative(distinct(value)), difference(distinct(value)) FROM cpu WHERE time >= now() - 1m GROUP BY time(5s)`, + `SELECT moving_average(distinct(value), 3) FROM cpu WHERE time >= now() - 5m GROUP BY time(1m)`, + `SELECT elapsed(distinct(value)) FROM cpu WHERE time >= now() - 5m GROUP BY time(1m)`, + `SELECT cumulative_sum(distinct(value)) FROM cpu WHERE time >= now() - 5m GROUP BY time(1m)`, `SELECT last(value) / (1 - 0) FROM cpu`, `SELECT abs(value) FROM cpu`, `SELECT sin(value) FROM cpu`,