expr.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. type Node interface {
  16. node()
  17. }
  18. type NameNode interface {
  19. Node
  20. GetName() string
  21. }
  22. type Expr interface {
  23. Node
  24. expr()
  25. }
  26. type Literal interface {
  27. Expr
  28. literal()
  29. }
  30. type ParenExpr struct {
  31. Expr Expr
  32. }
  33. type ArrowExpr struct {
  34. Expr Expr
  35. }
  36. type BracketExpr struct {
  37. Expr Expr
  38. }
  39. type ColonExpr struct {
  40. Start Expr
  41. End Expr
  42. }
  43. type IndexExpr struct {
  44. Index Expr
  45. }
  46. type BooleanLiteral struct {
  47. Val bool
  48. }
  49. type TimeLiteral struct {
  50. Val Token
  51. }
  52. type IntegerLiteral struct {
  53. Val int
  54. }
  55. type StringLiteral struct {
  56. Val string
  57. }
  58. type NumberLiteral struct {
  59. Val float64
  60. }
  61. type Wildcard struct {
  62. Token Token
  63. Replace []Field
  64. Except []string
  65. }
  66. func (pe *ParenExpr) expr() {}
  67. func (pe *ParenExpr) node() {}
  68. func (ae *ArrowExpr) expr() {}
  69. func (ae *ArrowExpr) node() {}
  70. func (be *BracketExpr) expr() {}
  71. func (be *BracketExpr) node() {}
  72. func (be *ColonExpr) expr() {}
  73. func (be *ColonExpr) node() {}
  74. func (be *IndexExpr) expr() {}
  75. func (be *IndexExpr) node() {}
  76. func (w *Wildcard) expr() {}
  77. func (w *Wildcard) node() {}
  78. func (bl *BooleanLiteral) expr() {}
  79. func (bl *BooleanLiteral) literal() {}
  80. func (bl *BooleanLiteral) node() {}
  81. func (tl *TimeLiteral) expr() {}
  82. func (tl *TimeLiteral) literal() {}
  83. func (tl *TimeLiteral) node() {}
  84. func (il *IntegerLiteral) expr() {}
  85. func (il *IntegerLiteral) literal() {}
  86. func (il *IntegerLiteral) node() {}
  87. func (nl *NumberLiteral) expr() {}
  88. func (nl *NumberLiteral) literal() {}
  89. func (nl *NumberLiteral) node() {}
  90. func (sl *StringLiteral) expr() {}
  91. func (sl *StringLiteral) literal() {}
  92. func (sl *StringLiteral) node() {}
  93. type FuncType int
  94. const (
  95. FuncTypeUnknown FuncType = iota - 1
  96. FuncTypeScalar
  97. FuncTypeAgg
  98. FuncTypeCols
  99. FuncTypeSrf
  100. )
  101. type Call struct {
  102. Name string
  103. FuncId int
  104. FuncType FuncType
  105. Args []Expr
  106. // This is used for analytic functions.
  107. // In planner, all analytic functions are planned to calculate in analytic_op which produce a new field.
  108. // This cachedField cached the new field name and when evaluating, just returned the field access evaluated value.
  109. CachedField string
  110. Cached bool
  111. Partition *PartitionExpr
  112. WhenExpr Expr
  113. }
  114. func (c *Call) expr() {}
  115. func (c *Call) literal() {}
  116. func (c *Call) node() {}
  117. type PartitionExpr struct {
  118. Exprs []Expr
  119. }
  120. func (pe *PartitionExpr) expr() {}
  121. func (pe *PartitionExpr) node() {}
  122. type BinaryExpr struct {
  123. OP Token
  124. LHS Expr
  125. RHS Expr
  126. }
  127. func (be *BinaryExpr) expr() {}
  128. func (be *BinaryExpr) node() {}
  129. type WhenClause struct {
  130. // The condition Expression
  131. Expr Expr
  132. Result Expr
  133. }
  134. func (w *WhenClause) expr() {}
  135. func (w *WhenClause) node() {}
  136. type CaseExpr struct {
  137. // The compare value Expression. It can be a value Expression or nil.
  138. // When it is nil, the WhenClause Expr must be a logical(comparison) Expression
  139. Value Expr
  140. WhenClauses []*WhenClause
  141. ElseClause Expr
  142. }
  143. func (c *CaseExpr) expr() {}
  144. func (c *CaseExpr) node() {}
  145. type ValueSetExpr struct {
  146. LiteralExprs []Expr // ("A", "B", "C") or (1, 2, 3)
  147. ArrayExpr Expr
  148. }
  149. func (c *ValueSetExpr) expr() {}
  150. func (c *ValueSetExpr) node() {}
  151. type BetweenExpr struct {
  152. Lower Expr
  153. Higher Expr
  154. }
  155. func (b *BetweenExpr) expr() {}
  156. func (b *BetweenExpr) node() {}
  157. type LimitExpr struct {
  158. LimitCount *IntegerLiteral
  159. }
  160. func (l *LimitExpr) expr() {}
  161. func (l *LimitExpr) node() {}
  162. type StreamName string
  163. func (sn *StreamName) node() {}
  164. const (
  165. DefaultStream = StreamName("$$default")
  166. AliasStream = StreamName("$$alias")
  167. )
  168. type MetaRef struct {
  169. StreamName StreamName
  170. Name string
  171. }
  172. func (fr *MetaRef) expr() {}
  173. func (fr *MetaRef) node() {}
  174. type JsonFieldRef struct {
  175. Name string
  176. }
  177. func (fr *JsonFieldRef) expr() {}
  178. func (fr *JsonFieldRef) node() {}
  179. type ColFuncField struct {
  180. Name string
  181. Expr Expr
  182. }
  183. func (fr *ColFuncField) expr() {}
  184. func (fr *ColFuncField) node() {}