expr_ref.go 3.4 KB

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