projectset_operator.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 err := ps.handleSRFRowForCollection(input); err != nil {
  49. return err
  50. }
  51. if ps.EnableLimit && ps.LimitCount > 0 && input.Len() > ps.LimitCount {
  52. var sel []int
  53. sel = make([]int, ps.LimitCount, ps.LimitCount)
  54. for i := 0; i < ps.LimitCount; i++ {
  55. sel[i] = i
  56. }
  57. return input.Filter(sel)
  58. }
  59. return input
  60. default:
  61. return fmt.Errorf("run Select error: invalid input %[1]T(%[1]v)", input)
  62. }
  63. }
  64. func (ps *ProjectSetOperator) handleSRFRowForCollection(data xsql.Collection) error {
  65. switch collection := data.(type) {
  66. case *xsql.JoinTuples:
  67. newContent := make([]*xsql.JoinTuple, 0)
  68. for _, c := range collection.Content {
  69. rs, err := ps.handleSRFRow(c)
  70. if err != nil {
  71. return err
  72. }
  73. newContent = append(newContent, rs.joinTuples...)
  74. }
  75. collection.Content = newContent
  76. case *xsql.GroupedTuplesSet:
  77. newGroups := make([]*xsql.GroupedTuples, 0)
  78. for _, c := range collection.Groups {
  79. rs, err := ps.handleSRFRow(c)
  80. if err != nil {
  81. return err
  82. }
  83. newGroups = append(newGroups, rs.groupTuples...)
  84. }
  85. collection.Groups = newGroups
  86. case *xsql.WindowTuples:
  87. newContent := make([]xsql.TupleRow, 0)
  88. for _, c := range collection.Content {
  89. rs, err := ps.handleSRFRow(c)
  90. if err != nil {
  91. return err
  92. }
  93. newContent = append(newContent, rs.rows...)
  94. }
  95. collection.Content = newContent
  96. default:
  97. return fmt.Errorf("run Select error: invalid input %[1]T(%[1]v)", data)
  98. }
  99. return nil
  100. }
  101. func (ps *ProjectSetOperator) handleSRFRow(row xsql.CloneAbleRow) (*resultWrapper, error) {
  102. // for now we only support 1 srf function in the field
  103. srfName := ""
  104. for k := range ps.SrfMapping {
  105. srfName = k
  106. break
  107. }
  108. aValue, ok := row.Value(srfName, "")
  109. if !ok {
  110. return nil, fmt.Errorf("can't find the result from the %v function", srfName)
  111. }
  112. aValues, ok := aValue.([]interface{})
  113. if !ok {
  114. return nil, fmt.Errorf("the argument for the %v function should be array", srfName)
  115. }
  116. res := newResultWrapper(len(aValues), row)
  117. for i, v := range aValues {
  118. newTupleRow := row.Clone()
  119. // clear original column value
  120. newTupleRow.Del(srfName)
  121. if mv, ok := v.(map[string]interface{}); ok {
  122. for k, v := range mv {
  123. newTupleRow.Set(k, v)
  124. }
  125. } else {
  126. newTupleRow.Set(srfName, v)
  127. }
  128. res.appendTuple(i, newTupleRow)
  129. }
  130. return res, nil
  131. }
  132. type resultWrapper struct {
  133. joinTuples []*xsql.JoinTuple
  134. groupTuples []*xsql.GroupedTuples
  135. rows []xsql.TupleRow
  136. }
  137. func newResultWrapper(len int, row xsql.CloneAbleRow) *resultWrapper {
  138. r := &resultWrapper{}
  139. switch row.(type) {
  140. case *xsql.JoinTuple:
  141. r.joinTuples = make([]*xsql.JoinTuple, len)
  142. case *xsql.GroupedTuples:
  143. r.groupTuples = make([]*xsql.GroupedTuples, len)
  144. case xsql.TupleRow:
  145. r.rows = make([]xsql.TupleRow, len)
  146. }
  147. return r
  148. }
  149. func (r *resultWrapper) appendTuple(index int, newRow xsql.CloneAbleRow) {
  150. switch row := newRow.(type) {
  151. case *xsql.JoinTuple:
  152. r.joinTuples[index] = row
  153. case *xsql.GroupedTuples:
  154. r.groupTuples[index] = row
  155. case xsql.TupleRow:
  156. r.rows[index] = row
  157. }
  158. }