expr.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. regstr := strings.ReplaceAll(strings.NewReplacer(
  168. `\%`, `\%`,
  169. `\_`, `\_`,
  170. `%`, `.*`,
  171. `_`, `.`,
  172. ).Replace(likestr), `\`, `\\`)
  173. re, err := regexp.Compile("^" + regstr + "$")
  174. if err != nil {
  175. return nil, err
  176. }
  177. return re, nil
  178. }
  179. type StreamName string
  180. func (sn *StreamName) node() {}
  181. const (
  182. DefaultStream = StreamName("$$default")
  183. AliasStream = StreamName("$$alias")
  184. )
  185. // FieldRef could be
  186. // 1. SQL Field
  187. // 1.1 Explicit field "stream.col"
  188. // 1.2 Implicit field "col" -> only exist in schemaless stream. Otherwise, explicit stream name will be bound
  189. // 1.3 Alias field "expr as c" -> refer to an Expression or column
  190. type FieldRef struct {
  191. // optional, bind in analyzer, empty means alias, default means not set
  192. // MUST have after binding for SQL fields. For 1.2,1.3 and 1.4, use special constant as stream name
  193. StreamName StreamName
  194. // optional, set only once. For selections, empty name will be assigned a default name
  195. // MUST have after binding, assign a name for 1.4
  196. Name string
  197. // Only for alias
  198. *AliasRef
  199. }
  200. func (fr *FieldRef) expr() {}
  201. func (fr *FieldRef) node() {}
  202. func (fr *FieldRef) IsColumn() bool {
  203. return fr.StreamName != AliasStream && fr.StreamName != ""
  204. }
  205. func (fr *FieldRef) IsAlias() bool {
  206. return fr.StreamName == AliasStream
  207. }
  208. func (fr *FieldRef) RefSelection(a *AliasRef) {
  209. fr.AliasRef = a
  210. }
  211. // RefSources Must call after binding or will get empty
  212. func (fr *FieldRef) RefSources() []StreamName {
  213. if fr.StreamName == AliasStream {
  214. return fr.refSources
  215. } else if fr.StreamName != "" {
  216. return []StreamName{fr.StreamName}
  217. } else {
  218. return nil
  219. }
  220. }
  221. // SetRefSource Only call this for alias field ref
  222. func (fr *FieldRef) SetRefSource(names []StreamName) {
  223. fr.refSources = names
  224. }
  225. type AliasRef struct {
  226. // MUST have, It is used for evaluation
  227. Expression Expr
  228. // MUST have after binding, calculate once in initializer. Could be 0 when alias an Expression without col like "1+2"
  229. refSources []StreamName
  230. // optional, lazy set when calculating isAggregate
  231. IsAggregate *bool
  232. }
  233. func NewAliasRef(e Expr) (*AliasRef, error) {
  234. r := make(map[StreamName]bool)
  235. var walkErr error
  236. WalkFunc(e, func(n Node) bool {
  237. switch f := n.(type) {
  238. case *FieldRef:
  239. switch f.StreamName {
  240. case AliasStream:
  241. walkErr = fmt.Errorf("cannot use alias %s inside another alias %v", f.Name, e)
  242. return false
  243. default:
  244. r[f.StreamName] = true
  245. }
  246. }
  247. return true
  248. })
  249. if walkErr != nil {
  250. return nil, walkErr
  251. }
  252. rs := make([]StreamName, 0)
  253. for k := range r {
  254. rs = append(rs, k)
  255. }
  256. return &AliasRef{
  257. Expression: e,
  258. refSources: rs,
  259. }, nil
  260. }
  261. // MockAliasRef is for testing only.
  262. func MockAliasRef(e Expr, r []StreamName, a *bool) *AliasRef {
  263. return &AliasRef{e, r, a}
  264. }
  265. type MetaRef struct {
  266. StreamName StreamName
  267. Name string
  268. }
  269. func (fr *MetaRef) expr() {}
  270. func (fr *MetaRef) node() {}
  271. type JsonFieldRef struct {
  272. Name string
  273. }
  274. func (fr *JsonFieldRef) expr() {}
  275. func (fr *JsonFieldRef) node() {}
  276. type ColFuncField struct {
  277. Name string
  278. Expr Expr
  279. }
  280. func (fr *ColFuncField) expr() {}
  281. func (fr *ColFuncField) node() {}