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

Remove restrictions on where derivative can be used entirely #6375

Merged
merged 1 commit into from
Apr 22, 2016
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
45 changes: 0 additions & 45 deletions influxql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1344,10 +1344,6 @@ func (s *SelectStatement) validate(tr targetRequirement) error {
return err
}

if err := s.validateDerivative(); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -1719,47 +1715,6 @@ func (s *SelectStatement) validateCountDistinct() error {
return nil
}

func (s *SelectStatement) validateDerivative() error {
if !s.HasDerivative() {
return nil
}

// If a derivative is requested, it must be the only field in the query. We don't support
// multiple fields in combination w/ derivaties yet.
if len(s.Fields) != 1 {
return fmt.Errorf("derivative cannot be used with other fields")
}

aggr := s.FunctionCalls()
if len(aggr) != 1 {
return fmt.Errorf("derivative cannot be used with other fields")
}

// Derivative requires two arguments
derivativeCall := aggr[0]
if len(derivativeCall.Args) == 0 {
return fmt.Errorf("derivative requires a field argument")
}

// First arg must be a field or aggr over a field e.g. (mean(field))
_, callOk := derivativeCall.Args[0].(*Call)
_, varOk := derivativeCall.Args[0].(*VarRef)

if !(callOk || varOk) {
return fmt.Errorf("derivative requires a field argument")
}

// If a duration arg is passed, make sure it's a duration
if len(derivativeCall.Args) == 2 {
// Second must be a duration .e.g (1h)
if _, ok := derivativeCall.Args[1].(*DurationLiteral); !ok {
return fmt.Errorf("derivative requires a duration argument")
}
}

return nil
}

// GroupByInterval extracts the time interval, if specified.
func (s *SelectStatement) GroupByInterval() (time.Duration, error) {
// return if we've already pulled it out
Expand Down
31 changes: 31 additions & 0 deletions influxql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,37 @@ func TestParser_ParseStatement(t *testing.T) {
},
},

{
s: `SELECT derivative(field1, 1h) / derivative(field2, 1h) FROM myseries`,
stmt: &influxql.SelectStatement{
IsRawQuery: false,
Fields: []*influxql.Field{
{
Expr: &influxql.BinaryExpr{
LHS: &influxql.Call{
Name: "derivative",
Args: []influxql.Expr{
&influxql.VarRef{Val: "field1"},
&influxql.DurationLiteral{Val: time.Hour},
},
},
RHS: &influxql.Call{
Name: "derivative",
Args: []influxql.Expr{
&influxql.VarRef{Val: "field2"},
&influxql.DurationLiteral{Val: time.Hour},
},
},
Op: influxql.DIV,
},
},
},
Sources: []influxql.Source{
&influxql.Measurement{Name: "myseries"},
},
},
},

// difference
{
s: `SELECT difference(field1) FROM myseries;`,
Expand Down