table_processor.go 2.5 KB

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