sqlValidator.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "github.com/lf-edge/ekuiper/pkg/ast"
  18. )
  19. // Validate validate select statement without context.
  20. // This is the pre-validation. In planner, there will be a more comprehensive validation after binding
  21. func Validate(stmt *ast.SelectStatement) error {
  22. if HasAggFuncs(stmt.Condition) {
  23. return fmt.Errorf("Not allowed to call aggregate functions in WHERE clause.")
  24. }
  25. for _, d := range stmt.Dimensions {
  26. if HasAggFuncs(d.Expr) {
  27. return fmt.Errorf("Not allowed to call aggregate functions in GROUP BY clause.")
  28. }
  29. }
  30. if err := validateSRFNestedForbidden("select", stmt.Fields); err != nil {
  31. return err
  32. }
  33. if err := validateMultiSRFForbidden("select", stmt.Fields); err != nil {
  34. return err
  35. }
  36. return validateSRFForbidden(stmt)
  37. }
  38. func validateSRFNestedForbidden(clause string, node ast.Node) error {
  39. if isSRFNested(node) {
  40. return fmt.Errorf("%s clause shouldn't has nested set-returning-functions", clause)
  41. }
  42. return nil
  43. }
  44. func validateMultiSRFForbidden(clause string, node ast.Node) error {
  45. firstSRF := false
  46. nextSRF := false
  47. ast.WalkFunc(node, func(n ast.Node) bool {
  48. switch f := n.(type) {
  49. case *ast.Call:
  50. if f.FuncType == ast.FuncTypeSrf {
  51. if !firstSRF {
  52. firstSRF = true
  53. } else {
  54. nextSRF = true
  55. return false
  56. }
  57. }
  58. }
  59. return true
  60. })
  61. if nextSRF {
  62. return fmt.Errorf("%s clause shouldn't has multi set-returning-functions", clause)
  63. }
  64. return nil
  65. }
  66. func validateSRFForbidden(node ast.Node) error {
  67. if isSRFExists(node) {
  68. return fmt.Errorf("select statement shouldn't has srf except fields")
  69. }
  70. return nil
  71. }
  72. func isSRFNested(node ast.Node) bool {
  73. srfNested := false
  74. ast.WalkFunc(node, func(n ast.Node) bool {
  75. switch f := n.(type) {
  76. case *ast.Call:
  77. for _, arg := range f.Args {
  78. exists := isSRFExists(arg)
  79. if exists {
  80. srfNested = true
  81. return false
  82. }
  83. }
  84. return true
  85. }
  86. return true
  87. })
  88. return srfNested
  89. }
  90. func isSRFExists(node ast.Node) bool {
  91. exists := false
  92. ast.WalkFunc(node, func(n ast.Node) bool {
  93. switch f := n.(type) {
  94. // skip checking Fields
  95. case ast.Fields:
  96. return false
  97. case *ast.Call:
  98. if f.FuncType == ast.FuncTypeSrf {
  99. exists = true
  100. return false
  101. }
  102. }
  103. return true
  104. })
  105. return exists
  106. }