funcsAggregate.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 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) Call(name string, args []interface{}) (interface{}, bool) {
  56. nf, fctx, err := v.fv.runtime.Get(name)
  57. switch err {
  58. case errorx.NotFoundErr:
  59. return nil, false
  60. case nil:
  61. // do nothing, continue
  62. default:
  63. return err, false
  64. }
  65. return ExecFunc(name, nf, args, fctx)
  66. }
  67. func (v *AggregateFunctionValuer) GetAllTuples() AggregateData {
  68. return v.data
  69. }