expr.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // Copyright 2022 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 Node interface {
  21. node()
  22. }
  23. type NameNode interface {
  24. Node
  25. GetName() string
  26. }
  27. type Expr interface {
  28. Node
  29. expr()
  30. }
  31. type Literal interface {
  32. Expr
  33. literal()
  34. }
  35. type ParenExpr struct {
  36. Expr Expr
  37. }
  38. type ArrowExpr struct {
  39. Expr Expr
  40. }
  41. type BracketExpr struct {
  42. Expr Expr
  43. }
  44. type ColonExpr struct {
  45. Start Expr
  46. End Expr
  47. }
  48. type IndexExpr struct {
  49. Index Expr
  50. }
  51. type BooleanLiteral struct {
  52. Val bool
  53. }
  54. type TimeLiteral struct {
  55. Val Token
  56. }
  57. type IntegerLiteral struct {
  58. Val int
  59. }
  60. type StringLiteral struct {
  61. Val string
  62. }
  63. type NumberLiteral struct {
  64. Val float64
  65. }
  66. type Wildcard struct {
  67. Token Token
  68. }
  69. func (pe *ParenExpr) expr() {}
  70. func (pe *ParenExpr) node() {}
  71. func (ae *ArrowExpr) expr() {}
  72. func (ae *ArrowExpr) node() {}
  73. func (be *BracketExpr) expr() {}
  74. func (be *BracketExpr) node() {}
  75. func (be *ColonExpr) expr() {}
  76. func (be *ColonExpr) node() {}
  77. func (be *IndexExpr) expr() {}
  78. func (be *IndexExpr) node() {}
  79. func (w *Wildcard) expr() {}
  80. func (w *Wildcard) node() {}
  81. func (bl *BooleanLiteral) expr() {}
  82. func (bl *BooleanLiteral) literal() {}
  83. func (bl *BooleanLiteral) node() {}
  84. func (tl *TimeLiteral) expr() {}
  85. func (tl *TimeLiteral) literal() {}
  86. func (tl *TimeLiteral) node() {}
  87. func (il *IntegerLiteral) expr() {}
  88. func (il *IntegerLiteral) literal() {}
  89. func (il *IntegerLiteral) node() {}
  90. func (nl *NumberLiteral) expr() {}
  91. func (nl *NumberLiteral) literal() {}
  92. func (nl *NumberLiteral) node() {}
  93. func (sl *StringLiteral) expr() {}
  94. func (sl *StringLiteral) literal() {}
  95. func (sl *StringLiteral) node() {}
  96. type Call struct {
  97. Name string
  98. FuncId int
  99. Args []Expr
  100. }
  101. func (c *Call) expr() {}
  102. func (c *Call) literal() {}
  103. func (c *Call) node() {}
  104. type BinaryExpr struct {
  105. OP Token
  106. LHS Expr
  107. RHS Expr
  108. }
  109. func (fe *BinaryExpr) expr() {}
  110. func (be *BinaryExpr) node() {}
  111. type WhenClause struct {
  112. // The condition Expression
  113. Expr Expr
  114. Result Expr
  115. }
  116. func (w *WhenClause) expr() {}
  117. func (w *WhenClause) node() {}
  118. type CaseExpr struct {
  119. // The compare value Expression. It can be a value Expression or nil.
  120. // When it is nil, the WhenClause Expr must be a logical(comparison) Expression
  121. Value Expr
  122. WhenClauses []*WhenClause
  123. ElseClause Expr
  124. }
  125. func (c *CaseExpr) expr() {}
  126. func (c *CaseExpr) node() {}
  127. type ValueSetExpr struct {
  128. LiteralExprs []Expr // ("A", "B", "C") or (1, 2, 3)
  129. ArrayExpr Expr
  130. }
  131. func (c *ValueSetExpr) expr() {}
  132. func (c *ValueSetExpr) node() {}
  133. type BetweenExpr struct {
  134. Lower Expr
  135. Higher Expr
  136. }
  137. func (b *BetweenExpr) expr() {}
  138. func (b *BetweenExpr) node() {}
  139. type LikePattern struct {
  140. Expr Expr
  141. Pattern *regexp.Regexp
  142. }
  143. func (l *LikePattern) expr() {}
  144. func (l *LikePattern) node() {}
  145. func (l *LikePattern) Compile(likestr string) (*regexp.Regexp, error) {
  146. likestr = strings.ReplaceAll(strings.ReplaceAll(likestr, `\%`, `!@#`), `\_`, `!@$`)
  147. regstr := strings.ReplaceAll(strings.ReplaceAll(likestr, "%", ".*"), "_", ".")
  148. regstr = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(regstr, `!@$`, `\_`), `!@#`, `\%`), `\`, `\\`)
  149. re, err := regexp.Compile("^" + regstr + "$")
  150. if err != nil {
  151. return nil, err
  152. }
  153. return re, nil
  154. }
  155. type StreamName string
  156. func (sn *StreamName) node() {}
  157. const (
  158. DefaultStream = StreamName("$$default")
  159. AliasStream = StreamName("$$alias")
  160. )
  161. // FieldRef could be
  162. // 1. SQL Field
  163. // 1.1 Explicit field "stream.col"
  164. // 1.2 Implicit field "col" -> only exist in schemaless stream. Otherwise, explicit stream name will be bound
  165. // 1.3 Alias field "expr as c" -> refer to an Expression or column
  166. type FieldRef struct {
  167. // optional, bind in analyzer, empty means alias, default means not set
  168. // MUST have after binding for SQL fields. For 1.2,1.3 and 1.4, use special constant as stream name
  169. StreamName StreamName
  170. // optional, set only once. For selections, empty name will be assigned a default name
  171. // MUST have after binding, assign a name for 1.4
  172. Name string
  173. // Only for alias
  174. *AliasRef
  175. }
  176. func (fr *FieldRef) expr() {}
  177. func (fr *FieldRef) node() {}
  178. func (fr *FieldRef) IsColumn() bool {
  179. return fr.StreamName != AliasStream && fr.StreamName != ""
  180. }
  181. func (fr *FieldRef) IsAlias() bool {
  182. return fr.StreamName == AliasStream
  183. }
  184. func (fr *FieldRef) RefSelection(a *AliasRef) {
  185. fr.AliasRef = a
  186. }
  187. // RefSources Must call after binding or will get empty
  188. func (fr *FieldRef) RefSources() []StreamName {
  189. if fr.StreamName == AliasStream {
  190. return fr.refSources
  191. } else if fr.StreamName != "" {
  192. return []StreamName{fr.StreamName}
  193. } else {
  194. return nil
  195. }
  196. }
  197. // SetRefSource Only call this for alias field ref
  198. func (fr *FieldRef) SetRefSource(names []StreamName) {
  199. fr.refSources = names
  200. }
  201. type AliasRef struct {
  202. // MUST have, It is used for evaluation
  203. Expression Expr
  204. // MUST have after binding, calculate once in initializer. Could be 0 when alias an Expression without col like "1+2"
  205. refSources []StreamName
  206. // optional, lazy set when calculating isAggregate
  207. IsAggregate *bool
  208. }
  209. func NewAliasRef(e Expr) (*AliasRef, error) {
  210. r := make(map[StreamName]bool)
  211. var walkErr error
  212. WalkFunc(e, func(n Node) bool {
  213. switch f := n.(type) {
  214. case *FieldRef:
  215. switch f.StreamName {
  216. case AliasStream:
  217. walkErr = fmt.Errorf("cannot use alias %s inside another alias %v", f.Name, e)
  218. return false
  219. default:
  220. r[f.StreamName] = true
  221. }
  222. }
  223. return true
  224. })
  225. if walkErr != nil {
  226. return nil, walkErr
  227. }
  228. rs := make([]StreamName, 0)
  229. for k := range r {
  230. rs = append(rs, k)
  231. }
  232. return &AliasRef{
  233. Expression: e,
  234. refSources: rs,
  235. }, nil
  236. }
  237. // for testing only
  238. func MockAliasRef(e Expr, r []StreamName, a *bool) *AliasRef {
  239. return &AliasRef{e, r, a}
  240. }
  241. type MetaRef struct {
  242. StreamName StreamName
  243. Name string
  244. }
  245. func (fr *MetaRef) expr() {}
  246. func (fr *MetaRef) node() {}
  247. type JsonFieldRef struct {
  248. Name string
  249. }
  250. func (fr *JsonFieldRef) expr() {}
  251. func (fr *JsonFieldRef) node() {}
  252. type ColFuncField struct {
  253. Name string
  254. Expr Expr
  255. }
  256. func (fr *ColFuncField) expr() {}
  257. func (fr *ColFuncField) node() {}