From 3f9eacf24cd052bbfec593e2838c598f6a741cd2 Mon Sep 17 00:00:00 2001 From: Cory LaNou Date: Tue, 12 May 2015 14:30:01 -0600 Subject: [PATCH] add validations to parser for distinct --- influxql/ast.go | 31 +++++++++++++++++++++++++++++++ influxql/parser_test.go | 5 +++++ 2 files changed, 36 insertions(+) diff --git a/influxql/ast.go b/influxql/ast.go index b2b837284d..ca7cf7ad04 100644 --- a/influxql/ast.go +++ b/influxql/ast.go @@ -893,6 +893,22 @@ func (s *SelectStatement) hasTimeDimensions(node Node) bool { // Validate checks certain edge conditions to determine if this is a valid select statment func (s *SelectStatement) Validate(tr targetRequirement) error { + if err := s.ValidateAggregates(tr); err != nil { + return err + } + + if err := s.ValidateDistinct(); err != nil { + return err + } + + if err := s.validateDerivative(); err != nil { + return err + } + + return nil +} + +func (s *SelectStatement) ValidateAggregates(tr targetRequirement) error { // fetch the group by duration groupByDuration, _ := s.GroupByInterval() @@ -907,6 +923,21 @@ func (s *SelectStatement) Validate(tr targetRequirement) error { return fmt.Errorf("aggregate functions with GROUP BY time require a WHERE time clause") } } + return nil +} + +func (s *SelectStatement) ValidateDistinct() error { + if !s.Distinct { + return nil + } + + if len(s.Fields) > 1 { + return fmt.Errorf("select DISTINCT may only have one field") + } + + if !s.IsRawQuery { + return fmt.Errorf("select DISTINCT does not allow for aggregate functions") + } if err := s.validateDerivative(); err != nil { return err diff --git a/influxql/parser_test.go b/influxql/parser_test.go index 43a5fde8fb..cbbe6d9b8b 100644 --- a/influxql/parser_test.go +++ b/influxql/parser_test.go @@ -1063,6 +1063,11 @@ func TestParser_ParseStatement(t *testing.T) { {s: `SELECT 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 FROM myseries`, err: `unable to parse number at line 1, char 8`}, {s: `SELECT 10.5h FROM myseries`, err: `found h, expected FROM at line 1, char 12`}, {s: `SELECT derivative(field1), field1 FROM myseries`, err: `derivative cannot be used with other fields`}, + {s: `SELECT DISTINCT FROM myseries`, err: `found FROM, expected identifier, string, number, bool at line 1, char 17`}, + {s: `SELECT DISTINCT field1, field2 FROM myseries`, err: `select DISTINCT may only have one field`}, + {s: `SELECT DISTINCT field1, sum(field1) FROM myseries`, err: `select DISTINCT may only have one field`}, + {s: `SELECT DISTINCT field1, count(field2) FROM myseries`, err: `select DISTINCT may only have one field`}, + {s: `SELECT DISTINCT sum(field1) FROM myseries`, err: `select DISTINCT does not allow for aggregate functions`}, {s: `DELETE`, err: `found EOF, expected FROM at line 1, char 8`}, {s: `DELETE FROM`, err: `found EOF, expected identifier at line 1, char 13`}, {s: `DELETE FROM myseries WHERE`, err: `found EOF, expected identifier, string, number, bool at line 1, char 28`},