add validations to parser for distinct

pull/2568/head
Cory LaNou 2015-05-12 14:30:01 -06:00
parent b6b916edbc
commit 3f9eacf24c
2 changed files with 36 additions and 0 deletions

View File

@ -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

View File

@ -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`},