table_processor.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2021 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. "github.com/lf-edge/ekuiper/pkg/ast"
  20. )
  21. type TableProcessor struct {
  22. //Pruned stream fields. Could be streamField(with data type info) or string
  23. defaultFieldProcessor
  24. isBatchInput bool // whether the inputs are batched, such as file which sends multiple messages at a batch. If batch input, only fires when EOF is received. This is mutual exclusive with retainSize.
  25. retainSize int // how many(maximum) messages to be retained for each output
  26. emitterName string
  27. // States
  28. output xsql.WindowTuples // current batched message collection
  29. batchEmitted bool // if batch input, this is the signal for whether the last batch has emitted. If true, reinitialize.
  30. }
  31. func NewTableProcessor(name string, fields []interface{}, options *ast.Options) (*TableProcessor, error) {
  32. p := &TableProcessor{emitterName: name, batchEmitted: true, retainSize: 1}
  33. p.defaultFieldProcessor = defaultFieldProcessor{
  34. streamFields: fields, isBinary: false, timestampFormat: options.TIMESTAMP_FORMAT,
  35. strictValidation: options.STRICT_VALIDATION,
  36. }
  37. if options.RETAIN_SIZE > 0 {
  38. p.retainSize = options.RETAIN_SIZE
  39. p.isBatchInput = false
  40. } else if isBatch(options.TYPE) {
  41. p.isBatchInput = true
  42. p.retainSize = 0
  43. }
  44. return p, nil
  45. }
  46. /*
  47. * input: *xsql.Tuple or BatchCount
  48. * output: WindowTuples
  49. */
  50. func (p *TableProcessor) Apply(ctx api.StreamContext, data interface{}, fv *xsql.FunctionValuer, _ *xsql.AggregateFunctionValuer) interface{} {
  51. logger := ctx.GetLogger()
  52. tuple, ok := data.(*xsql.Tuple)
  53. if !ok {
  54. return fmt.Errorf("expect *xsql.Tuple data type")
  55. }
  56. logger.Debugf("preprocessor receive %v", tuple)
  57. if p.batchEmitted {
  58. p.output = xsql.WindowTuples{
  59. Emitter: p.emitterName,
  60. Tuples: make([]xsql.Tuple, 0),
  61. }
  62. p.batchEmitted = false
  63. }
  64. if tuple.Message != nil {
  65. result, err := p.processField(tuple, fv)
  66. if err != nil {
  67. return fmt.Errorf("error in table processor: %s", err)
  68. }
  69. tuple.Message = result
  70. var newTuples []xsql.Tuple
  71. for i, ot := range p.output.Tuples {
  72. if p.retainSize > 0 && len(p.output.Tuples) == p.retainSize && i == 0 {
  73. continue
  74. }
  75. newTuples = append(newTuples, ot)
  76. }
  77. newTuples = append(newTuples, *tuple)
  78. p.output = xsql.WindowTuples{
  79. Emitter: p.emitterName,
  80. Tuples: newTuples,
  81. }
  82. if !p.isBatchInput {
  83. return p.output
  84. }
  85. } else if p.isBatchInput { // EOF
  86. p.batchEmitted = true
  87. return p.output
  88. }
  89. return nil
  90. }
  91. func isBatch(t string) bool {
  92. return t == "file" || t == ""
  93. }