parser_agg_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package xsql
  15. import (
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. "github.com/lf-edge/ekuiper/internal/testx"
  21. )
  22. func TestIsAggStatement(t *testing.T) {
  23. tests := []struct {
  24. s string
  25. agg bool
  26. err string
  27. }{
  28. {s: `SELECT avg(1) FROM tbl`, agg: true},
  29. {s: `SELECT sin(1) FROM tbl`, agg: false},
  30. {s: `SELECT sin(avg(f1)) FROM tbl`, agg: true},
  31. {s: `SELECT sum(f1) FROM tbl GROUP by f1`, agg: true},
  32. {s: `SELECT f1 FROM tbl GROUP by f1`, agg: true},
  33. {s: `SELECT count(f1) FROM tbl`, agg: true},
  34. {s: `SELECT max(f1) FROM tbl`, agg: true},
  35. {s: `SELECT min(f1) FROM tbl`, agg: true},
  36. {s: `SELECT count(f1) FROM tbl group by tumblingwindow(ss, 5)`, agg: true},
  37. {s: `SELECT f1 FROM tbl group by tumblingwindow(ss, 5) having count(f1) > 3`, agg: false},
  38. {s: `SELECT f1 FROM tbl left join tbl2 on tbl1.f1 = tbl2.f2`, agg: false},
  39. }
  40. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  41. for i, tt := range tests {
  42. // fmt.Printf("Parsing SQL %q.\n", tt.s)
  43. stmt, err := NewParser(strings.NewReader(tt.s)).Parse()
  44. isAgg := WithAggFields(stmt)
  45. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  46. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  47. } else if tt.err == "" && (tt.agg != isAgg) {
  48. t.Errorf("Error: expected %t, actual %t.", tt.agg, isAgg)
  49. }
  50. }
  51. }