From 22ea6761ae9c35d2f26bd6fb8648d2c472a7ee17 Mon Sep 17 00:00:00 2001
From: chenmingsong <59043531+m-schen@users.noreply.github.com>
Date: Fri, 22 Jul 2022 14:11:38 +0800
Subject: [PATCH] add ut for intHashMap and fix intHashMap bugs (#4155)

---
 pkg/common/hashmap/inthashmap.go      |  3 ++
 pkg/common/hashmap/inthashmap_test.go | 52 +++++++++++++++++++++++++++
 pkg/common/hashmap/iterator.go        |  4 ++-
 3 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 pkg/common/hashmap/inthashmap_test.go

diff --git a/pkg/common/hashmap/inthashmap.go b/pkg/common/hashmap/inthashmap.go
index 46a5948d3..64e31e937 100644
--- a/pkg/common/hashmap/inthashmap.go
+++ b/pkg/common/hashmap/inthashmap.go
@@ -27,8 +27,11 @@ var wrongUseOfIntHashTable = errors.New(errno.InternalError, "wrong use of IntHa
 
 var zeroUint64 []uint64
 
+var zeroUint32 []uint32
+
 func init() {
 	zeroUint64 = make([]uint64, UnitLimit)
+	zeroUint32 = make([]uint32, UnitLimit)
 }
 
 // IntHashMap key is int64, value is an uint64 (start from 1)
diff --git a/pkg/common/hashmap/inthashmap_test.go b/pkg/common/hashmap/inthashmap_test.go
new file mode 100644
index 000000000..ae1baf14b
--- /dev/null
+++ b/pkg/common/hashmap/inthashmap_test.go
@@ -0,0 +1,52 @@
+// Copyright 2021 Matrix Origin
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package hashmap
+
+import (
+	"github.com/matrixorigin/matrixone/pkg/container/batch"
+	"github.com/matrixorigin/matrixone/pkg/container/types"
+	"github.com/matrixorigin/matrixone/pkg/container/vector"
+	"github.com/matrixorigin/matrixone/pkg/testutil"
+	"github.com/matrixorigin/matrixone/pkg/vm/mheap"
+	"github.com/matrixorigin/matrixone/pkg/vm/mmu/guest"
+	"github.com/matrixorigin/matrixone/pkg/vm/mmu/host"
+	"github.com/stretchr/testify/require"
+	"testing"
+)
+
+func TestIntHashMap_Iterator(t *testing.T) {
+	mp := NewIntHashMap(false)
+	m := mheap.New(guest.New(1<<30, host.New(1<<30)))
+	rowCount := 10
+	bat := &batch.Batch{
+		Cnt: 1,
+		Vecs: []*vector.Vector{
+			testutil.NewVector(rowCount, types.T_int32.ToType(), m, false, []int32{
+				-1, -1, -1, 2, 2, 2, 3, 3, 3, 4,
+			}),
+			testutil.NewVector(rowCount, types.T_uint32.ToType(), m, false, []uint32{
+				1, 1, 1, 2, 2, 2, 3, 3, 3, 4,
+			}),
+		},
+		Zs: testutil.MakeBatchZs(rowCount, false),
+	}
+	itr := mp.NewIterator()
+	vs, _ := itr.Insert(0, rowCount, bat.Vecs, make([]int32, rowCount))
+	require.Equal(t, []uint64{1, 1, 1, 2, 2, 2, 3, 3, 3, 4}, vs)
+	vs, _ = itr.Find(0, rowCount, bat.Vecs, make([]int32, rowCount))
+	require.Equal(t, []uint64{1, 1, 1, 2, 2, 2, 3, 3, 3, 4}, vs)
+	bat.Clean(m)
+	require.Equal(t, int64(0), mheap.Size(m))
+}
diff --git a/pkg/common/hashmap/iterator.go b/pkg/common/hashmap/iterator.go
index 27f3808c5..161ff6a6e 100644
--- a/pkg/common/hashmap/iterator.go
+++ b/pkg/common/hashmap/iterator.go
@@ -52,6 +52,7 @@ func (itr *intHashMapIterator) Find(start, count int, vecs []*vector.Vector, _ [
 		for i := 0; i < count; i++ {
 			itr.mp.keys[i] = 0
 		}
+		copy(itr.mp.keyOffs[:count], zeroUint32)
 	}()
 	if err := itr.mp.encodeHashKeys(vecs, start, count); err != nil {
 		panic(err)
@@ -66,9 +67,10 @@ func (itr *intHashMapIterator) Insert(start, count int, vecs []*vector.Vector, _
 		for i := 0; i < count; i++ {
 			itr.mp.keys[i] = 0
 		}
+		copy(itr.mp.keyOffs[:count], zeroUint32)
 	}()
 
-	if itr.mp.hasNull {
+	if !itr.mp.hasNull {
 		copy(itr.mp.zValues[:count], OneInt64s[:count])
 	}
 	if err := itr.mp.encodeHashKeys(vecs, start, count); err != nil {
-- 
GitLab