Skip to content

Commit

Permalink
Merge pull request #426 from influxdb/fix-426
Browse files Browse the repository at this point in the history
Fill should fill the entire time range that is requested
  • Loading branch information
pauldix committed Aug 6, 2014
2 parents e74b941 + 680e6d9 commit 69ba4ef
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
17 changes: 17 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ type QueryEngine struct {
where *parser.WhereCondition
fillWithZero bool

// was start time set in the query, e.g. time > now() - 1d
startTimeSpecified bool
startTime int64
endTime int64

// output fields
responseChan chan *protocol.Response
limiter *Limiter
Expand Down Expand Up @@ -356,6 +361,15 @@ func (self *QueryEngine) executeCountQueryWithGroupBy(query *parser.SelectQuery,

self.fillWithZero = query.GetGroupByClause().FillWithZero

// This is a special case for issue #426. If the start time is
// specified and there's a group by clause and fill with zero, then
// we need to fill the entire range from start time to end time
if query.IsStartTimeSpecified() && self.duration != nil && self.fillWithZero {
self.startTimeSpecified = true
self.startTime = query.GetStartTime().Truncate(*self.duration).UnixNano() / 1000
self.endTime = query.GetEndTime().Truncate(*self.duration).UnixNano() / 1000
}

self.initializeFields()

err = self.distributeQuery(query, func(series *protocol.Series) error {
Expand Down Expand Up @@ -520,6 +534,9 @@ func (self *QueryEngine) runAggregatesForTable(table string) {
var err error
if self.duration != nil && self.fillWithZero {
timestampRange := state.pointsRange
if self.startTimeSpecified {
timestampRange = &PointRange{startTime: self.startTime, endTime: self.endTime}
}

// TODO: DRY this
if self.query.Ascending {
Expand Down
27 changes: 27 additions & 0 deletions integration/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,33 @@ func (self *DataTestSuite) DifferenceValues(c *C) (Fun, Fun) {
}
}

// issue #426
func (self *DataTestSuite) FilllingEntireRange(c *C) (Fun, Fun) {
return func(client Client) {
data := `
[
{
"name": "test_filling_range",
"columns": ["value"],
"points": [
[1]
]
}
]`
client.WriteJsonData(data, c, influxdb.Millisecond)
}, func(client Client) {
serieses := client.RunQuery("select sum(value) from test_filling_range where time > now() - 1d group by time(1h) fill(0)", c, "m")
c.Assert(serieses, HasLen, 1)
maps := ToMap(serieses[0])
fmt.Printf("lenght: %d\n", len(maps))
c.Assert(maps, HasLen, 25)
c.Assert(maps[0]["sum"], Equals, 1.0)
for i := 1; i < len(maps); i++ {
c.Assert(maps[i]["sum"], Equals, 0.0)
}
}
}

func (self *DataTestSuite) ModeWithInt(c *C) (Fun, Fun) {
return func(client Client) {
data := `
Expand Down
6 changes: 4 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ type IntoClause struct {
}

type BasicQuery struct {
startTime time.Time
endTime time.Time
startTime time.Time
endTime time.Time
startTimeSpecified bool
}

type SelectDeleteCommonQuery struct {
Expand Down Expand Up @@ -681,6 +682,7 @@ func parseSelectDeleteCommonQuery(fromClause *C.from_clause, whereCondition *C.c

if startTime != nil {
goQuery.startTime = *startTime
goQuery.startTimeSpecified = true
}

return goQuery, nil
Expand Down
7 changes: 7 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func (self *QueryParserSuite) TestGetQueryString(c *C) {
c.Assert(actualQuery, HasLen, 1)
expectedQuery[0].QueryString = ""
actualQuery[0].QueryString = ""
if expectedQuery[0].DeleteQuery != nil {
expectedQuery[0].DeleteQuery.startTimeSpecified = false
actualQuery[0].DeleteQuery.startTimeSpecified = false
} else if expectedQuery[0].SelectQuery != nil {
expectedQuery[0].SelectQuery.startTimeSpecified = false
actualQuery[0].SelectQuery.startTimeSpecified = false
}

c.Assert(actualQuery[0], DeepEquals, expectedQuery[0])
}
Expand Down
4 changes: 4 additions & 0 deletions parser/query_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ func (self *BasicQuery) GetStartTime() time.Time {
return self.startTime
}

func (self *BasicQuery) IsStartTimeSpecified() bool {
return self.startTimeSpecified
}

// Returns the start time of the query. Queries can only have
// one condition of the form time > start_time
func (self *BasicQuery) GetEndTime() time.Time {
Expand Down

0 comments on commit 69ba4ef

Please sign in to comment.