Skip to content

Commit

Permalink
Merge pull request #6375 from influxdata/js-6118-derivative-on-multip…
Browse files Browse the repository at this point in the history
…le-fields

Remove restrictions on where derivative can be used entirely
  • Loading branch information
jsternberg committed Apr 22, 2016
2 parents d55fb1b + 22a0505 commit c77cbb8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 45 deletions.
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

0 comments on commit c77cbb8

Please sign in to comment.