collection.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. // Range through each row. For grouped collection, each row is an aggregation of groups
  37. Range(func(i int, r Row) (bool, error)) error
  38. Filter(indexes []int) Collection
  39. GetWindowRange() *WindowRange
  40. Clone() Collection
  41. // ToAggMaps returns the aggregated data as a map
  42. ToAggMaps() []map[string]interface{}
  43. // ToRowMaps returns all the data in the collection
  44. ToRowMaps() []map[string]interface{}
  45. }
  46. type SingleCollection interface {
  47. Collection
  48. CollectionRow
  49. }
  50. type GroupedCollection interface {
  51. Collection
  52. }
  53. // MergedCollection is a collection of rows that are from different sources
  54. type MergedCollection interface {
  55. Collection
  56. GetBySrc(emitter string) []TupleRow
  57. }
  58. /*
  59. * Collection types definitions
  60. */
  61. type WindowTuples struct {
  62. Content []TupleRow // immutable
  63. *WindowRange
  64. contentBySrc map[string][]TupleRow // volatile, temporary cache]
  65. AffiliateRow
  66. cachedMap map[string]interface{}
  67. }
  68. var _ MergedCollection = &WindowTuples{}
  69. var _ SingleCollection = &WindowTuples{}
  70. // Window Tuples is also an aggregate row
  71. var _ CollectionRow = &WindowTuples{}
  72. type JoinTuples struct {
  73. Content []*JoinTuple
  74. *WindowRange
  75. AffiliateRow
  76. cachedMap map[string]interface{}
  77. }
  78. var _ SingleCollection = &JoinTuples{}
  79. var _ CollectionRow = &JoinTuples{}
  80. type GroupedTuplesSet struct {
  81. Groups []*GroupedTuples
  82. *WindowRange
  83. }
  84. var _ GroupedCollection = &GroupedTuplesSet{}
  85. /*
  86. * Collection implementations
  87. */
  88. func (w *WindowTuples) Index(index int) Row {
  89. return w.Content[index]
  90. }
  91. func (w *WindowTuples) Len() int {
  92. return len(w.Content)
  93. }
  94. func (w *WindowTuples) Swap(i, j int) {
  95. w.cachedMap = nil
  96. w.Content[i], w.Content[j] = w.Content[j], w.Content[i]
  97. }
  98. func (w *WindowTuples) GetBySrc(emitter string) []TupleRow {
  99. if w.contentBySrc == nil {
  100. w.contentBySrc = make(map[string][]TupleRow)
  101. for _, t := range w.Content {
  102. e := t.GetEmitter()
  103. if _, hasEmitter := w.contentBySrc[e]; !hasEmitter {
  104. w.contentBySrc[e] = make([]TupleRow, 0)
  105. }
  106. w.contentBySrc[e] = append(w.contentBySrc[e], t)
  107. }
  108. }
  109. return w.contentBySrc[emitter]
  110. }
  111. func (w *WindowTuples) GetWindowRange() *WindowRange {
  112. return w.WindowRange
  113. }
  114. func (w *WindowTuples) Range(f func(i int, r Row) (bool, error)) error {
  115. for i, r := range w.Content {
  116. b, e := f(i, r)
  117. if e != nil {
  118. return e
  119. }
  120. if !b {
  121. break
  122. }
  123. }
  124. return nil
  125. }
  126. func (w *WindowTuples) GroupRange(f func(i int, aggRow CollectionRow) (bool, error)) error {
  127. _, err := f(0, w)
  128. return err
  129. }
  130. func (w *WindowTuples) AddTuple(tuple *Tuple) *WindowTuples {
  131. w.Content = append(w.Content, tuple)
  132. return w
  133. }
  134. //Sort by tuple timestamp
  135. func (w *WindowTuples) Sort() {
  136. w.cachedMap = nil
  137. sort.SliceStable(w.Content, func(i, j int) bool {
  138. return w.Content[i].(Event).GetTimestamp() < w.Content[j].(Event).GetTimestamp()
  139. })
  140. }
  141. func (w *WindowTuples) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  142. var result []interface{}
  143. for _, t := range w.Content {
  144. result = append(result, Eval(expr, MultiValuer(t, &WindowRangeValuer{WindowRange: w.WindowRange}, v, &WildcardValuer{t})))
  145. }
  146. return result
  147. }
  148. // Filter the tuples by the given predicate
  149. func (w *WindowTuples) Filter(indexes []int) Collection {
  150. w.cachedMap = nil
  151. newC := make([]TupleRow, 0, len(indexes))
  152. for _, i := range indexes {
  153. newC = append(newC, w.Content[i])
  154. }
  155. w.Content = newC
  156. return w
  157. }
  158. func (w *WindowTuples) Value(key, table string) (interface{}, bool) {
  159. r, ok := w.AffiliateRow.Value(key, table)
  160. if ok {
  161. return r, ok
  162. }
  163. return w.Content[0].Value(key, table)
  164. }
  165. func (w *WindowTuples) Meta(key, table string) (interface{}, bool) {
  166. return w.Content[0].Meta(key, table)
  167. }
  168. func (w *WindowTuples) All(stream string) (Message, bool) {
  169. return w.ToMap(), true
  170. }
  171. func (w *WindowTuples) ToMap() map[string]interface{} {
  172. if w.cachedMap == nil {
  173. m := make(map[string]interface{})
  174. for k, v := range w.Content[0].ToMap() {
  175. m[k] = v
  176. }
  177. w.cachedMap = m
  178. }
  179. w.AffiliateRow.MergeMap(w.cachedMap)
  180. return w.cachedMap
  181. }
  182. func (w *WindowTuples) Clone() Collection {
  183. ts := make([]TupleRow, len(w.Content))
  184. for i, t := range w.Content {
  185. ts[i] = t.Clone()
  186. }
  187. c := &WindowTuples{
  188. Content: ts,
  189. WindowRange: w.WindowRange,
  190. AffiliateRow: w.AffiliateRow.Clone(),
  191. }
  192. return c
  193. }
  194. func (w *WindowTuples) ToAggMaps() []map[string]interface{} {
  195. return []map[string]interface{}{w.ToMap()}
  196. }
  197. func (w *WindowTuples) ToRowMaps() []map[string]interface{} {
  198. r := make([]map[string]interface{}, len(w.Content))
  199. for i, t := range w.Content {
  200. r[i] = t.ToMap()
  201. }
  202. return r
  203. }
  204. func (s *JoinTuples) Len() int { return len(s.Content) }
  205. func (s *JoinTuples) Swap(i, j int) {
  206. s.cachedMap = nil
  207. s.Content[i], s.Content[j] = s.Content[j], s.Content[i]
  208. }
  209. func (s *JoinTuples) Index(i int) Row { return s.Content[i] }
  210. func (s *JoinTuples) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  211. var result []interface{}
  212. for _, t := range s.Content {
  213. result = append(result, Eval(expr, MultiValuer(t, &WindowRangeValuer{WindowRange: s.WindowRange}, v, &WildcardValuer{t})))
  214. }
  215. return result
  216. }
  217. func (s *JoinTuples) GetWindowRange() *WindowRange {
  218. return s.WindowRange
  219. }
  220. func (s *JoinTuples) Range(f func(i int, r Row) (bool, error)) error {
  221. for i, r := range s.Content {
  222. b, e := f(i, r)
  223. if e != nil {
  224. return e
  225. }
  226. if !b {
  227. break
  228. }
  229. }
  230. return nil
  231. }
  232. func (s *JoinTuples) GroupRange(f func(i int, aggRow CollectionRow) (bool, error)) error {
  233. _, err := f(0, s)
  234. return err
  235. }
  236. // Filter the tuples by the given predicate
  237. func (s *JoinTuples) Filter(indexes []int) Collection {
  238. newC := make([]*JoinTuple, 0, len(indexes))
  239. for _, i := range indexes {
  240. newC = append(newC, s.Content[i])
  241. }
  242. s.Content = newC
  243. s.cachedMap = nil
  244. return s
  245. }
  246. func (s *JoinTuples) Value(key, table string) (interface{}, bool) {
  247. r, ok := s.AffiliateRow.Value(key, table)
  248. if ok {
  249. return r, ok
  250. }
  251. return s.Content[0].Value(key, table)
  252. }
  253. func (s *JoinTuples) Meta(key, table string) (interface{}, bool) {
  254. return s.Content[0].Meta(key, table)
  255. }
  256. func (s *JoinTuples) All(stream string) (Message, bool) {
  257. return s.ToMap(), true
  258. }
  259. func (s *JoinTuples) ToMap() map[string]interface{} {
  260. if s.cachedMap == nil {
  261. m := make(map[string]interface{})
  262. for k, v := range s.Content[0].ToMap() {
  263. m[k] = v
  264. }
  265. s.cachedMap = m
  266. }
  267. s.AffiliateRow.MergeMap(s.cachedMap)
  268. return s.cachedMap
  269. }
  270. func (s *JoinTuples) Clone() Collection {
  271. ts := make([]*JoinTuple, len(s.Content))
  272. for i, t := range s.Content {
  273. ts[i] = t.Clone().(*JoinTuple)
  274. }
  275. c := &JoinTuples{
  276. Content: ts,
  277. WindowRange: s.WindowRange,
  278. AffiliateRow: s.AffiliateRow.Clone(),
  279. }
  280. return c
  281. }
  282. func (s *JoinTuples) ToAggMaps() []map[string]interface{} {
  283. return []map[string]interface{}{s.ToMap()}
  284. }
  285. func (s *JoinTuples) ToRowMaps() []map[string]interface{} {
  286. r := make([]map[string]interface{}, len(s.Content))
  287. for i, t := range s.Content {
  288. r[i] = t.ToMap()
  289. }
  290. return r
  291. }
  292. func (s *GroupedTuplesSet) Len() int { return len(s.Groups) }
  293. func (s *GroupedTuplesSet) Swap(i, j int) { s.Groups[i], s.Groups[j] = s.Groups[j], s.Groups[i] }
  294. func (s *GroupedTuplesSet) Index(i int) Row { return s.Groups[i] }
  295. func (s *GroupedTuplesSet) GetWindowRange() *WindowRange {
  296. return s.WindowRange
  297. }
  298. func (s *GroupedTuplesSet) Range(f func(i int, r Row) (bool, error)) error {
  299. for i, r := range s.Groups {
  300. b, e := f(i, r)
  301. if e != nil {
  302. return e
  303. }
  304. if !b {
  305. break
  306. }
  307. }
  308. return nil
  309. }
  310. func (s *GroupedTuplesSet) GroupRange(f func(i int, aggRow CollectionRow) (bool, error)) error {
  311. for i, r := range s.Groups {
  312. b, e := f(i, r)
  313. if e != nil {
  314. return e
  315. }
  316. if !b {
  317. break
  318. }
  319. }
  320. return nil
  321. }
  322. // Filter clone and return the filtered set
  323. func (s *GroupedTuplesSet) Filter(groups []int) Collection {
  324. newC := make([]*GroupedTuples, 0, len(groups))
  325. for _, i := range groups {
  326. newC = append(newC, s.Groups[i])
  327. }
  328. s.Groups = newC
  329. return s
  330. }
  331. func (s *GroupedTuplesSet) Clone() Collection {
  332. ng := make([]*GroupedTuples, len(s.Groups))
  333. for i, g := range s.Groups {
  334. ng[i] = g.Clone().(*GroupedTuples)
  335. }
  336. return &GroupedTuplesSet{
  337. Groups: ng,
  338. WindowRange: s.WindowRange,
  339. }
  340. }
  341. func (s *GroupedTuplesSet) ToAggMaps() []map[string]interface{} {
  342. return s.ToRowMaps()
  343. }
  344. func (s *GroupedTuplesSet) ToRowMaps() []map[string]interface{} {
  345. r := make([]map[string]interface{}, len(s.Groups))
  346. for i, t := range s.Groups {
  347. r[i] = t.ToMap()
  348. }
  349. return r
  350. }
  351. /*
  352. * WindowRange definitions. It should be immutable
  353. */
  354. type WindowRangeValuer struct {
  355. *WindowRange
  356. }
  357. func (w WindowRangeValuer) Value(_, _ string) (interface{}, bool) {
  358. return nil, false
  359. }
  360. func (w WindowRangeValuer) Meta(_, _ string) (interface{}, bool) {
  361. return nil, false
  362. }
  363. type WindowRange struct {
  364. windowStart int64
  365. windowEnd int64
  366. }
  367. func NewWindowRange(windowStart int64, windowEnd int64) *WindowRange {
  368. return &WindowRange{windowStart, windowEnd}
  369. }
  370. func (r *WindowRange) FuncValue(key string) (interface{}, bool) {
  371. switch key {
  372. case "window_start":
  373. return r.windowStart, true
  374. case "window_end":
  375. return r.windowEnd, true
  376. default:
  377. return nil, false
  378. }
  379. }