funcsAggregate.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2021-2022 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 xsql
  15. import (
  16. "github.com/lf-edge/ekuiper/pkg/api"
  17. "github.com/lf-edge/ekuiper/pkg/errorx"
  18. )
  19. type AggregateFunctionValuer struct {
  20. data AggregateData
  21. fv *FunctionValuer
  22. }
  23. func NewFunctionValuersForOp(ctx api.StreamContext) (*FunctionValuer, *AggregateFunctionValuer) {
  24. p := NewFuncRuntime(ctx)
  25. return NewAggregateFunctionValuers(p)
  26. }
  27. // Should only be called by stream to make sure a single instance for an operation
  28. func NewAggregateFunctionValuers(p *funcRuntime) (*FunctionValuer, *AggregateFunctionValuer) {
  29. fv := NewFunctionValuer(p)
  30. return fv, &AggregateFunctionValuer{
  31. fv: fv,
  32. }
  33. }
  34. func (v *AggregateFunctionValuer) SetData(data AggregateData) {
  35. v.data = data
  36. }
  37. func (v *AggregateFunctionValuer) GetSingleCallValuer() CallValuer {
  38. return v.fv
  39. }
  40. func (v *AggregateFunctionValuer) Value(_, _ string) (interface{}, bool) {
  41. return nil, false
  42. }
  43. func (v *AggregateFunctionValuer) Meta(_, _ string) (interface{}, bool) {
  44. return nil, false
  45. }
  46. func (v *AggregateFunctionValuer) FuncValue(key string) (interface{}, bool) {
  47. if vv, ok := v.data.(FuncValuer); ok {
  48. return vv.FuncValue(key)
  49. }
  50. return nil, false
  51. }
  52. func (*AggregateFunctionValuer) AppendAlias(string, interface{}) bool {
  53. return false
  54. }
  55. func (v *AggregateFunctionValuer) AliasValue(_ string) (interface{}, bool) {
  56. return nil, false
  57. }
  58. func (v *AggregateFunctionValuer) Call(name string, funcId int, args []interface{}) (interface{}, bool) {
  59. nf, fctx, err := v.fv.runtime.Get(name, funcId)
  60. switch err {
  61. case errorx.NotFoundErr:
  62. return nil, false
  63. case nil:
  64. // do nothing, continue
  65. default:
  66. return err, false
  67. }
  68. return ExecFunc(name, nf, args, fctx)
  69. }
  70. func (v *AggregateFunctionValuer) GetAllTuples() AggregateData {
  71. return v.data
  72. }