소스 검색

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

Signed-off-by: Jiyong Huang <huangjy@emqx.io>
ngjaying 1 년 전
부모
커밋
c94606e4f6
2개의 변경된 파일18개의 추가작업 그리고 2개의 파일을 삭제
  1. 11 2
      internal/binder/function/funcs_array.go
  2. 7 0
      internal/binder/function/funcs_array_test.go

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

@@ -494,9 +494,18 @@ func registerArrayFunc() {
 			set := make(map[interface{}]bool)
 
 			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)
-					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},
 		},
 		{
+			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",
 			args: []interface{}{
 				"round", []interface{}{0, 0.4, 1.2},