project_operator.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 operator
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/binder/function"
  18. "github.com/lf-edge/ekuiper/internal/xsql"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "github.com/lf-edge/ekuiper/pkg/ast"
  21. "github.com/lf-edge/ekuiper/pkg/message"
  22. )
  23. type ProjectOp struct {
  24. ColNames [][]string // list of [col, table]
  25. AliasNames []string // list of alias name
  26. ExprNames []string // list of expr name
  27. ExceptNames []string // list of except name
  28. AllWildcard bool
  29. WildcardEmitters map[string]bool
  30. AliasFields ast.Fields
  31. ExprFields ast.Fields
  32. IsAggregate bool
  33. SendMeta bool
  34. kvs []interface{}
  35. alias []interface{}
  36. }
  37. // Apply
  38. //
  39. // input: *xsql.Tuple| xsql.Collection
  40. //
  41. // output: []map[string]interface{}
  42. func (pp *ProjectOp) Apply(ctx api.StreamContext, data interface{}, fv *xsql.FunctionValuer, afv *xsql.AggregateFunctionValuer) interface{} {
  43. log := ctx.GetLogger()
  44. log.Debugf("project plan receive %v", data)
  45. switch input := data.(type) {
  46. case error:
  47. return input
  48. case *xsql.Tuple:
  49. ve := pp.getVE(input, input, nil, fv, afv)
  50. if err := pp.project(input, ve); err != nil {
  51. return fmt.Errorf("run Select error: %s", err)
  52. } else {
  53. if pp.SendMeta && input.Metadata != nil {
  54. input.Set(message.MetaKey, input.Metadata)
  55. }
  56. }
  57. case xsql.SingleCollection:
  58. var err error
  59. if pp.IsAggregate {
  60. input.SetIsAgg(true)
  61. err = input.GroupRange(func(_ int, aggRow xsql.CollectionRow) (bool, error) {
  62. ve := pp.getVE(aggRow, aggRow, input.GetWindowRange(), fv, afv)
  63. if err := pp.project(aggRow, ve); err != nil {
  64. return false, fmt.Errorf("run Select error: %s", err)
  65. }
  66. return true, nil
  67. })
  68. } else {
  69. err = input.RangeSet(func(_ int, row xsql.Row) (bool, error) {
  70. aggData, ok := input.(xsql.AggregateData)
  71. if !ok {
  72. return false, fmt.Errorf("unexpected type, cannot find aggregate data")
  73. }
  74. ve := pp.getVE(row, aggData, input.GetWindowRange(), fv, afv)
  75. if err := pp.project(row, ve); err != nil {
  76. return false, fmt.Errorf("run Select error: %s", err)
  77. }
  78. return true, nil
  79. })
  80. }
  81. if err != nil {
  82. return err
  83. }
  84. case xsql.GroupedCollection: // The order is important, because single collection usually is also a groupedCollection
  85. err := input.GroupRange(func(_ int, aggRow xsql.CollectionRow) (bool, error) {
  86. ve := pp.getVE(aggRow, aggRow, input.GetWindowRange(), fv, afv)
  87. if err := pp.project(aggRow, ve); err != nil {
  88. return false, fmt.Errorf("run Select error: %s", err)
  89. }
  90. return true, nil
  91. })
  92. if err != nil {
  93. return err
  94. }
  95. default:
  96. return fmt.Errorf("run Select error: invalid input %[1]T(%[1]v)", input)
  97. }
  98. return data
  99. }
  100. func (pp *ProjectOp) getVE(tuple xsql.Row, agg xsql.AggregateData, wr *xsql.WindowRange, fv *xsql.FunctionValuer, afv *xsql.AggregateFunctionValuer) *xsql.ValuerEval {
  101. afv.SetData(agg)
  102. if pp.IsAggregate {
  103. return &xsql.ValuerEval{Valuer: xsql.MultiAggregateValuer(agg, fv, tuple, fv, afv, &xsql.WildcardValuer{Data: tuple})}
  104. } else {
  105. if wr != nil {
  106. return &xsql.ValuerEval{Valuer: xsql.MultiValuer(tuple, &xsql.WindowRangeValuer{WindowRange: wr}, fv, &xsql.WildcardValuer{Data: tuple})}
  107. }
  108. return &xsql.ValuerEval{Valuer: xsql.MultiValuer(tuple, fv, &xsql.WildcardValuer{Data: tuple})}
  109. }
  110. }
  111. func (pp *ProjectOp) project(row xsql.Row, ve *xsql.ValuerEval) error {
  112. // Calculate all fields then pick the needed ones
  113. // To make sure all calculations are run with the same context (e.g. alias values)
  114. // Do not set value during calculations
  115. for _, f := range pp.ExprFields {
  116. vi := ve.Eval(f.Expr)
  117. if e, ok := vi.(error); ok {
  118. return e
  119. }
  120. if vi != nil {
  121. switch vt := vi.(type) {
  122. case function.ResultCols:
  123. for k, v := range vt {
  124. pp.kvs = append(pp.kvs, k, v)
  125. }
  126. default:
  127. pp.kvs = append(pp.kvs, f.Name, vi)
  128. }
  129. }
  130. }
  131. for _, f := range pp.AliasFields {
  132. vi := ve.Eval(f.Expr)
  133. if e, ok := vi.(error); ok {
  134. return e
  135. }
  136. if vi != nil {
  137. pp.alias = append(pp.alias, f.AName, vi)
  138. }
  139. }
  140. row.Pick(pp.AllWildcard, pp.ColNames, pp.WildcardEmitters, pp.ExceptNames)
  141. for i := 0; i < len(pp.kvs); i += 2 {
  142. row.Set(pp.kvs[i].(string), pp.kvs[i+1])
  143. }
  144. pp.kvs = pp.kvs[:0]
  145. for i := 0; i < len(pp.alias); i += 2 {
  146. row.AppendAlias(pp.alias[i].(string), pp.alias[i+1])
  147. }
  148. pp.alias = pp.alias[:0]
  149. return nil
  150. }