expr.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Copyright 2021 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. Args []Expr
  97. }
  98. func (c *Call) expr() {}
  99. func (c *Call) literal() {}
  100. func (c *Call) node() {}
  101. type BinaryExpr struct {
  102. OP Token
  103. LHS Expr
  104. RHS Expr
  105. }
  106. func (fe *BinaryExpr) expr() {}
  107. func (be *BinaryExpr) node() {}
  108. type WhenClause struct {
  109. // The condition Expression
  110. Expr Expr
  111. Result Expr
  112. }
  113. func (w *WhenClause) expr() {}
  114. func (w *WhenClause) node() {}
  115. type CaseExpr struct {
  116. // The compare value Expression. It can be a value Expression or nil.
  117. // When it is nil, the WhenClause Expr must be a logical(comparison) Expression
  118. Value Expr
  119. WhenClauses []*WhenClause
  120. ElseClause Expr
  121. }
  122. func (c *CaseExpr) expr() {}
  123. func (c *CaseExpr) node() {}
  124. type StreamName string
  125. func (sn *StreamName) node() {}
  126. const (
  127. DefaultStream = StreamName("$$default")
  128. AliasStream = StreamName("$$alias")
  129. )
  130. // FieldRef could be
  131. // 1. SQL Field
  132. // 1.1 Explicit field "stream.col"
  133. // 1.2 Implicit field "col" -> only exist in schemaless stream. Otherwise, explicit stream name will be bound
  134. // 1.3 Alias field "expr as c" -> refer to an Expression or column
  135. // 2. Json Expression field like a->b
  136. type FieldRef struct {
  137. // optional, bind in analyzer, empty means alias, default means not set
  138. // MUST have after binding for SQL fields. For 1.2,1.3 and 1.4, use special constant as stream name
  139. StreamName StreamName
  140. // optional, set only once. For selections, empty name will be assigned a default name
  141. // MUST have after binding, assign a name for 1.4
  142. Name string
  143. // Only for alias
  144. *AliasRef
  145. }
  146. func (fr *FieldRef) expr() {}
  147. func (fr *FieldRef) node() {}
  148. func (fr *FieldRef) IsColumn() bool {
  149. return fr.StreamName != AliasStream && fr.StreamName != ""
  150. }
  151. func (fr *FieldRef) IsAlias() bool {
  152. return fr.StreamName == AliasStream
  153. }
  154. func (fr *FieldRef) RefSelection(a *AliasRef) {
  155. fr.AliasRef = a
  156. }
  157. // RefSources Must call after binding or will get empty
  158. func (fr *FieldRef) RefSources() []StreamName {
  159. if fr.StreamName == AliasStream {
  160. return fr.refSources
  161. } else if fr.StreamName != "" {
  162. return []StreamName{fr.StreamName}
  163. } else {
  164. return nil
  165. }
  166. }
  167. // SetRefSource Only call this for alias field ref
  168. func (fr *FieldRef) SetRefSource(names []StreamName) {
  169. fr.refSources = names
  170. }
  171. type AliasRef struct {
  172. // MUST have, It is used for evaluation
  173. Expression Expr
  174. // MUST have after binding, calculate once in initializer. Could be 0 when alias an Expression without col like "1+2"
  175. refSources []StreamName
  176. // optional, lazy set when calculating isAggregate
  177. IsAggregate *bool
  178. }
  179. func NewAliasRef(e Expr) (*AliasRef, error) {
  180. r := make(map[StreamName]bool)
  181. var walkErr error
  182. WalkFunc(e, func(n Node) bool {
  183. switch f := n.(type) {
  184. case *FieldRef:
  185. switch f.StreamName {
  186. case AliasStream:
  187. walkErr = fmt.Errorf("cannot use alias %s inside another alias %v", f.Name, e)
  188. return false
  189. default:
  190. r[f.StreamName] = true
  191. }
  192. }
  193. return true
  194. })
  195. if walkErr != nil {
  196. return nil, walkErr
  197. }
  198. rs := make([]StreamName, 0)
  199. for k := range r {
  200. rs = append(rs, k)
  201. }
  202. return &AliasRef{
  203. Expression: e,
  204. refSources: rs,
  205. }, nil
  206. }
  207. // for testing only
  208. func MockAliasRef(e Expr, r []StreamName, a *bool) *AliasRef {
  209. return &AliasRef{e, r, a}
  210. }
  211. type MetaRef struct {
  212. StreamName StreamName
  213. Name string
  214. }
  215. func (fr *MetaRef) expr() {}
  216. func (fr *MetaRef) node() {}
  217. type JsonFieldRef struct {
  218. Name string
  219. }
  220. func (fr *JsonFieldRef) expr() {}
  221. func (fr *JsonFieldRef) node() {}