From 0160bc9908d79207d20c80bd5b21d38ddb6d3475 Mon Sep 17 00:00:00 2001 From: WinfredLIN Date: Fri, 6 Dec 2024 14:40:58 +0800 Subject: [PATCH 1/2] fix: panic when input slice are all zero length --- sqle/utils/util.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/sqle/utils/util.go b/sqle/utils/util.go index b1b31f9d0..270611677 100644 --- a/sqle/utils/util.go +++ b/sqle/utils/util.go @@ -91,19 +91,27 @@ func MergeAndDeduplicateSort(arr1, arr2 []string) []string { // 合并两个数组 merged := append(arr1, arr2...) - // 排序 - sort.Strings(merged) - - // 去重 - i := 0 - for j := 1; j < len(merged); j++ { - if merged[i] != merged[j] { - i++ - merged[i] = merged[j] + // 如果合并后的数组是空的,直接返回空切片 + if len(merged) == 0 { + return []string{} + } + + // 使用map去重 + seen := make(map[string]struct{}) + // 预分配足够的空间,避免多次内存分配 + result := make([]string, 0, len(merged)) + + for _, str := range merged { + if _, exists := seen[str]; !exists { + seen[str] = struct{}{} + result = append(result, str) } } - return merged[:i+1] + // 排序 + sort.Strings(result) + + return result } func RemoveDuplicatePtrUint64(c []*uint64) []*uint64 { From c3d663ead2180105f6a4b56580642c2df2bd0fce Mon Sep 17 00:00:00 2001 From: WinfredLIN Date: Fri, 6 Dec 2024 14:41:14 +0800 Subject: [PATCH 2/2] test: unit test for MergeAndDeduplicate --- sqle/utils/util_test.go | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/sqle/utils/util_test.go b/sqle/utils/util_test.go index 7a24db76c..21cb92dd9 100644 --- a/sqle/utils/util_test.go +++ b/sqle/utils/util_test.go @@ -42,20 +42,77 @@ func TestMergeAndDeduplicate(t *testing.T) { expect []string }{ { + // 普通情况:两个数组有重复元素,且有不同的元素 []string{"apple", "banana", "cherry", "apple"}, []string{"banana", "orange", "grape"}, []string{"apple", "banana", "cherry", "grape", "orange"}, }, { + // 一个数组为空,另一个数组有多个元素 []string{}, []string{"apple", "banana"}, []string{"apple", "banana"}, }, { + // 一个数组有单个元素,另一个数组为空 []string{"apple"}, []string{}, []string{"apple"}, }, + { + // 两个数组都为空 + []string{}, + []string{}, + []string{}, + }, + { + // 数组中所有元素相同 + []string{"apple", "apple", "apple"}, + []string{"apple", "apple"}, + []string{"apple"}, + }, + { + // 数组已经是有序的 + []string{"apple", "banana", "cherry"}, + []string{"date", "grape", "orange"}, + []string{"apple", "banana", "cherry", "date", "grape", "orange"}, + }, + { + // 两个数组完全不同,且没有重复 + []string{"apple", "banana", "cherry"}, + []string{"date", "grape", "orange"}, + []string{"apple", "banana", "cherry", "date", "grape", "orange"}, + }, + { + // 两个数组有重复元素,且重复项位于不同位置 + []string{"apple", "banana", "cherry"}, + []string{"banana", "cherry", "apple"}, + []string{"apple", "banana", "cherry"}, + }, + { + // 数组中有空字符串 + []string{"apple", "banana", "", "cherry"}, + []string{"", "grape", "orange"}, + []string{"", "apple", "banana", "cherry", "grape", "orange"}, + }, + { + // 数组中有大小写不同的相同元素 + []string{"apple", "banana", "Apple"}, + []string{"banana", "orange", "APPLE"}, + []string{"APPLE", "Apple", "apple", "banana", "orange"}, + }, + { + // 大数组测试,随机生成的大数据 + []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}, + []string{"j", "i", "h", "g", "f", "e", "d", "c", "b", "a"}, + []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}, + }, + { + // 边界情况:只有一个元素在两个数组中 + []string{"apple"}, + []string{"apple"}, + []string{"apple"}, + }, } for _, tc := range testCases {