-
Notifications
You must be signed in to change notification settings - Fork 28k
/
OffHeapColumnVector.java
638 lines (555 loc) · 18.9 KB
/
OffHeapColumnVector.java
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.spark.sql.execution.vectorized;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import com.google.common.annotations.VisibleForTesting;
import org.apache.spark.sql.types.*;
import org.apache.spark.unsafe.Platform;
import org.apache.spark.unsafe.types.UTF8String;
/**
* Column data backed using offheap memory.
*/
public final class OffHeapColumnVector extends WritableColumnVector {
private static final boolean bigEndianPlatform =
ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN);
/**
* Allocates columns to store elements of each field of the schema off heap.
* Capacity is the initial capacity of the vector and it will grow as necessary. Capacity is
* in number of elements, not number of bytes.
*/
public static OffHeapColumnVector[] allocateColumns(int capacity, StructType schema) {
return allocateColumns(capacity, schema.fields());
}
/**
* Allocates columns to store elements of each field off heap.
* Capacity is the initial capacity of the vector and it will grow as necessary. Capacity is
* in number of elements, not number of bytes.
*/
public static OffHeapColumnVector[] allocateColumns(int capacity, StructField[] fields) {
OffHeapColumnVector[] vectors = new OffHeapColumnVector[fields.length];
for (int i = 0; i < fields.length; i++) {
vectors[i] = new OffHeapColumnVector(capacity, fields[i].dataType());
}
return vectors;
}
// The data stored in these two allocations need to maintain binary compatible. We can
// directly pass this buffer to external components.
private long nulls;
private long data;
// Only set if type is Array or Map.
private long lengthData;
private long offsetData;
public OffHeapColumnVector(int capacity, DataType type) {
super(capacity, type);
nulls = 0;
data = 0;
lengthData = 0;
offsetData = 0;
reserveInternal(capacity);
reset();
}
/**
* Returns the off heap pointer for the values buffer.
*/
@VisibleForTesting
public long valuesNativeAddress() {
return data;
}
protected void releaseMemory() {
Platform.freeMemory(nulls);
Platform.freeMemory(data);
Platform.freeMemory(lengthData);
Platform.freeMemory(offsetData);
nulls = 0;
data = 0;
lengthData = 0;
offsetData = 0;
}
@Override
public void close() {
super.close();
}
//
// APIs dealing with nulls
//
@Override
public void putNotNull(int rowId) {
Platform.putByte(null, nulls + rowId, (byte) 0);
}
@Override
public void putNull(int rowId) {
Platform.putByte(null, nulls + rowId, (byte) 1);
++numNulls;
}
@Override
public void putNulls(int rowId, int count) {
long offset = nulls + rowId;
for (int i = 0; i < count; ++i, ++offset) {
Platform.putByte(null, offset, (byte) 1);
}
numNulls += count;
}
@Override
public void putNotNulls(int rowId, int count) {
if (!hasNull()) return;
long offset = nulls + rowId;
for (int i = 0; i < count; ++i, ++offset) {
Platform.putByte(null, offset, (byte) 0);
}
}
@Override
public boolean isNullAt(int rowId) {
return isAllNull || Platform.getByte(null, nulls + rowId) == 1;
}
//
// APIs dealing with Booleans
//
@Override
public void putBoolean(int rowId, boolean value) {
Platform.putByte(null, data + rowId, (byte)((value) ? 1 : 0));
}
@Override
public void putBooleans(int rowId, int count, boolean value) {
byte v = (byte)((value) ? 1 : 0);
for (int i = 0; i < count; ++i) {
Platform.putByte(null, data + rowId + i, v);
}
}
@Override
public void putBooleans(int rowId, byte src) {
Platform.putByte(null, data + rowId, (byte)(src & 1));
Platform.putByte(null, data + rowId + 1, (byte)(src >>> 1 & 1));
Platform.putByte(null, data + rowId + 2, (byte)(src >>> 2 & 1));
Platform.putByte(null, data + rowId + 3, (byte)(src >>> 3 & 1));
Platform.putByte(null, data + rowId + 4, (byte)(src >>> 4 & 1));
Platform.putByte(null, data + rowId + 5, (byte)(src >>> 5 & 1));
Platform.putByte(null, data + rowId + 6, (byte)(src >>> 6 & 1));
Platform.putByte(null, data + rowId + 7, (byte)(src >>> 7 & 1));
}
@Override
public boolean getBoolean(int rowId) { return Platform.getByte(null, data + rowId) == 1; }
@Override
public boolean[] getBooleans(int rowId, int count) {
assert(dictionary == null);
boolean[] array = new boolean[count];
for (int i = 0; i < count; ++i) {
array[i] = (Platform.getByte(null, data + rowId + i) == 1);
}
return array;
}
//
// APIs dealing with Bytes
//
@Override
public void putByte(int rowId, byte value) {
Platform.putByte(null, data + rowId, value);
}
@Override
public void putBytes(int rowId, int count, byte value) {
for (int i = 0; i < count; ++i) {
Platform.putByte(null, data + rowId + i, value);
}
}
@Override
public void putBytes(int rowId, int count, byte[] src, int srcIndex) {
Platform.copyMemory(src, Platform.BYTE_ARRAY_OFFSET + srcIndex, null, data + rowId, count);
}
@Override
public byte getByte(int rowId) {
if (dictionary == null) {
return Platform.getByte(null, data + rowId);
} else {
return (byte) dictionary.decodeToInt(dictionaryIds.getDictId(rowId));
}
}
@Override
public byte[] getBytes(int rowId, int count) {
byte[] array = new byte[count];
if (dictionary == null) {
Platform.copyMemory(null, data + rowId, array, Platform.BYTE_ARRAY_OFFSET, count);
} else {
for (int i = 0; i < count; i++) {
if (!isNullAt(rowId + i)) {
array[i] = (byte) dictionary.decodeToInt(dictionaryIds.getDictId(rowId + i));
}
}
}
return array;
}
@Override
protected UTF8String getBytesAsUTF8String(int rowId, int count) {
return UTF8String.fromAddress(null, data + rowId, count);
}
@Override
public ByteBuffer getByteBuffer(int rowId, int count) {
return ByteBuffer.wrap(getBytes(rowId, count));
}
//
// APIs dealing with shorts
//
@Override
public void putShort(int rowId, short value) {
Platform.putShort(null, data + 2L * rowId, value);
}
@Override
public void putShorts(int rowId, int count, short value) {
long offset = data + 2L * rowId;
for (int i = 0; i < count; ++i, offset += 2) {
Platform.putShort(null, offset, value);
}
}
@Override
public void putShorts(int rowId, int count, short[] src, int srcIndex) {
Platform.copyMemory(src, Platform.SHORT_ARRAY_OFFSET + srcIndex * 2L,
null, data + 2L * rowId, count * 2L);
}
@Override
public void putShorts(int rowId, int count, byte[] src, int srcIndex) {
Platform.copyMemory(src, Platform.BYTE_ARRAY_OFFSET + srcIndex,
null, data + rowId * 2L, count * 2L);
}
@Override
public short getShort(int rowId) {
if (dictionary == null) {
return Platform.getShort(null, data + 2L * rowId);
} else {
return (short) dictionary.decodeToInt(dictionaryIds.getDictId(rowId));
}
}
@Override
public short[] getShorts(int rowId, int count) {
short[] array = new short[count];
if (dictionary == null) {
Platform.copyMemory(null, data + rowId * 2L, array, Platform.SHORT_ARRAY_OFFSET, count * 2L);
} else {
for (int i = 0; i < count; i++) {
if (!isNullAt(rowId + i)) {
array[i] = (short) dictionary.decodeToInt(dictionaryIds.getDictId(rowId + i));
}
}
}
return array;
}
//
// APIs dealing with ints
//
@Override
public void putInt(int rowId, int value) {
Platform.putInt(null, data + 4L * rowId, value);
}
@Override
public void putInts(int rowId, int count, int value) {
long offset = data + 4L * rowId;
for (int i = 0; i < count; ++i, offset += 4) {
Platform.putInt(null, offset, value);
}
}
@Override
public void putInts(int rowId, int count, int[] src, int srcIndex) {
Platform.copyMemory(src, Platform.INT_ARRAY_OFFSET + srcIndex * 4L,
null, data + 4L * rowId, count * 4L);
}
@Override
public void putInts(int rowId, int count, byte[] src, int srcIndex) {
Platform.copyMemory(src, Platform.BYTE_ARRAY_OFFSET + srcIndex,
null, data + rowId * 4L, count * 4L);
}
@Override
public void putIntsLittleEndian(int rowId, int count, byte[] src, int srcIndex) {
if (!bigEndianPlatform) {
Platform.copyMemory(src, srcIndex + Platform.BYTE_ARRAY_OFFSET,
null, data + 4L * rowId, count * 4L);
} else {
int srcOffset = srcIndex + Platform.BYTE_ARRAY_OFFSET;
long offset = data + 4L * rowId;
for (int i = 0; i < count; ++i, offset += 4, srcOffset += 4) {
Platform.putInt(null, offset,
java.lang.Integer.reverseBytes(Platform.getInt(src, srcOffset)));
}
}
}
@Override
public int getInt(int rowId) {
if (dictionary == null) {
return Platform.getInt(null, data + 4L * rowId);
} else {
return dictionary.decodeToInt(dictionaryIds.getDictId(rowId));
}
}
@Override
public int[] getInts(int rowId, int count) {
int[] array = new int[count];
if (dictionary == null) {
Platform.copyMemory(null, data + rowId * 4L, array, Platform.INT_ARRAY_OFFSET, count * 4L);
} else {
for (int i = 0; i < count; i++) {
if (!isNullAt(rowId + i)) {
array[i] = dictionary.decodeToInt(dictionaryIds.getDictId(rowId + i));
}
}
}
return array;
}
/**
* Returns the dictionary Id for rowId.
* This should only be called when the ColumnVector is dictionaryIds.
* We have this separate method for dictionaryIds as per SPARK-16928.
*/
@Override
public int getDictId(int rowId) {
assert(dictionary == null)
: "A ColumnVector dictionary should not have a dictionary for itself.";
return Platform.getInt(null, data + 4L * rowId);
}
//
// APIs dealing with Longs
//
@Override
public void putLong(int rowId, long value) {
Platform.putLong(null, data + 8L * rowId, value);
}
@Override
public void putLongs(int rowId, int count, long value) {
long offset = data + 8L * rowId;
for (int i = 0; i < count; ++i, offset += 8) {
Platform.putLong(null, offset, value);
}
}
@Override
public void putLongs(int rowId, int count, long[] src, int srcIndex) {
Platform.copyMemory(src, Platform.LONG_ARRAY_OFFSET + srcIndex * 8L,
null, data + 8L * rowId, count * 8L);
}
@Override
public void putLongs(int rowId, int count, byte[] src, int srcIndex) {
Platform.copyMemory(src, Platform.BYTE_ARRAY_OFFSET + srcIndex,
null, data + rowId * 8L, count * 8L);
}
@Override
public void putLongsLittleEndian(int rowId, int count, byte[] src, int srcIndex) {
if (!bigEndianPlatform) {
Platform.copyMemory(src, srcIndex + Platform.BYTE_ARRAY_OFFSET,
null, data + 8L * rowId, count * 8L);
} else {
int srcOffset = srcIndex + Platform.BYTE_ARRAY_OFFSET;
long offset = data + 8L * rowId;
for (int i = 0; i < count; ++i, offset += 8, srcOffset += 8) {
Platform.putLong(null, offset,
java.lang.Long.reverseBytes(Platform.getLong(src, srcOffset)));
}
}
}
@Override
public long getLong(int rowId) {
if (dictionary == null) {
return Platform.getLong(null, data + 8L * rowId);
} else {
return dictionary.decodeToLong(dictionaryIds.getDictId(rowId));
}
}
@Override
public long[] getLongs(int rowId, int count) {
long[] array = new long[count];
if (dictionary == null) {
Platform.copyMemory(null, data + rowId * 8L, array, Platform.LONG_ARRAY_OFFSET, count * 8L);
} else {
for (int i = 0; i < count; i++) {
if (!isNullAt(rowId + i)) {
array[i] = dictionary.decodeToLong(dictionaryIds.getDictId(rowId + i));
}
}
}
return array;
}
//
// APIs dealing with floats
//
@Override
public void putFloat(int rowId, float value) {
Platform.putFloat(null, data + rowId * 4L, value);
}
@Override
public void putFloats(int rowId, int count, float value) {
long offset = data + 4L * rowId;
for (int i = 0; i < count; ++i, offset += 4) {
Platform.putFloat(null, offset, value);
}
}
@Override
public void putFloats(int rowId, int count, float[] src, int srcIndex) {
Platform.copyMemory(src, Platform.FLOAT_ARRAY_OFFSET + srcIndex * 4L,
null, data + 4L * rowId, count * 4L);
}
@Override
public void putFloats(int rowId, int count, byte[] src, int srcIndex) {
Platform.copyMemory(src, Platform.BYTE_ARRAY_OFFSET + srcIndex,
null, data + rowId * 4L, count * 4L);
}
@Override
public void putFloatsLittleEndian(int rowId, int count, byte[] src, int srcIndex) {
if (!bigEndianPlatform) {
putFloats(rowId, count, src, srcIndex);
} else {
ByteBuffer bb = ByteBuffer.wrap(src).order(ByteOrder.LITTLE_ENDIAN);
long offset = data + 4L * rowId;
for (int i = 0; i < count; ++i, offset += 4) {
Platform.putFloat(null, offset, bb.getFloat(srcIndex + (4 * i)));
}
}
}
@Override
public float getFloat(int rowId) {
if (dictionary == null) {
return Platform.getFloat(null, data + rowId * 4L);
} else {
return dictionary.decodeToFloat(dictionaryIds.getDictId(rowId));
}
}
@Override
public float[] getFloats(int rowId, int count) {
float[] array = new float[count];
if (dictionary == null) {
Platform.copyMemory(null, data + rowId * 4L, array, Platform.FLOAT_ARRAY_OFFSET, count * 4L);
} else {
for (int i = 0; i < count; i++) {
if (!isNullAt(rowId + i)) {
array[i] = dictionary.decodeToFloat(dictionaryIds.getDictId(rowId + i));
}
}
}
return array;
}
//
// APIs dealing with doubles
//
@Override
public void putDouble(int rowId, double value) {
Platform.putDouble(null, data + rowId * 8L, value);
}
@Override
public void putDoubles(int rowId, int count, double value) {
long offset = data + 8L * rowId;
for (int i = 0; i < count; ++i, offset += 8) {
Platform.putDouble(null, offset, value);
}
}
@Override
public void putDoubles(int rowId, int count, double[] src, int srcIndex) {
Platform.copyMemory(src, Platform.DOUBLE_ARRAY_OFFSET + srcIndex * 8L,
null, data + 8L * rowId, count * 8L);
}
@Override
public void putDoubles(int rowId, int count, byte[] src, int srcIndex) {
Platform.copyMemory(src, Platform.BYTE_ARRAY_OFFSET + srcIndex,
null, data + rowId * 8L, count * 8L);
}
@Override
public void putDoublesLittleEndian(int rowId, int count, byte[] src, int srcIndex) {
if (!bigEndianPlatform) {
putDoubles(rowId, count, src, srcIndex);
} else {
ByteBuffer bb = ByteBuffer.wrap(src).order(ByteOrder.LITTLE_ENDIAN);
long offset = data + 8L * rowId;
for (int i = 0; i < count; ++i, offset += 8) {
Platform.putDouble(null, offset, bb.getDouble(srcIndex + (8 * i)));
}
}
}
@Override
public double getDouble(int rowId) {
if (dictionary == null) {
return Platform.getDouble(null, data + rowId * 8L);
} else {
return dictionary.decodeToDouble(dictionaryIds.getDictId(rowId));
}
}
@Override
public double[] getDoubles(int rowId, int count) {
double[] array = new double[count];
if (dictionary == null) {
Platform.copyMemory(null, data + rowId * 8L, array, Platform.DOUBLE_ARRAY_OFFSET,
count * 8L);
} else {
for (int i = 0; i < count; i++) {
if (!isNullAt(rowId + i)) {
array[i] = dictionary.decodeToDouble(dictionaryIds.getDictId(rowId + i));
}
}
}
return array;
}
//
// APIs dealing with Arrays.
//
@Override
public void putArray(int rowId, int offset, int length) {
assert(offset >= 0 && offset + length <= childColumns[0].capacity);
Platform.putInt(null, lengthData + 4L * rowId, length);
Platform.putInt(null, offsetData + 4L * rowId, offset);
}
@Override
public int getArrayLength(int rowId) {
return Platform.getInt(null, lengthData + 4L * rowId);
}
@Override
public int getArrayOffset(int rowId) {
return Platform.getInt(null, offsetData + 4L * rowId);
}
// APIs dealing with ByteArrays
@Override
public int putByteArray(int rowId, byte[] value, int offset, int length) {
int result = arrayData().appendBytes(length, value, offset);
Platform.putInt(null, lengthData + 4L * rowId, length);
Platform.putInt(null, offsetData + 4L * rowId, result);
return result;
}
// Split out the slow path.
@Override
protected void reserveInternal(int newCapacity) {
int oldCapacity = (nulls == 0L) ? 0 : capacity;
if (isArray() || type instanceof MapType) {
this.lengthData =
Platform.reallocateMemory(lengthData, oldCapacity * 4L, newCapacity * 4L);
this.offsetData =
Platform.reallocateMemory(offsetData, oldCapacity * 4L, newCapacity * 4L);
} else if (type instanceof ByteType || type instanceof BooleanType) {
this.data = Platform.reallocateMemory(data, oldCapacity, newCapacity);
} else if (type instanceof ShortType) {
this.data = Platform.reallocateMemory(data, oldCapacity * 2L, newCapacity * 2L);
} else if (type instanceof IntegerType || type instanceof FloatType ||
type instanceof DateType || DecimalType.is32BitDecimalType(type) ||
type instanceof YearMonthIntervalType) {
this.data = Platform.reallocateMemory(data, oldCapacity * 4L, newCapacity * 4L);
} else if (type instanceof LongType || type instanceof DoubleType ||
DecimalType.is64BitDecimalType(type) || type instanceof TimestampType ||
type instanceof TimestampNTZType || type instanceof DayTimeIntervalType) {
this.data = Platform.reallocateMemory(data, oldCapacity * 8L, newCapacity * 8L);
} else if (childColumns != null) {
// Nothing to store.
} else {
throw new RuntimeException("Unhandled " + type);
}
this.nulls = Platform.reallocateMemory(nulls, oldCapacity, newCapacity);
Platform.setMemory(nulls + oldCapacity, (byte)0, newCapacity - oldCapacity);
capacity = newCapacity;
}
@Override
protected OffHeapColumnVector reserveNewColumn(int capacity, DataType type) {
return new OffHeapColumnVector(capacity, type);
}
}