expr_ref.go 3.9 KB

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