Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::SortedRunMergerLocalState Class Reference
Inheritance diagram for duckdb::SortedRunMergerLocalState:
Collaboration diagram for duckdb::SortedRunMergerLocalState:

Public Member Functions

 SortedRunMergerLocalState (SortedRunMergerGlobalState &gstate_p)
bool TaskFinished () const
 Whether this thread has finished the work it has been assigned.
SourceResultType ExecuteTask (SortedRunMergerGlobalState &gstate, optional_ptr< DataChunk > chunk)
 Do the work this thread has been assigned.
void Clear ()
 Clear outstanding allocations.
Public Member Functions inherited from duckdb::LocalSourceState
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const

Public Attributes

const BlockIteratorStateType iterator_state_type
 Types for templating.
const SortKeyType sort_key_type
SortedRunMergerTask task
optional_idx partition_idx

Private Member Functions

void ComputePartitionBoundaries (SortedRunMergerGlobalState &gstate, const optional_idx &p_idx)
 Computes upper partition boundaries using K-way Merge Path.
template<class STATE>
void ComputePartitionBoundariesSwitch (SortedRunMergerGlobalState &gstate, const optional_idx &p_idx, unsafe_vector< STATE > &states)
template<class STATE, SortKeyType SORT_KEY_TYPE>
void TemplatedComputePartitionBoundaries (SortedRunMergerGlobalState &gstate, const optional_idx &p_idx, unsafe_vector< STATE > &states)
void AcquirePartitionBoundaries (SortedRunMergerGlobalState &gstate)
 Acquires lower partition boundaries from the global state.
void MergePartition (SortedRunMergerGlobalState &gstate)
 Merge the partition to obtain the next chunk.
template<class STATE>
void MergePartitionSwitch (SortedRunMergerGlobalState &gstate, unsafe_vector< STATE > &states)
template<class STATE, SortKeyType SORT_KEY_TYPE>
void TemplatedMergePartition (SortedRunMergerGlobalState &gstate, unsafe_vector< STATE > &states)
void ScanPartition (SortedRunMergerGlobalState &gstate, DataChunk &chunk)
 Scan from the merged partition.
template<SortKeyType SORT_KEY_TYPE>
void TemplatedScanPartition (SortedRunMergerGlobalState &gstate, DataChunk &chunk)
void MaterializePartition (SortedRunMergerGlobalState &gstate)
 Materialize the merge.
template<SortKeyType SORT_KEY_TYPE>
unique_ptr< SortedRun > TemplatedMaterializePartition (SortedRunMergerGlobalState &gstate)

Private Attributes

unsafe_vector< SortedRunPartitionBoundary > run_boundaries
 Computed run boundaries.
unsafe_vector< BlockIteratorState< BlockIteratorStateType::IN_MEMORY > > in_memory_states
 States for the iterator types.
unsafe_vector< BlockIteratorState< BlockIteratorStateType::EXTERNAL > > external_states
AllocatedData merged_partition
 Allocation for merging/scanning the partition.
idx_t merged_partition_count
 Variables for scanning.
idx_t merged_partition_index
Vector sort_key_pointers
 For scanning.
SortedRunScanState sorted_run_scan_state

Constructor & Destructor Documentation

◆ SortedRunMergerLocalState()

duckdb::SortedRunMergerLocalState::SortedRunMergerLocalState ( SortedRunMergerGlobalState & gstate_p)
explicit
107389 : iterator_state_type(gstate.iterator_state_type), sort_key_type(gstate.sort_key_type),
107390 task(SortedRunMergerTask::FINISHED), run_boundaries(gstate.num_runs),
107392 sort_key_pointers(LogicalType::POINTER), sorted_run_scan_state(gstate.context, gstate.merger.sort) {
107393 for (const auto &run : gstate.merger.sorted_runs) {
107394 auto &key_data = *run->key_data;
107395 switch (iterator_state_type) {
107397 in_memory_states.push_back(BlockIteratorState<BlockIteratorStateType::IN_MEMORY>(key_data));
107398 break;
107400 external_states.push_back(
107401 BlockIteratorState<BlockIteratorStateType::EXTERNAL>(key_data, run->payload_data.get()));
107402 break;
107403 default:
107404 throw NotImplementedException("SortedRunMergerLocalState::SortedRunMergerLocalState for %s",
107405 EnumUtil::ToString(iterator_state_type));
107406 }
107407 }
107408}
idx_t merged_partition_count
Variables for scanning.
Definition duckdb.cpp:107249
unsafe_vector< BlockIteratorState< BlockIteratorStateType::IN_MEMORY > > in_memory_states
States for the iterator types.
Definition duckdb.cpp:107242
const BlockIteratorStateType iterator_state_type
Types for templating.
Definition duckdb.cpp:107231
Vector sort_key_pointers
For scanning.
Definition duckdb.cpp:107253
unsafe_vector< SortedRunPartitionBoundary > run_boundaries
Computed run boundaries.
Definition duckdb.cpp:107239
@ FINISHED
No task.
Definition duckdb.cpp:107179
@ EXTERNAL
For ExternalBlockIteratorState.
Definition duckdb.cpp:57030
@ IN_MEMORY
For InMemoryBlockIteratorState.
Definition duckdb.cpp:57028
static constexpr const idx_t INVALID_INDEX
The value used to signify an invalid index entry.
Definition duckdb.hpp:1117

