expr.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // Copyright 2022-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 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 FuncType int
  97. const (
  98. FuncTypeUnknown FuncType = iota - 1
  99. FuncTypeScalar
  100. FuncTypeAgg
  101. FuncTypeCols
  102. FuncTypeSrf
  103. )
  104. type Call struct {
  105. Name string
  106. FuncId int
  107. FuncType FuncType
  108. Args []Expr
  109. // This is used for analytic functions.
  110. // In planner, all analytic functions are planned to calculate in analytic_op which produce a new field.
  111. // This cachedField cached the new field name and when evaluating, just return the field access evaluated value.
  112. CachedField string
  113. Cached bool
  114. Partition *PartitionExpr
  115. WhenExpr Expr
  116. }
  117. func (c *Call) expr() {}
  118. func (c *Call) literal() {}
  119. func (c *Call) node() {}
  120. type PartitionExpr struct {
  121. Exprs []Expr
  122. }
  123. func (pe *PartitionExpr) expr() {}
  124. func (pe *PartitionExpr) node() {}
  125. type BinaryExpr struct {
  126. OP Token
  127. LHS Expr
  128. RHS Expr
  129. }
  130. func (be *BinaryExpr) expr() {}
  131. func (be *BinaryExpr) node() {}
  132. type WhenClause struct {
  133. // The condition Expression
  134. Expr Expr
  135. Result Expr
  136. }
  137. func (w *WhenClause) expr() {}
  138. func (w *WhenClause) node() {}
  139. type CaseExpr struct {
  140. // The compare value Expression. It can be a value Expression or nil.
  141. // When it is nil, the WhenClause Expr must be a logical(comparison) Expression
  142. Value Expr
  143. WhenClauses []*WhenClause
  144. ElseClause Expr
  145. }
  146. func (c *CaseExpr) expr() {}
  147. func (c *CaseExpr) node() {}
  148. type ValueSetExpr struct {
  149. LiteralExprs []Expr // ("A", "B", "C") or (1, 2, 3)
  150. ArrayExpr Expr
  151. }
  152. func (c *ValueSetExpr) expr() {}
  153. func (c *ValueSetExpr) node() {}
  154. type BetweenExpr struct {
  155. Lower Expr
  156. Higher Expr
  157. }
  158. func (b *BetweenExpr) expr() {}
  159. func (b *BetweenExpr) node() {}
  160. type LikePattern struct {
  161. Expr Expr
  162. Pattern *regexp.Regexp
  163. }
  164. func (l *LikePattern) expr() {}
  165. func (l *LikePattern) node() {}
  166. func (l *LikePattern) Compile(likestr string) (*regexp.Regexp, error) {
  167. likestr = strings.ReplaceAll(strings.ReplaceAll(likestr, `\%`, `!@#`), `\_`, `!@$`)
  168. regstr := strings.ReplaceAll(strings.ReplaceAll(likestr, "%", ".*"), "_", ".")
  169. regstr = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(regstr, `!@$`, `\_`), `!@#`, `\%`), `\`, `\\`)
  170. re, err := regexp.Compile("^" + regstr + "$")
  171. if err != nil {
  172. return nil, err
  173. }
  174. return re, nil
  175. }
  176. type StreamName string
  177. func (sn *StreamName) node() {}
  178. const (
  179. DefaultStream = StreamName("$$default")
  180. AliasStream = StreamName("$$alias")
  181. )
  182. // FieldRef could be
  183. // 1. SQL Field
  184. // 1.1 Explicit field "stream.col"
  185. // 1.2 Implicit field "col" -> only exist in schemaless stream. Otherwise, explicit stream name will be bound
  186. // 1.3 Alias field "expr as c" -> refer to an Expression or column
  187. type FieldRef struct {
  188. // optional, bind in analyzer, empty means alias, default means not set
  189. // MUST have after binding for SQL fields. For 1.2,1.3 and 1.4, use special constant as stream name
  190. StreamName StreamName
  191. // optional, set only once. For selections, empty name will be assigned a default name
  192. // MUST have after binding, assign a name for 1.4
  193. Name string
  194. // Only for alias
  195. *AliasRef
  196. }
  197. func (fr *FieldRef) expr() {}
  198. func (fr *FieldRef) node() {}
  199. func (fr *FieldRef) IsColumn() bool {
  200. return fr.StreamName != AliasStream && fr.StreamName != ""
  201. }
  202. func (fr *FieldRef) IsAlias() bool {
  203. return fr.StreamName == AliasStream
  204. }
  205. func (fr *FieldRef) RefSelection(a *AliasRef) {
  206. fr.AliasRef = a
  207. }
  208. // RefSources Must call after binding or will get empty
  209. func (fr *FieldRef) RefSources() []StreamName {
  210. if fr.StreamName == AliasStream {
  211. return fr.refSources
  212. } else if fr.StreamName != "" {
  213. return []StreamName{fr.StreamName}
  214. } else {
  215. return nil
  216. }
  217. }
  218. // SetRefSource Only call this for alias field ref
  219. func (fr *FieldRef) SetRefSource(names []StreamName) {
  220. fr.refSources = names
  221. }
  222. type AliasRef struct {
  223. // MUST have, It is used for evaluation
  224. Expression Expr
  225. // MUST have after binding, calculate once in initializer. Could be 0 when alias an Expression without col like "1+2"
  226. refSources []StreamName
  227. // optional, lazy set when calculating isAggregate
  228. IsAggregate *bool
  229. }
  230. func NewAliasRef(e Expr) (*AliasRef, error) {
  231. r := make(map[StreamName]bool)
  232. var walkErr error
  233. WalkFunc(e, func(n Node) bool {
  234. switch f := n.(type) {
  235. case *FieldRef:
  236. switch f.StreamName {
  237. case AliasStream:
  238. walkErr = fmt.Errorf("cannot use alias %s inside another alias %v", f.Name, e)
  239. return false
  240. default:
  241. r[f.StreamName] = true
  242. }
  243. }
  244. return true
  245. })
  246. if walkErr != nil {
  247. return nil, walkErr
  248. }
  249. rs := make([]StreamName, 0)
  250. for k := range r {
  251. rs = append(rs, k)
  252. }
  253. return &AliasRef{
  254. Expression: e,
  255. refSources: rs,
  256. }, nil
  257. }
  258. // for testing only
  259. func MockAliasRef(e Expr, r []StreamName, a *bool) *AliasRef {
  260. return &AliasRef{e, r, a}
  261. }
  262. type MetaRef struct {
  263. StreamName StreamName
  264. Name string
  265. }
  266. func (fr *MetaRef) expr() {}
  267. func (fr *MetaRef) node() {}
  268. type JsonFieldRef struct {
  269. Name string
  270. }
  271. func (fr *JsonFieldRef) expr() {}
  272. func (fr *JsonFieldRef) node() {}
  273. type ColFuncField struct {
  274. Name string
  275. Expr Expr
  276. }
  277. func (fr *ColFuncField) expr() {}
  278. func (fr *ColFuncField) node() {}