Skip to content

Commit

Permalink
fix bug: SortAndCompact get incorrect value (#19056) (#19240)
Browse files Browse the repository at this point in the history
fix bug: SortAndCompact get incorrect value

Approved by: @m-schen, @heni02, @XuPeng-SH, @aunjgr, @sukki37
  • Loading branch information
ouyuanning authored and qin shuqi committed Oct 11, 2024
1 parent 42a7c45 commit 6f6fdf5
Show file tree
Hide file tree
Showing 5 changed files with 625 additions and 12 deletions.
26 changes: 16 additions & 10 deletions pkg/container/vector/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3691,6 +3691,14 @@ func (v *Vector) GetMinMaxValue() (ok bool, minv, maxv []byte) {

// InplaceSortAndCompact @todo optimization in the future
func (v *Vector) InplaceSortAndCompact() {
cleanDataNotResetArea := func() {
if v.data != nil {
v.length = 0
}
v.nsp.Reset()
v.sorted = true
}

switch v.GetType().Oid {
case types.T_bool:
col := MustFixedCol[bool](v)
Expand Down Expand Up @@ -3973,49 +3981,47 @@ func (v *Vector) InplaceSortAndCompact() {
newCol := slices.CompactFunc(col, func(a, b types.Varlena) bool {
return bytes.Equal(a.GetByteSlice(area), b.GetByteSlice(area))
})

if len(newCol) != len(col) {
v.CleanOnlyData()
v.SetSorted(true)
cleanDataNotResetArea()
appendList(v, newCol, nil, nil)
}

case types.T_array_float32:
col, area := MustVarlenaRawData(v)
sort.Slice(col, func(i, j int) bool {
return moarray.Compare[float32](
return moarray.Compare(
types.GetArray[float32](&col[i], area),
types.GetArray[float32](&col[j], area),
) < 0
})
newCol := slices.CompactFunc(col, func(a, b types.Varlena) bool {
return moarray.Compare[float32](
return moarray.Compare(
types.GetArray[float32](&a, area),
types.GetArray[float32](&b, area),
) == 0
})
if len(newCol) != len(col) {
v.CleanOnlyData()
v.SetSorted(true)
cleanDataNotResetArea()
appendList(v, newCol, nil, nil)
}

case types.T_array_float64:
col, area := MustVarlenaRawData(v)
sort.Slice(col, func(i, j int) bool {
return moarray.Compare[float64](
return moarray.Compare(
types.GetArray[float64](&col[i], area),
types.GetArray[float64](&col[j], area),
) < 0
})
newCol := slices.CompactFunc(col, func(a, b types.Varlena) bool {
return moarray.Compare[float64](
return moarray.Compare(
types.GetArray[float64](&a, area),
types.GetArray[float64](&b, area),
) == 0
})
if len(newCol) != len(col) {
v.CleanOnlyData()
v.SetSorted(true)
cleanDataNotResetArea()
appendList(v, newCol, nil, nil)
}
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/container/vector/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,21 @@ func TestSetFunction(t *testing.T) {
}
}

func TestSortAndCompact(t *testing.T) {
mp := mpool.MustNewZero()
v := NewVec(types.New(types.T_array_float32, 4, 0))
err := AppendArrayList(v, [][]float32{{1, 2, 3, 0}, {1, 2, 3, 0}}, nil, mp)
require.NoError(t, err)
v.InplaceSortAndCompact()
require.Equal(t, v.length, 1)

v = NewVec(types.New(types.T_array_float64, 4, 0))
err = AppendArrayList(v, [][]float64{{1.1, 2, 3, 0}, {1.1, 2, 3, 0}}, nil, mp)
require.NoError(t, err)
v.InplaceSortAndCompact()
require.Equal(t, v.length, 1)
}

func TestSetFunction2(t *testing.T) {
// set vec to const value -> const null -> const value -> const null.
// bool type
Expand Down
Loading

0 comments on commit 6f6fdf5

Please sign in to comment.