statement.go 3.0 KB

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