Skip to content

Commit

Permalink
vector: always check types in *NoTypeCheck functions if race tag is set
Browse files Browse the repository at this point in the history
  • Loading branch information
reusee committed Feb 13, 2025
1 parent baf10a6 commit 944c17c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/container/vector/functionTool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestReuseFunctionParameterFixed(t *testing.T) {
var err error
vec1 := NewVec(types.T_int32.ToType())
for i := uint64(0); i < 10; i++ {
err = appendOneFixed(vec1, i, false, mp)
err = appendOneFixed(vec1, int32(i), false, mp)
require.NoError(t, err)
}
g2 := GenerateFunctionFixedTypeParameter[int32](vec1)
Expand All @@ -162,7 +162,7 @@ func TestReuseFunctionParameterFixed(t *testing.T) {
ok = ReuseFunctionFixedTypeParameter(vec1, g2)
require.Equal(t, true, ok)

err = appendOneFixed(vec1, 0, false, mp)
err = appendOneFixed(vec1, int32(0), false, mp)
require.NoError(t, err)
ok = ReuseFunctionFixedTypeParameter(vec1, g2)
require.Equal(t, false, ok)
Expand Down
21 changes: 21 additions & 0 deletions pkg/container/vector/type_check_no_race.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2025 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.

//go:build !race

package vector

func checkTypeIfRaceDetectorEnabled[T any](vec *Vector) {
// do nothing
}
21 changes: 21 additions & 0 deletions pkg/container/vector/type_check_race.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2025 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.

//go:build race

package vector

func checkTypeIfRaceDetectorEnabled[T any](vec *Vector) {
checkType[T](vec)
}
8 changes: 6 additions & 2 deletions pkg/container/vector/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,19 @@ func typeCompatible[T any](typ types.Type) bool {
case int32:
return typ.Oid == types.T_int32
case int64:
return typ.Oid == types.T_int64
return typ.Oid == types.T_int64 ||
typ.Oid == types.T_timestamp ||
typ.Oid == types.T_decimal64
case uint8:
return typ.Oid == types.T_uint8
case uint16:
return typ.Oid == types.T_uint16
case uint32:
return typ.Oid == types.T_uint32
case uint64:
return typ.Oid == types.T_uint64 || typ.Oid == types.T_bit
return typ.Oid == types.T_uint64 ||
typ.Oid == types.T_bit ||
typ.Oid == types.T_decimal64
case float32:
return typ.Oid == types.T_float32
case float64:
Expand Down
10 changes: 8 additions & 2 deletions pkg/container/vector/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,24 @@ func (t *typedSlice) setFromVector(v *Vector) {
}

func ToSliceNoTypeCheck[T any](vec *Vector, ret *[]T) {
checkTypeIfRaceDetectorEnabled[T](vec)
*ret = unsafe.Slice((*T)(vec.col.Ptr), vec.col.Cap)
}

func ToSliceNoTypeCheck2[T any](vec *Vector) []T {
checkTypeIfRaceDetectorEnabled[T](vec)
return unsafe.Slice((*T)(vec.col.Ptr), vec.col.Cap)
}

func ToSlice[T any](vec *Vector, ret *[]T) {
checkType[T](vec)
*ret = unsafe.Slice((*T)(vec.col.Ptr), vec.col.Cap)
}

func checkType[T any](vec *Vector) {
if !typeCompatible[T](vec.typ) {
panic(fmt.Sprintf("type mismatch: %T %v", []T{}, vec.typ.String()))
panic(fmt.Sprintf("type mismatch: casting %v vector to %T", vec.typ.String(), []T{}))
}
*ret = unsafe.Slice((*T)(vec.col.Ptr), vec.col.Cap)
}

func (v *Vector) GetSorted() bool {
Expand Down

0 comments on commit 944c17c

Please sign in to comment.