collection.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. ** Various Data Types for SQL transformation
  21. */
  22. type AggregateData interface {
  23. AggregateEval(expr ast.Expr, v CallValuer) []interface{}
  24. }
  25. type Event interface {
  26. GetTimestamp() int64
  27. IsWatermark() bool
  28. }
  29. type WindowTuples struct {
  30. Emitter string
  31. Tuples []Tuple
  32. }
  33. type WindowRangeValuer struct {
  34. *WindowRange
  35. }
  36. func (r *WindowRangeValuer) Value(_, _ string) (interface{}, bool) {
  37. return nil, false
  38. }
  39. func (r *WindowRangeValuer) Meta(_, _ string) (interface{}, bool) {
  40. return nil, false
  41. }
  42. func (r *WindowRangeValuer) AppendAlias(_ string, _ interface{}) bool {
  43. return false
  44. }
  45. func (r *WindowRangeValuer) AliasValue(_ string) (interface{}, bool) {
  46. return nil, false
  47. }
  48. type WindowRange struct {
  49. WindowStart int64
  50. WindowEnd int64
  51. }
  52. func (r *WindowRange) FuncValue(key string) (interface{}, bool) {
  53. switch key {
  54. case "window_start":
  55. return r.WindowStart, true
  56. case "window_end":
  57. return r.WindowEnd, true
  58. default:
  59. return nil, false
  60. }
  61. }
  62. type WindowTuplesSet struct {
  63. Content []WindowTuples
  64. *WindowRange
  65. }
  66. func (w WindowTuplesSet) GetBySrc(src string) []Tuple {
  67. for _, me := range w.Content {
  68. if me.Emitter == src {
  69. return me.Tuples
  70. }
  71. }
  72. return nil
  73. }
  74. func (w WindowTuplesSet) Len() int {
  75. if len(w.Content) > 0 {
  76. return len(w.Content[0].Tuples)
  77. }
  78. return 0
  79. }
  80. func (w WindowTuplesSet) Swap(i, j int) {
  81. if len(w.Content) > 0 {
  82. s := w.Content[0].Tuples
  83. s[i], s[j] = s[j], s[i]
  84. }
  85. }
  86. func (w WindowTuplesSet) Index(i int) Valuer {
  87. if len(w.Content) > 0 {
  88. s := w.Content[0].Tuples
  89. return &(s[i])
  90. }
  91. return nil
  92. }
  93. func (w WindowTuplesSet) AddTuple(tuple *Tuple) WindowTuplesSet {
  94. found := false
  95. for i, t := range w.Content {
  96. if t.Emitter == tuple.Emitter {
  97. t.Tuples = append(t.Tuples, *tuple)
  98. found = true
  99. w.Content[i] = t
  100. break
  101. }
  102. }
  103. if !found {
  104. ets := &WindowTuples{Emitter: tuple.Emitter}
  105. ets.Tuples = append(ets.Tuples, *tuple)
  106. w.Content = append(w.Content, *ets)
  107. }
  108. return w
  109. }
  110. //Sort by tuple timestamp
  111. func (w WindowTuplesSet) Sort() {
  112. for _, t := range w.Content {
  113. tuples := t.Tuples
  114. sort.SliceStable(tuples, func(i, j int) bool {
  115. return tuples[i].Timestamp < tuples[j].Timestamp
  116. })
  117. t.Tuples = tuples
  118. }
  119. }
  120. func (w WindowTuplesSet) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  121. var result []interface{}
  122. if len(w.Content) != 1 { //should never happen
  123. return nil
  124. }
  125. for _, t := range w.Content[0].Tuples {
  126. result = append(result, Eval(expr, MultiValuer(&t, &WindowRangeValuer{WindowRange: w.WindowRange}, v, &WildcardValuer{&t})))
  127. }
  128. return result
  129. }
  130. func getTupleValue(tuple Tuple, key string, isVal bool) (interface{}, bool) {
  131. if isVal {
  132. return tuple.Value(key, "")
  133. } else {
  134. return tuple.Meta(key, "")
  135. }
  136. }
  137. type JoinTupleSets struct {
  138. Content []JoinTuple
  139. *WindowRange
  140. }
  141. func (s *JoinTupleSets) Len() int { return len(s.Content) }
  142. func (s *JoinTupleSets) Swap(i, j int) { s.Content[i], s.Content[j] = s.Content[j], s.Content[i] }
  143. func (s *JoinTupleSets) Index(i int) Valuer { return &(s.Content[i]) }
  144. func (s *JoinTupleSets) AggregateEval(expr ast.Expr, v CallValuer) []interface{} {
  145. var result []interface{}
  146. for _, t := range s.Content {
  147. result = append(result, Eval(expr, MultiValuer(&t, &WindowRangeValuer{WindowRange: s.WindowRange}, v, &WildcardValuer{&t})))
  148. }
  149. return result
  150. }
  151. type GroupedTuplesSet []GroupedTuples
  152. func (s GroupedTuplesSet) Len() int { return len(s) }
  153. func (s GroupedTuplesSet) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  154. func (s GroupedTuplesSet) Index(i int) Valuer { return s[i].Content[0] }