github.com/matrixorigin/matrixone@v1.2.0/pkg/sort/sort_test.go (about) 1 // Copyright 2021 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 package sort 16 17 import ( 18 "sort" 19 "testing" 20 21 "github.com/matrixorigin/matrixone/pkg/common/mpool" 22 "github.com/matrixorigin/matrixone/pkg/container/types" 23 "github.com/matrixorigin/matrixone/pkg/container/vector" 24 "github.com/matrixorigin/matrixone/pkg/testutil" 25 "github.com/matrixorigin/matrixone/pkg/vm/process" 26 "github.com/stretchr/testify/require" 27 ) 28 29 const ( 30 Rows = 1000 31 BenchmarkRows = 100000 32 ) 33 34 type testCase struct { 35 desc bool 36 vec *vector.Vector 37 proc *process.Process 38 } 39 40 var ( 41 tcs []testCase 42 ) 43 44 func init() { 45 mp := mpool.MustNewZero() 46 tcs = []testCase{ 47 newTestCase(true, mp, types.New(types.T_bool, 0, 0)), 48 newTestCase(false, mp, types.New(types.T_bool, 0, 0)), 49 50 newTestCase(true, mp, types.New(types.T_bit, 64, 0)), 51 newTestCase(false, mp, types.New(types.T_bit, 64, 0)), 52 53 newTestCase(true, mp, types.New(types.T_int8, 0, 0)), 54 newTestCase(false, mp, types.New(types.T_int8, 0, 0)), 55 newTestCase(true, mp, types.New(types.T_int16, 0, 0)), 56 newTestCase(false, mp, types.New(types.T_int16, 0, 0)), 57 newTestCase(true, mp, types.New(types.T_int32, 0, 0)), 58 newTestCase(false, mp, types.New(types.T_int32, 0, 0)), 59 newTestCase(true, mp, types.New(types.T_int64, 0, 0)), 60 newTestCase(false, mp, types.New(types.T_int64, 0, 0)), 61 62 newTestCase(true, mp, types.New(types.T_uint8, 0, 0)), 63 newTestCase(false, mp, types.New(types.T_uint8, 0, 0)), 64 newTestCase(true, mp, types.New(types.T_uint16, 0, 0)), 65 newTestCase(false, mp, types.New(types.T_uint16, 0, 0)), 66 newTestCase(true, mp, types.New(types.T_uint32, 0, 0)), 67 newTestCase(false, mp, types.New(types.T_uint32, 0, 0)), 68 newTestCase(true, mp, types.New(types.T_uint64, 0, 0)), 69 newTestCase(false, mp, types.New(types.T_uint64, 0, 0)), 70 71 newTestCase(true, mp, types.New(types.T_float32, 0, 0)), 72 newTestCase(false, mp, types.New(types.T_float32, 0, 0)), 73 74 newTestCase(true, mp, types.New(types.T_float64, 0, 0)), 75 newTestCase(false, mp, types.New(types.T_float64, 0, 0)), 76 77 newTestCase(true, mp, types.New(types.T_date, 0, 0)), 78 newTestCase(false, mp, types.New(types.T_date, 0, 0)), 79 80 newTestCase(true, mp, types.New(types.T_datetime, 0, 0)), 81 newTestCase(false, mp, types.New(types.T_datetime, 0, 0)), 82 83 newTestCase(true, mp, types.New(types.T_timestamp, 0, 0)), 84 newTestCase(false, mp, types.New(types.T_timestamp, 0, 0)), 85 86 newTestCase(true, mp, types.New(types.T_decimal64, 0, 0)), 87 newTestCase(false, mp, types.New(types.T_decimal64, 0, 0)), 88 89 newTestCase(true, mp, types.New(types.T_decimal128, 0, 0)), 90 newTestCase(false, mp, types.New(types.T_decimal128, 0, 0)), 91 92 newTestCase(true, mp, types.New(types.T_varchar, types.MaxVarcharLen, 0)), 93 newTestCase(false, mp, types.New(types.T_varchar, types.MaxVarcharLen, 0)), 94 95 newTestCase(true, mp, types.New(types.T_array_float32, types.MaxArrayDimension, 0)), 96 newTestCase(false, mp, types.New(types.T_array_float32, types.MaxArrayDimension, 0)), 97 98 newTestCase(true, mp, types.New(types.T_array_float64, types.MaxArrayDimension, 0)), 99 newTestCase(false, mp, types.New(types.T_array_float64, types.MaxArrayDimension, 0)), 100 } 101 } 102 103 func TestSort(t *testing.T) { 104 for _, tc := range tcs { 105 os := make([]int64, tc.vec.Length()) 106 for i := range os { 107 os[i] = int64(i) 108 } 109 nb0 := tc.proc.Mp().CurrNB() 110 Sort(tc.desc, false, false, os, tc.vec, nil) 111 checkResult(t, tc.desc, tc.vec, os) 112 nb1 := tc.proc.Mp().CurrNB() 113 require.Equal(t, nb0, nb1) 114 tc.vec.Free(tc.proc.Mp()) 115 } 116 } 117 118 func BenchmarkSortInt(b *testing.B) { 119 vs := make([]int, BenchmarkRows) 120 for i := range vs { 121 vs[i] = i 122 } 123 for i := 0; i < b.N; i++ { 124 sort.Ints(vs) 125 } 126 } 127 128 func BenchmarkSortIntVector(b *testing.B) { 129 m := mpool.MustNewZero() 130 vec := testutil.NewInt32Vector(BenchmarkRows, types.T_int32.ToType(), m, true, nil) 131 os := make([]int64, vec.Length()) 132 for i := range os { 133 os[i] = int64(i) 134 } 135 for i := 0; i < b.N; i++ { 136 Sort(false, false, false, os, vec, nil) 137 } 138 } 139 140 func checkResult(t *testing.T, desc bool, vec *vector.Vector, os []int64) { 141 switch vec.GetType().Oid { 142 case types.T_bit: 143 vs := make([]int, len(os)) 144 col := vector.MustFixedCol[uint64](vec) 145 for i := range vs { 146 vs[i] = int(col[i]) 147 } 148 sort.Ints(vs) 149 if desc { 150 j := len(vs) - 1 151 for _, v := range vs { 152 require.Equal(t, v, int(col[os[j]])) 153 j-- 154 } 155 } else { 156 for i, v := range vs { 157 require.Equal(t, v, int(col[os[i]])) 158 } 159 } 160 case types.T_int32: 161 vs := make([]int, len(os)) 162 col := vector.MustFixedCol[int32](vec) 163 for i := range vs { 164 vs[i] = int(col[i]) 165 } 166 sort.Ints(vs) 167 if desc { 168 j := len(vs) - 1 169 for _, v := range vs { 170 require.Equal(t, v, int(col[os[j]])) 171 j-- 172 } 173 } else { 174 for i, v := range vs { 175 require.Equal(t, v, int(col[os[i]])) 176 } 177 } 178 case types.T_int64: 179 vs := make([]int, len(os)) 180 col := vector.MustFixedCol[int64](vec) 181 for i := range vs { 182 vs[i] = int(col[i]) 183 } 184 sort.Ints(vs) 185 if desc { 186 j := len(vs) - 1 187 for _, v := range vs { 188 require.Equal(t, v, int(col[os[j]])) 189 j-- 190 } 191 } else { 192 for i, v := range vs { 193 require.Equal(t, v, int(col[os[i]])) 194 } 195 } 196 case types.T_float32: 197 vs := make([]float64, len(os)) 198 col := vector.MustFixedCol[float32](vec) 199 for i := range vs { 200 vs[i] = float64(col[i]) 201 } 202 sort.Float64s(vs) 203 if desc { 204 j := len(vs) - 1 205 for _, v := range vs { 206 require.Equal(t, v, float64(col[os[j]])) 207 j-- 208 } 209 } else { 210 for i, v := range vs { 211 require.Equal(t, v, float64(col[os[i]])) 212 } 213 } 214 case types.T_float64: 215 vs := make([]float64, len(os)) 216 col := vector.MustFixedCol[float64](vec) 217 for i := range vs { 218 vs[i] = float64(col[i]) 219 } 220 sort.Float64s(vs) 221 if desc { 222 j := len(vs) - 1 223 for _, v := range vs { 224 require.Equal(t, v, float64(col[os[j]])) 225 j-- 226 } 227 } else { 228 for i, v := range vs { 229 require.Equal(t, v, float64(col[os[i]])) 230 } 231 } 232 } 233 } 234 235 func newTestCase(desc bool, m *mpool.MPool, typ types.Type) testCase { 236 return testCase{ 237 desc: desc, 238 proc: testutil.NewProcessWithMPool(m), 239 vec: testutil.NewVector(Rows, typ, m, true, nil), 240 } 241 }