Does the actual group matching / creation.
146648 {
146649 D_ASSERT(groups.ColumnCount() + 1 ==
layout_ptr->ColumnCount());
146650 D_ASSERT(group_hashes_v.GetType() == LogicalType::HASH);
146651 D_ASSERT(state.ht_offsets.GetVectorType() == VectorType::FLAT_VECTOR);
146652 D_ASSERT(state.ht_offsets.GetType() == LogicalType::UBIGINT);
146653 D_ASSERT(addresses_v.GetType() == LogicalType::POINTER);
146654 D_ASSERT(state.hash_salts.GetType() == LogicalType::HASH);
146655
146656
146657 const auto chunk_size = groups.size();
146661 }
146663
146664
146665 const SelectionVector *sel_vector = FlatVector::IncrementalSelectionVector();
146666
146667
146668 if (state.group_chunk.ColumnCount() == 0) {
146669 state.group_chunk.InitializeEmpty(
layout_ptr->GetTypes());
146670 }
146671 D_ASSERT(state.group_chunk.ColumnCount() ==
layout_ptr->GetTypes().size());
146672 for (
idx_t grp_idx = 0; grp_idx < groups.ColumnCount(); grp_idx++) {
146673 state.group_chunk.data[grp_idx].Reference(groups.
data[grp_idx]);
146674 }
146675 state.group_chunk.data[groups.ColumnCount()].Reference(group_hashes_v);
146676 state.group_chunk.SetCardinality(groups);
146677
146678
146680
146682 hll.Update(group_hashes_v, group_hashes_v, groups.size());
146683 }
146684
146685 group_hashes_v.Flatten(chunk_size);
146686 const auto hashes = FlatVector::GetData<hash_t>(group_hashes_v);
146687
146688 addresses_v.Flatten(chunk_size);
146689 const auto addresses = FlatVector::GetData<data_ptr_t>(addresses_v);
146690
146692
146693 partitioned_data->AppendUnified(state.partitioned_append_state, state.group_chunk,
146694 *FlatVector::IncrementalSelectionVector(), chunk_size);
146696 *FlatVector::IncrementalSelectionVector(), chunk_size);
146697
146698 const auto row_locations =
146699 FlatVector::GetData<data_ptr_t>(state.partitioned_append_state.chunk_state.row_locations);
146700 const auto &row_sel = state.partitioned_append_state.reverse_partition_sel;
146701 for (
idx_t i = 0; i < chunk_size; i++) {
146702 const auto row_idx = row_sel.get_index_unsafe(i);
146703 const auto &row_location = row_locations[row_idx];
146704 addresses[i] = row_location;
146705 }
146706 count += chunk_size;
146707 return chunk_size;
146708 }
146709
146710
146711
146712 const auto ht_offsets = FlatVector::GetData<uint64_t>(state.ht_offsets);
146713 const auto hash_salts = FlatVector::GetData<hash_t>(state.hash_salts);
146714
146715
146716
146717
146718 idx_t occupied_count = 0;
146719 for (
idx_t r = 0; r < chunk_size; r++) {
146720 const auto &hash = hashes[r];
146721 auto &ht_offset = ht_offsets[r];
146723 occupied_count += entries[ht_offset].IsOccupied();
146724 D_ASSERT(ht_offset == hash %
capacity);
146725 hash_salts[r] = ht_entry_t::ExtractSalt(hash);
146726 }
146727
146728 SelectionVector empty_vector;
146729 idx_t new_group_count = 0;
146730 idx_t remaining_entries = chunk_size;
146731 idx_t iteration_count;
146732 for (iteration_count = 0; remaining_entries > 0 && iteration_count <
capacity; iteration_count++) {
146733 idx_t new_entry_count = 0;
146734 idx_t need_compare_count = 0;
146735 idx_t no_match_count = 0;
146736
146737
146738
146739 empty_vector.Initialize(new_groups_out.data() + new_group_count);
146740 if (sel_vector->IsSet()) {
146741 GroupedAggregateHashTableInnerLoop<true>(entries,
capacity,
bitmask, hash_salts, ht_offsets, sel_vector,
146742 remaining_entries, empty_vector, state.group_compare_vector,
146743 new_entry_count, need_compare_count);
146744 } else {
146745 GroupedAggregateHashTableInnerLoop<false>(entries,
capacity,
bitmask, hash_salts, ht_offsets, sel_vector,
146746 remaining_entries, empty_vector, state.group_compare_vector,
146747 new_entry_count, need_compare_count);
146748 }
146749 new_group_count += new_entry_count;
146750
146751 if (DUCKDB_UNLIKELY(occupied_count > new_entry_count + need_compare_count)) {
146752
146753
146754 throw InternalException("Internal validation failed in GroupedAggregateHashTable");
146755 }
146756 occupied_count = 0;
146757
146758 if (new_entry_count != 0) {
146759
146760 optional_ptr<PartitionedTupleData> data;
146761 optional_ptr<PartitionedTupleDataAppendState> append_state;
146765 data = unpartitioned_data.get();
146766 append_state = &state.unpartitioned_append_state;
146767 } else {
146769 append_state = &state.partitioned_append_state;
146770 }
146771 data->AppendUnified(*append_state, state.group_chunk, empty_vector, new_entry_count);
146773 *FlatVector::IncrementalSelectionVector(), new_entry_count);
146774
146775
146776 const auto row_locations = FlatVector::GetData<data_ptr_t>(append_state->chunk_state.row_locations);
146777 const auto &row_sel = append_state->reverse_partition_sel;
146778 for (
idx_t new_entry_idx = 0; new_entry_idx < new_entry_count; new_entry_idx++) {
146779 const auto index = empty_vector.get_index_unsafe(new_entry_idx);
146780 const auto row_idx = row_sel.get_index_unsafe(index);
146781 const auto &row_location = row_locations[row_idx];
146782
146783 auto &entry = entries[ht_offsets[
index]];
146784
146785 entry.SetPointer(row_location);
146786 addresses[
index] = row_location;
146787 }
146788 }
146789
146790 if (need_compare_count != 0) {
146791
146792 for (
idx_t need_compare_idx = 0; need_compare_idx < need_compare_count; need_compare_idx++) {
146793 const auto index = state.group_compare_vector.get_index_unsafe(need_compare_idx);
146794 const auto &entry = entries[ht_offsets[
index]];
146795 addresses[
index] = entry.GetPointer();
146796 }
146797
146798
146799 row_matcher.Match(state.group_chunk, state.partitioned_append_state.chunk_state.vector_data,
146800 state.group_compare_vector, need_compare_count, addresses_v, &state.no_match_vector,
146801 no_match_count);
146802 }
146803
146804
146805 for (
idx_t i = 0; i < no_match_count; i++) {
146806 const auto index = state.no_match_vector.get_index_unsafe(i);
146807 auto &ht_offset = ht_offsets[
index];
146808 const auto salt = hash_salts[
index];
146809 SaltIncrementAndWrap(ht_offset, salt,
bitmask);
146810 }
146811 sel_vector = &state.no_match_vector;
146812 remaining_entries = no_match_count;
146813 }
146814 if (iteration_count ==
capacity) {
146815 throw InternalException("Maximum outer iteration count reached in GroupedAggregateHashTable");
146816 }
146817
146818 count += new_group_count;
146819 return new_group_count;
146820}
static void ToUnifiedFormat(TupleDataChunkState &chunk_state, DataChunk &new_chunk)
Creates a UnifiedVectorFormat in the given Chunk state for the given DataChunk.
Definition duckdb.cpp:125999
static constexpr idx_t NumberOfPartitions(idx_t radix_bits)
The number of partitions for a given number of radix bits.
Definition duckdb.cpp:93425
static void InitializeStates(TupleDataLayout &layout, Vector &addresses, const SelectionVector &sel, idx_t count)
initialize - unaligned addresses
Definition duckdb.cpp:99774