project_operator.go 4.8 KB

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