From f1fbb55909677215333e187a08a71ac575d5938f Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Tue, 25 Oct 2016 15:05:44 -0500 Subject: [PATCH] Change default time boundaries for raw queries Changes the default time boundaries for raw queries so raw queries will range until the end of time. Aggregate queries continue to have their default end time be `now()`. --- CHANGELOG.md | 1 + cmd/influxd/run/server_test.go | 57 +++++++++++++++++++++++++++++++ coordinator/statement_executor.go | 8 ++++- 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4794333941d..4e907e537c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ The following configuration changes in the `[data]` section may need to changed - [#7320](https://github.com/influxdata/influxdb/issues/7320): Update defaults in config for latest best practices - [#7495](https://github.com/influxdata/influxdb/pull/7495): Rewrite regexes of the form host = /^server-a$/ to host = 'server-a', to take advantage of the tsdb index. - [#6704](https://github.com/influxdata/influxdb/issues/6704): Optimize first/last when no group by interval is present. +- [#4461](https://github.com/influxdata/influxdb/issues/4461): Change default time boundaries for raw queries. ### Bugfixes diff --git a/cmd/influxd/run/server_test.go b/cmd/influxd/run/server_test.go index 5a6dd62fb48..bccd56534f6 100644 --- a/cmd/influxd/run/server_test.go +++ b/cmd/influxd/run/server_test.go @@ -6981,3 +6981,60 @@ func TestServer_WhereTimeInclusive(t *testing.T) { } } } + +func TestServer_Query_ImplicitEndTime(t *testing.T) { + t.Parallel() + s := OpenServer(NewConfig()) + defer s.Close() + + if err := s.CreateDatabaseAndRetentionPolicy("db0", newRetentionPolicySpec("rp0", 1, 0)); err != nil { + t.Fatal(err) + } + if err := s.MetaClient.SetDefaultRetentionPolicy("db0", "rp0"); err != nil { + t.Fatal(err) + } + + now := time.Now().UTC().Truncate(time.Second) + past := now.Add(-10 * time.Second) + future := now.Add(10 * time.Minute) + writes := []string{ + fmt.Sprintf(`cpu value=1 %d`, past.UnixNano()), + fmt.Sprintf(`cpu value=2 %d`, future.UnixNano()), + } + + test := NewTest("db0", "rp0") + test.writes = Writes{ + &Write{data: strings.Join(writes, "\n")}, + } + + test.addQueries([]*Query{ + &Query{ + name: "raw query", + params: url.Values{"db": []string{"db0"}}, + command: `SELECT * FROM cpu`, + exp: fmt.Sprintf(`{"results":[{"series":[{"name":"cpu","columns":["time","value"],"values":[["%s",1],["%s",2]]}]}]}`, past.Format(time.RFC3339Nano), future.Format(time.RFC3339Nano)), + }, + &Query{ + name: "aggregate query", + params: url.Values{"db": []string{"db0"}}, + command: `SELECT mean(value) FROM cpu WHERE time > now() - 1m GROUP BY time(1m) FILL(none)`, + exp: fmt.Sprintf(`{"results":[{"series":[{"name":"cpu","columns":["time","mean"],"values":[["%s",1]]}]}]}`, now.Truncate(time.Minute).Format(time.RFC3339Nano)), + }, + }...) + + if err := test.init(s); err != nil { + t.Fatalf("test init failed: %s", err) + } + + for _, query := range test.queries { + if query.skip { + t.Logf("SKIP:: %s", query.name) + continue + } + if err := query.Execute(s); err != nil { + t.Error(query.Error(err)) + } else if !query.success() { + t.Error(query.failureMessage()) + } + } +} diff --git a/coordinator/statement_executor.go b/coordinator/statement_executor.go index b84da9f7c52..a5ec146c95e 100644 --- a/coordinator/statement_executor.go +++ b/coordinator/statement_executor.go @@ -529,7 +529,13 @@ func (e *StatementExecutor) createIterators(stmt *influxql.SelectStatement, ctx if influxql.Sources(stmt.Sources).HasSystemSource() { opt.MaxTime = time.Unix(0, influxql.MaxTime).UTC() } else { - opt.MaxTime = now + if interval, err := stmt.GroupByInterval(); err != nil { + return nil, stmt, err + } else if interval > 0 { + opt.MaxTime = now + } else { + opt.MaxTime = time.Unix(0, influxql.MaxTime).UTC() + } } } if opt.MinTime.IsZero() {