-
Notifications
You must be signed in to change notification settings - Fork 2
/
column_store.h
507 lines (428 loc) · 19.9 KB
/
column_store.h
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
#ifndef COLUMN_STORE_H_
#define COLUMN_STORE_H_
#include <cstdint>
#include <memory>
#include <algorithm>
#include <cassert>
#include <iostream>
#include <cmath>
#include <vector>
#include "pil.h"
#include "buffer_builder.h"
#include "transform/transform_meta.h"
#include "column_dictionary.h"
#include "bloom_filter.h"
#include <bitset>
namespace pil {
// ColumnStore can store ANY primitive type: e.g. CHROM, POS
//
// A ColumnStore is **MUTABLE** and should be used during importing/constructing
// procedures **ONLY**. Retrieving data should take place through Array structs and
// downcast to one of its concrete types.
class ColumnStore {
public:
explicit ColumnStore(MemoryPool* pool = default_memory_pool()) :
have_dictionary(false), have_bloom(false),
n_records(0), n_elements(0), n_null(0), uncompressed_size(0), compressed_size(0),
m_nullity(0), nullity_u(0), nullity_c(0),
pool_(pool)
{
memset(md5_checksum, 0, 16);
}
uint32_t size() const { return n_records; }
uint32_t GetMemoryUsage() const {
uint32_t ret = uncompressed_size + nullity_u;
if(have_dictionary) ret += dictionary->GetUncompressedSize();
return(ret);
}
uint8_t* mutable_data() { return buffer.mutable_data(); }
void ComputeChecksum() { Digest::GenerateMd5(mutable_data(), uncompressed_size, md5_checksum); }
// PrettyPrint representation of array suitable for debugging.
std::string ToString() const {
std::string ret = "Records: " + std::to_string(n_records) + ", elements: " + std::to_string(n_elements) + ", nulls: " + std::to_string(n_null) + '\n';
ret += "Compressed: " + std::to_string(compressed_size) + " b, Uncompressed: " + std::to_string(uncompressed_size) + " b\n";
if(nullity.get() == nullptr) {
ret += "Nullity: no\n";
} else {
ret += "Nullity: yes\n";
ret += "\tCompressed: " + std::to_string(nullity_c) + " b, Uncompressed: " + std::to_string(nullity_u) + " b\n";
}
if(have_dictionary) {
ret += "Dictionary: yes\n";
if(dictionary->IsTensorBased()) {
ret += "\Type: Tensor\n";
ret += "\tRecords: " + std::to_string(dictionary->NumberRecords()) + ", elements: " + std::to_string(dictionary->NumberElements()) + "\n";
ret += "\tCompressed (data): " + std::to_string(dictionary->GetCompressedSize()) + " b, uncompressed (data): " + std::to_string(dictionary->GetUncompressedSize()) + " b\n";
ret += "\tCompressed (strides): " + std::to_string(dictionary->GetCompressedLengthSize()) + " b, uncompressed (strides): " + std::to_string(dictionary->GetUncompressedLengthSize()) + " b\n";
} else{
ret += "\Type: Column\n";
ret += "\tRecords: " + std::to_string(dictionary->NumberRecords()) + ", elements: " + std::to_string(dictionary->NumberElements()) + "\n";
ret += "\tCompressed: " + std::to_string(dictionary->GetCompressedSize()) + " b, uncompressed: " + std::to_string(dictionary->GetUncompressedSize()) + " b\n";
}
} else {
ret += "Dictionary: no\n";
}
if(transformation_args.size()) {
ret += "Transformations: " + std::to_string(transformation_args.size()) + "\n";
for(int i = 0; i < transformation_args.size(); ++i) {
ret += "\t" + PIL_TRANSFORM_TYPE_STRING[transformation_args[i]->ctype] + ": " + std::to_string(transformation_args[i]->u_sz) + "->" + std::to_string(transformation_args[i]->c_sz) + " MD5: ";
for(int j = 0; j < 16; ++j) {
ret += std::to_string(transformation_args[i]->md5_checksum[j]);
}
ret += '\n';
}
} else {
ret += "Transformations: 0\n";
}
return(ret);
}
// Serialize/deserialize to/from disk
int Serialize(std::ostream& stream);
// Deserialization is for DEBUG use only. Otherwise, use the
// correct concrete Array types.
int Deserialize(std::ostream& stream);
// Check if the given element is valid by looking up that bit in the bitmap.
bool IsValid(const uint32_t p) { return(reinterpret_cast<uint32_t*>(nullity->mutable_data())[p / 32] & (1 << (p % 32))); }
public:
bool have_dictionary, have_bloom;
uint32_t n_records, n_elements, n_null;
uint32_t uncompressed_size, compressed_size;
uint32_t m_nullity, nullity_u, nullity_c; // nullity_u is not required as we can compute it. but is convenient to have during deserialization
// Any memory is owned by the respective Buffer instance (or its parents).
MemoryPool* pool_;
BufferBuilder buffer;
std::shared_ptr<ResizableBuffer> nullity; // NULLity vector
std::shared_ptr<ColumnDictionary> dictionary; // Dictionary used for predicate pushdown
std::shared_ptr<BlockSplitBloomFilter> bloom; // Bloom filter used for predicate pushdown
std::vector< std::shared_ptr<TransformMeta> > transformation_args; // Every transform MUST store a value.
uint8_t md5_checksum[16]; // **uncompressed** checksum
};
template <class T>
class ColumnStoreBuilder : public ColumnStore {
public:
explicit ColumnStoreBuilder(MemoryPool* pool = default_memory_pool()) : ColumnStore(pool){}
/**<
* Set the validity of the current object in the Nullity/Validity bitmap.
* This function MUST be called BEFORE the appending data to make sure that
* the correct position is set.
* @param yes Logical flag set to TRUE if the data is VALID or FALSE otherwise.
* @param adjust Adjust the record count downward by this value.
* @return Return 1.
*/
int AppendValidity(const bool yes, const int32_t adjust = 0) {
if(nullity.get() == nullptr) {
assert(AllocateResizableBuffer(pool_, 16384*sizeof(uint32_t), &nullity) == 1);
//nullity->ZeroPadding();
memset(nullity->mutable_data(), 0, sizeof(uint32_t)*16384);
m_nullity = 16384 * 32; // 32 bits for every integer used in the Nullity bitmap
}
if(n_records == m_nullity) {
assert(nullity->Resize(m_nullity + 16384*sizeof(uint32_t)) == 1);
nullity->ZeroPadding();
m_nullity += 16384 * 32;
}
n_null += (yes == false);
reinterpret_cast<uint32_t*>(nullity->mutable_data())[(n_records - adjust) / 32] |= (yes << ((n_records - adjust) % 32));
return 1;
}
int Append(const T value) {
buffer.Append(reinterpret_cast<const uint8_t*>(&value), sizeof(T));
++n_records;
++n_elements;
uncompressed_size += sizeof(T);
return(1);
}
int Append(const T* value, uint32_t n_values) {
buffer.Append(reinterpret_cast<const uint8_t*>(value), sizeof(T)*n_values);
n_records += n_values;
n_elements += n_values;
uncompressed_size += n_values * sizeof(T);
return(1);
}
int Append(const std::vector<T>& values) {
buffer.Append(reinterpret_cast<const uint8_t*>(&values[0]), sizeof(T)*values.size());
n_records += values.size();
n_elements += values.size();
uncompressed_size += values.size() * sizeof(T);
return(1);
}
int AppendArray(const T* value, uint32_t n_values) {
buffer.Append(reinterpret_cast<const uint8_t*>(value), sizeof(T)*n_values);
++n_records;
n_elements += n_values;
uncompressed_size += n_values * sizeof(T);
return(1);
}
int AppendArray(const std::vector<T>& values) {
buffer.Append(reinterpret_cast<const uint8_t*>(&values[0]), sizeof(T)*values.size());
++n_records;
n_elements += values.size();
uncompressed_size += values.size() * sizeof(T);
return(1);
}
const T* data() const { return reinterpret_cast<const T*>(buffer.data()); }
};
// ColumnSet groups any number of ColumnStores into a joint set of
// columns. This is useful when there are variable length vectors
// of values associated with a single field. This can potentially occur in all
// valid fields such as ALT and in INFO and FORMAT fields.
//
// When ANY of the ColumnStore objects reach the upper limit (batch size) they
// will ALL be processed en-mass and the encapsulaing Segment will be
// processed and flushed.
class ColumnSet {
public:
explicit ColumnSet(MemoryPool* pool = default_memory_pool()) :
n(0)
{
}
size_t size() const { return(columns.size()); }
uint32_t GetMemoryUsage() const {
uint32_t total = 0;
for(size_t i = 0; i < size(); ++i)
total += columns[i]->GetMemoryUsage();
return(total);
}
void clear() {
n = 0;
memset(md5_checksum, 0, 16);
columns.clear();
}
int Append(std::shared_ptr<ColumnStore> cstore) {
columns.push_back(cstore);
++n;
return(1);
}
template <class T>
int Append(std::shared_ptr< ColumnStoreBuilder<T> > builder) {
Append( std::static_pointer_cast<ColumnStore>(builder) );
return(1);
}
public:
uint32_t n;
uint8_t md5_checksum[16]; // checksum of the checksum vector -> md5(&checksums, n); this check is to guarantee there is no reordering of the set
std::vector< std::shared_ptr<ColumnStore> > columns;
};
template <class T>
class ColumnSetBuilder : public ColumnSet {
public:
explicit ColumnSetBuilder(MemoryPool* pool = default_memory_pool()) : ColumnSet(pool){}
int Append(const T value) {
// Check if columns[0] is set
if(columns.size() == 0) {
columns.push_back( std::make_shared<ColumnStore>(pil::default_memory_pool()) );
++n;
}
std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[0])->AppendValidity(true);
int ret = std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[0])->Append(value);
assert(ret == 1);
for(uint32_t i = 1; i < columns.size(); ++i){
std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->AppendValidity(false);
int ret = std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->Append(0);
assert(ret == 1);
}
return(1);
}
int Append(const std::vector<T>& values) {
// Add values up until N
if(n < values.size()) {
const int start_size = columns.size();
for(int i = start_size; i < values.size(); ++i, ++n)
columns.push_back( std::make_shared<ColumnStore>(pil::default_memory_pool()) );
assert(n >= values.size());
const uint32_t padding_to = columns[0]->n_records;
// Pad every column added this way.
for(int i = start_size; i < values.size(); ++i) {
for(int j = 0; j < padding_to; ++j){
std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->AppendValidity(false);
int ret = std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->Append(0);
assert(ret == 1);
}
}
}
for(int i = 0; i < values.size(); ++i){
std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->AppendValidity(true);
int ret = std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->Append(values[i]);
assert(ret == 1);
}
for(int i = values.size(); i < columns.size(); ++i){
std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->AppendValidity(false);
int ret = std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->Append(0);
assert(ret == 1);
}
return(1);
}
int Append(const T* value, int n_values) {
// Add values up until N
if((int)n < n_values){
const int start_size = columns.size();
for(int i = start_size; i < n_values; ++i, ++n)
columns.push_back( std::make_shared<ColumnStore>(pil::default_memory_pool()) );
assert((int)n >= n_values);
const uint32_t padding_to = columns[0]->n_records;
// Pad every column added this way.
for(int i = start_size; i < n_values; ++i) {
for(uint32_t j = 0; j < padding_to; ++j) {
std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->AppendValidity(false);
int ret = std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->Append(0);
assert(ret == 1);
}
}
}
for(int i = 0; i < n_values; ++i) {
std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->AppendValidity(true);
int ret = std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->Append(value[i]);
assert(ret == 1);
}
for(uint32_t i = n_values; i < columns.size(); ++i){
std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->AppendValidity(false);
int ret = std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->Append(0);
assert(ret == 1);
}
return(1);
}
/**<
* Pad all the ColumnStores in this ColumnSet with NULL values.
* @return
*/
int PadNull() {
if(columns.size() == 0) {
columns.push_back( std::make_shared<ColumnStore>(pil::default_memory_pool()) );
++n;
}
for(uint32_t i = 0; i < columns.size(); ++i) {
std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->AppendValidity(false);
int ret = std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[i])->Append(0);
assert(ret == 1);
}
return(1);
}
std::vector<int64_t> ColumnLengths() const{
std::vector<int64_t> lengths;
for(int i = 0; i < n; ++i)
lengths.push_back(columns[i]->n_records);
return(lengths);
}
};
template <class T>
class ColumnSetBuilderTensor : public ColumnSet {
public:
explicit ColumnSetBuilderTensor(MemoryPool* pool = default_memory_pool()) : ColumnSet(pool){}
int Append(const T value) {
// Check if columns[0] is set
if(columns.size() == 0) {
columns.push_back( std::make_shared<ColumnStore>(pil::default_memory_pool()) );
columns.push_back( std::make_shared<ColumnStore>(pil::default_memory_pool()) );
n += 2;
}
assert(n == 2);
// If this is the first value added to the ColumnSet then we add an addition
// 0 to the start such to support constant time lookup. This will resulting
// in this column being n + 1 length.
// We do NOT care about the Nullity vector for the data column as it has no meaning.
if(columns[0]->n_records == 0) {
std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->AppendValidity(true);
int ret = std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->Append(0);
assert(ret == 1);
ret = std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->Append(1);
assert(ret == 1);
} else {
const uint32_t n_recs = columns[0]->n_records;
assert(n_recs != 0);
const uint32_t cum = reinterpret_cast<uint32_t*>(columns[0]->mutable_data())[n_recs - 1];
std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->AppendValidity(true, 1);
int ret = std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->Append(cum + 1);
assert(ret == 1);
}
int ret = std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[1])->Append(value);
assert(ret == 1);
return(1);
}
int Append(const std::vector<T>& values) {
if(columns.size() == 0) {
columns.push_back( std::make_shared<ColumnStore>(pil::default_memory_pool()) );
columns.push_back( std::make_shared<ColumnStore>(pil::default_memory_pool()) );
n += 2;
}
assert(n == 2);
if(columns[0]->n_records == 0) {
std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->AppendValidity(true);
int ret = std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->Append(0);
assert(ret == 1);
ret = std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->Append(values.size());
assert(ret == 1);
} else {
const uint32_t n_recs = columns[0]->n_records;
assert(n_recs != 0);
const uint32_t cum = reinterpret_cast<uint32_t*>(columns[0]->mutable_data())[n_recs - 1];
std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->AppendValidity(true, 1);
int ret = std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->Append(cum + values.size());
assert(ret == 1);
}
int ret = std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[1])->AppendArray(values.data(), values.size());
assert(ret == 1);
return(1);
}
int Append(const T* value, int n_values) {
if(columns.size() == 0) {
columns.push_back( std::make_shared<ColumnStore>(pil::default_memory_pool()) );
columns.push_back( std::make_shared<ColumnStore>(pil::default_memory_pool()) );
n += 2;
}
assert(n == 2);
if(columns[0]->n_records == 0) {
std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->AppendValidity(true);
int ret = std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->Append(0);
assert(ret == 1);
ret = std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->Append(n_values);
assert(ret == 1);
} else {
const uint32_t n_recs = columns[0]->n_records;
assert(n_recs != 0);
const uint32_t cum = reinterpret_cast<uint32_t*>(columns[0]->mutable_data())[n_recs - 1];
std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->AppendValidity(true, 1);
int ret = std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->Append(cum + n_values);
assert(ret == 1);
}
assert(n_values > 0);
int ret = std::static_pointer_cast< ColumnStoreBuilder<T> >(columns[1])->AppendArray(value, n_values);
assert(ret == 1);
return(1);
}
/**<
* Padding a Tensor-style ColumnStore simply involves adding an offset of 0 and
* setting the appropriate Null-vector bit.
* @return
*/
int PadNull() {
if(columns.size() == 0) {
columns.push_back( std::make_shared<ColumnStore>(pil::default_memory_pool()) );
columns.push_back( std::make_shared<ColumnStore>(pil::default_memory_pool()) );
n += 2;
}
assert(n == 2);
if(columns[0]->n_records == 0) {
std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->AppendValidity(false);
int ret = std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->Append(0);
assert(ret == 1);
ret = std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->Append(0);
assert(ret == 1);
} else {
const uint32_t n_recs = columns[0]->n_records;
assert(n_recs != 0);
const uint32_t cum = reinterpret_cast<uint32_t*>(columns[0]->mutable_data())[n_recs - 1];
std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->AppendValidity(false);
int ret = std::static_pointer_cast< ColumnStoreBuilder<uint32_t> >(columns[0])->Append(cum + 0);
assert(ret == 1);
}
return(1);
}
std::vector<int64_t> ColumnLengths() const{
std::vector<int64_t> lengths;
for(int i = 0; i < n; ++i)
lengths.push_back(columns[i]->n_records);
return(lengths);
}
};
}
#endif /* COLUMN_STORE_H_ */