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

Fix the validation for multiple nested distinct calls #9751

Merged
merged 1 commit into from
Apr 23, 2018
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
28 changes: 22 additions & 6 deletions query/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions query/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down