projectset_operator.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. }
  23. func (ps *ProjectSetOperator) Apply(_ api.StreamContext, data interface{}, _ *xsql.FunctionValuer, _ *xsql.AggregateFunctionValuer) interface{} {
  24. // for now we only support 1 srf function in the field
  25. srfName := ""
  26. for k := range ps.SrfMapping {
  27. srfName = k
  28. break
  29. }
  30. switch input := data.(type) {
  31. case error:
  32. return []interface{}{input}
  33. case xsql.TupleRow:
  34. aValue, ok := input.Value(srfName, "")
  35. if !ok {
  36. return fmt.Errorf("can't find the result from the %v function", srfName)
  37. }
  38. aValues, ok := aValue.([]interface{})
  39. if !ok {
  40. return fmt.Errorf("the result from the %v function should be array", srfName)
  41. }
  42. newData := make([]interface{}, 0)
  43. for _, v := range aValues {
  44. newTupleRow := input.Clone()
  45. // clear original column value
  46. newTupleRow.Del(srfName)
  47. if mv, ok := v.(map[string]interface{}); ok {
  48. for k, v := range mv {
  49. newTupleRow.Set(k, v)
  50. }
  51. } else {
  52. newTupleRow.Set(srfName, v)
  53. }
  54. newData = append(newData, newTupleRow)
  55. }
  56. return newData
  57. default:
  58. return fmt.Errorf("run Select error: invalid input %[1]T(%[1]v)", input)
  59. }
  60. }