collection.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Copyright 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 xsql
  15. import (
  16. "github.com/lf-edge/ekuiper/pkg/ast"
  17. "sort"
  18. )
  19. /*
  20. * Collection interfaces
  21. */
  22. // AggregateData Could be a tuple or collection
  23. type AggregateData interface {
  24. AggregateEval(expr ast.Expr, v CallValuer) []interface{}
  25. }
  26. type SortingData interface {
  27. Len() int
  28. Swap(i, j int)
  29. Index(i int) Row
  30. }
  31. // Collection A collection of rows as a table. It is used for window, join, group by, etc.
  32. type Collection interface {
  33. SortingData
  34. // GroupRange through each group. For non-grouped collection, the whole data is a single group
  35. GroupRange(func(i int, aggRow CollectionRow) (bool, error)) error
  36. Filter(indexes []int) Collection
  37. GetWindowRange() *WindowRange
  38. }
  39. type SingleCollection interface {
  40. Collection
  41. // Range through each row. For grouped collection, each row is an aggregation of groups
  42. Range(func(i int, r TupleRow) (bool, error)) error
  43. }
  44. type GroupedCollection interface {
  45. Collection
  46. }
  47. // MergedCollection is a collection of rows that are from different sources
  48. type MergedCollection interface {
  49. Collection
  50. GetBySrc(emitter string) []TupleRow
  51. }
  52. /*
  53. * Collection types definitions
  54. */
  55. type WindowTuples struct {
  56. Content []TupleRow // immutable
  57. *WindowRange
  58. Alias
  59. contentBySrc map[string][]TupleRow // volatile, temporary cache
  60. }
  61. var _ MergedCollection = &WindowTuples{}
  62. // Window Tuples is also an aggregate row
  63. var _ CollectionRow = &WindowTuples{}
  64. type JoinTuples struct {
  65. Content []*JoinTuple
  66. *WindowRange
  67. Alias
  68. }
  69. var _ Collection = &JoinTuples{}
  70. var _ CollectionRow = &JoinTuples{}
  71. type GroupedTuplesSet struct {
  72. Groups []*GroupedTuples
  73. *WindowRange
  74. }
  75. var _ Collection = &GroupedTuplesSet{}
  76. /*
  77. * Collection implementations
  78. */
  79. func (w *WindowTuples) Index(index int) Row {
  80. return w.Content[index]
  81. }
  82. func (w *WindowTuples) Len() int {
  83. return len(w.Content)
  84. }
  85. func (w *WindowTuples) Swap(i, j int) {
  86. w.Content[i], w.Content[j] = w.Content[j], w.Content[i]
  87. }
  88. func (w *WindowTuples) GetBySrc(emitter string) []TupleRow {
  89. if w.contentBySrc == nil {
  90. w.contentBySrc = make(map[string][]TupleRow)
  91. for _, t := range w.Content {
  92. e := t.GetEmitter()
  93. if _, hasEmitter := w.contentBySrc[e]; !hasEmitter {
  94. w.contentBySrc[e] = make([]TupleRow, 0)
  95. }
  96. w.contentBySrc[e] = append(w.contentBySrc[e], t)
  97. }
  98. }
  99. return w.contentBySrc[emitter]
  100. }
  101. func (w *WindowTuples) GetWindowRange() *WindowRange {
  102. return w.WindowRange
  103. }
  104. func (w *WindowTuples) Range(f func(i int, r TupleRow) (bool, error)) error {
  105. for i, r := range w.Content {
  106. b, e := f(i, r)
  107. if e != nil {
  108. return e
  109. }
  110. if !b {
  111. break
  112. }
  113. }
  114. return nil
  115. }
  116. func (w *WindowTuples) GroupRange(f func(i int, aggRow CollectionRow) (bool, error)) error {
  117. _, err := f(0, w)
  118. return err
  119. }
  120. func (w *WindowTuples) AddTuple(tuple *Tuple) *WindowTuples {
  121. w.Content = append(w.Content, tuple)
  122. return w
  123. }
  124. //Sort by tuple timestamp
  125. func (w *WindowTuples) Sort() {
  126. sort.SliceStable(w.Content, func(i, j int) bool {
  127. return w.Content[i].(Event).GetTimestamp() < w.Content[j].(Event).GetTimestamp()
  128. })
  129. }
  130. func (w *WindowTuples) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  131. var result []interface{}
  132. for _, t := range w.Content {
  133. result = append(result, Eval(expr, MultiValuer(t, &WindowRangeValuer{WindowRange: w.WindowRange}, v, &WildcardValuer{t})))
  134. }
  135. return result
  136. }
  137. // Filter the tuples by the given predicate
  138. func (w *WindowTuples) Filter(indexes []int) Collection {
  139. newC := make([]TupleRow, 0, len(indexes))
  140. for _, i := range indexes {
  141. newC = append(newC, w.Content[i])
  142. }
  143. w.Content = newC
  144. return w
  145. }
  146. func (w *WindowTuples) Value(key, table string) (interface{}, bool) {
  147. return w.Content[0].Value(key, table)
  148. }
  149. func (w *WindowTuples) Meta(key, table string) (interface{}, bool) {
  150. return w.Content[0].Meta(key, table)
  151. }
  152. func (w *WindowTuples) All(stream string) (Message, bool) {
  153. return w.Content[0].All(stream)
  154. }
  155. func (s *JoinTuples) Len() int { return len(s.Content) }
  156. func (s *JoinTuples) Swap(i, j int) { s.Content[i], s.Content[j] = s.Content[j], s.Content[i] }
  157. func (s *JoinTuples) Index(i int) Row { return s.Content[i] }
  158. func (s *JoinTuples) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  159. var result []interface{}
  160. for _, t := range s.Content {
  161. result = append(result, Eval(expr, MultiValuer(t, &WindowRangeValuer{WindowRange: s.WindowRange}, v, &WildcardValuer{t})))
  162. }
  163. return result
  164. }
  165. func (s *JoinTuples) GetWindowRange() *WindowRange {
  166. return s.WindowRange
  167. }
  168. func (s *JoinTuples) Range(f func(i int, r TupleRow) (bool, error)) error {
  169. for i, r := range s.Content {
  170. b, e := f(i, r)
  171. if e != nil {
  172. return e
  173. }
  174. if !b {
  175. break
  176. }
  177. }
  178. return nil
  179. }
  180. func (s *JoinTuples) GroupRange(f func(i int, aggRow CollectionRow) (bool, error)) error {
  181. _, err := f(0, s)
  182. return err
  183. }
  184. // Filter the tuples by the given predicate
  185. func (s *JoinTuples) Filter(indexes []int) Collection {
  186. newC := make([]*JoinTuple, 0, len(indexes))
  187. for _, i := range indexes {
  188. newC = append(newC, s.Content[i])
  189. }
  190. s.Content = newC
  191. return s
  192. }
  193. func (s *JoinTuples) Value(key, table string) (interface{}, bool) {
  194. return s.Content[0].Value(key, table)
  195. }
  196. func (s *JoinTuples) Meta(key, table string) (interface{}, bool) {
  197. return s.Content[0].Meta(key, table)
  198. }
  199. func (s *JoinTuples) All(stream string) (Message, bool) {
  200. return s.Content[0].All(stream)
  201. }
  202. func (s *GroupedTuplesSet) Len() int { return len(s.Groups) }
  203. func (s *GroupedTuplesSet) Swap(i, j int) { s.Groups[i], s.Groups[j] = s.Groups[j], s.Groups[i] }
  204. func (s *GroupedTuplesSet) Index(i int) Row { return s.Groups[i] }
  205. func (s *GroupedTuplesSet) GetWindowRange() *WindowRange {
  206. return s.WindowRange
  207. }
  208. func (s *GroupedTuplesSet) GroupRange(f func(i int, aggRow CollectionRow) (bool, error)) error {
  209. for i, r := range s.Groups {
  210. b, e := f(i, r)
  211. if e != nil {
  212. return e
  213. }
  214. if !b {
  215. break
  216. }
  217. }
  218. return nil
  219. }
  220. // Filter clone and return the filtered set
  221. func (s *GroupedTuplesSet) Filter(groups []int) Collection {
  222. newC := make([]*GroupedTuples, 0, len(groups))
  223. for _, i := range groups {
  224. newC = append(newC, s.Groups[i])
  225. }
  226. s.Groups = newC
  227. return s
  228. }
  229. /*
  230. * WindowRange definitions. It should be immutable
  231. */
  232. type WindowRangeValuer struct {
  233. *WindowRange
  234. }
  235. func (w WindowRangeValuer) Value(_, _ string) (interface{}, bool) {
  236. return nil, false
  237. }
  238. func (w WindowRangeValuer) Meta(_, _ string) (interface{}, bool) {
  239. return nil, false
  240. }
  241. type WindowRange struct {
  242. windowStart int64
  243. windowEnd int64
  244. }
  245. func NewWindowRange(windowStart int64, windowEnd int64) *WindowRange {
  246. return &WindowRange{windowStart, windowEnd}
  247. }
  248. func (r *WindowRange) FuncValue(key string) (interface{}, bool) {
  249. switch key {
  250. case "window_start":
  251. return r.windowStart, true
  252. case "window_end":
  253. return r.windowEnd, true
  254. default:
  255. return nil, false
  256. }
  257. }