Переглянути джерело

fix(func): array distinct ignore un-hashable (#2190)

Signed-off-by: Jiyong Huang <huangjy@emqx.io>
ngjaying 1 рік тому
батько
коміт
c94606e4f6

+ 11 - 2
internal/binder/function/funcs_array.go

@@ -494,9 +494,18 @@ func registerArrayFunc() {
 			set := make(map[interface{}]bool)
 			set := make(map[interface{}]bool)
 
 
 			for _, val := range array {
 			for _, val := range array {
-				if !set[val] {
+				switch val.(type) {
+				case int, int8, int16, int32, int64,
+					uint, uint8, uint16, uint32, uint64,
+					float32, float64,
+					string,
+					bool:
+					if !set[val] {
+						output = append(output, val)
+						set[val] = true
+					}
+				default: // all un-hashable types are not deduplicated, including array, map, etc.
 					output = append(output, val)
 					output = append(output, val)
-					set[val] = true
 				}
 				}
 			}
 			}
 
 

+ 7 - 0
internal/binder/function/funcs_array_test.go

@@ -533,6 +533,13 @@ func TestArrayCommonFunctions(t *testing.T) {
 			result: []interface{}{1, 2},
 			result: []interface{}{1, 2},
 		},
 		},
 		{
 		{
+			name: "array_distinct",
+			args: []interface{}{
+				[]interface{}{map[string]any{"a": 1}, map[string]any{"a": 1}, map[string]any{"a": 2}},
+			},
+			result: []interface{}{map[string]any{"a": 1}, map[string]any{"a": 1}, map[string]any{"a": 2}},
+		},
+		{
 			name: "array_map",
 			name: "array_map",
 			args: []interface{}{
 			args: []interface{}{
 				"round", []interface{}{0, 0.4, 1.2},
 				"round", []interface{}{0, 0.4, 1.2},