Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug: SortAndCompact get incorrect value (#19056) #19240

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading