expr.go 7.1 KB

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