Member Function Documentation

◆ TaskFinished()

bool duckdb::SortedRunMergerLocalState::TaskFinished ( ) const

Whether this thread has finished the work it has been assigned.

107417 {
107418 switch (task) {
107423 D_ASSERT(partition_idx.IsValid());
107424 return false;
107426 D_ASSERT(!partition_idx.IsValid());
107427 return true;
107428 default:
107429 throw NotImplementedException("SortedRunMergerLocalState::TaskFinished for task");
107430 }
107431}
@ SCAN_PARTITION
Scan the merged partition.
Definition duckdb.cpp:107177
@ COMPUTE_BOUNDARIES
Compute boundaries of the assigned partition.
Definition duckdb.cpp:107171
@ ACQUIRE_BOUNDARIES
Acquire boundaries of the previous partition.
Definition duckdb.cpp:107173
@ MERGE_PARTITION
Merge the assigned partition.
Definition duckdb.cpp:107175
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ExecuteTask()

SourceResultType duckdb::SortedRunMergerLocalState::ExecuteTask ( SortedRunMergerGlobalState & gstate,
optional_ptr< DataChunk > chunk )

Do the work this thread has been assigned.

107434 {
107435 D_ASSERT(task != SortedRunMergerTask::FINISHED);
107436 switch (task) {
107438 ComputePartitionBoundaries(gstate, partition_idx);
107440 break;
107444 break;
107446 MergePartition(gstate);
107448 break;
107450 if (chunk) {
107451 ScanPartition(gstate, *chunk);
107452 } else {
107453 MaterializePartition(gstate);
107454 }
107455 if (!chunk || chunk->size() == 0) {
107456 gstate.DestroyScannedData();
107457 gstate.partitions[partition_idx.GetIndex()]->scanned = true;
107458 // fetch_add returns the _previous_ value!
107459 const auto scan_count_before_adding = gstate.total_scanned.fetch_add(merged_partition_count);
107460 const auto scan_count_after_adding = scan_count_before_adding + merged_partition_count;
107461 partition_idx = optional_idx::Invalid();
107463 if (scan_count_after_adding == gstate.merger.total_count) {
107464 return SourceResultType::FINISHED;
107465 }
107466 }
107467 break;
107468 default:
107469 throw NotImplementedException("SortedRunMergerLocalState::ExecuteTask for task");
107470 }
107471 return SourceResultType::HAVE_MORE_OUTPUT;
107472}
void ScanPartition(SortedRunMergerGlobalState &gstate, DataChunk &chunk)
Scan from the merged partition.
Definition duckdb.cpp:107771
void MergePartition(SortedRunMergerGlobalState &gstate)
Merge the partition to obtain the next chunk.
Definition duckdb.cpp:107686
void ComputePartitionBoundaries(SortedRunMergerGlobalState &gstate, const optional_idx &p_idx)
Computes upper partition boundaries using K-way Merge Path.
Definition duckdb.cpp:107474
void AcquirePartitionBoundaries(SortedRunMergerGlobalState &gstate)
Acquires lower partition boundaries from the global state.
Definition duckdb.cpp:107664
void MaterializePartition(SortedRunMergerGlobalState &gstate)
Materialize the merge.
Definition duckdb.cpp:107814
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Clear()

void duckdb::SortedRunMergerLocalState::Clear ( )

Clear outstanding allocations.

107410 {
107411 in_memory_states.clear();
107412 external_states.clear();
107413 merged_partition.Reset();
107414 sorted_run_scan_state.Clear();
107415}
AllocatedData merged_partition
Allocation for merging/scanning the partition.
Definition duckdb.cpp:107246
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputePartitionBoundaries()

