statement.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright 2021-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 Statement interface {
  16. stmt()
  17. Node
  18. }
  19. type SelectStatement struct {
  20. Fields Fields
  21. Sources Sources
  22. Joins Joins
  23. Condition Expr
  24. Limit Expr
  25. Dimensions Dimensions
  26. Having Expr
  27. SortFields SortFields
  28. Statement
  29. }
  30. type Fields []Field
  31. func (f Fields) node() {}
  32. func (f Fields) Len() int {
  33. return len(f)
  34. }
  35. func (f Fields) Swap(i, j int) {
  36. f[i], f[j] = f[j], f[i]
  37. }
  38. func (f Fields) Less(i int, j int) bool {
  39. m := f[i].AName
  40. if m == "" {
  41. m = f[i].Name
  42. }
  43. n := f[j].AName
  44. if n == "" {
  45. n = f[j].Name
  46. }
  47. return m < n
  48. }
  49. type Field struct {
  50. Name string
  51. AName string
  52. Expr Expr
  53. Node
  54. }
  55. func (f *Field) GetName() string {
  56. if f.AName != "" {
  57. return f.AName
  58. } else {
  59. return f.Name
  60. }
  61. }
  62. func (f *Field) IsSelectionField() bool {
  63. if f.AName != "" {
  64. return true
  65. }
  66. _, ok := f.Expr.(*FieldRef)
  67. if ok {
  68. return true
  69. }
  70. return false
  71. }
  72. func (f *Field) IsColumn() bool {
  73. if f.AName != "" {
  74. return false
  75. }
  76. _, ok := f.Expr.(*FieldRef)
  77. if ok {
  78. return true
  79. }
  80. return false
  81. }
  82. type Sources []Source
  83. func (s Sources) node() {}
  84. type Source interface {
  85. Node
  86. source()
  87. }
  88. type Table struct {
  89. Name string
  90. Alias string
  91. Source
  92. }
  93. type JoinType int
  94. const (
  95. LEFT_JOIN JoinType = iota
  96. INNER_JOIN
  97. RIGHT_JOIN
  98. FULL_JOIN
  99. CROSS_JOIN
  100. )
  101. type Join struct {
  102. Name string
  103. Alias string
  104. JoinType JoinType
  105. Expr Expr
  106. Node
  107. }
  108. type Joins []Join
  109. func (j Joins) node() {}
  110. type Dimension struct {
  111. Expr Expr
  112. Node
  113. }
  114. type Dimensions []Dimension
  115. func (d Dimensions) node() {}
  116. func (d *Dimensions) GetWindow() *Window {
  117. for _, child := range *d {
  118. if w, ok := child.Expr.(*Window); ok {
  119. return w
  120. }
  121. }
  122. return nil
  123. }
  124. func (d *Dimensions) GetGroups() Dimensions {
  125. var nd Dimensions
  126. for _, child := range *d {
  127. if _, ok := child.Expr.(*Window); !ok {
  128. nd = append(nd, child)
  129. }
  130. }
  131. return nd
  132. }
  133. type WindowType int
  134. const (
  135. NOT_WINDOW WindowType = iota
  136. TUMBLING_WINDOW
  137. HOPPING_WINDOW
  138. SLIDING_WINDOW
  139. SESSION_WINDOW
  140. COUNT_WINDOW
  141. )
  142. type Window struct {
  143. TriggerCondition Expr
  144. WindowType WindowType
  145. Delay *IntegerLiteral
  146. Length *IntegerLiteral
  147. Interval *IntegerLiteral
  148. TimeUnit *TimeLiteral
  149. Filter Expr
  150. Expr
  151. }
  152. type SortField struct {
  153. Name string
  154. StreamName StreamName
  155. Uname string // unique name of a field
  156. Ascending bool
  157. FieldExpr Expr
  158. Expr
  159. }
  160. type SortFields []SortField
  161. func (d SortFields) node() {}
  162. const (
  163. RowkindInsert = "insert"
  164. RowkindUpdate = "update"
  165. RowkindUpsert = "upsert"
  166. RowkindDelete = "delete"
  167. )