projectset_operator.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 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 operator
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/xsql"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. )
  20. type ProjectSetOperator struct {
  21. SrfMapping map[string]struct{}
  22. EnableLimit bool
  23. LimitCount int
  24. }
  25. // Apply implement UnOperation
  26. // ProjectSetOperator will extract the results from the set-returning-function into multi rows by aligning other columns
  27. // For tuple, ProjectSetOperator will do the following transform:
  28. // {"a":[1,2],"b":3} => {"a":1,"b":3},{"a":2,"b":3}
  29. // For Collection, ProjectSetOperator will do the following transform:
  30. // [{"a":[1,2],"b":3},{"a":[1,2],"b":4}] = > [{"a":"1","b":3},{"a":"2","b":3},{"a":"1","b":4},{"a":"2","b":4}]
  31. func (ps *ProjectSetOperator) Apply(_ api.StreamContext, data interface{}, _ *xsql.FunctionValuer, _ *xsql.AggregateFunctionValuer) interface{} {
  32. if ps.LimitCount == 0 && ps.EnableLimit {
  33. return []xsql.TupleRow{}
  34. }
  35. switch input := data.(type) {
  36. case error:
  37. return input
  38. case xsql.TupleRow:
  39. results, err := ps.handleSRFRow(input)
  40. if err != nil {
  41. return err
  42. }
  43. if ps.EnableLimit && ps.LimitCount > 0 && len(results.rows) > ps.LimitCount {
  44. return results.rows[:ps.LimitCount]
  45. }
  46. return results.rows
  47. case xsql.Collection:
  48. if ps.EnableLimit && ps.LimitCount > 0 && input.Len() > ps.LimitCount {
  49. sel := make([]int, 0, ps.LimitCount)
  50. for i := 0; i < ps.LimitCount; i++ {
  51. sel = append(sel, i)
  52. }
  53. input = input.Filter(sel)
  54. }
  55. if err := ps.handleSRFRowForCollection(input); err != nil {
  56. return err
  57. }
  58. if ps.EnableLimit && ps.LimitCount > 0 && input.Len() > ps.LimitCount {
  59. var sel []int
  60. sel = make([]int, ps.LimitCount, ps.LimitCount)
  61. for i := 0; i < ps.LimitCount; i++ {
  62. sel[i] = i
  63. }
  64. return input.Filter(sel)
  65. }
  66. return input
  67. default:
  68. return fmt.Errorf("run Select error: invalid input %[1]T(%[1]v)", input)
  69. }
  70. }
  71. func (ps *ProjectSetOperator) handleSRFRowForCollection(data xsql.Collection) error {
  72. switch collection := data.(type) {
  73. case *xsql.JoinTuples:
  74. newContent := make([]*xsql.JoinTuple, 0)
  75. for _, c := range collection.Content {
  76. rs, err := ps.handleSRFRow(c)
  77. if err != nil {
  78. return err
  79. }
  80. newContent = append(newContent, rs.joinTuples...)
  81. }
  82. collection.Content = newContent
  83. case *xsql.GroupedTuplesSet:
  84. newGroups := make([]*xsql.GroupedTuples, 0)
  85. for _, c := range collection.Groups {
  86. rs, err := ps.handleSRFRow(c)
  87. if err != nil {
  88. return err
  89. }
  90. newGroups = append(newGroups, rs.groupTuples...)
  91. }
  92. collection.Groups = newGroups
  93. case *xsql.WindowTuples:
  94. newContent := make([]xsql.TupleRow, 0)
  95. for _, c := range collection.Content {
  96. rs, err := ps.handleSRFRow(c)
  97. if err != nil {
  98. return err
  99. }
  100. newContent = append(newContent, rs.rows...)
  101. }
  102. collection.Content = newContent
  103. default:
  104. return fmt.Errorf("run Select error: invalid input %[1]T(%[1]v)", data)
  105. }
  106. return nil
  107. }
  108. func (ps *ProjectSetOperator) handleSRFRow(row xsql.CloneAbleRow) (*resultWrapper, error) {
  109. // for now we only support 1 srf function in the field
  110. srfName := ""
  111. for k := range ps.SrfMapping {
  112. srfName = k
  113. break
  114. }
  115. aValue, ok := row.Value(srfName, "")
  116. if !ok {
  117. return nil, fmt.Errorf("can't find the result from the %v function", srfName)
  118. }
  119. aValues, ok := aValue.([]interface{})
  120. if !ok {
  121. return nil, fmt.Errorf("the argument for the %v function should be array", srfName)
  122. }
  123. res := newResultWrapper(len(aValues), row)
  124. for i, v := range aValues {
  125. newTupleRow := row.Clone()
  126. // clear original column value
  127. newTupleRow.Del(srfName)
  128. if mv, ok := v.(map[string]interface{}); ok {
  129. for k, v := range mv {
  130. newTupleRow.Set(k, v)
  131. }
  132. } else {
  133. newTupleRow.Set(srfName, v)
  134. }
  135. res.appendTuple(i, newTupleRow)
  136. }
  137. return res, nil
  138. }
  139. type resultWrapper struct {
  140. joinTuples []*xsql.JoinTuple
  141. groupTuples []*xsql.GroupedTuples
  142. rows []xsql.TupleRow
  143. }
  144. func newResultWrapper(len int, row xsql.CloneAbleRow) *resultWrapper {
  145. r := &resultWrapper{}
  146. switch row.(type) {
  147. case *xsql.JoinTuple:
  148. r.joinTuples = make([]*xsql.JoinTuple, len)
  149. case *xsql.GroupedTuples:
  150. r.groupTuples = make([]*xsql.GroupedTuples, len)
  151. case xsql.TupleRow:
  152. r.rows = make([]xsql.TupleRow, len)
  153. }
  154. return r
  155. }
  156. func (r *resultWrapper) appendTuple(index int, newRow xsql.CloneAbleRow) {
  157. switch row := newRow.(type) {
  158. case *xsql.JoinTuple:
  159. r.joinTuples[index] = row
  160. case *xsql.GroupedTuples:
  161. r.groupTuples[index] = row
  162. case xsql.TupleRow:
  163. r.rows[index] = row
  164. }
  165. }