Skip to content

Commit

Permalink
Fix #557. Using group by time(1y) doesn't work while time(365d) is fine
Browse files Browse the repository at this point in the history
  • Loading branch information
jvshahid committed May 20, 2014
1 parent a64ee2a commit b6a5a10
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Bugfixes

- [Issue #557](https://github.com/influxdb/influxdb/issues/557). Group by time(1y) doesn't work while time(365d) works

## v0.6.5 [2014-05-19]

### Features
Expand Down
6 changes: 6 additions & 0 deletions src/datastore/leveldb_shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ func (self *LevelDbShard) executeQueryForSeries(querySpec *parser.QuerySpec, ser
// because a db is distributed across the cluster, it's possible we don't have the series indexed here. ignore
switch err := err.(type) {
case FieldLookupError:
log.Debug("Cannot find fields %v", columns)
return nil
default:
log.Error("Error looking up fields for %s: %s", seriesName, err)
return fmt.Errorf("Error looking up fields for %s: %s", seriesName, err)
}
}
Expand All @@ -189,6 +191,7 @@ func (self *LevelDbShard) executeQueryForSeries(querySpec *parser.QuerySpec, ser
if querySpec.IsSinglePointQuery() {
series, err := self.fetchSinglePoint(querySpec, seriesName, fields)
if err != nil {
log.Error("Error reading a single point: %s", err)
return err
}
if len(series.Points) > 0 {
Expand Down Expand Up @@ -275,6 +278,7 @@ func (self *LevelDbShard) executeQueryForSeries(querySpec *parser.QuerySpec, ser
valueBuffer.SetBuf(rawColumnValues[i].value)
err := valueBuffer.Unmarshal(fv)
if err != nil {
log.Error("Error while running query: %s", err)
return err
}
point.Values[i] = fv
Expand Down Expand Up @@ -313,6 +317,7 @@ func (self *LevelDbShard) executeQueryForSeries(querySpec *parser.QuerySpec, ser
Points: seriesOutgoing.Points,
}
if !processor.YieldSeries(series) {
log.Info("Stopping processing")
shouldContinue = false
}
}
Expand All @@ -333,6 +338,7 @@ func (self *LevelDbShard) executeQueryForSeries(querySpec *parser.QuerySpec, ser
}
}

log.Debug("Finished running query %s")
return nil
}

Expand Down
18 changes: 18 additions & 0 deletions src/integration/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1921,3 +1921,21 @@ func (self *DataTestSuite) BottomWithMultipleGroupBy(c *C) (Fun, Fun) {
c.Assert(tops, DeepEquals, []tmp{tmp{60, "hosta"}, tmp{70, "hosta"}, tmp{70, "hostb"}, tmp{80, "hostb"}})
}
}

// issue #557
func (self *DataTestSuite) GroupByYear(c *C) (Fun, Fun) {
return func(client Client) {
data := `[{"points": [[4], [10], [5]], "name": "test_group_by_day", "columns": ["value"]}]`
client.WriteJsonData(data, c)
t := time.Now().Truncate(time.Hour).Add(-24 * 365 * time.Hour).Unix()
data = fmt.Sprintf(`[{"points": [[2, %d]], "name": "test_group_by_day", "columns": ["value", "time"]}]`, t)
client.WriteJsonData(data, c, "s")
}, func(client Client) {
collection := client.RunQuery("select count(value) from test_group_by_day group by time(1y)", c)
c.Assert(collection, HasLen, 1)
maps := ToMap(collection[0])
c.Assert(maps, HasLen, 2)
c.Assert(maps[0]["count"], Equals, 3.0)
c.Assert(maps[1]["count"], Equals, 1.0)
}
}
11 changes: 8 additions & 3 deletions src/parser/group_by.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"bytes"
"common"
"fmt"
"strings"
"time"

log "code.google.com/p/log4go"
)

type GroupByClause struct {
Expand All @@ -15,13 +18,15 @@ type GroupByClause struct {

func (self GroupByClause) GetGroupByTime() (*time.Duration, error) {
for _, groupBy := range self.Elems {
if groupBy.IsFunctionCall() {
if groupBy.IsFunctionCall() && strings.ToLower(groupBy.Name) == "time" {
// TODO: check the number of arguments and return an error
if len(groupBy.Elems) != 1 {
return nil, common.NewQueryError(common.WrongNumberOfArguments, "time function only accepts one argument")
}
// TODO: check the function name
// TODO: error checking

if groupBy.Elems[0].Type != ValueDuration {
log.Debug("Get a time function without a duration argument %s", groupBy.Elems[0].Type)
}
arg := groupBy.Elems[0].Name
durationInt, err := common.ParseTimeDuration(arg)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/query.lex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static int yycolumn = 1;

[0-9]+ { yylval->string = strdup(yytext); return INT_VALUE; }

([0-9]+|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)[usmhdw] { yylval->string = strdup(yytext); return DURATION; }
([0-9]+|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)[usmhdwy] { yylval->string = strdup(yytext); return DURATION; }

[0-9]*\.[0-9]+|[0-9]+\.[0-9]* { yylval->string = strdup(yytext); return FLOAT_VALUE; }

Expand Down

0 comments on commit b6a5a10

Please sign in to comment.