project_operator.go 4.8 KB

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