Repository: lf-edge/ekuiper
Pull request #1593 (2023-01-05 14:53:14)

fix(function): add stats function for built-in function
Signed-off-by: Jianxiang Ran <rxan_embedded@163.com>


Patch:
diff --git a/internal/binder/function/funcs_agg.go b/internal/binder/function/funcs_agg.go
index 0baa0666..02b69023 100644
--- a/internal/binder/function/funcs_agg.go
+++ b/internal/binder/function/funcs_agg.go
@@ -290,12 +290,18 @@ func registerAggFunc() {
 			if err := ValidateLen(2, len(args)); err != nil {
 				return err, false
 			}
+			var arg1Float64 float64 = 1
 			arg0 := args[0].([]interface{})
-			arg1 := args[1]
-			arg1Float64, err := cast.ToFloat64(arg1, cast.CONVERT_SAMEKIND)
-			if err != nil {
-				return fmt.Errorf("the second parameter requires float64 but found %[1]T(%[1]v)", arg1), false
+			arg1 := args[1].([]interface{})
+			if len(arg1) > 0 {
+				v1 := getFirstValidArg(arg1)
+				val, err := cast.ToFloat64(v1, cast.CONVERT_SAMEKIND)
+				if err != nil {
+					return fmt.Errorf("the second parameter requires float64 but found %[1]T(%[1]v)", arg1), false
+				}
+				arg1Float64 = val
 			}
+
 			if len(arg0) > 0 {
 				float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)
 				if err != nil {
@@ -317,11 +323,16 @@ func registerAggFunc() {
 			if err := ValidateLen(2, len(args)); err != nil {
 				return err, false
 			}
+			var arg1Float64 float64 = 1
 			arg0 := args[0].([]interface{})
-			arg1 := args[1]
-			arg1Float64, err := cast.ToFloat64(arg1, cast.CONVERT_SAMEKIND)
-			if err != nil {
-				return fmt.Errorf("the second parameter requires float64 but found %[1]T(%[1]v)", arg1), false
+			arg1 := args[1].([]interface{})
+			if len(arg1) > 0 {
+				v1 := getFirstValidArg(arg1)
+				val, err := cast.ToFloat64(v1, cast.CONVERT_SAMEKIND)
+				if err != nil {
+					return fmt.Errorf("the second parameter requires float64 but found %[1]T(%[1]v)", arg1), false
+				}
+				arg1Float64 = val
 			}
 			if len(arg0) > 0 {
 				float64Slice, err := cast.ToFloat64Slice(arg0, cast.CONVERT_SAMEKIND)


Test patch:
diff --git a/internal/binder/function/funcs_agg_test.go b/internal/binder/function/funcs_agg_test.go
index 228cd125..5a08c63b 100644
--- a/internal/binder/function/funcs_agg_test.go
+++ b/internal/binder/function/funcs_agg_test.go
@@ -184,7 +184,7 @@ func TestPercentileExec(t *testing.T) {
 					"bar",
 					"self",
 				},
-				0.25,
+				[]interface{}{0.25, 0.25, 0.25},
 			},
 			pCont: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
 			pDisc: fmt.Errorf("requires float64 slice but found []interface {}([foo bar self])"),
@@ -205,7 +205,7 @@ func TestPercentileExec(t *testing.T) {
 					int64(150),
 					int64(200),
 				},
-				0.5,
+				[]interface{}{0.5, 0.5, 0.5},
 			},
 			pCont: float64(125),
 			pDisc: float64(150),
@@ -216,7 +216,7 @@ func TestPercentileExec(t *testing.T) {
 					float64(150),
 					float64(200),
 				},
-				0.5,
+				[]interface{}{0.5, 0.5, 0.5},
 			},
 			pCont: float64(125),
 			pDisc: float64(150),
@@ -225,7 +225,7 @@ func TestPercentileExec(t *testing.T) {
 				[]interface{}{
 					100, 150, 200,
 				},
-				0.5,
+				[]interface{}{0.5, 0.5, 0.5},
 			},
 			pCont: float64(125),
 			pDisc: float64(150),