37 template <
typename INPUT_TYPE,
typename AGG_TYPE>
42 int32_t
const* original_input_col_idx_buf,
43 int64_t
const* ordered_input_col_idx_buf,
48 ? reinterpret_cast<const INPUT_TYPE*>(input_col_bufs.front())
50 ,
cond_col_buf_(input_col_bufs.size() == 2 ? input_col_bufs[1] : nullptr)
61 ? std::numeric_limits<INPUT_TYPE>::max()
63 ? std::numeric_limits<INPUT_TYPE>::min()
68 leaf_range_ = max_tree_height_and_leaf_range.second;
112 if (query_range.first > query_range.second || query_range.first < 0 ||
122 }
else if (sum_and_count_pair.
count == 0) {
126 return (static_cast<double>(sum_and_count_pair.
sum) /
128 sum_and_count_pair.
count;
130 return sum_and_count_pair.
sum / sum_and_count_pair.
count;
186 AGG_TYPE
build(int64_t cur_node_idx,
size_t cur_node_depth) {
189 int64_t input_col_idx = cur_node_idx -
leaf_range_.first;
197 const auto refined_input_col_idx =
207 std::vector<AGG_TYPE> child_vals =
220 int64_t input_col_idx = cur_node_idx -
leaf_range_.first;
228 const auto refined_input_col_idx =
237 std::vector<AGG_TYPE> child_vals =
250 int64_t input_col_idx = cur_node_idx -
leaf_range_.first;
258 const auto refined_input_col_idx =
267 std::vector<AGG_TYPE> child_vals =
279 int64_t input_col_idx = cur_node_idx -
leaf_range_.first;
286 if (input_col_idx == 0) {
295 ? col_val != prev_col_val
303 std::vector<AGG_TYPE> child_vals =
317 size_t cur_node_depth) {
319 int64_t input_col_idx = cur_node_idx -
leaf_range_.first;
324 const auto refined_input_col_idx =
336 std::vector<SumAndCountPair<AGG_TYPE>> child_vals =
345 size_t cur_node_depth) {
346 std::vector<AGG_TYPE> child_vals(
fan_out_);
347 int64_t
const offset = cur_node_depth ? parent_idx *
fan_out_ + 1 : 1;
348 size_t const next_node_depth = cur_node_depth + 1;
349 for (
size_t i = 0; i <
fan_out_; ++i) {
350 child_vals[i] =
build(offset + i, next_node_depth);
358 size_t cur_node_depth) {
359 std::vector<AGG_TYPE> child_vals(
fan_out_);
360 int64_t
const offset = cur_node_depth ? parent_idx *
fan_out_ + 1 : 1;
361 size_t const next_node_depth = cur_node_depth + 1;
362 for (
size_t i = 0; i <
fan_out_; ++i) {
370 size_t cur_node_depth) {
371 std::vector<AGG_TYPE> child_vals(
fan_out_);
372 int64_t
const offset = cur_node_depth ? parent_idx *
fan_out_ + 1 : 1;
373 size_t const next_node_depth = cur_node_depth + 1;
374 for (
size_t i = 0; i <
fan_out_; ++i) {
382 int64_t
const parent_idx,
383 size_t const cur_node_depth) {
384 std::vector<AGG_TYPE> child_vals(
fan_out_);
385 int64_t
const offset = cur_node_depth ? parent_idx *
fan_out_ + 1 : 1;
386 size_t const next_node_depth = cur_node_depth + 1;
387 for (
size_t i = 0; i <
fan_out_; ++i) {
395 size_t cur_node_depth) {
396 std::vector<SumAndCountPair<AGG_TYPE>> child_vals(
fan_out_);
397 int64_t
const offset = cur_node_depth ? parent_idx *
fan_out_ + 1 : 1;
398 size_t const next_node_depth = cur_node_depth + 1;
399 for (
size_t i = 0; i <
fan_out_; ++i) {
408 bool all_nulls =
true;
410 AGG_TYPE min_val = std::numeric_limits<AGG_TYPE>::max();
412 vals.begin(), vals.end(), [&min_val, &all_nulls,
this](
const AGG_TYPE val) {
415 min_val = std::min(min_val, val);
423 AGG_TYPE max_val = std::numeric_limits<AGG_TYPE>::min();
425 vals.begin(), vals.end(), [&max_val, &all_nulls,
this](
const AGG_TYPE val) {
428 max_val = std::max(max_val, val);
436 AGG_TYPE agg_val = 0;
438 vals.begin(), vals.end(), [&agg_val, &all_nulls,
this](
const AGG_TYPE val) {
453 bool all_nulls =
true;
454 auto end_idx = cur_col_idx + num_visits;
456 AGG_TYPE min_val = std::numeric_limits<AGG_TYPE>::max();
457 for (
size_t i = cur_col_idx; i < end_idx; ++i) {
461 min_val = std::min(min_val, val);
469 AGG_TYPE max_val = std::numeric_limits<AGG_TYPE>::min();
470 for (
size_t i = cur_col_idx; i < end_idx; ++i) {
474 max_val = std::max(max_val, val);
482 AGG_TYPE agg_val = 0;
483 for (
size_t i = cur_col_idx; i < end_idx; ++i) {
500 bool all_nulls =
true;
501 std::for_each(vals.begin(),
506 res.count += pair.count;
518 size_t num_visits)
const {
520 bool all_nulls =
true;
521 auto end_idx = cur_col_idx + num_visits;
522 for (
size_t i = cur_col_idx; i < end_idx; ++i) {
525 res.sum += cur_pair_val.
sum;
539 int64_t cur_node_idx,
540 size_t cur_node_depth,
541 int64_t search_range_start_idx,
542 int64_t search_range_end_idx)
const {
546 if (search_range_end_idx < query_range.first ||
547 query_range.second < search_range_start_idx) {
550 }
else if (query_range.first <= search_range_start_idx &&
551 search_range_end_idx <= query_range.second) {
559 size_t num_visits = query_range.second - search_range_start_idx + 1;
564 int64_t pivot_idx = search_range_start_idx +
565 ((search_range_end_idx - search_range_start_idx) /
fan_out_);
566 int64_t child_search_start_idx = search_range_start_idx;
567 int64_t child_search_end_idx = pivot_idx;
568 std::vector<size_t> child_indexes(
fan_out_);
570 std::vector<AGG_TYPE> child_vals(
fan_out_);
571 for (
size_t i = 0; i < child_indexes.size(); ++i) {
572 child_vals[i] =
search(query_range,
575 child_search_start_idx,
576 child_search_end_idx);
577 child_search_start_idx = child_search_end_idx + 1;
578 child_search_end_idx = child_search_start_idx + pivot_idx;
579 if (child_search_end_idx > search_range_end_idx) {
580 child_search_end_idx = search_range_end_idx;
590 int64_t cur_node_idx,
591 size_t cur_node_depth,
592 int64_t search_range_start_idx,
593 int64_t search_range_end_idx)
const {
594 if (search_range_end_idx < query_range.first ||
595 query_range.second < search_range_start_idx) {
597 }
else if (query_range.first <= search_range_start_idx &&
598 search_range_end_idx <= query_range.second) {
602 size_t num_visits = query_range.second - search_range_start_idx + 1;
607 std::vector<int64_t> child_indexes(
fan_out_);
610 int64_t pivot_idx = search_range_start_idx +
611 ((search_range_end_idx - search_range_start_idx) /
fan_out_);
612 int64_t child_search_start_idx = search_range_start_idx;
613 int64_t child_search_end_idx = pivot_idx;
614 for (
size_t i = 0; i < child_indexes.size(); ++i) {
618 child_search_start_idx,
619 child_search_end_idx);
620 child_search_start_idx = child_search_end_idx + 1;
621 child_search_end_idx = child_search_start_idx + pivot_idx;
622 if (child_search_end_idx > search_range_end_idx) {
623 child_search_end_idx = search_range_end_idx;
636 size_t parent_tree_depth)
const {
637 if (parent_tree_depth == 0) {
638 for (
size_t i = 0; i <
fan_out_; ++i) {
639 child_indexes[i] = i + 1;
642 int64_t cur_depth_start_offset = parent_idx *
fan_out_ + 1;
643 for (
size_t i = 0; i <
fan_out_; ++i) {
644 child_indexes[i] = cur_depth_start_offset + i;
647 return child_indexes;
654 return std::make_pair(0, std::make_pair(0, 0));
656 int64_t cur_level_start_offset = 0;
658 IndexPair index_pair = std::make_pair(0, 0);
660 size_t maximum_node_at_next_level = pow(fan_out, depth);
661 if (num_elem < maximum_node_at_next_level) {
662 index_pair = std::make_pair(cur_level_start_offset,
663 cur_level_start_offset + maximum_node_at_next_level);
664 return std::make_pair(depth, index_pair);
667 cur_level_start_offset += maximum_node_at_next_level;
const int64_t *const ordered_input_col_idx_buf_
AGG_TYPE aggregateValueViaColumnAccess(int64_t cur_col_idx, size_t num_visits) const
SumAndCountPair< AGG_TYPE > * derived_aggregated_
IndexPair getLeafRange() const
SQLTypeInfo const input_col_ti_
size_t getLeafDepth() const
const INPUT_TYPE *const input_col_buf_
HOST DEVICE int get_scale() const
std::vector< AGG_TYPE > prepareChildValuesforConditionalAggregation(int64_t parent_idx, size_t cur_node_depth)
size_t getTreeSize() const
AGG_TYPE buildForConditionalChangeEvent(int64_t cur_node_idx, size_t cur_node_depth)
size_t getLeafSize() const
AGG_TYPE search(const IndexPair &query_range, int64_t cur_node_idx, size_t cur_node_depth, int64_t search_range_start_idx, int64_t search_range_end_idx) const
Constants for Builtin SQL Types supported by HEAVY.AI.
SumAndCountPair< AGG_TYPE > buildForDerivedAggregate(int64_t cur_node_idx, size_t cur_node_depth)
AGG_TYPE buildForCount(int64_t cur_node_idx, size_t cur_node_depth)
size_t getTreeFanout() const
bool const is_conditional_agg_
AGG_TYPE fetch_col_for_non_cond_agg(size_t const idx)
std::vector< int64_t > computeChildIndexes(std::vector< int64_t > &child_indexes, int64_t parent_idx, size_t parent_tree_depth) const
SumAndCountPair< AGG_TYPE > searchForDerivedAggregate(const IndexPair &query_range, int64_t cur_node_idx, size_t cur_node_depth, int64_t search_range_start_idx, int64_t search_range_end_idx) const
SegmentTree(std::vector< const int8_t * > const &input_col_bufs, SQLTypeInfo const &input_col_ti, int32_t const *original_input_col_idx_buf, int64_t const *ordered_input_col_idx_buf, int64_t num_elems, SqlWindowFunctionKind agg_type, size_t fan_out)
DEVICE TextEncodingDict inline_null_value()
size_t getNumElems() const
void * checked_malloc(const size_t size)
SumAndCountPair< AGG_TYPE > aggregateValueForDerivedAggregateViaColumnAccess(int64_t cur_col_idx, size_t num_visits) const
AGG_TYPE buildForCondAgg(int64_t cur_node_idx, size_t cur_node_depth)
SumAndCountPair< AGG_TYPE > aggregateValueForDerivedAggregate(const std::vector< SumAndCountPair< AGG_TYPE >> &vals) const
AGG_TYPE query(const IndexPair &query_range) const
std::pair< int64_t, int64_t > IndexPair
AGG_TYPE * getAggregatedValues() const
INPUT_TYPE const input_type_null_val_
std::vector< SumAndCountPair< AGG_TYPE > > prepareChildValuesforDerivedAggregate(int64_t parent_idx, size_t cur_node_depth)
const int32_t *const original_input_col_idx_buf_
std::vector< AGG_TYPE > prepareChildValuesforAggregation(int64_t parent_idx, size_t cur_node_depth)
AGG_TYPE fetch_col_for_cond_agg(size_t const idx)
SqlWindowFunctionKind const agg_type_
AGG_TYPE fetch_col_for_count(size_t const idx)
AGG_TYPE build(int64_t cur_node_idx, size_t cur_node_depth)
Common Enum definitions for SQL processing.
std::pair< size_t, IndexPair > findMaxTreeHeight(int64_t num_elem, int fan_out)
SumAndCountPair< AGG_TYPE > * getDerivedAggregatedValues() const
AGG_TYPE aggregateValue(const std::vector< AGG_TYPE > &vals) const
const int8_t *const cond_col_buf_
std::vector< AGG_TYPE > prepareChildValuesforAggregationForCount(int64_t parent_idx, size_t cur_node_depth)
AGG_TYPE * aggregated_values_
INPUT_TYPE const invalid_val_
std::vector< AGG_TYPE > prepareChildValuesforAggregationForConditionalChangeEvent(int64_t const parent_idx, size_t const cur_node_depth)