make count(*) illegal.

pull/17/head
John Shahid 2013-10-24 11:19:57 -04:00
parent c207be4a18
commit 113d2ff0c7
3 changed files with 31 additions and 9 deletions

View File

@ -1,5 +1,9 @@
package common
import (
"fmt"
)
const (
WrongNumberOfArguments = iota
InvalidArgument
@ -15,6 +19,6 @@ func (self *QueryError) Error() string {
return self.ErrorMsg
}
func NewQueryError(code int, msg string) *QueryError {
return &QueryError{code, msg}
func NewQueryError(code int, msg string, args ...interface{}) *QueryError {
return &QueryError{code, fmt.Sprintf(msg, args...)}
}

View File

@ -69,7 +69,19 @@ func (self *CountAggregator) GetValue(series string, group interface{}) []*proto
func (self *CountAggregator) InitializeFieldsMetadata(series *protocol.Series) error { return nil }
func NewCountAggregator(*parser.Query, *parser.Value) (Aggregator, error) {
func NewCountAggregator(_ *parser.Query, v *parser.Value) (Aggregator, error) {
if len(v.Elems) != 1 {
return nil, common.NewQueryError(common.WrongNumberOfArguments, "function count() requires exactly one argument")
}
if v.Elems[0].Type == parser.ValueWildcard {
return nil, common.NewQueryError(common.InvalidArgument, "function count() doesn't work with wildcards")
}
if v.Elems[0].Type != parser.ValueSimpleName {
return nil, common.NewQueryError(common.InvalidArgument, "%s isn't a valid argument to count")
}
return &CountAggregator{make(map[string]map[interface{}]int32)}, nil
}

View File

@ -162,7 +162,7 @@ func (self *EngineSuite) TestCountQuery(c *C) {
]
`)
runQuery(engine, "select count(*) from foo;", c, `[
runQuery(engine, "select count(column_one) from foo;", c, `[
{
"points": [
{
@ -220,7 +220,7 @@ func (self *EngineSuite) TestCountQueryWithRegexTables(c *C) {
]
`)
runQuery(engine, "select count(*) from /foo.*/;", c, `[
runQuery(engine, "select count(column_one) from /foo.*/;", c, `[
{
"points": [
{
@ -287,7 +287,7 @@ func (self *EngineSuite) TestCountQueryWithGroupByClause(c *C) {
]
`)
runQuery(engine, "select count(*), column_one from foo group by column_one;", c, `[
runQuery(engine, "select count(column_one), column_one from foo group by column_one;", c, `[
{
"points": [
{
@ -392,7 +392,7 @@ func (self *EngineSuite) TestCountQueryWithGroupByClauseWithMultipleColumns(c *C
]
`)
runQuery(engine, "select count(*), column_one, column_two from foo group by column_one, column_two;", c, `[
runQuery(engine, "select count(column_one), column_one, column_two from foo group by column_one, column_two;", c, `[
{
"points": [
{
@ -488,7 +488,7 @@ func (self *EngineSuite) TestCountQueryWithGroupByTime(c *C) {
]
`)
runQuery(engine, "select count(*) from foo group by time(1m);", c, `[
runQuery(engine, "select count(column_one) from foo group by time(1m);", c, `[
{
"points": [
{
@ -530,7 +530,7 @@ func (self *EngineSuite) TestCountQueryWithGroupByTimeAndColumn(c *C) {
}
]`)
runQuery(engine, "select count(*), column_one from foo group by time(1m), column_one;", c, `[
runQuery(engine, "select count(column_one), column_one from foo group by time(1m), column_one;", c, `[
{
"points": [
{ "values": [{ "int64_value": 1 }, { "string_value": "some_value" }], "timestamp": 1381346640000000, "sequence_number": 1 },
@ -799,6 +799,12 @@ func (self *EngineSuite) TestCountQueryWithGroupByTimeInvalidNumberOfArguments(c
runQueryRunError(engine, "select count(*) from foo group by time(1h, 1m);", c, err)
}
func (self *EngineSuite) TestCountQueryWithInvalidWildcardArgument(c *C) {
err := common.NewQueryError(common.InvalidArgument, "function count() doesn't work with wildcards")
engine := createEngine(c, `[]`)
runQueryRunError(engine, "select count(*) from foo;", c, err)
}
func (self *EngineSuite) TestCountQueryWithGroupByTimeInvalidArgument(c *C) {
err := common.NewQueryError(common.InvalidArgument, "invalid argument foobar to the time function")
engine := createEngine(c, `[]`)