Skip to content

Commit 944c17c

Browse files
committed
vector: always check types in *NoTypeCheck functions if race tag is set
1 parent baf10a6 commit 944c17c

File tree

5 files changed

+58
-6
lines changed

5 files changed

+58
-6
lines changed

pkg/container/vector/functionTool_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func TestReuseFunctionParameterFixed(t *testing.T) {
140140
var err error
141141
vec1 := NewVec(types.T_int32.ToType())
142142
for i := uint64(0); i < 10; i++ {
143-
err = appendOneFixed(vec1, i, false, mp)
143+
err = appendOneFixed(vec1, int32(i), false, mp)
144144
require.NoError(t, err)
145145
}
146146
g2 := GenerateFunctionFixedTypeParameter[int32](vec1)
@@ -162,7 +162,7 @@ func TestReuseFunctionParameterFixed(t *testing.T) {
162162
ok = ReuseFunctionFixedTypeParameter(vec1, g2)
163163
require.Equal(t, true, ok)
164164

165-
err = appendOneFixed(vec1, 0, false, mp)
165+
err = appendOneFixed(vec1, int32(0), false, mp)
166166
require.NoError(t, err)
167167
ok = ReuseFunctionFixedTypeParameter(vec1, g2)
168168
require.Equal(t, false, ok)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2025 Matrix Origin
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build !race
16+
17+
package vector
18+
19+
func checkTypeIfRaceDetectorEnabled[T any](vec *Vector) {
20+
// do nothing
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2025 Matrix Origin
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build race
16+
17+
package vector
18+
19+
func checkTypeIfRaceDetectorEnabled[T any](vec *Vector) {
20+
checkType[T](vec)
21+
}

pkg/container/vector/utils.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,19 @@ func typeCompatible[T any](typ types.Type) bool {
346346
case int32:
347347
return typ.Oid == types.T_int32
348348
case int64:
349-
return typ.Oid == types.T_int64
349+
return typ.Oid == types.T_int64 ||
350+
typ.Oid == types.T_timestamp ||
351+
typ.Oid == types.T_decimal64
350352
case uint8:
351353
return typ.Oid == types.T_uint8
352354
case uint16:
353355
return typ.Oid == types.T_uint16
354356
case uint32:
355357
return typ.Oid == types.T_uint32
356358
case uint64:
357-
return typ.Oid == types.T_uint64 || typ.Oid == types.T_bit
359+
return typ.Oid == types.T_uint64 ||
360+
typ.Oid == types.T_bit ||
361+
typ.Oid == types.T_decimal64
358362
case float32:
359363
return typ.Oid == types.T_float32
360364
case float64:

pkg/container/vector/vector.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,24 @@ func (t *typedSlice) setFromVector(v *Vector) {
8888
}
8989

9090
func ToSliceNoTypeCheck[T any](vec *Vector, ret *[]T) {
91+
checkTypeIfRaceDetectorEnabled[T](vec)
9192
*ret = unsafe.Slice((*T)(vec.col.Ptr), vec.col.Cap)
9293
}
9394

9495
func ToSliceNoTypeCheck2[T any](vec *Vector) []T {
96+
checkTypeIfRaceDetectorEnabled[T](vec)
9597
return unsafe.Slice((*T)(vec.col.Ptr), vec.col.Cap)
9698
}
9799

98100
func ToSlice[T any](vec *Vector, ret *[]T) {
101+
checkType[T](vec)
102+
*ret = unsafe.Slice((*T)(vec.col.Ptr), vec.col.Cap)
103+
}
104+
105+
func checkType[T any](vec *Vector) {
99106
if !typeCompatible[T](vec.typ) {
100-
panic(fmt.Sprintf("type mismatch: %T %v", []T{}, vec.typ.String()))
107+
panic(fmt.Sprintf("type mismatch: casting %v vector to %T", vec.typ.String(), []T{}))
101108
}
102-
*ret = unsafe.Slice((*T)(vec.col.Ptr), vec.col.Cap)
103109
}
104110

105111
func (v *Vector) GetSorted() bool {

0 commit comments

Comments
 (0)