project_operator.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package plans
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/emqx/kuiper/xsql"
  6. "github.com/emqx/kuiper/xstream/api"
  7. "strconv"
  8. "strings"
  9. )
  10. type ProjectPlan struct {
  11. Fields xsql.Fields
  12. IsAggregate bool
  13. isTest bool
  14. }
  15. /**
  16. * input: *xsql.Tuple from preprocessor or filterOp | xsql.WindowTuplesSet from windowOp or filterOp | xsql.JoinTupleSets from joinOp or filterOp
  17. * output: []map[string]interface{}
  18. */
  19. func (pp *ProjectPlan) Apply(ctx api.StreamContext, data interface{}) interface{} {
  20. log := ctx.GetLogger()
  21. log.Debugf("project plan receive %s", data)
  22. var results []map[string]interface{}
  23. switch input := data.(type) {
  24. case *xsql.Tuple:
  25. ve := pp.getVE(input, input)
  26. results = append(results, project(pp.Fields, ve, pp.isTest))
  27. case xsql.WindowTuplesSet:
  28. if len(input) != 1 {
  29. log.Infof("WindowTuplesSet with multiple tuples cannot be evaluated")
  30. return nil
  31. }
  32. ms := input[0].Tuples
  33. for _, v := range ms {
  34. ve := pp.getVE(&v, input)
  35. results = append(results, project(pp.Fields, ve, pp.isTest))
  36. if pp.IsAggregate{
  37. break
  38. }
  39. }
  40. case xsql.JoinTupleSets:
  41. ms := input
  42. for _, v := range ms {
  43. ve := pp.getVE(&v, input)
  44. results = append(results, project(pp.Fields, ve, pp.isTest))
  45. if pp.IsAggregate{
  46. break
  47. }
  48. }
  49. case xsql.GroupedTuplesSet:
  50. for _, v := range input{
  51. ve := pp.getVE(v[0], v)
  52. results = append(results, project(pp.Fields, ve, pp.isTest))
  53. }
  54. default:
  55. log.Errorf("Expect xsql.Valuer or its array type")
  56. return nil
  57. }
  58. if ret, err := json.Marshal(results); err == nil {
  59. return ret
  60. } else {
  61. fmt.Printf("Found error: %v", err)
  62. return nil
  63. }
  64. }
  65. func (pp *ProjectPlan) getVE(tuple xsql.DataValuer, agg xsql.AggregateData) *xsql.ValuerEval{
  66. if pp.IsAggregate{
  67. return &xsql.ValuerEval{Valuer: xsql.MultiAggregateValuer(agg, tuple, &xsql.FunctionValuer{}, &xsql.AggregateFunctionValuer{Data: agg}, &xsql.WildcardValuer{Data: tuple})}
  68. }else{
  69. return &xsql.ValuerEval{Valuer: xsql.MultiValuer(tuple, &xsql.FunctionValuer{}, &xsql.WildcardValuer{Data: tuple})}
  70. }
  71. }
  72. func project(fs xsql.Fields, ve *xsql.ValuerEval, isTest bool) map[string]interface{} {
  73. result := make(map[string]interface{})
  74. for _, f := range fs {
  75. //Avoid to re-evaluate for non-agg field has alias name, which was already evaluated in pre-processor operator.
  76. if f.AName != "" && (!xsql.HasAggFuncs(f.Expr)) && !isTest{
  77. fr := &xsql.FieldRef{StreamName:"", Name:f.AName}
  78. v := ve.Eval(fr)
  79. result[f.AName] = v
  80. } else {
  81. v := ve.Eval(f.Expr)
  82. if _, ok := f.Expr.(*xsql.Wildcard); ok || f.Name == "*"{
  83. switch val := v.(type) {
  84. case map[string]interface{} :
  85. for k, v := range val{
  86. if _, ok := result[k]; !ok{
  87. result[k] = v
  88. }
  89. }
  90. case xsql.Message:
  91. for k, v := range val{
  92. if _, ok := result[k]; !ok{
  93. result[k] = v
  94. }
  95. }
  96. default:
  97. fmt.Printf("Wildcarder does not return map")
  98. }
  99. } else {
  100. if v != nil {
  101. n := assignName(f.Name, f.AName, result)
  102. if _, ok := result[n]; !ok{
  103. result[n] = v
  104. }
  105. }
  106. }
  107. }
  108. }
  109. return result
  110. }
  111. const DEFAULT_FIELD_NAME_PREFIX string = "rengine_field_"
  112. func assignName(name, alias string, fields map[string] interface{}) string {
  113. if result := strings.Trim(alias, " "); result != "" {
  114. return result
  115. }
  116. if result := strings.Trim(name, " "); result != "" {
  117. return result
  118. }
  119. for i := 0; i < 2048; i++ {
  120. key := DEFAULT_FIELD_NAME_PREFIX + strconv.Itoa(i)
  121. if _, ok := fields[key]; !ok {
  122. return key
  123. }
  124. }
  125. fmt.Printf("Cannot assign a default field name")
  126. return ""
  127. }