-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCollisionFreeVectorTable.hpp
More file actions
731 lines (663 loc) · 27.3 KB
/
Copy pathCollisionFreeVectorTable.hpp
File metadata and controls
731 lines (663 loc) · 27.3 KB
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/**
* 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.
**/
#ifndef QUICKSTEP_STORAGE_COLLISION_FREE_AGGREGATION_STATE_HASH_TABLE_HPP_
#define QUICKSTEP_STORAGE_COLLISION_FREE_AGGREGATION_STATE_HASH_TABLE_HPP_
#include <algorithm>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include <utility>
#include <vector>
#include "catalog/CatalogTypedefs.hpp"
#include "expressions/aggregation/AggregationID.hpp"
#include "storage/HashTableBase.hpp"
#include "storage/StorageBlob.hpp"
#include "storage/StorageConstants.hpp"
#include "storage/ValueAccessorMultiplexer.hpp"
#include "types/Type.hpp"
#include "types/TypeID.hpp"
#include "types/containers/ColumnVector.hpp"
#include "utility/BarrieredReadWriteConcurrentBitVector.hpp"
#include "utility/Macros.hpp"
#include "glog/logging.h"
namespace quickstep {
class AggregationHandle;
class StorageManager;
/** \addtogroup Storage
* @{
*/
class CollisionFreeVectorTable : public AggregationStateHashTableBase {
public:
/**
* @brief Constructor.
*
* @param key_type The group-by key type.
* @param num_entries The estimated number of entries this table will hold.
* @param memory_size The memory size for this table.
* @param num_init_partitions The number of partitions to be used for
* initializing the aggregation.
* @param num_finalize_partitions The number of partitions to be used for
* finalizing the aggregation.
* @param state_offsets The offsets for each state in the table.
* @param handles The aggregation handles.
* @param storage_manager The StorageManager to use (a StorageBlob will be
* allocated to hold this table's contents).
**/
CollisionFreeVectorTable(
const Type *key_type,
const std::size_t num_entries,
const std::size_t memory_size,
const std::size_t num_init_partitions,
const std::size_t num_finalize_partitions,
const std::vector<std::size_t> &state_offsets,
const std::vector<AggregationHandle *> &handles,
StorageManager *storage_manager);
~CollisionFreeVectorTable() override;
HashTableImplType getImplType() const override {
return HashTableImplType::kCollisionFreeVector;
}
void destroyPayload() override;
/**
* @brief Get the number of partitions to be used for initializing the table.
*
* @return The number of partitions to be used for initializing the table.
*/
inline std::size_t getNumInitializationPartitions() const {
return num_init_partitions_;
}
/**
* @brief Get the number of partitions to be used for finalizing the aggregation.
*
* @return The number of partitions to be used for finalizing the aggregation.
*/
inline std::size_t getNumFinalizationPartitions() const {
return num_finalize_partitions_;
}
/**
* @brief Get the exact number of tuples in the specified finalization partition.
*
* @return The exact number of tuples in the specified finalization partition.
*/
inline std::size_t getNumTuplesInFinalizationPartition(
const std::size_t partition_id) const {
const std::size_t start_position =
calculatePartitionStartPosition(partition_id);
const std::size_t end_position =
calculatePartitionEndPosition(partition_id);
return existence_map_->onesCountInRange(start_position, end_position);
}
/**
* @brief Get the existence map for this vector table.
*
* @return The existence map for this vector table.
*/
inline BarrieredReadWriteConcurrentBitVector* getExistenceMap() const {
return existence_map_.get();
}
/**
* @brief Initialize the specified partition of this aggregation table.
*
* @param partition_id ID of the partition to be initialized.
*/
inline void initialize(const std::size_t partition_id) {
const std::size_t memory_segment_size =
(memory_size_ + num_init_partitions_ - 1) / num_init_partitions_;
const std::size_t memory_start = memory_segment_size * partition_id;
std::memset(reinterpret_cast<char *>(blob_->getMemoryMutable()) + memory_start,
0,
std::min(memory_segment_size, memory_size_ - memory_start));
}
/**
* @brief Use aggregation handles to update (multiple) aggregation states in
* this vector table, with group-by keys and arguments drawn from the
* given ValueAccessors.
*
* @param argument_ids The multi-source attribute IDs of each argument
* component to be read from \p accessor_mux.
* @param key_ids The multi-source attribute IDs of each group-by key
* component to be read from \p accessor_mux.
* @param accessor_mux A ValueAccessorMultiplexer object that contains the
* ValueAccessors which will be used to access keys. beginIteration()
* should be called on the accessors before calling this method.
* @return Always return true.
**/
bool upsertValueAccessorCompositeKey(
const std::vector<std::vector<MultiSourceAttributeId>> &argument_ids,
const std::vector<MultiSourceAttributeId> &key_ids,
const ValueAccessorMultiplexer &accessor_mux) override;
/**
* @brief Copy the keys from this table to a NativeColumnVector, for the
* specified partition.
*
* @param partition_id ID of the partition to copy keys from.
* @param output_cv The NativeColumnVector to copy keys to.
*/
void finalizeKey(const std::size_t partition_id,
NativeColumnVector *output_cv) const;
/**
* @brief Finalize the aggregation states to a NativeColumnVector, for the
* specified partition and aggregation handle.
*
* @param partition_id ID of the partition to finalize.
* @param handle_id ID of the aggregation handle to finalize.
* @param output_cv The NativeColumnVector to write finalized values to.
*/
void finalizeState(const std::size_t partition_id,
const std::size_t handle_id,
NativeColumnVector *output_cv) const;
std::size_t getMemoryConsumptionBytes() const override {
return memory_size_;
}
private:
inline std::size_t calculatePartitionLength() const {
const std::size_t partition_length =
(num_entries_ + num_finalize_partitions_ - 1) / num_finalize_partitions_;
DCHECK_GE(partition_length, 0u);
return partition_length;
}
inline std::size_t calculatePartitionStartPosition(
const std::size_t partition_id) const {
return calculatePartitionLength() * partition_id;
}
inline std::size_t calculatePartitionEndPosition(
const std::size_t partition_id) const {
return std::min(calculatePartitionLength() * (partition_id + 1),
num_entries_);
}
template <bool use_two_accessors, typename ...ArgTypes>
inline void upsertValueAccessorDispatchHelper(
const bool is_key_nullable,
const bool is_argument_nullable,
ArgTypes &&...args);
template <bool ...bool_values, typename ...ArgTypes>
inline void upsertValueAccessorDispatchHelper(
const Type *key_type,
ArgTypes &&...args);
template <bool use_two_accessors, bool is_key_nullable, bool is_argument_nullable,
typename KeyT, typename ...ArgTypes>
inline void upsertValueAccessorDispatchHelper(
const Type *argument_type,
const AggregationID agg_id,
ArgTypes &&...args);
template <bool use_two_accessors, bool is_key_nullable, bool is_argument_nullable,
typename KeyT, typename KeyValueAccessorT, typename ArgumentValueAccessorT>
inline void upsertValueAccessorCountHelper(
const attribute_id key_attr_id,
const attribute_id argument_id,
void *vec_table,
KeyValueAccessorT *key_accessor,
ArgumentValueAccessorT *argument_accessor);
template <bool use_two_accessors, bool is_key_nullable, bool is_argument_nullable,
typename KeyT, typename KeyValueAccessorT, typename ArgumentValueAccessorT>
inline void upsertValueAccessorSumHelper(
const Type *argument_type,
const attribute_id key_attr_id,
const attribute_id argument_id,
void *vec_table,
KeyValueAccessorT *key_accessor,
ArgumentValueAccessorT *argument_accessor);
template <typename ...ArgTypes>
inline void upsertValueAccessorKeyOnlyHelper(
const bool is_key_nullable,
const Type *key_type,
ArgTypes &&...args);
template <bool is_key_nullable, typename KeyT, typename KeyValueAccessorT>
inline void upsertValueAccessorKeyOnly(
const attribute_id key_attr_id,
KeyValueAccessorT *key_accessor);
template <bool is_key_nullable, typename KeyT, typename KeyValueAccessorT>
inline void upsertValueAccessorCountNullary(
const attribute_id key_attr_id,
std::atomic<std::size_t> *vec_table,
KeyValueAccessorT *key_accessor);
template <bool use_two_accessors, bool is_key_nullable, typename KeyT,
typename KeyValueAccessorT, typename ArgumentValueAccessorT>
inline void upsertValueAccessorCountUnary(
const attribute_id key_attr_id,
const attribute_id argument_id,
std::atomic<std::size_t> *vec_table,
KeyValueAccessorT *key_accessor,
ArgumentValueAccessorT *argument_accessor);
template <bool use_two_accessors, bool is_key_nullable, bool is_argument_nullable,
typename KeyT, typename ArgumentT, typename StateT,
typename KeyValueAccessorT, typename ArgumentValueAccessorT>
inline void upsertValueAccessorIntegerSum(
const attribute_id key_attr_id,
const attribute_id argument_id,
std::atomic<StateT> *vec_table,
KeyValueAccessorT *key_accessor,
ArgumentValueAccessorT *argument_accessor);
template <bool use_two_accessors, bool is_key_nullable, bool is_argument_nullable,
typename KeyT, typename ArgumentT, typename StateT,
typename KeyValueAccessorT, typename ArgumentValueAccessorT>
inline void upsertValueAccessorGenericSum(
const attribute_id key_attr_id,
const attribute_id argument_id,
std::atomic<StateT> *vec_table,
KeyValueAccessorT *key_accessor,
ArgumentValueAccessorT *argument_accessor);
template <typename KeyT>
inline void finalizeKeyInternal(const std::size_t start_position,
const std::size_t end_position,
NativeColumnVector *output_cv) const;
template <typename ...ArgTypes>
inline void finalizeStateDispatchHelper(const AggregationID agg_id,
const Type *argument_type,
const void *vec_table,
ArgTypes &&...args) const;
template <typename ...ArgTypes>
inline void finalizeStateSumHelper(const Type *argument_type,
const void *vec_table,
ArgTypes &&...args) const;
inline void finalizeStateCount(const std::atomic<std::size_t> *vec_table,
const std::size_t start_position,
const std::size_t end_position,
NativeColumnVector *output_cv) const;
template <typename ResultT, typename StateT>
inline void finalizeStateSum(const std::atomic<StateT> *vec_table,
const std::size_t start_position,
const std::size_t end_position,
NativeColumnVector *output_cv) const;
const Type *key_type_;
const std::size_t num_entries_;
const std::size_t num_handles_;
const std::vector<AggregationHandle *> handles_;
std::unique_ptr<BarrieredReadWriteConcurrentBitVector> existence_map_;
std::vector<void *> vec_tables_;
const std::size_t memory_size_;
const std::size_t num_init_partitions_;
const std::size_t num_finalize_partitions_;
StorageManager *storage_manager_;
MutableBlobReference blob_;
DISALLOW_COPY_AND_ASSIGN(CollisionFreeVectorTable);
};
// ----------------------------------------------------------------------------
// Implementations of template methods follow.
template <bool use_two_accessors, typename ...ArgTypes>
inline void CollisionFreeVectorTable
::upsertValueAccessorDispatchHelper(const bool is_key_nullable,
const bool is_argument_nullable,
ArgTypes &&...args) {
if (is_key_nullable) {
if (is_argument_nullable) {
upsertValueAccessorDispatchHelper<use_two_accessors, true, true>(
std::forward<ArgTypes>(args)...);
} else {
upsertValueAccessorDispatchHelper<use_two_accessors, true, false>(
std::forward<ArgTypes>(args)...);
}
} else {
if (is_argument_nullable) {
upsertValueAccessorDispatchHelper<use_two_accessors, false, true>(
std::forward<ArgTypes>(args)...);
} else {
upsertValueAccessorDispatchHelper<use_two_accessors, false, false>(
std::forward<ArgTypes>(args)...);
}
}
}
template <bool ...bool_values, typename ...ArgTypes>
inline void CollisionFreeVectorTable
::upsertValueAccessorDispatchHelper(const Type *key_type,
ArgTypes &&...args) {
switch (key_type->getTypeID()) {
case TypeID::kInt:
upsertValueAccessorDispatchHelper<bool_values..., int>(
std::forward<ArgTypes>(args)...);
return;
case TypeID::kLong:
upsertValueAccessorDispatchHelper<bool_values..., std::int64_t>(
std::forward<ArgTypes>(args)...);
return;
default:
LOG(FATAL) << "Not supported";
}
}
template <bool use_two_accessors, bool is_key_nullable, bool is_argument_nullable,
typename KeyT, typename ...ArgTypes>
inline void CollisionFreeVectorTable
::upsertValueAccessorDispatchHelper(const Type *argument_type,
const AggregationID agg_id,
ArgTypes &&...args) {
switch (agg_id) {
case AggregationID::kCount:
upsertValueAccessorCountHelper<
use_two_accessors, is_key_nullable, is_argument_nullable, KeyT>(
std::forward<ArgTypes>(args)...);
return;
case AggregationID::kSum:
upsertValueAccessorSumHelper<
use_two_accessors, is_key_nullable, is_argument_nullable, KeyT>(
argument_type, std::forward<ArgTypes>(args)...);
return;
default:
LOG(FATAL) << "Not supported";
}
}
template <typename ...ArgTypes>
inline void CollisionFreeVectorTable
::upsertValueAccessorKeyOnlyHelper(const bool is_key_nullable,
const Type *key_type,
ArgTypes &&...args) {
switch (key_type->getTypeID()) {
case TypeID::kInt: {
if (is_key_nullable) {
upsertValueAccessorKeyOnly<true, int>(std::forward<ArgTypes>(args)...);
} else {
upsertValueAccessorKeyOnly<false, int>(std::forward<ArgTypes>(args)...);
}
return;
}
case TypeID::kLong: {
if (is_key_nullable) {
upsertValueAccessorKeyOnly<true, std::int64_t>(std::forward<ArgTypes>(args)...);
} else {
upsertValueAccessorKeyOnly<false, std::int64_t>(std::forward<ArgTypes>(args)...);
}
return;
}
default:
LOG(FATAL) << "Not supported";
}
}
template <bool is_key_nullable, typename KeyT, typename ValueAccessorT>
inline void CollisionFreeVectorTable
::upsertValueAccessorKeyOnly(const attribute_id key_attr_id,
ValueAccessorT *accessor) {
accessor->beginIteration();
while (accessor->next()) {
const KeyT *key = static_cast<const KeyT *>(
accessor->template getUntypedValue<is_key_nullable>(key_attr_id));
if (is_key_nullable && key == nullptr) {
continue;
}
existence_map_->setBit(*key);
}
}
template <bool use_two_accessors, bool is_key_nullable, bool is_argument_nullable,
typename KeyT, typename KeyValueAccessorT, typename ArgumentValueAccessorT>
inline void CollisionFreeVectorTable
::upsertValueAccessorCountHelper(const attribute_id key_attr_id,
const attribute_id argument_id,
void *vec_table,
KeyValueAccessorT *key_accessor,
ArgumentValueAccessorT *argument_accessor) {
DCHECK_GE(key_attr_id, 0);
if (is_argument_nullable && argument_id != kInvalidAttributeID) {
upsertValueAccessorCountUnary<use_two_accessors, is_key_nullable, KeyT>(
key_attr_id,
argument_id,
static_cast<std::atomic<std::size_t> *>(vec_table),
key_accessor,
argument_accessor);
return;
} else {
upsertValueAccessorCountNullary<is_key_nullable, KeyT>(
key_attr_id,
static_cast<std::atomic<std::size_t> *>(vec_table),
key_accessor);
return;
}
}
template <bool use_two_accessors, bool is_key_nullable, bool is_argument_nullable,
typename KeyT, typename KeyValueAccessorT, typename ArgumentValueAccessorT>
inline void CollisionFreeVectorTable
::upsertValueAccessorSumHelper(const Type *argument_type,
const attribute_id key_attr_id,
const attribute_id argument_id,
void *vec_table,
KeyValueAccessorT *key_accessor,
ArgumentValueAccessorT *argument_accessor) {
DCHECK_GE(key_attr_id, 0);
DCHECK_GE(argument_id, 0);
DCHECK(argument_type != nullptr);
switch (argument_type->getTypeID()) {
case TypeID::kInt:
upsertValueAccessorIntegerSum<
use_two_accessors, is_key_nullable, is_argument_nullable, KeyT, int>(
key_attr_id,
argument_id,
static_cast<std::atomic<std::int64_t> *>(vec_table),
key_accessor,
argument_accessor);
return;
case TypeID::kLong:
upsertValueAccessorIntegerSum<
use_two_accessors, is_key_nullable, is_argument_nullable, KeyT, std::int64_t>(
key_attr_id,
argument_id,
static_cast<std::atomic<std::int64_t> *>(vec_table),
key_accessor,
argument_accessor);
return;
case TypeID::kFloat:
upsertValueAccessorGenericSum<
use_two_accessors, is_key_nullable, is_argument_nullable, KeyT, float>(
key_attr_id,
argument_id,
static_cast<std::atomic<double> *>(vec_table),
key_accessor,
argument_accessor);
return;
case TypeID::kDouble:
upsertValueAccessorGenericSum<
use_two_accessors, is_key_nullable, is_argument_nullable, KeyT, double>(
key_attr_id,
argument_id,
static_cast<std::atomic<double> *>(vec_table),
key_accessor,
argument_accessor);
return;
default:
LOG(FATAL) << "Not supported";
}
}
template <bool is_key_nullable, typename KeyT, typename ValueAccessorT>
inline void CollisionFreeVectorTable
::upsertValueAccessorCountNullary(const attribute_id key_attr_id,
std::atomic<std::size_t> *vec_table,
ValueAccessorT *accessor) {
accessor->beginIteration();
while (accessor->next()) {
const KeyT *key = static_cast<const KeyT *>(
accessor->template getUntypedValue<is_key_nullable>(key_attr_id));
if (is_key_nullable && key == nullptr) {
continue;
}
const std::size_t loc = *key;
vec_table[loc].fetch_add(1u, std::memory_order_relaxed);
existence_map_->setBit(loc);
}
}
template <bool use_two_accessors, bool is_key_nullable, typename KeyT,
typename KeyValueAccessorT, typename ArgumentValueAccessorT>
inline void CollisionFreeVectorTable
::upsertValueAccessorCountUnary(const attribute_id key_attr_id,
const attribute_id argument_id,
std::atomic<std::size_t> *vec_table,
KeyValueAccessorT *key_accessor,
ArgumentValueAccessorT *argument_accessor) {
key_accessor->beginIteration();
if (use_two_accessors) {
argument_accessor->beginIteration();
}
while (key_accessor->next()) {
if (use_two_accessors) {
argument_accessor->next();
}
const KeyT *key = static_cast<const KeyT *>(
key_accessor->template getUntypedValue<is_key_nullable>(key_attr_id));
if (is_key_nullable && key == nullptr) {
continue;
}
const std::size_t loc = *key;
existence_map_->setBit(loc);
if (argument_accessor->getUntypedValue(argument_id) == nullptr) {
continue;
}
vec_table[loc].fetch_add(1u, std::memory_order_relaxed);
}
}
template <bool use_two_accessors, bool is_key_nullable, bool is_argument_nullable,
typename KeyT, typename ArgumentT, typename StateT,
typename KeyValueAccessorT, typename ArgumentValueAccessorT>
inline void CollisionFreeVectorTable
::upsertValueAccessorIntegerSum(const attribute_id key_attr_id,
const attribute_id argument_id,
std::atomic<StateT> *vec_table,
KeyValueAccessorT *key_accessor,
ArgumentValueAccessorT *argument_accessor) {
key_accessor->beginIteration();
if (use_two_accessors) {
argument_accessor->beginIteration();
}
while (key_accessor->next()) {
if (use_two_accessors) {
argument_accessor->next();
}
const KeyT *key = static_cast<const KeyT *>(
key_accessor->template getUntypedValue<is_key_nullable>(key_attr_id));
if (is_key_nullable && key == nullptr) {
continue;
}
const std::size_t loc = *key;
existence_map_->setBit(loc);
const ArgumentT *argument = static_cast<const ArgumentT *>(
argument_accessor->template getUntypedValue<is_argument_nullable>(argument_id));
if (is_argument_nullable && argument == nullptr) {
continue;
}
vec_table[loc].fetch_add(*argument, std::memory_order_relaxed);
}
}
template <bool use_two_accessors, bool is_key_nullable, bool is_argument_nullable,
typename KeyT, typename ArgumentT, typename StateT,
typename KeyValueAccessorT, typename ArgumentValueAccessorT>
inline void CollisionFreeVectorTable
::upsertValueAccessorGenericSum(const attribute_id key_attr_id,
const attribute_id argument_id,
std::atomic<StateT> *vec_table,
KeyValueAccessorT *key_accessor,
ArgumentValueAccessorT *argument_accessor) {
key_accessor->beginIteration();
if (use_two_accessors) {
argument_accessor->beginIteration();
}
while (key_accessor->next()) {
if (use_two_accessors) {
argument_accessor->next();
}
const KeyT *key = static_cast<const KeyT *>(
key_accessor->template getUntypedValue<is_key_nullable>(key_attr_id));
if (is_key_nullable && key == nullptr) {
continue;
}
const std::size_t loc = *key;
existence_map_->setBit(loc);
const ArgumentT *argument = static_cast<const ArgumentT *>(
argument_accessor->template getUntypedValue<is_argument_nullable>(argument_id));
if (is_argument_nullable && argument == nullptr) {
continue;
}
const ArgumentT arg_val = *argument;
std::atomic<StateT> &state = vec_table[loc];
StateT state_val = state.load(std::memory_order_relaxed);
while (!state.compare_exchange_weak(state_val, state_val + arg_val)) {}
}
}
template <typename KeyT>
inline void CollisionFreeVectorTable
::finalizeKeyInternal(const std::size_t start_position,
const std::size_t end_position,
NativeColumnVector *output_cv) const {
std::size_t loc = start_position - 1;
while ((loc = existence_map_->nextOne(loc)) < end_position) {
*static_cast<KeyT *>(output_cv->getPtrForDirectWrite()) = loc;
}
}
template <typename ...ArgTypes>
inline void CollisionFreeVectorTable
::finalizeStateDispatchHelper(const AggregationID agg_id,
const Type *argument_type,
const void *vec_table,
ArgTypes &&...args) const {
switch (agg_id) {
case AggregationID::kCount:
finalizeStateCount(static_cast<const std::atomic<std::size_t> *>(vec_table),
std::forward<ArgTypes>(args)...);
return;
case AggregationID::kSum:
finalizeStateSumHelper(argument_type,
vec_table,
std::forward<ArgTypes>(args)...);
return;
default:
LOG(FATAL) << "Not supported";
}
}
template <typename ...ArgTypes>
inline void CollisionFreeVectorTable
::finalizeStateSumHelper(const Type *argument_type,
const void *vec_table,
ArgTypes &&...args) const {
DCHECK(argument_type != nullptr);
switch (argument_type->getTypeID()) {
case TypeID::kInt: // Fall through
case TypeID::kLong:
finalizeStateSum<std::int64_t>(
static_cast<const std::atomic<std::int64_t> *>(vec_table),
std::forward<ArgTypes>(args)...);
return;
case TypeID::kFloat: // Fall through
case TypeID::kDouble:
finalizeStateSum<double>(
static_cast<const std::atomic<double> *>(vec_table),
std::forward<ArgTypes>(args)...);
return;
default:
LOG(FATAL) << "Not supported";
}
}
inline void CollisionFreeVectorTable
::finalizeStateCount(const std::atomic<std::size_t> *vec_table,
const std::size_t start_position,
const std::size_t end_position,
NativeColumnVector *output_cv) const {
std::size_t loc = start_position - 1;
while ((loc = existence_map_->nextOne(loc)) < end_position) {
*static_cast<std::int64_t *>(output_cv->getPtrForDirectWrite()) =
vec_table[loc].load(std::memory_order_relaxed);
}
}
template <typename ResultT, typename StateT>
inline void CollisionFreeVectorTable
::finalizeStateSum(const std::atomic<StateT> *vec_table,
const std::size_t start_position,
const std::size_t end_position,
NativeColumnVector *output_cv) const {
std::size_t loc = start_position - 1;
while ((loc = existence_map_->nextOne(loc)) < end_position) {
*static_cast<ResultT *>(output_cv->getPtrForDirectWrite()) =
vec_table[loc].load(std::memory_order_relaxed);
}
}
} // namespace quickstep
#endif // QUICKSTEP_STORAGE_COLLISION_FREE_AGGREGATION_STATE_HASH_TABLE_HPP_