expr.go 7.0 KB

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