Skip to content

Commit

Permalink
Merge pull request #9651 from influxdata/js-1.5-backport-9633
Browse files Browse the repository at this point in the history
Fix regression to allow now() to be used as the group by offset again
  • Loading branch information
jsternberg committed Mar 28, 2018
2 parents fee27ec + b4fcb1c commit 3ebf477
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
13 changes: 12 additions & 1 deletion query/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,10 @@ func (c *compiledField) compileTopBottom(call *influxql.Call) error {

func (c *compiledStatement) compileDimensions(stmt *influxql.SelectStatement) error {
for _, d := range stmt.Dimensions {
switch expr := d.Expr.(type) {
// Reduce the expression before attempting anything. Do not evaluate the call.
expr := influxql.Reduce(d.Expr, nil)

switch expr := expr.(type) {
case *influxql.VarRef:
if strings.ToLower(expr.Val) == "time" {
return errors.New("time() is a function and expects at least one argument")
Expand Down Expand Up @@ -691,6 +694,11 @@ func (c *compiledStatement) compileDimensions(stmt *influxql.SelectStatement) er
}
now := c.Options.Now
c.Interval.Offset = now.Sub(now.Truncate(c.Interval.Duration))

// Use the evaluated offset to replace the argument. Ideally, we would
// use the interval assigned above, but the query engine hasn't been changed
// to use the compiler information yet.
expr.Args[1] = &influxql.DurationLiteral{Val: c.Interval.Offset}
case *influxql.StringLiteral:
// If literal looks like a date time then parse it as a time literal.
if lit.IsTimeLiteral() {
Expand All @@ -712,6 +720,9 @@ func (c *compiledStatement) compileDimensions(stmt *influxql.SelectStatement) er
default:
return errors.New("only time and tag dimensions allowed")
}

// Assign the reduced/changed expression to the dimension.
d.Expr = expr
}
return nil
}
Expand Down
39 changes: 38 additions & 1 deletion query/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestSelect(t *testing.T) {
expr string
itrs []query.Iterator
points [][]query.Point
now time.Time
err string
}{
{
Expand Down Expand Up @@ -2757,6 +2758,27 @@ func TestSelect(t *testing.T) {
{&query.FloatPoint{Name: "cpu", Time: 22 * Second, Value: 7.953140268154609}},
},
},
{
name: "GroupByOffset",
q: `SELECT mean(value) FROM cpu WHERE time >= now() - 2m AND time < now() GROUP BY time(1m, now())`,
typ: influxql.Float,
expr: `mean(value::float)`,
itrs: []query.Iterator{
&FloatIterator{Points: []query.FloatPoint{
{Name: "cpu", Tags: ParseTags("region=west,host=A"), Time: 34 * Second, Value: 20},
{Name: "cpu", Tags: ParseTags("region=west,host=A"), Time: 57 * Second, Value: 3},
{Name: "cpu", Tags: ParseTags("region=west,host=A"), Time: 92 * Second, Value: 100},
}},
&FloatIterator{Points: []query.FloatPoint{
{Name: "cpu", Tags: ParseTags("region=west,host=B"), Time: 45 * Second, Value: 10},
}},
},
points: [][]query.Point{
{&query.FloatPoint{Name: "cpu", Time: 30 * Second, Value: 11, Aggregated: 3}},
{&query.FloatPoint{Name: "cpu", Time: 90 * Second, Value: 100, Aggregated: 1}},
},
now: mustParseTime("1970-01-01T00:02:30Z"),
},
} {
t.Run(tt.name, func(t *testing.T) {
shardMapper := ShardMapper{
Expand Down Expand Up @@ -2790,7 +2812,22 @@ func TestSelect(t *testing.T) {
},
}

itrs, _, err := query.Select(context.Background(), MustParseSelectStatement(tt.q), &shardMapper, query.SelectOptions{})
stmt := MustParseSelectStatement(tt.q)
stmt.OmitTime = true
itrs, _, err := func(stmt *influxql.SelectStatement) ([]query.Iterator, []string, error) {
c, err := query.Compile(stmt, query.CompileOptions{
Now: tt.now,
})
if err != nil {
return nil, nil, err
}

p, err := c.Prepare(&shardMapper, query.SelectOptions{})
if err != nil {
return nil, nil, err
}
return p.Select(context.Background())
}(stmt)
if err != nil {
if tt.err == "" {
t.Fatal(err)
Expand Down

0 comments on commit 3ebf477

Please sign in to comment.