add validations to parser for distinct
parent
b6b916edbc
commit
3f9eacf24c
|
@ -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
|
// Validate checks certain edge conditions to determine if this is a valid select statment
|
||||||
func (s *SelectStatement) Validate(tr targetRequirement) error {
|
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
|
// fetch the group by duration
|
||||||
groupByDuration, _ := s.GroupByInterval()
|
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 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 {
|
if err := s.validateDerivative(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -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 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 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 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`, 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`, 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`},
|
{s: `DELETE FROM myseries WHERE`, err: `found EOF, expected identifier, string, number, bool at line 1, char 28`},
|
||||||
|
|
Loading…
Reference in New Issue