expr_ref.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 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 ast
  15. import (
  16. "fmt"
  17. "regexp"
  18. "strings"
  19. )
  20. type LikePattern struct {
  21. Expr Expr
  22. Pattern *regexp.Regexp
  23. }
  24. func (l *LikePattern) expr() {}
  25. func (l *LikePattern) node() {}
  26. func (l *LikePattern) String() string {
  27. return "likePattern:" + l.Pattern.String()
  28. }
  29. func (l *LikePattern) Compile(likestr string) (*regexp.Regexp, error) {
  30. regstr := strings.ReplaceAll(strings.NewReplacer(
  31. `\%`, `\%`,
  32. `\_`, `\_`,
  33. `%`, `.*`,
  34. `_`, `.`,
  35. ).Replace(likestr), `\`, `\\`)
  36. re, err := regexp.Compile("^" + regstr + "$")
  37. if err != nil {
  38. return nil, err
  39. }
  40. return re, nil
  41. }
  42. // FieldRef could be
  43. // 1. SQL Field
  44. // 1.1 Explicit field "stream.col"
  45. // 1.2 Implicit field "col" -> only exist in schemaless stream. Otherwise, explicit stream name will be bound
  46. // 1.3 Alias field "expr as c" -> refer to an Expression or column
  47. type FieldRef struct {
  48. // optional, bind in analyzer, empty means alias, default means not set
  49. // MUST have after binding for SQL fields. For 1.2,1.3 and 1.4, use special constant as stream name
  50. StreamName StreamName
  51. // optional, set only once. For selections, empty name will be assigned a default name
  52. // MUST have after binding, assign a name for 1.4
  53. Name string
  54. // Only for alias
  55. *AliasRef
  56. }
  57. func (fr *FieldRef) expr() {}
  58. func (fr *FieldRef) node() {}
  59. func (fr *FieldRef) IsColumn() bool {
  60. return fr.StreamName != AliasStream && fr.StreamName != ""
  61. }
  62. func (fr *FieldRef) String() string {
  63. sn := ""
  64. n := ""
  65. if fr.StreamName != "" {
  66. sn += string(fr.StreamName)
  67. }
  68. if fr.Name != "" {
  69. if fr.StreamName != "" {
  70. n += "."
  71. }
  72. n += fr.Name
  73. }
  74. return sn + n
  75. }
  76. func (fr *FieldRef) IsAlias() bool {
  77. return fr.StreamName == AliasStream
  78. }
  79. func (fr *FieldRef) RefSelection(a *AliasRef) {
  80. fr.AliasRef = a
  81. }
  82. // RefSources Must call after binding or will get empty
  83. func (fr *FieldRef) RefSources() []StreamName {
  84. if fr.StreamName == AliasStream {
  85. return fr.refSources
  86. } else if fr.StreamName != "" {
  87. return []StreamName{fr.StreamName}
  88. } else {
  89. return nil
  90. }
  91. }
  92. // SetRefSource Only call this for alias field ref
  93. func (fr *FieldRef) SetRefSource(names []StreamName) {
  94. fr.refSources = names
  95. }
  96. type AliasRef struct {
  97. // MUST have, It is used for evaluation
  98. Expression Expr
  99. // MUST have after binding, calculate once in initializer. Could be 0 when alias an Expression without col like "1+2"
  100. refSources []StreamName
  101. // optional, lazy set when calculating isAggregate
  102. IsAggregate *bool
  103. }
  104. func NewAliasRef(e Expr) (*AliasRef, error) {
  105. r := make(map[StreamName]bool)
  106. var walkErr error
  107. WalkFunc(e, func(n Node) bool {
  108. switch f := n.(type) {
  109. case *FieldRef:
  110. switch f.StreamName {
  111. case AliasStream:
  112. walkErr = fmt.Errorf("cannot use alias %s inside another alias %v", f.Name, e)
  113. return false
  114. default:
  115. r[f.StreamName] = true
  116. }
  117. }
  118. return true
  119. })
  120. if walkErr != nil {
  121. return nil, walkErr
  122. }
  123. rs := make([]StreamName, 0)
  124. for k := range r {
  125. rs = append(rs, k)
  126. }
  127. return &AliasRef{
  128. Expression: e,
  129. refSources: rs,
  130. }, nil
  131. }
  132. // MockAliasRef is for testing only.
  133. func MockAliasRef(e Expr, r []StreamName, a *bool) *AliasRef {
  134. return &AliasRef{e, r, a}
  135. }