refactor selectStatement validate

pull/2540/head
Cory LaNou 2015-05-12 08:56:38 -06:00
parent bfdf05033d
commit eb1a4f669a
1 changed files with 5 additions and 2 deletions

View File

@ -861,14 +861,17 @@ 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 {
// fetch the group by duration
groupByDuration, _ := s.GroupByInterval()
// If we have a group by interval, but no aggregate function, it's an invalid statement
if d, _ := s.GroupByInterval(); s.IsRawQuery && d > 0 {
if s.IsRawQuery && groupByDuration > 0 {
return fmt.Errorf("GROUP BY requires at least one aggregate function")
}
// If we have an aggregate function with a group by time without a where clause, it's an invalid statement
if tr == targetNotRequired { // ignore create continuous query statements
if d, _ := s.GroupByInterval(); !s.IsRawQuery && d > 0 && !s.hasTimeDimensions(s.Condition) {
if !s.IsRawQuery && groupByDuration > 0 && !s.hasTimeDimensions(s.Condition) {
return fmt.Errorf("aggregate functions with GROUP BY time require a WHERE time clause")
}
}