6
6
//
7
7
// Identification: src/codegen/aggregation.cpp
8
8
//
9
- // Copyright (c) 2015-2017 , Carnegie Mellon University Database Group
9
+ // Copyright (c) 2015-2018 , Carnegie Mellon University Database Group
10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
13
13
#include " codegen/aggregation.h"
14
14
15
+ #include " codegen/lang/if.h"
15
16
#include " codegen/proxy/oa_hash_table_proxy.h"
16
17
#include " codegen/type/boolean_type.h"
17
18
#include " codegen/type/bigint_type.h"
@@ -136,7 +137,7 @@ void Aggregation::Setup(
136
137
}
137
138
138
139
// Register the hash-table instance in the runtime state
139
- auto hash_table_id = runtime_state_ .RegisterState (
140
+ auto hash_table_id = query_state_ .RegisterState (
140
141
" agg_hash" + std::to_string (agg_info.source_index ),
141
142
OAHashTableProxy::GetType (codegen));
142
143
@@ -177,30 +178,28 @@ void Aggregation::Setup(
177
178
}
178
179
179
180
// Codegen any initialization work for the hash tables
180
- void Aggregation::InitializeState (CodeGen &codegen) {
181
+ void Aggregation::InitializeQueryState (CodeGen &codegen) {
181
182
for (auto hash_table_info : hash_table_infos_) {
182
183
auto &hash_table = hash_table_info.first ;
183
184
auto hash_table_id = hash_table_info.second ;
184
- hash_table.Init (codegen,
185
- runtime_state_.LoadStatePtr (codegen, hash_table_id));
185
+ hash_table.Init (codegen, query_state_.LoadStatePtr (codegen, hash_table_id));
186
186
}
187
187
}
188
188
189
189
// Cleanup by destroying the aggregation hash tables
190
- void Aggregation::TearDownState (CodeGen &codegen) {
190
+ void Aggregation::TearDownQueryState (CodeGen &codegen) {
191
191
for (auto hash_table_info : hash_table_infos_) {
192
192
auto &hash_table = hash_table_info.first ;
193
193
auto hash_table_id = hash_table_info.second ;
194
194
hash_table.Destroy (codegen,
195
- runtime_state_ .LoadStatePtr (codegen, hash_table_id));
195
+ query_state_ .LoadStatePtr (codegen, hash_table_id));
196
196
}
197
197
}
198
198
199
199
void Aggregation::CreateInitialGlobalValues (CodeGen &codegen,
200
200
llvm::Value *space) const {
201
201
PELOTON_ASSERT (IsGlobal ());
202
- auto null_bitmap =
203
- UpdateableStorage::NullBitmap{codegen, GetAggregateStorage (), space};
202
+ UpdateableStorage::NullBitmap null_bitmap{codegen, storage_, space};
204
203
null_bitmap.InitAllNull (codegen);
205
204
null_bitmap.WriteBack (codegen);
206
205
}
@@ -260,23 +259,20 @@ void Aggregation::CreateInitialValues(
260
259
auto &hash_table = hash_table_infos_[agg_info.hast_table_index ].first ;
261
260
auto hash_table_id = hash_table_infos_[agg_info.hast_table_index ].second ;
262
261
263
- // Get runtime state pointer for this hash table
262
+ // Get the hash table for this hash table
264
263
llvm::Value *state_pointer =
265
- runtime_state_.LoadStatePtr (codegen, hash_table_id);
266
-
267
- // TODO(marcel): is it possible to cache the hash value for this
268
- // expression?
269
- llvm::Value *hash = nullptr ;
264
+ query_state_.LoadStatePtr (codegen, hash_table_id);
270
265
271
266
// Prepare the hash keys
272
267
std::vector<codegen::Value> key = grouping_keys;
273
268
key.push_back (input_val);
274
269
275
270
// Perform the dummy lookup in the hash table that creates the entry
271
+ llvm::Value *hash = nullptr ;
276
272
HashTable::NoOpInsertCallback insert_callback;
277
273
hash_table.Insert (codegen, state_pointer, hash, key, insert_callback);
278
274
}
279
- } // iterate aggregations
275
+ }
280
276
}
281
277
282
278
void Aggregation::DoInitializeValue (
@@ -288,11 +284,7 @@ void Aggregation::DoInitializeValue(
288
284
case ExpressionType::AGGREGATE_MIN:
289
285
case ExpressionType::AGGREGATE_MAX: {
290
286
// For the above aggregations, the initial value is the attribute value
291
- if (null_bitmap.IsNullable (storage_index)) {
292
- storage_.SetValue (codegen, space, storage_index, initial, null_bitmap);
293
- } else {
294
- storage_.SetValueSkipNull (codegen, space, storage_index, initial);
295
- }
287
+ storage_.SetValue (codegen, space, storage_index, initial, null_bitmap);
296
288
break ;
297
289
}
298
290
case ExpressionType::AGGREGATE_COUNT: {
@@ -303,13 +295,13 @@ void Aggregation::DoInitializeValue(
303
295
} else {
304
296
raw_initial = codegen.Const64 (1 );
305
297
}
306
- auto initial_val = codegen::Value{type::BigInt::Instance (), raw_initial};
298
+ codegen::Value initial_val {type::BigInt::Instance (), raw_initial};
307
299
storage_.SetValueSkipNull (codegen, space, storage_index, initial_val);
308
300
break ;
309
301
}
310
302
case ExpressionType::AGGREGATE_COUNT_STAR: {
311
303
// The initial value for COUNT(*) is 1
312
- auto one = codegen::Value{type::BigInt::Instance (), codegen.Const64 (1 )};
304
+ codegen::Value one {type::BigInt::Instance (), codegen.Const64 (1 )};
313
305
storage_.SetValueSkipNull (codegen, space, storage_index, one);
314
306
break ;
315
307
}
@@ -364,7 +356,7 @@ void Aggregation::DoAdvanceValue(CodeGen &codegen, llvm::Value *space,
364
356
}
365
357
case ExpressionType::AGGREGATE_COUNT_STAR: {
366
358
auto curr = storage_.GetValueSkipNull (codegen, space, storage_index);
367
- auto delta = Value{type::BigInt::Instance (), codegen.Const64 (1 )};
359
+ auto delta = codegen:: Value{type::BigInt::Instance (), codegen.Const64 (1 )};
368
360
next = curr.Add (codegen, delta);
369
361
break ;
370
362
}
@@ -494,7 +486,7 @@ void Aggregation::AdvanceValue(
494
486
LOG_ERROR (" %s" , message.c_str ());
495
487
throw Exception{ExceptionType::UNKNOWN_TYPE, message};
496
488
}
497
- } // switch
489
+ }
498
490
}
499
491
500
492
// Advance each of the aggregates stored in the provided storage space
@@ -503,7 +495,7 @@ void Aggregation::AdvanceValues(
503
495
const std::vector<codegen::Value> &next_vals,
504
496
const std::vector<codegen::Value> &grouping_keys) const {
505
497
// The null bitmap tracker
506
- UpdateableStorage::NullBitmap null_bitmap{ codegen, storage_, space} ;
498
+ UpdateableStorage::NullBitmap null_bitmap ( codegen, storage_, space) ;
507
499
508
500
// Loop over all aggregates, advancing each
509
501
for (const auto &aggregate_info : aggregate_infos_) {
@@ -522,7 +514,7 @@ void Aggregation::AdvanceValues(
522
514
hash_table_infos_[aggregate_info.hast_table_index ].second ;
523
515
524
516
// Get runtime state pointer for this hash table
525
- llvm::Value *ht_ptr = runtime_state_ .LoadStatePtr (codegen, hash_table_id);
517
+ llvm::Value *ht_ptr = query_state_ .LoadStatePtr (codegen, hash_table_id);
526
518
527
519
// TODO(marcel): is it possible to cache the hash value for this
528
520
// expression?
@@ -586,39 +578,27 @@ void Aggregation::FinalizeValues(
586
578
CodeGen &codegen, llvm::Value *space,
587
579
std::vector<codegen::Value> &final_vals) const {
588
580
// The null bitmap tracker
589
- UpdateableStorage::NullBitmap null_bitmap{ codegen, storage_, space} ;
581
+ UpdateableStorage::NullBitmap null_bitmap ( codegen, storage_, space) ;
590
582
591
583
for (const auto &agg_info : aggregate_infos_) {
592
584
ExpressionType agg_type = agg_info.aggregate_type ;
593
585
switch (agg_type) {
594
586
case ExpressionType::AGGREGATE_SUM:
595
587
case ExpressionType::AGGREGATE_MIN:
596
588
case ExpressionType::AGGREGATE_MAX: {
597
- codegen::Value final_val;
598
- if (null_bitmap.IsNullable (agg_info.storage_indices [0 ])) {
599
- final_val = storage_.GetValue (
600
- codegen, space, agg_info.storage_indices [0 ], null_bitmap);
601
- } else {
602
- final_val = storage_.GetValueSkipNull (codegen, space,
603
- agg_info.storage_indices [0 ]);
604
- }
589
+ codegen::Value final_val = storage_.GetValue (
590
+ codegen, space, agg_info.storage_indices [0 ], null_bitmap);
605
591
606
592
// append final value to result vector
607
593
final_vals.push_back (final_val);
608
594
break ;
609
595
}
610
596
case ExpressionType::AGGREGATE_AVG: {
611
- // collect the final values of the SUM and the COUNT components and
612
- // calculate the average
613
- codegen::Value sum;
614
- if (null_bitmap.IsNullable (agg_info.storage_indices [0 ])) {
615
- sum = storage_.GetValue (codegen, space, agg_info.storage_indices [0 ],
616
- null_bitmap);
617
- } else {
618
- sum = storage_.GetValueSkipNull (codegen, space,
619
- agg_info.storage_indices [0 ]);
620
- }
597
+ // collect the final values of the SUM and the COUNT components
598
+ codegen::Value sum = storage_.GetValue (
599
+ codegen, space, agg_info.storage_indices [0 ], null_bitmap);
621
600
601
+ PELOTON_ASSERT (!null_bitmap.IsNullable (agg_info.storage_indices [1 ]));
622
602
codegen::Value count = storage_.GetValueSkipNull (
623
603
codegen, space, agg_info.storage_indices [1 ]);
624
604
@@ -628,7 +608,7 @@ void Aggregation::FinalizeValues(
628
608
codegen::Value count_casted =
629
609
count.CastTo (codegen, type::Decimal::Instance ());
630
610
631
- // add the actual calculation
611
+ // Compute the average
632
612
codegen::Value final_val =
633
613
sum_casted.Div (codegen, count_casted, OnError::ReturnNull);
634
614
0 commit comments