checkAgg.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2021 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 ast
  15. // IsAggregate check if an expression is aggregate with the binding alias info
  16. func IsAggregate(expr Expr) (r bool) {
  17. WalkFunc(expr, func(n Node) bool {
  18. switch f := n.(type) {
  19. case *Call:
  20. if ok := FuncFinderSingleton().IsAggFunc(f); ok {
  21. r = true
  22. return false
  23. }
  24. case *FieldRef:
  25. if f.IsAggregate() {
  26. r = true
  27. return false
  28. }
  29. }
  30. return true
  31. })
  32. return
  33. }
  34. func IsAggStatement(stmt *SelectStatement) bool {
  35. if stmt.Dimensions != nil {
  36. ds := stmt.Dimensions.GetGroups()
  37. if ds != nil && len(ds) > 0 {
  38. return true
  39. }
  40. }
  41. r := false
  42. WalkFunc(stmt.Fields, func(n Node) bool {
  43. switch f := n.(type) {
  44. case *Call:
  45. if ok := FuncFinderSingleton().IsAggFunc(f); ok {
  46. r = true
  47. return false
  48. }
  49. }
  50. return true
  51. })
  52. return r
  53. }
  54. func HasAggFuncs(node Node) bool {
  55. if node == nil {
  56. return false
  57. }
  58. var r = false
  59. WalkFunc(node, func(n Node) bool {
  60. if f, ok := n.(*Call); ok {
  61. if ok := FuncFinderSingleton().IsAggFunc(f); ok {
  62. r = true
  63. return false
  64. }
  65. }
  66. return true
  67. })
  68. return r
  69. }