collection.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. // Copyright 2022-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 xsql
  15. import (
  16. "github.com/lf-edge/ekuiper/pkg/ast"
  17. )
  18. /*
  19. * Collection interfaces
  20. */
  21. // AggregateData Could be a tuple or collection
  22. type AggregateData interface {
  23. AggregateEval(expr ast.Expr, v CallValuer) []interface{}
  24. }
  25. type SortingData interface {
  26. Len() int
  27. Swap(i, j int)
  28. Index(i int) Row
  29. }
  30. // Collection A collection of rows as a table. It is used for window, join, group by, etc.
  31. type Collection interface {
  32. SortingData
  33. // GroupRange through each group. For non-grouped collection, the whole data is a single group
  34. GroupRange(func(i int, aggRow CollectionRow) (bool, error)) error
  35. // Range through each row. For grouped collection, each row is an aggregation of groups
  36. Range(func(i int, r ReadonlyRow) (bool, error)) error
  37. // RangeSet range through each row by cloneing the row
  38. RangeSet(func(i int, r Row) (bool, error)) error
  39. Filter(indexes []int) Collection
  40. GetWindowRange() *WindowRange
  41. Clone() Collection
  42. // ToMaps returns the data as a map
  43. ToMaps() []map[string]interface{}
  44. }
  45. type SingleCollection interface {
  46. Collection
  47. CollectionRow
  48. SetIsAgg(isAgg bool)
  49. // ToAggMaps returns the aggregated data as a map
  50. ToAggMaps() []map[string]interface{}
  51. // ToRowMaps returns all the data in the collection
  52. ToRowMaps() []map[string]interface{}
  53. }
  54. type GroupedCollection interface {
  55. Collection
  56. }
  57. // MergedCollection is a collection of rows that are from different sources
  58. type MergedCollection interface {
  59. Collection
  60. GetBySrc(emitter string) []TupleRow
  61. }
  62. /*
  63. * Collection types definitions
  64. */
  65. type WindowTuples struct {
  66. Content []TupleRow // immutable
  67. *WindowRange
  68. contentBySrc map[string][]TupleRow // volatile, temporary cache]
  69. AffiliateRow
  70. cachedMap map[string]interface{}
  71. isAgg bool
  72. }
  73. var (
  74. _ MergedCollection = &WindowTuples{}
  75. _ SingleCollection = &WindowTuples{}
  76. )
  77. // Window Tuples is also an aggregate row
  78. var _ CollectionRow = &WindowTuples{}
  79. type JoinTuples struct {
  80. Content []*JoinTuple
  81. *WindowRange
  82. AffiliateRow
  83. cachedMap map[string]interface{}
  84. isAgg bool
  85. }
  86. var (
  87. _ SingleCollection = &JoinTuples{}
  88. _ CollectionRow = &JoinTuples{}
  89. )
  90. type GroupedTuplesSet struct {
  91. Groups []*GroupedTuples
  92. *WindowRange
  93. }
  94. var _ GroupedCollection = &GroupedTuplesSet{}
  95. /*
  96. * Collection implementations
  97. */
  98. func (w *WindowTuples) Index(index int) Row {
  99. return w.Content[index]
  100. }
  101. func (w *WindowTuples) Len() int {
  102. return len(w.Content)
  103. }
  104. func (w *WindowTuples) Swap(i, j int) {
  105. w.cachedMap = nil
  106. w.Content[i], w.Content[j] = w.Content[j], w.Content[i]
  107. }
  108. func (w *WindowTuples) GetBySrc(emitter string) []TupleRow {
  109. if w.contentBySrc == nil {
  110. w.contentBySrc = make(map[string][]TupleRow)
  111. for _, t := range w.Content {
  112. e := t.GetEmitter()
  113. if _, hasEmitter := w.contentBySrc[e]; !hasEmitter {
  114. w.contentBySrc[e] = make([]TupleRow, 0)
  115. }
  116. w.contentBySrc[e] = append(w.contentBySrc[e], t)
  117. }
  118. }
  119. return w.contentBySrc[emitter]
  120. }
  121. func (w *WindowTuples) GetWindowRange() *WindowRange {
  122. return w.WindowRange
  123. }
  124. func (w *WindowTuples) Range(f func(i int, r ReadonlyRow) (bool, error)) error {
  125. for i, r := range w.Content {
  126. b, e := f(i, r)
  127. if e != nil {
  128. return e
  129. }
  130. if !b {
  131. break
  132. }
  133. }
  134. return nil
  135. }
  136. func (w *WindowTuples) RangeSet(f func(i int, r Row) (bool, error)) error {
  137. for i, r := range w.Content {
  138. rc := r.Clone()
  139. b, e := f(i, rc)
  140. if e != nil {
  141. return e
  142. }
  143. if !b {
  144. break
  145. }
  146. w.Content[i] = rc.(TupleRow)
  147. }
  148. return nil
  149. }
  150. func (w *WindowTuples) GroupRange(f func(i int, aggRow CollectionRow) (bool, error)) error {
  151. _, err := f(0, w)
  152. return err
  153. }
  154. func (w *WindowTuples) AddTuple(tuple *Tuple) *WindowTuples {
  155. w.Content = append(w.Content, tuple)
  156. return w
  157. }
  158. func (w *WindowTuples) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  159. var result []interface{}
  160. for _, t := range w.Content {
  161. result = append(result, Eval(expr, MultiValuer(t, &WindowRangeValuer{WindowRange: w.WindowRange}, v, &WildcardValuer{t})))
  162. }
  163. return result
  164. }
  165. // Filter the tuples by the given predicate
  166. func (w *WindowTuples) Filter(indexes []int) Collection {
  167. w.cachedMap = nil
  168. newC := make([]TupleRow, 0, len(indexes))
  169. for _, i := range indexes {
  170. newC = append(newC, w.Content[i])
  171. }
  172. w.Content = newC
  173. return w
  174. }
  175. func (w *WindowTuples) Value(key, table string) (interface{}, bool) {
  176. r, ok := w.AffiliateRow.Value(key, table)
  177. if ok {
  178. return r, ok
  179. }
  180. if len(w.Content) > 0 {
  181. return w.Content[0].Value(key, table)
  182. }
  183. return nil, false
  184. }
  185. func (w *WindowTuples) Meta(key, table string) (interface{}, bool) {
  186. if len(w.Content) > 0 {
  187. return w.Content[0].Value(key, table)
  188. }
  189. return nil, false
  190. }
  191. func (w *WindowTuples) All(_ string) (map[string]interface{}, bool) {
  192. if len(w.Content) == 0 {
  193. m := make(map[string]interface{})
  194. return m, true
  195. }
  196. return w.Content[0].All("")
  197. }
  198. func (w *WindowTuples) ToMap() map[string]interface{} {
  199. if w.cachedMap == nil {
  200. m := make(map[string]interface{})
  201. if len(w.Content) > 0 {
  202. for k, v := range w.Content[0].ToMap() {
  203. m[k] = v
  204. }
  205. }
  206. w.cachedMap = m
  207. }
  208. w.AffiliateRow.MergeMap(w.cachedMap)
  209. return w.cachedMap
  210. }
  211. func (w *WindowTuples) Clone() Collection {
  212. ts := make([]TupleRow, len(w.Content))
  213. for i, t := range w.Content {
  214. ts[i] = t.Clone().(TupleRow)
  215. }
  216. c := &WindowTuples{
  217. Content: ts,
  218. WindowRange: w.WindowRange,
  219. AffiliateRow: w.AffiliateRow.Clone(),
  220. isAgg: w.isAgg,
  221. }
  222. return c
  223. }
  224. func (w *WindowTuples) ToAggMaps() []map[string]interface{} {
  225. return []map[string]interface{}{w.ToMap()}
  226. }
  227. func (w *WindowTuples) ToRowMaps() []map[string]interface{} {
  228. r := make([]map[string]interface{}, len(w.Content))
  229. for i, t := range w.Content {
  230. r[i] = t.ToMap()
  231. }
  232. return r
  233. }
  234. func (w *WindowTuples) ToMaps() []map[string]interface{} {
  235. if w.isAgg {
  236. return w.ToAggMaps()
  237. } else {
  238. return w.ToRowMaps()
  239. }
  240. }
  241. func (w *WindowTuples) Pick(allWildcard bool, cols [][]string, wildcardEmitters map[string]bool, except []string) {
  242. cols = w.AffiliateRow.Pick(cols)
  243. for i, t := range w.Content {
  244. tc := t.Clone()
  245. tc.Pick(allWildcard, cols, wildcardEmitters, except)
  246. w.Content[i] = tc.(TupleRow)
  247. }
  248. }
  249. func (w *WindowTuples) SetIsAgg(_ bool) {
  250. w.isAgg = true
  251. }
  252. func (s *JoinTuples) Len() int { return len(s.Content) }
  253. func (s *JoinTuples) Swap(i, j int) {
  254. s.cachedMap = nil
  255. s.Content[i], s.Content[j] = s.Content[j], s.Content[i]
  256. }
  257. func (s *JoinTuples) Index(i int) Row { return s.Content[i] }
  258. func (s *JoinTuples) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  259. var result []interface{}
  260. for _, t := range s.Content {
  261. result = append(result, Eval(expr, MultiValuer(t, &WindowRangeValuer{WindowRange: s.WindowRange}, v, &WildcardValuer{t})))
  262. }
  263. return result
  264. }
  265. func (s *JoinTuples) GetWindowRange() *WindowRange {
  266. return s.WindowRange
  267. }
  268. func (s *JoinTuples) Range(f func(i int, r ReadonlyRow) (bool, error)) error {
  269. for i, r := range s.Content {
  270. b, e := f(i, r)
  271. if e != nil {
  272. return e
  273. }
  274. if !b {
  275. break
  276. }
  277. }
  278. return nil
  279. }
  280. func (s *JoinTuples) RangeSet(f func(i int, r Row) (bool, error)) error {
  281. for i, r := range s.Content {
  282. rc := r.Clone()
  283. b, e := f(i, rc)
  284. if e != nil {
  285. return e
  286. }
  287. if !b {
  288. break
  289. }
  290. s.Content[i] = rc.(*JoinTuple)
  291. }
  292. return nil
  293. }
  294. func (s *JoinTuples) GroupRange(f func(i int, aggRow CollectionRow) (bool, error)) error {
  295. _, err := f(0, s)
  296. return err
  297. }
  298. // Filter the tuples by the given predicate
  299. func (s *JoinTuples) Filter(indexes []int) Collection {
  300. newC := make([]*JoinTuple, 0, len(indexes))
  301. for _, i := range indexes {
  302. newC = append(newC, s.Content[i])
  303. }
  304. s.Content = newC
  305. s.cachedMap = nil
  306. return s
  307. }
  308. func (s *JoinTuples) Value(key, table string) (interface{}, bool) {
  309. r, ok := s.AffiliateRow.Value(key, table)
  310. if ok {
  311. return r, ok
  312. }
  313. return s.Content[0].Value(key, table)
  314. }
  315. func (s *JoinTuples) Meta(key, table string) (interface{}, bool) {
  316. return s.Content[0].Meta(key, table)
  317. }
  318. func (s *JoinTuples) All(_ string) (map[string]interface{}, bool) {
  319. return s.Content[0].All("")
  320. }
  321. func (s *JoinTuples) ToMap() map[string]interface{} {
  322. if s.cachedMap == nil {
  323. m := make(map[string]interface{})
  324. for k, v := range s.Content[0].ToMap() {
  325. m[k] = v
  326. }
  327. s.cachedMap = m
  328. }
  329. s.AffiliateRow.MergeMap(s.cachedMap)
  330. return s.cachedMap
  331. }
  332. func (s *JoinTuples) Clone() Collection {
  333. ts := make([]*JoinTuple, len(s.Content))
  334. for i, t := range s.Content {
  335. ts[i] = t.Clone().(*JoinTuple)
  336. }
  337. c := &JoinTuples{
  338. Content: ts,
  339. WindowRange: s.WindowRange,
  340. AffiliateRow: s.AffiliateRow.Clone(),
  341. isAgg: s.isAgg,
  342. }
  343. return c
  344. }
  345. func (s *JoinTuples) ToAggMaps() []map[string]interface{} {
  346. return []map[string]interface{}{s.ToMap()}
  347. }
  348. func (s *JoinTuples) ToRowMaps() []map[string]interface{} {
  349. r := make([]map[string]interface{}, len(s.Content))
  350. for i, t := range s.Content {
  351. r[i] = t.ToMap()
  352. }
  353. return r
  354. }
  355. func (s *JoinTuples) ToMaps() []map[string]interface{} {
  356. if s.isAgg {
  357. return s.ToAggMaps()
  358. } else {
  359. return s.ToRowMaps()
  360. }
  361. }
  362. func (s *JoinTuples) Pick(allWildcard bool, cols [][]string, wildcardEmitters map[string]bool, except []string) {
  363. cols = s.AffiliateRow.Pick(cols)
  364. for i, t := range s.Content {
  365. tc := t.Clone().(*JoinTuple)
  366. tc.Pick(allWildcard, cols, wildcardEmitters, except)
  367. s.Content[i] = tc
  368. }
  369. }
  370. func (s *JoinTuples) SetIsAgg(_ bool) {
  371. s.isAgg = true
  372. }
  373. func (s *GroupedTuplesSet) Len() int { return len(s.Groups) }
  374. func (s *GroupedTuplesSet) Swap(i, j int) { s.Groups[i], s.Groups[j] = s.Groups[j], s.Groups[i] }
  375. func (s *GroupedTuplesSet) Index(i int) Row { return s.Groups[i] }
  376. func (s *GroupedTuplesSet) GetWindowRange() *WindowRange {
  377. return s.WindowRange
  378. }
  379. func (s *GroupedTuplesSet) Range(f func(i int, r ReadonlyRow) (bool, error)) error {
  380. for i, r := range s.Groups {
  381. b, e := f(i, r)
  382. if e != nil {
  383. return e
  384. }
  385. if !b {
  386. break
  387. }
  388. }
  389. return nil
  390. }
  391. func (s *GroupedTuplesSet) RangeSet(f func(i int, r Row) (bool, error)) error {
  392. for i, r := range s.Groups {
  393. rc := r.Clone()
  394. b, e := f(i, rc)
  395. if e != nil {
  396. return e
  397. }
  398. if !b {
  399. break
  400. }
  401. s.Groups[i] = rc.(*GroupedTuples)
  402. }
  403. return nil
  404. }
  405. func (s *GroupedTuplesSet) GroupRange(f func(i int, aggRow CollectionRow) (bool, error)) error {
  406. for i, r := range s.Groups {
  407. b, e := f(i, r)
  408. if e != nil {
  409. return e
  410. }
  411. if !b {
  412. break
  413. }
  414. }
  415. return nil
  416. }
  417. // Filter clone and return the filtered set
  418. func (s *GroupedTuplesSet) Filter(groups []int) Collection {
  419. newC := make([]*GroupedTuples, 0, len(groups))
  420. for _, i := range groups {
  421. newC = append(newC, s.Groups[i])
  422. }
  423. s.Groups = newC
  424. return s
  425. }
  426. func (s *GroupedTuplesSet) Clone() Collection {
  427. ng := make([]*GroupedTuples, len(s.Groups))
  428. for i, g := range s.Groups {
  429. ng[i] = g.Clone().(*GroupedTuples)
  430. }
  431. return &GroupedTuplesSet{
  432. Groups: ng,
  433. WindowRange: s.WindowRange,
  434. }
  435. }
  436. func (s *GroupedTuplesSet) ToMaps() []map[string]interface{} {
  437. r := make([]map[string]interface{}, len(s.Groups))
  438. for i, t := range s.Groups {
  439. r[i] = t.ToMap()
  440. }
  441. return r
  442. }
  443. /*
  444. * WindowRange definitions. It should be immutable
  445. */
  446. type WindowRangeValuer struct {
  447. *WindowRange
  448. }
  449. func (w WindowRangeValuer) Value(_, _ string) (interface{}, bool) {
  450. return nil, false
  451. }
  452. func (w WindowRangeValuer) Meta(_, _ string) (interface{}, bool) {
  453. return nil, false
  454. }
  455. type WindowRange struct {
  456. windowStart int64
  457. windowEnd int64
  458. }
  459. func NewWindowRange(windowStart int64, windowEnd int64) *WindowRange {
  460. return &WindowRange{windowStart, windowEnd}
  461. }
  462. func (r *WindowRange) FuncValue(key string) (interface{}, bool) {
  463. switch key {
  464. case "window_start":
  465. return r.windowStart, true
  466. case "window_end":
  467. return r.windowEnd, true
  468. case "event_time":
  469. return r.windowEnd, true
  470. default:
  471. return nil, false
  472. }
  473. }