table_processor.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package operator
  2. import (
  3. "fmt"
  4. "github.com/emqx/kuiper/internal/xsql"
  5. "github.com/emqx/kuiper/pkg/api"
  6. "github.com/emqx/kuiper/pkg/ast"
  7. )
  8. type TableProcessor struct {
  9. //Pruned stream fields. Could be streamField(with data type info) or string
  10. defaultFieldProcessor
  11. 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.
  12. retainSize int // how many(maximum) messages to be retained for each output
  13. emitterName string
  14. // States
  15. output xsql.WindowTuples // current batched message collection
  16. batchEmitted bool // if batch input, this is the signal for whether the last batch has emitted. If true, reinitialize.
  17. }
  18. func NewTableProcessor(name string, fields []interface{}, options *ast.Options) (*TableProcessor, error) {
  19. p := &TableProcessor{emitterName: name, batchEmitted: true, retainSize: 1}
  20. p.defaultFieldProcessor = defaultFieldProcessor{
  21. streamFields: fields, isBinary: false, timestampFormat: options.TIMESTAMP_FORMAT,
  22. }
  23. if options.RETAIN_SIZE > 0 {
  24. p.retainSize = options.RETAIN_SIZE
  25. p.isBatchInput = false
  26. } else if isBatch(options.TYPE) {
  27. p.isBatchInput = true
  28. p.retainSize = 0
  29. }
  30. return p, nil
  31. }
  32. /*
  33. * input: *xsql.Tuple or BatchCount
  34. * output: WindowTuples
  35. */
  36. func (p *TableProcessor) Apply(ctx api.StreamContext, data interface{}, fv *xsql.FunctionValuer, _ *xsql.AggregateFunctionValuer) interface{} {
  37. logger := ctx.GetLogger()
  38. tuple, ok := data.(*xsql.Tuple)
  39. if !ok {
  40. return fmt.Errorf("expect *xsql.Tuple data type")
  41. }
  42. logger.Debugf("preprocessor receive %v", tuple)
  43. if p.batchEmitted {
  44. p.output = xsql.WindowTuples{
  45. Emitter: p.emitterName,
  46. Tuples: make([]xsql.Tuple, 0),
  47. }
  48. p.batchEmitted = false
  49. }
  50. if tuple.Message != nil {
  51. result, err := p.processField(tuple, fv)
  52. if err != nil {
  53. return fmt.Errorf("error in table processor: %s", err)
  54. }
  55. tuple.Message = result
  56. var newTuples []xsql.Tuple
  57. for i, ot := range p.output.Tuples {
  58. if p.retainSize > 0 && len(p.output.Tuples) == p.retainSize && i == 0 {
  59. continue
  60. }
  61. newTuples = append(newTuples, ot)
  62. }
  63. newTuples = append(newTuples, *tuple)
  64. p.output = xsql.WindowTuples{
  65. Emitter: p.emitterName,
  66. Tuples: newTuples,
  67. }
  68. if !p.isBatchInput {
  69. return p.output
  70. }
  71. } else if p.isBatchInput { // EOF
  72. p.batchEmitted = true
  73. return p.output
  74. }
  75. return nil
  76. }
  77. func isBatch(t string) bool {
  78. return t == "file" || t == ""
  79. }