expr.go 5.8 KB

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