expr.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. }
  64. func (pe *ParenExpr) expr() {}
  65. func (pe *ParenExpr) node() {}
  66. func (ae *ArrowExpr) expr() {}
  67. func (ae *ArrowExpr) node() {}
  68. func (be *BracketExpr) expr() {}
  69. func (be *BracketExpr) node() {}
  70. func (be *ColonExpr) expr() {}
  71. func (be *ColonExpr) node() {}
  72. func (be *IndexExpr) expr() {}
  73. func (be *IndexExpr) node() {}
  74. func (w *Wildcard) expr() {}
  75. func (w *Wildcard) node() {}
  76. func (bl *BooleanLiteral) expr() {}
  77. func (bl *BooleanLiteral) literal() {}
  78. func (bl *BooleanLiteral) node() {}
  79. func (tl *TimeLiteral) expr() {}
  80. func (tl *TimeLiteral) literal() {}
  81. func (tl *TimeLiteral) node() {}
  82. func (il *IntegerLiteral) expr() {}
  83. func (il *IntegerLiteral) literal() {}
  84. func (il *IntegerLiteral) node() {}
  85. func (nl *NumberLiteral) expr() {}
  86. func (nl *NumberLiteral) literal() {}
  87. func (nl *NumberLiteral) node() {}
  88. func (sl *StringLiteral) expr() {}
  89. func (sl *StringLiteral) literal() {}
  90. func (sl *StringLiteral) node() {}
  91. type FuncType int
  92. const (
  93. FuncTypeUnknown FuncType = iota - 1
  94. FuncTypeScalar
  95. FuncTypeAgg
  96. FuncTypeCols
  97. FuncTypeSrf
  98. )
  99. type Call struct {
  100. Name string
  101. FuncId int
  102. FuncType FuncType
  103. Args []Expr
  104. // This is used for analytic functions.
  105. // In planner, all analytic functions are planned to calculate in analytic_op which produce a new field.
  106. // This cachedField cached the new field name and when evaluating, just return the field access evaluated value.
  107. CachedField string
  108. Cached bool
  109. Partition *PartitionExpr
  110. WhenExpr Expr
  111. }
  112. func (c *Call) expr() {}
  113. func (c *Call) literal() {}
  114. func (c *Call) node() {}
  115. type PartitionExpr struct {
  116. Exprs []Expr
  117. }
  118. func (pe *PartitionExpr) expr() {}
  119. func (pe *PartitionExpr) node() {}
  120. type BinaryExpr struct {
  121. OP Token
  122. LHS Expr
  123. RHS Expr
  124. }
  125. func (be *BinaryExpr) expr() {}
  126. func (be *BinaryExpr) node() {}
  127. type WhenClause struct {
  128. // The condition Expression
  129. Expr Expr
  130. Result Expr
  131. }
  132. func (w *WhenClause) expr() {}
  133. func (w *WhenClause) node() {}
  134. type CaseExpr struct {
  135. // The compare value Expression. It can be a value Expression or nil.
  136. // When it is nil, the WhenClause Expr must be a logical(comparison) Expression
  137. Value Expr
  138. WhenClauses []*WhenClause
  139. ElseClause Expr
  140. }
  141. func (c *CaseExpr) expr() {}
  142. func (c *CaseExpr) node() {}
  143. type ValueSetExpr struct {
  144. LiteralExprs []Expr // ("A", "B", "C") or (1, 2, 3)
  145. ArrayExpr Expr
  146. }
  147. func (c *ValueSetExpr) expr() {}
  148. func (c *ValueSetExpr) node() {}
  149. type BetweenExpr struct {
  150. Lower Expr
  151. Higher Expr
  152. }
  153. func (b *BetweenExpr) expr() {}
  154. func (b *BetweenExpr) node() {}
  155. type StreamName string
  156. func (sn *StreamName) node() {}
  157. const (
  158. DefaultStream = StreamName("$$default")
  159. AliasStream = StreamName("$$alias")
  160. )
  161. type MetaRef struct {
  162. StreamName StreamName
  163. Name string
  164. }
  165. func (fr *MetaRef) expr() {}
  166. func (fr *MetaRef) node() {}
  167. type JsonFieldRef struct {
  168. Name string
  169. }
  170. func (fr *JsonFieldRef) expr() {}
  171. func (fr *JsonFieldRef) node() {}
  172. type ColFuncField struct {
  173. Name string
  174. Expr Expr
  175. }
  176. func (fr *ColFuncField) expr() {}
  177. func (fr *ColFuncField) node() {}