statement.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. import "strconv"
  16. type Statement interface {
  17. stmt()
  18. Node
  19. }
  20. type SelectStatement struct {
  21. Fields Fields
  22. Sources Sources
  23. Joins Joins
  24. Condition Expr
  25. Limit Expr
  26. Dimensions Dimensions
  27. Having Expr
  28. SortFields SortFields
  29. Statement
  30. }
  31. type Fields []Field
  32. func (f Fields) node() {}
  33. func (f Fields) Len() int {
  34. return len(f)
  35. }
  36. func (f Fields) Swap(i, j int) {
  37. f[i], f[j] = f[j], f[i]
  38. }
  39. func (f Fields) Less(i int, j int) bool {
  40. m := f[i].AName
  41. if m == "" {
  42. m = f[i].Name
  43. }
  44. n := f[j].AName
  45. if n == "" {
  46. n = f[j].Name
  47. }
  48. return m < n
  49. }
  50. type Field struct {
  51. Name string
  52. AName string
  53. Expr Expr
  54. Node
  55. }
  56. func (f *Field) GetName() string {
  57. if f.AName != "" {
  58. return f.AName
  59. } else {
  60. return f.Name
  61. }
  62. }
  63. func (f *Field) IsSelectionField() bool {
  64. if f.AName != "" {
  65. return true
  66. }
  67. _, ok := f.Expr.(*FieldRef)
  68. if ok {
  69. return true
  70. }
  71. return false
  72. }
  73. func (f *Field) IsColumn() bool {
  74. if f.AName != "" {
  75. return false
  76. }
  77. _, ok := f.Expr.(*FieldRef)
  78. if ok {
  79. return true
  80. }
  81. return false
  82. }
  83. type Sources []Source
  84. func (s Sources) node() {}
  85. type Source interface {
  86. Node
  87. source()
  88. }
  89. type Table struct {
  90. Name string
  91. Alias string
  92. Source
  93. }
  94. type JoinType int
  95. const (
  96. LEFT_JOIN JoinType = iota
  97. INNER_JOIN
  98. RIGHT_JOIN
  99. FULL_JOIN
  100. CROSS_JOIN
  101. )
  102. func (j JoinType) String() string {
  103. switch j {
  104. case LEFT_JOIN:
  105. return "LEFT_JOIN"
  106. case INNER_JOIN:
  107. return "INNER_JOIN"
  108. case RIGHT_JOIN:
  109. return "RIGHT_JOIN"
  110. case FULL_JOIN:
  111. return "FULL_JOIN"
  112. case CROSS_JOIN:
  113. return "CROSS_JOIN"
  114. default:
  115. return ""
  116. }
  117. }
  118. type Join struct {
  119. Name string
  120. Alias string
  121. JoinType JoinType
  122. Expr Expr
  123. Node
  124. }
  125. type Joins []Join
  126. func (j Joins) node() {}
  127. type Dimension struct {
  128. Expr Expr
  129. Node
  130. }
  131. type Dimensions []Dimension
  132. func (d Dimensions) node() {}
  133. func (d *Dimensions) GetWindow() *Window {
  134. for _, child := range *d {
  135. if w, ok := child.Expr.(*Window); ok {
  136. return w
  137. }
  138. }
  139. return nil
  140. }
  141. func (d *Dimensions) GetGroups() Dimensions {
  142. var nd Dimensions
  143. for _, child := range *d {
  144. if _, ok := child.Expr.(*Window); !ok {
  145. nd = append(nd, child)
  146. }
  147. }
  148. return nd
  149. }
  150. type WindowType int
  151. const (
  152. NOT_WINDOW WindowType = iota
  153. TUMBLING_WINDOW
  154. HOPPING_WINDOW
  155. SLIDING_WINDOW
  156. SESSION_WINDOW
  157. COUNT_WINDOW
  158. )
  159. func (w WindowType) String() string {
  160. switch w {
  161. case NOT_WINDOW:
  162. return "NOT_WINDOW"
  163. case TUMBLING_WINDOW:
  164. return "TUMBLING_WINDOW"
  165. case HOPPING_WINDOW:
  166. return "HOPPING_WINDOW"
  167. case SLIDING_WINDOW:
  168. return "SLIDING_WINDOW"
  169. case SESSION_WINDOW:
  170. return "SESSION_WINDOW"
  171. case COUNT_WINDOW:
  172. return "COUNT_WINDOW"
  173. }
  174. return ""
  175. }
  176. type Window struct {
  177. TriggerCondition Expr
  178. WindowType WindowType
  179. Delay *IntegerLiteral
  180. Length *IntegerLiteral
  181. Interval *IntegerLiteral
  182. TimeUnit *TimeLiteral
  183. Filter Expr
  184. Expr
  185. }
  186. type SortField struct {
  187. Name string
  188. StreamName StreamName
  189. Uname string // unique name of a field
  190. Ascending bool
  191. FieldExpr Expr
  192. Expr
  193. }
  194. func (sf *SortField) String() string {
  195. fe := ""
  196. if sf.FieldExpr != nil {
  197. fe += ", fieldExpr:{ " + sf.FieldExpr.String() + " }"
  198. }
  199. return "sortField:{ name:" + sf.Name + ", ascending:" + strconv.FormatBool(sf.Ascending) + fe + " }"
  200. }
  201. func (wd *Window) String() string {
  202. tu := ""
  203. if wd.TimeUnit != nil {
  204. tu += ", timeUnit: " + wd.TimeUnit.String() + " "
  205. }
  206. filter := ""
  207. if wd.Filter != nil {
  208. filter += ", " + wd.Filter.String()
  209. }
  210. return "window:{ windowType:" + wd.WindowType.String() + tu + filter + " }"
  211. }
  212. type SortFields []SortField
  213. func (d SortFields) node() {}
  214. const (
  215. RowkindInsert = "insert"
  216. RowkindUpdate = "update"
  217. RowkindUpsert = "upsert"
  218. RowkindDelete = "delete"
  219. )