expr.go 5.1 KB

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