-
Notifications
You must be signed in to change notification settings - Fork 265
/
compute.go
308 lines (294 loc) · 7.64 KB
/
compute.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2021 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.
package compute
import (
"bytes"
"github.com/matrixorigin/matrixone/pkg/container/nulls"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/container/vector"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
)
func ShuffleByDeletes(inputDeletes, deletes *nulls.Bitmap) (outDeletes *nulls.Bitmap) {
if deletes.IsEmpty() || inputDeletes.IsEmpty() {
return inputDeletes
}
delIt := inputDeletes.GetBitmap().Iterator()
outDeletes = nulls.NewWithSize(1)
deleteIt := deletes.GetBitmap().Iterator()
deleteCnt := uint64(0)
for deleteIt.HasNext() {
del := deleteIt.Next()
for delIt.HasNext() {
row := delIt.PeekNext()
if row < del {
outDeletes.Add(row - deleteCnt)
delIt.Next()
} else if row == del {
delIt.Next()
} else {
break
}
}
deleteCnt++
}
for delIt.HasNext() {
row := delIt.Next()
outDeletes.Add(row - deleteCnt)
}
return outDeletes
}
func GetOffsetMapBeforeApplyDeletes(deletes *nulls.Bitmap) []uint32 {
if deletes.IsEmpty() {
return nil
}
prev := -1
mapping := make([]uint32, 0)
it := deletes.GetBitmap().Iterator()
for it.HasNext() {
del := it.Next()
for i := uint32(prev + 1); i < uint32(del); i++ {
mapping = append(mapping, i)
}
prev = int(del)
}
mapping = append(mapping, uint32(prev)+1)
return mapping
}
func GetOffsetOfBytes(
data *vector.Vector,
val []byte,
skipmask *nulls.Bitmap,
) (offset int, exist bool) {
start, end := 0, data.Length()-1
var mid int
for start <= end {
mid = (start + end) / 2
res := bytes.Compare(data.GetBytesAt(mid), val)
if res > 0 {
end = mid - 1
} else if res < 0 {
start = mid + 1
} else {
if skipmask != nil && skipmask.Contains(uint64(mid)) {
return
}
offset = mid
exist = true
return
}
}
return
}
func GetOffsetWithFunc[T any](
vals []T,
val T,
compare func(a, b T) int,
skipmask *nulls.Bitmap,
) (offset int, exist bool) {
start, end := 0, len(vals)-1
var mid int
for start <= end {
mid = (start + end) / 2
res := compare(vals[mid], val)
if res > 0 {
end = mid - 1
} else if res < 0 {
start = mid + 1
} else {
if skipmask != nil && skipmask.Contains(uint64(mid)) {
return
}
offset = mid
exist = true
return
}
}
return
}
func GetOffsetOfOrdered[T types.OrderedT](column []T, val T, skipmask *nulls.Bitmap) (offset int, exist bool) {
start, end := 0, len(column)-1
var mid int
for start <= end {
mid = (start + end) / 2
if column[mid] > val {
end = mid - 1
} else if column[mid] < val {
start = mid + 1
} else {
if skipmask != nil && skipmask.Contains(uint64(mid)) {
return
}
offset = mid
exist = true
return
}
}
return
}
func GetOffsetByVal(data containers.Vector, v any, skipmask *nulls.Bitmap) (offset int, exist bool) {
vec := data.GetDownstreamVector()
switch data.GetType().Oid {
case types.T_bool:
vs := vector.MustFixedCol[bool](vec)
return GetOffsetWithFunc(vs, v.(bool), CompareBool, skipmask)
case types.T_int8:
vs := vector.MustFixedCol[int8](vec)
return GetOffsetOfOrdered(vs, v.(int8), skipmask)
case types.T_int16:
vs := vector.MustFixedCol[int16](vec)
return GetOffsetOfOrdered(vs, v.(int16), skipmask)
case types.T_int32:
vs := vector.MustFixedCol[int32](vec)
return GetOffsetOfOrdered(vs, v.(int32), skipmask)
case types.T_int64:
vs := vector.MustFixedCol[int64](vec)
return GetOffsetOfOrdered(vs, v.(int64), skipmask)
case types.T_uint8:
vs := vector.MustFixedCol[uint8](vec)
return GetOffsetOfOrdered(vs, v.(uint8), skipmask)
case types.T_uint16:
vs := vector.MustFixedCol[uint16](vec)
return GetOffsetOfOrdered(vs, v.(uint16), skipmask)
case types.T_uint32:
vs := vector.MustFixedCol[uint32](vec)
return GetOffsetOfOrdered(vs, v.(uint32), skipmask)
case types.T_uint64:
vs := vector.MustFixedCol[uint64](vec)
return GetOffsetOfOrdered(vs, v.(uint64), skipmask)
case types.T_float32:
vs := vector.MustFixedCol[float32](vec)
return GetOffsetOfOrdered(vs, v.(float32), skipmask)
case types.T_float64:
vs := vector.MustFixedCol[float64](vec)
return GetOffsetOfOrdered(vs, v.(float64), skipmask)
case types.T_date:
vs := vector.MustFixedCol[types.Date](vec)
return GetOffsetOfOrdered(vs, v.(types.Date), skipmask)
case types.T_time:
vs := vector.MustFixedCol[types.Time](vec)
return GetOffsetOfOrdered(vs, v.(types.Time), skipmask)
case types.T_datetime:
vs := vector.MustFixedCol[types.Datetime](vec)
return GetOffsetOfOrdered(vs, v.(types.Datetime), skipmask)
case types.T_timestamp:
vs := vector.MustFixedCol[types.Timestamp](vec)
return GetOffsetOfOrdered(vs, v.(types.Timestamp), skipmask)
case types.T_enum:
vs := vector.MustFixedCol[types.Enum](vec)
return GetOffsetOfOrdered(vs, v.(types.Enum), skipmask)
case types.T_decimal64:
vs := vector.MustFixedCol[types.Decimal64](vec)
return GetOffsetWithFunc(
vs,
v.(types.Decimal64),
types.CompareDecimal64,
skipmask)
case types.T_decimal128:
vs := vector.MustFixedCol[types.Decimal128](vec)
return GetOffsetWithFunc(
vs,
v.(types.Decimal128),
types.CompareDecimal128,
skipmask)
case types.T_TS:
return GetOffsetWithFunc(
vector.MustFixedCol[types.TS](vec),
v.(types.TS),
types.CompareTSTSAligned,
skipmask)
case types.T_Rowid:
return GetOffsetWithFunc(
vector.MustFixedCol[types.Rowid](vec),
v.(types.Rowid),
types.CompareRowidRowidAligned,
skipmask)
case types.T_Blockid:
return GetOffsetWithFunc(
vector.MustFixedCol[types.Blockid](vec),
v.(types.Blockid),
types.CompareBlockidBlockidAligned,
skipmask)
case types.T_uuid:
return GetOffsetWithFunc(
vector.MustFixedCol[types.Uuid](vec),
v.(types.Uuid),
types.CompareUuid,
skipmask)
case types.T_char, types.T_varchar, types.T_blob,
types.T_binary, types.T_varbinary, types.T_json, types.T_text,
types.T_array_float32, types.T_array_float64:
// data is retrieved from DN vector, hence T_array can be handled here.
val := v.([]byte)
start, end := 0, data.Length()-1
var mid int
for start <= end {
mid = (start + end) / 2
res := bytes.Compare(data.ShallowGet(mid).([]byte), val)
if res > 0 {
end = mid - 1
} else if res < 0 {
start = mid + 1
} else {
if skipmask != nil && skipmask.Contains(uint64(mid)) {
return
}
offset = mid
exist = true
return
}
}
return
default:
panic("unsupported type")
}
}
func GetOrderedMinAndMax[T types.OrderedT](vs ...T) (minv, maxv T) {
minv = vs[0]
maxv = vs[0]
for _, v := range vs[1:] {
if v < minv {
minv = v
}
if v > maxv {
maxv = v
}
}
return
}
func GetDecimal64MinAndMax(vs []types.Decimal64) (minv, maxv types.Decimal64) {
minv = vs[0]
maxv = vs[0]
for _, v := range vs[1:] {
if types.CompareDecimal64(v, minv) < 0 {
minv = v
}
if types.CompareDecimal64(v, maxv) > 0 {
maxv = v
}
}
return
}
func GetDecimal128MinAndMax(vs []types.Decimal128) (minv, maxv types.Decimal128) {
minv = vs[0]
maxv = vs[0]
for _, v := range vs[1:] {
if types.CompareDecimal128(v, minv) < 0 {
minv = v
}
if types.CompareDecimal128(v, maxv) > 0 {
maxv = v
}
}
return
}