Skip to content

Commit 6f6fdf5

Browse files
ouyuanningqin shuqi
authored and
qin shuqi
committed
fix bug: SortAndCompact get incorrect value (#19056) (#19240)
fix bug: SortAndCompact get incorrect value Approved by: @m-schen, @heni02, @XuPeng-SH, @aunjgr, @sukki37
1 parent 42a7c45 commit 6f6fdf5

File tree

5 files changed

+625
-12
lines changed

5 files changed

+625
-12
lines changed

pkg/container/vector/vector.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -3691,6 +3691,14 @@ func (v *Vector) GetMinMaxValue() (ok bool, minv, maxv []byte) {
36913691

36923692
// InplaceSortAndCompact @todo optimization in the future
36933693
func (v *Vector) InplaceSortAndCompact() {
3694+
cleanDataNotResetArea := func() {
3695+
if v.data != nil {
3696+
v.length = 0
3697+
}
3698+
v.nsp.Reset()
3699+
v.sorted = true
3700+
}
3701+
36943702
switch v.GetType().Oid {
36953703
case types.T_bool:
36963704
col := MustFixedCol[bool](v)
@@ -3973,49 +3981,47 @@ func (v *Vector) InplaceSortAndCompact() {
39733981
newCol := slices.CompactFunc(col, func(a, b types.Varlena) bool {
39743982
return bytes.Equal(a.GetByteSlice(area), b.GetByteSlice(area))
39753983
})
3984+
39763985
if len(newCol) != len(col) {
3977-
v.CleanOnlyData()
3978-
v.SetSorted(true)
3986+
cleanDataNotResetArea()
39793987
appendList(v, newCol, nil, nil)
39803988
}
39813989

39823990
case types.T_array_float32:
39833991
col, area := MustVarlenaRawData(v)
39843992
sort.Slice(col, func(i, j int) bool {
3985-
return moarray.Compare[float32](
3993+
return moarray.Compare(
39863994
types.GetArray[float32](&col[i], area),
39873995
types.GetArray[float32](&col[j], area),
39883996
) < 0
39893997
})
39903998
newCol := slices.CompactFunc(col, func(a, b types.Varlena) bool {
3991-
return moarray.Compare[float32](
3999+
return moarray.Compare(
39924000
types.GetArray[float32](&a, area),
39934001
types.GetArray[float32](&b, area),
39944002
) == 0
39954003
})
39964004
if len(newCol) != len(col) {
3997-
v.CleanOnlyData()
3998-
v.SetSorted(true)
4005+
cleanDataNotResetArea()
39994006
appendList(v, newCol, nil, nil)
40004007
}
40014008

40024009
case types.T_array_float64:
40034010
col, area := MustVarlenaRawData(v)
40044011
sort.Slice(col, func(i, j int) bool {
4005-
return moarray.Compare[float64](
4012+
return moarray.Compare(
40064013
types.GetArray[float64](&col[i], area),
40074014
types.GetArray[float64](&col[j], area),
40084015
) < 0
40094016
})
40104017
newCol := slices.CompactFunc(col, func(a, b types.Varlena) bool {
4011-
return moarray.Compare[float64](
4018+
return moarray.Compare(
40124019
types.GetArray[float64](&a, area),
40134020
types.GetArray[float64](&b, area),
40144021
) == 0
40154022
})
40164023
if len(newCol) != len(col) {
4017-
v.CleanOnlyData()
4018-
v.SetSorted(true)
4024+
cleanDataNotResetArea()
40194025
appendList(v, newCol, nil, nil)
40204026
}
40214027
}

pkg/container/vector/vector_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,21 @@ func TestSetFunction(t *testing.T) {
17411741
}
17421742
}
17431743

1744+
func TestSortAndCompact(t *testing.T) {
1745+
mp := mpool.MustNewZero()
1746+
v := NewVec(types.New(types.T_array_float32, 4, 0))
1747+
err := AppendArrayList(v, [][]float32{{1, 2, 3, 0}, {1, 2, 3, 0}}, nil, mp)
1748+
require.NoError(t, err)
1749+
v.InplaceSortAndCompact()
1750+
require.Equal(t, v.length, 1)
1751+
1752+
v = NewVec(types.New(types.T_array_float64, 4, 0))
1753+
err = AppendArrayList(v, [][]float64{{1.1, 2, 3, 0}, {1.1, 2, 3, 0}}, nil, mp)
1754+
require.NoError(t, err)
1755+
v.InplaceSortAndCompact()
1756+
require.Equal(t, v.length, 1)
1757+
}
1758+
17441759
func TestSetFunction2(t *testing.T) {
17451760
// set vec to const value -> const null -> const value -> const null.
17461761
// bool type

0 commit comments

Comments
 (0)