void duckdb::SortedRunMergerLocalState::ComputePartitionBoundaries ( SortedRunMergerGlobalState & gstate,
const optional_idx & p_idx )
private

Computes upper partition boundaries using K-way Merge Path.

107475 {
107476 D_ASSERT(p_idx.IsValid());
107477 D_ASSERT(task == SortedRunMergerTask::COMPUTE_BOUNDARIES);
107478
107479 // Copy over the run boundaries from the assigned partition (under lock)
107480 auto &current_partition = *gstate.partitions[p_idx.GetIndex()];
107481 auto current_partition_guard = current_partition.Lock();
107482 const auto begin_computed = current_partition.GetBeginComputed();
107483 run_boundaries = current_partition.GetRunBoundaries(current_partition_guard);
107484 current_partition_guard.unlock();
107485
107486 if (!begin_computed) {
107487 // We can use information from previous partitions to speed up computing this partition
107488 for (idx_t prev = p_idx.GetIndex(); prev > 1; prev--) {
107489 auto &prev_partition = *gstate.partitions[prev - 1];
107490 if (!prev_partition.GetBeginComputed()) {
107491 continue;
107492 }
107493 auto prev_partition_guard = prev_partition.Lock();
107494 const auto &prev_partition_run_boundaries = prev_partition.GetRunBoundaries(prev_partition_guard);
107495 for (idx_t run_idx = 0; run_idx < gstate.num_runs; run_idx++) {
107496 run_boundaries[run_idx].begin = prev_partition_run_boundaries[run_idx].begin;
107497 }
107498 break;
107499 }
107500 }
107501
107502 // Compute the end partition boundaries (lock-free)
107503 switch (iterator_state_type) {
107505 ComputePartitionBoundariesSwitch<BlockIteratorState<BlockIteratorStateType::IN_MEMORY>>(gstate, p_idx,
107507 break;
107509 ComputePartitionBoundariesSwitch<BlockIteratorState<BlockIteratorStateType::EXTERNAL>>(gstate, p_idx,
107510 external_states);
107511 break;
107512 default:
107513 throw NotImplementedException(
107514 "SortedRunMergerLocalState::ComputePartitionBoundaries for SortedRunIteratorType");
107515 }
107516
107517 // The computed boundaries of the current partition may be the start boundaries of the next partition
107518 // Another thread depends on this, set them first
107519 if (p_idx.GetIndex() != gstate.num_partitions - 1) {
107520 auto &next_partition = *gstate.partitions[p_idx.GetIndex() + 1];
107521 if (!next_partition.GetBeginComputed()) {
107522 auto next_partition_guard = next_partition.Lock();
107523 if (!next_partition.GetBeginComputed()) {
107524 auto &next_partition_run_boundaries = next_partition.GetRunBoundaries(next_partition_guard);
107525 for (idx_t run_idx = 0; run_idx < gstate.num_runs; run_idx++) {
107526 const auto &computed_boundary = run_boundaries[run_idx];
107527 D_ASSERT(computed_boundary.begin == computed_boundary.end);
107528 next_partition_run_boundaries[run_idx].begin = computed_boundary.begin;
107529 }
107530 next_partition.SetBeginComputed();
107531 }
107532 }
107533 }
107534
107535 // Set the computed end partition boundaries of the current partition
107536 current_partition_guard.lock();
107537 auto &current_partition_run_boundaries = current_partition.GetRunBoundaries(current_partition_guard);
107538 for (idx_t run_idx = 0; run_idx < gstate.num_runs; run_idx++) {
107539 const auto &computed_boundary = run_boundaries[run_idx];
107540 D_ASSERT(computed_boundary.begin == computed_boundary.end);
107541 current_partition_run_boundaries[run_idx].end = computed_boundary.end;
107542 }
107543}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputePartitionBoundariesSwitch()

template<class STATE>
void duckdb::SortedRunMergerLocalState::ComputePartitionBoundariesSwitch ( SortedRunMergerGlobalState & gstate,
const optional_idx & p_idx,
unsafe_vector< STATE > & states )
private
107548 {
107549 switch (sort_key_type) {
107551 return TemplatedComputePartitionBoundaries<STATE, SortKeyType::NO_PAYLOAD_FIXED_8>(gstate, p_idx, states);
107552 case SortKeyType::NO_PAYLOAD_FIXED_16:
107553 return TemplatedComputePartitionBoundaries<STATE, SortKeyType::NO_PAYLOAD_FIXED_16>(gstate, p_idx, states);
107554 case SortKeyType::NO_PAYLOAD_FIXED_24:
107555 return TemplatedComputePartitionBoundaries<STATE, SortKeyType::NO_PAYLOAD_FIXED_24>(gstate, p_idx, states);
107556 case SortKeyType::NO_PAYLOAD_FIXED_32:
107557 return TemplatedComputePartitionBoundaries<STATE, SortKeyType::NO_PAYLOAD_FIXED_32>(gstate, p_idx, states);
107558 case SortKeyType::NO_PAYLOAD_VARIABLE_32:
107559 return TemplatedComputePartitionBoundaries<STATE, SortKeyType::NO_PAYLOAD_VARIABLE_32>(gstate, p_idx, states);
107561 return TemplatedComputePartitionBoundaries<STATE, SortKeyType::PAYLOAD_FIXED_16>(gstate, p_idx, states);
107562 case SortKeyType::PAYLOAD_FIXED_24:
107563 return TemplatedComputePartitionBoundaries<STATE, SortKeyType::PAYLOAD_FIXED_24>(gstate, p_idx, states);
107564 case SortKeyType::PAYLOAD_FIXED_32:
107565 return TemplatedComputePartitionBoundaries<STATE, SortKeyType::PAYLOAD_FIXED_32>(gstate, p_idx, states);
107566 case SortKeyType::PAYLOAD_VARIABLE_32:
107567 return TemplatedComputePartitionBoundaries<STATE, SortKeyType::PAYLOAD_VARIABLE_32>(gstate, p_idx, states);
107568 default:
107569 throw NotImplementedException("SortedRunMergerLocalState::ComputePartitionBoundariesSwitch for %s",
107570 EnumUtil::ToString(sort_key_type));
107571 }
107572}
@ NO_PAYLOAD_FIXED_8
Without payload.
Definition duckdb.cpp:55338
@ PAYLOAD_FIXED_16
With payload (requires row pointer in key).
Definition duckdb.cpp:55344

◆ TemplatedComputePartitionBoundaries()

template<class STATE, SortKeyType SORT_KEY_TYPE>
void duckdb::SortedRunMergerLocalState::TemplatedComputePartitionBoundaries ( SortedRunMergerGlobalState & gstate,
const optional_idx & p_idx,
unsafe_vector< STATE > & states )
private
107584 {
107585 using SORT_KEY = SortKey<SORT_KEY_TYPE>;
107586 using BLOCK_ITERATOR = block_iterator_t<STATE, SORT_KEY>;
107587
107588 D_ASSERT(run_boundaries.size() == gstate.num_runs);
107589
107590 // Check if last partition: boundary is always end of each sorted run
107591 if (p_idx == gstate.num_partitions - 1) {
107592 for (idx_t run_idx = 0; run_idx < gstate.num_runs; run_idx++) {
107593 run_boundaries[run_idx].begin = gstate.merger.sorted_runs[run_idx]->Count();
107594 }
107595 return;
107596 }
107597
107598 // Initialize "total_remaining", i.e., how much we still need to update the boundaries until we're done
107599 idx_t total_remaining = (p_idx.GetIndex() + 1) * gstate.merger.partition_size;
107600
107601 // Initialize iterators, and track of which runs are actively being used in the computation, i.e., not yet fixed
107602 unsafe_vector<BLOCK_ITERATOR> run_iterators;
107603 run_iterators.reserve(gstate.num_runs);
107604 unsafe_vector<idx_t> active_run_idxs;
107605 active_run_idxs.reserve(gstate.num_runs);
107606 for (idx_t run_idx = 0; run_idx < gstate.num_runs; run_idx++) {
107607 auto &state = states[run_idx];
107608 state.SetKeepPinned(false);
107609 state.SetPinPayload(false);
107610
107611 run_iterators.emplace_back(state);
107612 const auto &run_boundary = run_boundaries[run_idx];
107613 if (run_boundary.begin != run_boundary.end) {
107614 active_run_idxs.emplace_back(run_idx);
107615 }
107616
107617 // Reduce total remaining by what we already have (from previous partition)
107618 D_ASSERT(run_boundaries[run_idx].begin <= total_remaining);
107619 total_remaining -= run_boundaries[run_idx].begin;
107620 }
107621
107622 D_ASSERT(total_remaining <
107623 gstate.merger.total_count); // This is not the last partition, so should be less than total
107624 while (total_remaining != 0) {
107625 D_ASSERT(!active_run_idxs.empty());
107626 const idx_t k = active_run_idxs.size();
107627
107628 // Compute step size for each run: ceil(total_remaining / k), or less if run has less remaining
107629 auto min_idx = active_run_idxs[0];
107630 auto min_delta = ComputeBoundaryDelta(total_remaining, k, run_boundaries[min_idx]);
107631 reference<SORT_KEY> min_value = run_iterators[min_idx][run_boundaries[min_idx].begin + min_delta - 1];
107632 for (idx_t i = 1; i < active_run_idxs.size(); i++) {
107633 const auto &active_run_idx = active_run_idxs[i];
107634 const auto &run_boundary = run_boundaries[active_run_idx];
107635 const auto run_delta = ComputeBoundaryDelta(total_remaining, k, run_boundary);
107636 auto &run_value = run_iterators[active_run_idx][run_boundary.begin + run_delta - 1];
107637 if (run_value < min_value.get()) {
107638 min_idx = active_run_idx;
107639 min_delta = run_delta;
107640 min_value = run_value;
107641 }
107642 }
107643
107644 // Increment boundary begin value of the min run by the delta
107645 auto &min_run_boundary = run_boundaries[min_idx];
107646 min_run_boundary.begin += min_delta;
107647
107648 // Erase from active if begin is equal to end
107649 if (min_run_boundary.begin == min_run_boundary.end) {
107650 active_run_idxs.erase(std::find(active_run_idxs.begin(), active_run_idxs.end(), min_idx));
107651 }
107652
107653 // Update total remaining accordingly
107654 D_ASSERT(min_delta <= total_remaining);
107655 total_remaining -= min_delta;
107656 }
107657
107658 // End of boundary is meaningless now, just set equal to begin so there's no confusion
107659 for (auto &run_boundary : run_boundaries) {
107660 run_boundary.end = run_boundary.begin;
107661 }
107662}

◆ AcquirePartitionBoundaries()

void duckdb::SortedRunMergerLocalState::AcquirePartitionBoundaries ( SortedRunMergerGlobalState & gstate)
private

Acquires lower partition boundaries from the global state.

107664 {
107665 D_ASSERT(partition_idx.IsValid());
107666 D_ASSERT(task == SortedRunMergerTask::ACQUIRE_BOUNDARIES);
107667 auto &current_partition = *gstate.partitions[partition_idx.GetIndex()];
107668 if (current_partition.GetBeginComputed()) {
107669 // Begin has been computed, boundaries are ready to use. Copy to local
107670 auto guard = current_partition.Lock();
107671 run_boundaries = current_partition.GetRunBoundaries(guard);
107672 return;
107673 }
107674
107675 // Begin has not yet been computed by another thread, let this thread do it
107677 ComputePartitionBoundaries(gstate, partition_idx.GetIndex() - 1);
107679
107680 // Copy to local
107681 auto guard = current_partition.Lock();
107682 D_ASSERT(current_partition.GetBeginComputed());
107683 run_boundaries = current_partition.GetRunBoundaries(guard);
107684}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MergePartition()

void duckdb::SortedRunMergerLocalState::MergePartition ( SortedRunMergerGlobalState & gstate)
private

Merge the partition to obtain the next chunk.

107686 {
107687 switch (iterator_state_type) {
107689 MergePartitionSwitch<BlockIteratorState<BlockIteratorStateType::IN_MEMORY>>(gstate, in_memory_states);
107690 break;
107692 MergePartitionSwitch<BlockIteratorState<BlockIteratorStateType::EXTERNAL>>(gstate, external_states);
107693 break;
107694 default:
107695 throw NotImplementedException("SortedRunMergerLocalState::MergePartition for %s",
107696 EnumUtil::ToString(iterator_state_type));
107697 }
107698}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MergePartitionSwitch()

template<class STATE>
void duckdb::SortedRunMergerLocalState::MergePartitionSwitch ( SortedRunMergerGlobalState & gstate,
unsafe_vector< STATE > & states )
private
107701 {
107702 switch (sort_key_type) {
107704 return TemplatedMergePartition<STATE, SortKeyType::NO_PAYLOAD_FIXED_8>(gstate, states);
107705 case SortKeyType::NO_PAYLOAD_FIXED_16:
107706 return TemplatedMergePartition<STATE, SortKeyType::NO_PAYLOAD_FIXED_16>(gstate, states);
107707 case SortKeyType::NO_PAYLOAD_FIXED_24:
107708 return TemplatedMergePartition<STATE, SortKeyType::NO_PAYLOAD_FIXED_24>(gstate, states);
107709 case SortKeyType::NO_PAYLOAD_FIXED_32:
107710 return TemplatedMergePartition<STATE, SortKeyType::NO_PAYLOAD_FIXED_32>(gstate, states);
107711 case SortKeyType::NO_PAYLOAD_VARIABLE_32:
107712 return TemplatedMergePartition<STATE, SortKeyType::NO_PAYLOAD_VARIABLE_32>(gstate, states);
107714 return TemplatedMergePartition<STATE, SortKeyType::PAYLOAD_FIXED_16>(gstate, states);
107715 case SortKeyType::PAYLOAD_FIXED_24:
107716 return TemplatedMergePartition<STATE, SortKeyType::PAYLOAD_FIXED_24>(gstate, states);
107717 case SortKeyType::PAYLOAD_FIXED_32:
107718 return TemplatedMergePartition<STATE, SortKeyType::PAYLOAD_FIXED_32>(gstate, states);
107719 case SortKeyType::PAYLOAD_VARIABLE_32:
107720 return TemplatedMergePartition<STATE, SortKeyType::PAYLOAD_VARIABLE_32>(gstate, states);
107721 default:
107722 throw NotImplementedException("SortedRunMergerLocalState::MergePartitionSwitch for %s",
107723 EnumUtil::ToString(sort_key_type));
107724 }
107725}

◆ TemplatedMergePartition()

template<class STATE, SortKeyType SORT_KEY_TYPE>
void duckdb::SortedRunMergerLocalState::TemplatedMergePartition ( SortedRunMergerGlobalState & gstate,
unsafe_vector< STATE > & states )
private
107729 {
107730 using SORT_KEY = SortKey<SORT_KEY_TYPE>;
107731 using BLOCK_ITERATOR = block_iterator_t<STATE, SORT_KEY>;
107732
107733 if (!merged_partition.IsSet()) {
107735 BufferAllocator::Get(gstate.context).Allocate(gstate.merger.partition_size * sizeof(SORT_KEY));
107736 }
107737 auto merged_partition_keys = reinterpret_cast<SORT_KEY *>(merged_partition.get());
107739 merged_partition_index = 0;
107740
107741 idx_t active_runs = 0;
107742 for (idx_t run_idx = 0; run_idx < gstate.num_runs; run_idx++) {
107743 auto &state = states[run_idx];
107744 state.SetKeepPinned(true);
107745 state.SetPinPayload(true);
107746
107747 // Keep track of how many runs are actively being merged
107748 const auto &run_boundary = run_boundaries[run_idx];
107749 if (run_boundary.begin == run_boundary.end) {
107750 continue;
107751 }
107752 active_runs++;
107753
107754 for (auto it = BLOCK_ITERATOR(state, run_boundary.begin); it != BLOCK_ITERATOR(state, run_boundary.end); ++it) {
107755 merged_partition_keys[merged_partition_count++] = *it;
107756 }
107757 }
107758
107759 if (active_runs == 1 || gstate.merger.is_index_sort) {
107760 return; // Only one active run, no need to sort (or index sort, which is approximate sorting)
107761 }
107762
107763 // Seems counter-intuitive to re-sort instead of merging, but modern sorting algorithms detect and merge
107764 static const auto fallback = [](SORT_KEY *begin, SORT_KEY *end) {
107765 duckdb_pdqsort::pdqsort_branchless(begin, end);
107766 };
107767 duckdb_vergesort::vergesort(merged_partition_keys, merged_partition_keys + merged_partition_count,
107768 std::less<SORT_KEY>(), fallback);
107769}

◆ ScanPartition()

void duckdb::SortedRunMergerLocalState::ScanPartition ( SortedRunMergerGlobalState & gstate,
DataChunk & chunk )
private

Scan from the merged partition.

107771 {
107772 switch (sort_key_type) {
107774 return TemplatedScanPartition<SortKeyType::NO_PAYLOAD_FIXED_8>(gstate, chunk);
107775 case SortKeyType::NO_PAYLOAD_FIXED_16:
107776 return TemplatedScanPartition<SortKeyType::NO_PAYLOAD_FIXED_16>(gstate, chunk);
107777 case SortKeyType::NO_PAYLOAD_FIXED_24:
107778 return TemplatedScanPartition<SortKeyType::NO_PAYLOAD_FIXED_24>(gstate, chunk);
107779 case SortKeyType::NO_PAYLOAD_FIXED_32:
107780 return TemplatedScanPartition<SortKeyType::NO_PAYLOAD_FIXED_32>(gstate, chunk);
107781 case SortKeyType::NO_PAYLOAD_VARIABLE_32:
107782 return TemplatedScanPartition<SortKeyType::NO_PAYLOAD_VARIABLE_32>(gstate, chunk);
107784 return TemplatedScanPartition<SortKeyType::PAYLOAD_FIXED_16>(gstate, chunk);
107785 case SortKeyType::PAYLOAD_FIXED_24:
107786 return TemplatedScanPartition<SortKeyType::PAYLOAD_FIXED_24>(gstate, chunk);
107787 case SortKeyType::PAYLOAD_FIXED_32:
107788 return TemplatedScanPartition<SortKeyType::PAYLOAD_FIXED_32>(gstate, chunk);
107789 case SortKeyType::PAYLOAD_VARIABLE_32:
107790 return TemplatedScanPartition<SortKeyType::PAYLOAD_VARIABLE_32>(gstate, chunk);
107791 default:
107792 throw NotImplementedException("SortedRunMergerLocalState::ScanPartition for %s",
107793 EnumUtil::ToString(sort_key_type));
107794 }
107795}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TemplatedScanPartition()

template<SortKeyType SORT_KEY_TYPE>
void duckdb::SortedRunMergerLocalState::TemplatedScanPartition ( SortedRunMergerGlobalState & gstate,
DataChunk & chunk )
private
107798 {
107799 using SORT_KEY = SortKey<SORT_KEY_TYPE>;
107800 const auto count = MinValue<idx_t>(merged_partition_count - merged_partition_index, STANDARD_VECTOR_SIZE);
107801
107802 // Grab pointers to sort keys
107803 const auto merged_partition_keys = reinterpret_cast<SORT_KEY *>(merged_partition.get()) + merged_partition_index;
107804 const auto sort_keys = FlatVector::GetData<SORT_KEY *>(sort_key_pointers);
107805 for (idx_t i = 0; i < count; i++) {
107806 sort_keys[i] = &merged_partition_keys[i];
107807 }
107808 merged_partition_index += count;
107809
107810 // Scan
107811 sorted_run_scan_state.Scan(*gstate.merger.sorted_runs[0], sort_key_pointers, count, chunk);
107812}

◆ MaterializePartition()

void duckdb::SortedRunMergerLocalState::MaterializePartition ( SortedRunMergerGlobalState & gstate)
private

Materialize the merge.

107814 {
107815 unique_ptr<SortedRun> sorted_run;
107816 switch (sort_key_type) {
107818 sorted_run = TemplatedMaterializePartition<SortKeyType::NO_PAYLOAD_FIXED_8>(gstate);
107819 break;
107820 case SortKeyType::NO_PAYLOAD_FIXED_16:
107821 sorted_run = TemplatedMaterializePartition<SortKeyType::NO_PAYLOAD_FIXED_16>(gstate);
107822 break;
107823 case SortKeyType::NO_PAYLOAD_FIXED_24:
107824 sorted_run = TemplatedMaterializePartition<SortKeyType::NO_PAYLOAD_FIXED_24>(gstate);
107825 break;
107826 case SortKeyType::NO_PAYLOAD_FIXED_32:
107827 sorted_run = TemplatedMaterializePartition<SortKeyType::NO_PAYLOAD_FIXED_32>(gstate);
107828 break;
107829 case SortKeyType::NO_PAYLOAD_VARIABLE_32:
107830 sorted_run = TemplatedMaterializePartition<SortKeyType::NO_PAYLOAD_VARIABLE_32>(gstate);
107831 break;
107833 sorted_run = TemplatedMaterializePartition<SortKeyType::PAYLOAD_FIXED_16>(gstate);
107834 break;
107835 case SortKeyType::PAYLOAD_FIXED_24:
107836 sorted_run = TemplatedMaterializePartition<SortKeyType::PAYLOAD_FIXED_24>(gstate);
107837 break;
107838 case SortKeyType::PAYLOAD_FIXED_32:
107839 sorted_run = TemplatedMaterializePartition<SortKeyType::PAYLOAD_FIXED_32>(gstate);
107840 break;
107841 case SortKeyType::PAYLOAD_VARIABLE_32:
107842 sorted_run = TemplatedMaterializePartition<SortKeyType::PAYLOAD_VARIABLE_32>(gstate);
107843 break;
107844 default:
107845 throw NotImplementedException("SortedRunMergerLocalState::MaterializePartition for %s",
107846 EnumUtil::ToString(sort_key_type));
107847 }
107848
107849 // Add to global state
107850 lock_guard<mutex> guard(gstate.materialized_partition_lock);
107851 if (gstate.materialized_partitions.size() < partition_idx.GetIndex() + 1) {
107852 gstate.materialized_partitions.resize(partition_idx.GetIndex() + 1);
107853 }
107854 gstate.materialized_partitions[partition_idx.GetIndex()] = std::move(sorted_run);
107855}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TemplatedMaterializePartition()

template<SortKeyType SORT_KEY_TYPE>
unique_ptr< SortedRun > duckdb::SortedRunMergerLocalState::TemplatedMaterializePartition ( SortedRunMergerGlobalState & gstate)
private
107858 {
107859 using SORT_KEY = SortKey<SORT_KEY_TYPE>;
107860 const auto merged_partition_keys = reinterpret_cast<SORT_KEY *>(merged_partition.get()) + merged_partition_index;
107861
107862 TupleDataChunkState key_data_input;
107863 const auto key_locations = FlatVector::GetData<data_ptr_t>(key_data_input.row_locations);
107864 const auto key_heap_locations = FlatVector::GetData<data_ptr_t>(key_data_input.heap_locations);
107865 const auto key_heap_sizes = FlatVector::GetData<idx_t>(key_data_input.heap_sizes);
107866
107867 TupleDataChunkState payload_data_input;
107868 const auto payload_locations = FlatVector::GetData<data_ptr_t>(payload_data_input.row_locations);
107869
107870 auto sorted_run = gstate.merger.sorted_runs[0]->CreateRunForMaterialization();
107871
107872 while (merged_partition_index < merged_partition_count) {
107873 const auto count = MinValue<idx_t>(merged_partition_count - merged_partition_index, STANDARD_VECTOR_SIZE);
107874 for (idx_t i = 0; i < count; i++) {
107875 auto &key = merged_partition_keys[merged_partition_index + i];
107876 key_locations[i] = data_ptr_cast(&key);
107877 if (!SORT_KEY::CONSTANT_SIZE) {
107878 key_heap_locations[i] = key.GetData();
107879 key_heap_sizes[i] = key.GetHeapSize();
107880 }
107881 if (SORT_KEY::HAS_PAYLOAD) {
107882 payload_locations[i] = key.GetPayload();
107883 }
107884 }
107885
107886 if (SORT_KEY::HAS_PAYLOAD) {
107887 if (!sorted_run->payload_data->GetLayout().AllConstant()) {
107888 sorted_run->payload_data->FindHeapPointers(payload_data_input, count);
107889 }
107890 sorted_run->payload_append_state.chunk_state.heap_sizes.Reference(payload_data_input.heap_sizes);
107891 sorted_run->payload_data->Build(sorted_run->payload_append_state.pin_state,
107892 sorted_run->payload_append_state.chunk_state, 0, count);
107893 sorted_run->payload_data->CopyRows(sorted_run->payload_append_state.chunk_state, payload_data_input,
107894 *FlatVector::IncrementalSelectionVector(), count);
107895
107896 const auto new_payload_locations =
107897 FlatVector::GetData<const data_ptr_t>(sorted_run->payload_append_state.chunk_state.row_locations);
107898 for (idx_t i = 0; i < count; i++) {
107899 auto &key = merged_partition_keys[merged_partition_index + i];
107900 key.SetPayload(new_payload_locations[i]);
107901 }
107902 }
107903
107904 sorted_run->key_append_state.chunk_state.heap_sizes.Reference(key_data_input.heap_sizes);
107905 sorted_run->key_data->Build(sorted_run->key_append_state.pin_state, sorted_run->key_append_state.chunk_state, 0,
107906 count);
107907 sorted_run->key_data->CopyRows(sorted_run->key_append_state.chunk_state, key_data_input,
107908 *FlatVector::IncrementalSelectionVector(), count);
107909
107910 merged_partition_index += count;
107911 }
107912
107913 sorted_run->key_data->FinalizePinState(sorted_run->key_append_state.pin_state);
107914 if (sorted_run->payload_data) {
107915 sorted_run->payload_data->FinalizePinState(sorted_run->payload_append_state.pin_state);
107916 }
107917
107918 return sorted_run;
107919}

The documentation for this class was generated from the following file: