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::AsOfProbeBuffer Class Reference
Collaboration diagram for duckdb::AsOfProbeBuffer:

Public Types

using Orders = vector<BoundOrderByNode>
using Task = AsOfSourceTask
using TaskPtr = optional_ptr<Task>
using resolve_join_t = void (duckdb::AsOfProbeBuffer::*)(idx_t *)

Public Member Functions

 AsOfProbeBuffer (ClientContext &client, const PhysicalAsOfJoin &op, AsOfGlobalSourceState &gsource)
template<SortKeyType SORT_KEY_TYPE>
void ResolveJoin (idx_t *matches)
void BeginLeftScan (TaskPtr task)
bool NextLeft ()
void ScanLeft ()
void EndLeftScan ()
void ResolveSimpleJoin (ExecutionContext &context, DataChunk &chunk)
void ResolveComplexJoin (ExecutionContext &context, DataChunk &chunk)
void GetData (ExecutionContext &context, DataChunk &chunk)
bool HasMoreData () const

Static Public Member Functions

static bool IsStrictComparison (ExpressionType comparison)
template<typename T>
static bool Compare (const T &lhs, const T &rhs, const bool strict)
 Is left cmp right?
static unique_ptr< ExternalBlockIteratorState > CreateIteratorState (SortedRun &sorted)
 Create a new iterator for the sorted run.
static void Repin (ExternalBlockIteratorState &iter)
 Reset the pins for an iterator so we release memory in a timely manner.

Public Attributes

resolve_join_t resolve_join_func
ClientContext & client
const PhysicalAsOfJoin & op
AsOfGlobalSourceState & gsource
 The source state.
const bool strict
 Is the inequality strict?
optional_ptr< AsOfHashGroup > asof_hash_group
 The current hash group.
TaskPtr task
 The task we are processing.
optional_ptr< SortedRun > left_group
OuterJoinMarker left_outer
unique_ptr< ExternalBlockIteratorState > left_itr
unique_ptr< AsOfPayloadScanner > lhs_scanner
DataChunk lhs_payload
ExpressionExecutor lhs_executor
DataChunk lhs_keys
ValidityMask lhs_valid_mask
idx_t left_bin = 0
SelectionVector lhs_match_sel
optional_ptr< SortedRun > right_group
optional_ptr< OuterJoinMarker > right_outer
unique_ptr< ExternalBlockIteratorState > right_itr
idx_t right_pos
unique_ptr< AsOfPayloadScanner > rhs_scanner
DataChunk rhs_payload
DataChunk rhs_input
SelectionVector rhs_match_sel
idx_t right_bin = 0
idx_t lhs_match_count
bool fetch_next_left
SortKeyPrefixComparison prefix

Constructor & Destructor Documentation

◆ AsOfProbeBuffer()

duckdb::AsOfProbeBuffer::AsOfProbeBuffer ( ClientContext & client,
const PhysicalAsOfJoin & op,
AsOfGlobalSourceState & gsource )
182563 : client(client), op(op), gsource(gsource), strict(IsStrictComparison(op.comparison_type)),
182564 left_outer(IsLeftOuterJoin(op.join_type)), lhs_executor(client), fetch_next_left(true) {
182565 lhs_keys.Initialize(client, op.join_key_types);
182566 for (const auto &cond : op.conditions) {
182567 lhs_executor.AddExpression(*cond.left);
182568 }
182569
182570 lhs_payload.Initialize(client, op.children[0].get().GetTypes());
182571 rhs_payload.Initialize(client, op.children[1].get().GetTypes());
182572 rhs_input.Initialize(client, op.children[1].get().GetTypes());
182573
182574 lhs_match_sel.Initialize();
182575 rhs_match_sel.Initialize();
182576 left_outer.Initialize(STANDARD_VECTOR_SIZE);
182577
182578 // If we have equality predicates, set up the prefix data.
182579 vector<LogicalType> prefix_types;
182580 for (idx_t i = 0; i < op.conditions.size() - 1; ++i) {
182581 const auto &cond = op.conditions[i];
182582 const auto &type = cond.left->return_type;
182583 prefix_types.emplace_back(type);
182584 SortKeyPrefixComparisonColumn col;
182585 col.size = DConstants::INVALID_INDEX;
182586 switch (type.id()) {
182587 case LogicalTypeId::VARCHAR:
182588 case LogicalTypeId::BLOB:
182589 col.type = SortKeyPrefixComparisonType::VARCHAR;
182590 break;
182591 case LogicalTypeId::STRUCT:
182592 case LogicalTypeId::LIST:
182593 case LogicalTypeId::ARRAY:
182594 col.type = SortKeyPrefixComparisonType::NESTED;
182595 break;
182596 default:
182597 col.type = SortKeyPrefixComparisonType::FIXED;
182598 col.size = 1 + GetTypeIdSize(type.InternalType());
182599 break;
182600 }
182601 prefix.columns.emplace_back(col);
182602 }
182603 if (!prefix_types.empty()) {
182604 // LHS, RHS
182605 prefix.decoded.Initialize(client, prefix_types, 2);
182606 }
182607}
const bool strict
Is the inequality strict?
Definition duckdb.cpp:182526
AsOfGlobalSourceState & gsource
The source state.
Definition duckdb.cpp:182524
bool IsLeftOuterJoin(JoinType type)
True if join is left or full outer join.
Definition duckdb.cpp:68419
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
static constexpr const idx_t INVALID_INDEX
The value used to signify an invalid index entry.
Definition duckdb.hpp:1117

Member Function Documentation

◆ IsStrictComparison()

bool duckdb::AsOfProbeBuffer::IsStrictComparison ( ExpressionType comparison)
inlinestatic
182463 {
182464 switch (comparison) {
182465 case ExpressionType::COMPARE_LESSTHAN:
182466 case ExpressionType::COMPARE_GREATERTHAN:
182467 return true;
182468 case ExpressionType::COMPARE_LESSTHANOREQUALTO:
182469 case ExpressionType::COMPARE_GREATERTHANOREQUALTO:
182470 return false;
182471 default:
182472 throw NotImplementedException("Unsupported comparison type for ASOF join");
182473 }
182474 }

◆ Compare()

template<typename T>
bool duckdb::AsOfProbeBuffer::Compare ( const T & lhs,
const T & rhs,
const bool strict )
inlinestatic

Is left cmp right?

182478 {
182479 const bool less_than = lhs < rhs;
182480 if (!less_than && !strict) {
182481 return !(rhs < lhs);
182482 }
182483 return less_than;
182484 }

◆ ResolveJoin()

template<SortKeyType SORT_KEY_TYPE>
void duckdb::AsOfProbeBuffer::ResolveJoin ( idx_t * matches)
182734 {
182735 using SORT_KEY = SortKey<SORT_KEY_TYPE>;
182736 using BLOCKS_ITERATOR = block_iterator_t<ExternalBlockIteratorState, SORT_KEY>;
182737
182738 // If there was no right partition, there are no matches
182739 lhs_match_count = 0;
182740 if (!right_itr) {
182741 return;
182742 }
182743
182744 Repin(*left_itr);
182745 BLOCKS_ITERATOR left_key(*left_itr);
182746
182747 Repin(*right_itr);
182748 BLOCKS_ITERATOR right_key(*right_itr);
182749
182750 const auto count = lhs_scanner->NextSize();
182751 const auto left_base = lhs_scanner->Scanned();
182752 // Searching for right <= left
182753 for (idx_t i = 0; i < count; ++i) {
182754 // If right > left, then there is no match
182755 const auto left_pos = left_base + i;
182756 if (!Compare(right_key[right_pos], left_key[left_pos], strict)) {
182757 continue;
182758 }
182759
182760 // Exponential search forward for a non-matching value using radix iterators
182761 // (We use exponential search to avoid thrashing the block manager on large probes)
182762 idx_t bound = 1;
182763 idx_t begin = right_pos;
182764 while (begin + bound < right_group->Count()) {
182765 if (Compare(right_key[begin + bound], left_key[left_pos], strict)) {
182766 // If right <= left, jump ahead
182767 bound *= 2;
182768 } else {
182769 break;
182770 }
182771 }
182772
182773 // Binary search for the first non-matching value using radix iterators
182774 // The previous value (which we know exists) is the match
182775 auto first = begin + bound / 2;
182776 auto last = MinValue<idx_t>(begin + bound, right_group->Count());
182777 while (first < last) {
182778 const auto mid = first + (last - first) / 2;
182779 if (Compare(right_key[mid], left_key[left_pos], strict)) {
182780 // If right <= left, new lower bound
182781 first = mid + 1;
182782 } else {
182783 last = mid;
182784 }
182785 }
182786 right_pos = --first;
182787
182788 // Check partitions for strict equality
182789 if (!prefix.columns.empty()) {
182790 const auto cmp = prefix.Compare(left_key[left_pos], right_key[right_pos]);
182791 if (cmp.column_index < prefix.columns.size()) {
182792 continue;
182793 }
182794 }
182795
182796 // Emit match data
182797 if (matches) {
182798 matches[i] = first;
182799 }
182800 lhs_match_sel.set_index(lhs_match_count++, i);
182801 }
182802}
static void Repin(ExternalBlockIteratorState &iter)
Reset the pins for an iterator so we release memory in a timely manner.
Definition duckdb.cpp:182507
static bool Compare(const T &lhs, const T &rhs, const bool strict)
Is left cmp right?
Definition duckdb.cpp:182478

◆ BeginLeftScan()

void duckdb::AsOfProbeBuffer::BeginLeftScan ( TaskPtr task)
182609 {
182610 auto &gsink = op.sink_state->Cast<AsOfGlobalSinkState>();
182611 task = task_p;
182612 const auto scan_bin = task->group_idx;
182613
182614 asof_hash_group = gsource.asof_groups[scan_bin].get();
182615
182616 // Always set right_bin too for memory management
182617 right_group = asof_hash_group->right_group;
182618 right_bin = right_group ? scan_bin : gsource.asof_groups.size();
182619
182620 left_group = asof_hash_group->left_group;
182621 left_bin = left_group ? scan_bin : gsource.asof_groups.size();
182622 if (!left_group || !left_group->Count()) {
182623 return;
182624 }
182625
182626 // Set up function pointer for sort type
182627 const auto sort_key_type = left_group->key_data->GetLayout().GetSortKeyType();
182628 switch (sort_key_type) {
182630 resolve_join_func = &AsOfProbeBuffer::ResolveJoin<SortKeyType::NO_PAYLOAD_FIXED_8>;
182631 break;
182632 case SortKeyType::NO_PAYLOAD_FIXED_16:
182633 resolve_join_func = &AsOfProbeBuffer::ResolveJoin<SortKeyType::NO_PAYLOAD_FIXED_16>;
182634 break;
182635 case SortKeyType::NO_PAYLOAD_FIXED_24:
182636 resolve_join_func = &AsOfProbeBuffer::ResolveJoin<SortKeyType::NO_PAYLOAD_FIXED_24>;
182637 break;
182638 case SortKeyType::NO_PAYLOAD_FIXED_32:
182639 resolve_join_func = &AsOfProbeBuffer::ResolveJoin<SortKeyType::NO_PAYLOAD_FIXED_32>;
182640 break;
182641 case SortKeyType::NO_PAYLOAD_VARIABLE_32:
182642 resolve_join_func = &AsOfProbeBuffer::ResolveJoin<SortKeyType::NO_PAYLOAD_VARIABLE_32>;
182643 break;
182645 resolve_join_func = &AsOfProbeBuffer::ResolveJoin<SortKeyType::PAYLOAD_FIXED_16>;
182646 break;
182647 case SortKeyType::PAYLOAD_FIXED_24:
182648 resolve_join_func = &AsOfProbeBuffer::ResolveJoin<SortKeyType::PAYLOAD_FIXED_24>;
182649 break;
182650 case SortKeyType::PAYLOAD_FIXED_32:
182651 resolve_join_func = &AsOfProbeBuffer::ResolveJoin<SortKeyType::PAYLOAD_FIXED_32>;
182652 break;
182653 case SortKeyType::PAYLOAD_VARIABLE_32:
182654 resolve_join_func = &AsOfProbeBuffer::ResolveJoin<SortKeyType::PAYLOAD_VARIABLE_32>;
182655 break;
182656 default:
182657 throw NotImplementedException("Unsupported comparison type for ASOF join");
182658 }
182659
182660 lhs_scanner = make_uniq<AsOfPayloadScanner>(*left_group, *gsink.sort_strategies[0]);
182661 lhs_scanner->SeekBlock(task->begin_idx);
182662 left_itr = CreateIteratorState(*left_group);
182663
182664 // We are only probing the corresponding right side bin, which may be empty
182665 // If it is empty, we leave the iterator as null so we can emit left matches
182666 right_pos = 0;
182667 if (right_group) {
182668 right_outer = &asof_hash_group->right_outer;
182669 if (right_group && right_group->Count()) {
182670 right_itr = CreateIteratorState(*right_group);
182671 rhs_scanner = make_uniq<AsOfPayloadScanner>(*right_group, *gsink.sort_strategies[1]);
182672 }
182673 }
182674}
static unique_ptr< ExternalBlockIteratorState > CreateIteratorState(SortedRun &sorted)
Create a new iterator for the sorted run.
Definition duckdb.cpp:182498
TaskPtr task
The task we are processing.
Definition duckdb.cpp:182530
optional_ptr< AsOfHashGroup > asof_hash_group
The current hash group.
Definition duckdb.cpp:182528
@ NO_PAYLOAD_FIXED_8
Without payload.
Definition duckdb.cpp:55338
@ PAYLOAD_FIXED_16
With payload (requires row pointer in key).
Definition duckdb.cpp:55344

◆ NextLeft()

bool duckdb::AsOfProbeBuffer::NextLeft ( )
182676 {
182677 return task->begin_idx < task->end_idx;
182678}

◆ ScanLeft()

void duckdb::AsOfProbeBuffer::ScanLeft ( )
182680 {
182681 // Scan the next sorted chunk
182682 lhs_payload.Reset();
182683 lhs_scanner->Scan(lhs_payload);
182684 ++task->begin_idx;
182685
182686 // Compute the join keys
182687 lhs_keys.Reset();
182688 lhs_executor.Execute(lhs_payload, lhs_keys);
182689 lhs_keys.Flatten();
182690
182691 // Combine the NULLs
182692 const auto count = lhs_payload.size();
182693 lhs_valid_mask.Reset();
182694 for (auto col_idx : op.null_sensitive) {
182695 auto &col = lhs_keys.data[col_idx];
182696 UnifiedVectorFormat unified;
182697 col.ToUnifiedFormat(count, unified);
182698 lhs_valid_mask.Combine(unified.validity, count);
182699 }
182700
182701 // Filter out NULL matches
182702 if (!lhs_valid_mask.AllValid()) {
182703 const auto count = lhs_match_count;
182704 lhs_match_count = 0;
182705 for (idx_t i = 0; i < count; ++i) {
182706 const auto idx = lhs_match_sel.get_index(i);
182707 if (lhs_valid_mask.RowIsValidUnsafe(idx)) {
182708 lhs_match_sel.set_index(lhs_match_count++, idx);
182709 }
182710 }
182711 }
182712}

◆ EndLeftScan()

void duckdb::AsOfProbeBuffer::EndLeftScan ( )
182714 {
182715 if (task->stage != AsOfJoinSourceStage::LEFT) {
182716 return;
182717 }
182718 task->stage = AsOfJoinSourceStage::DONE;
182719
182720 D_ASSERT(asof_hash_group);
182721 asof_hash_group->left_completed++;
182722
182723 right_group = nullptr;
182724 right_itr.reset();
182725 rhs_scanner.reset();
182726 right_outer = nullptr;
182727
182728 left_group = nullptr;
182729 left_itr.reset();
182730 lhs_scanner.reset();
182731}

◆ CreateIteratorState()

unique_ptr< ExternalBlockIteratorState > duckdb::AsOfProbeBuffer::CreateIteratorState ( SortedRun & sorted)
inlinestatic

Create a new iterator for the sorted run.

182498 {
182499 auto state = make_uniq<ExternalBlockIteratorState>(*sorted.key_data, sorted.payload_data.get());
182500
182501 // Unless we do this, we will only get values from the first chunk
182502 Repin(*state);
182503
182504 return state;
182505 }
Here is the call graph for this function:

◆ Repin()

void duckdb::AsOfProbeBuffer::Repin ( ExternalBlockIteratorState & iter)
inlinestatic

Reset the pins for an iterator so we release memory in a timely manner.

182507 {
182508 // Don't pin the payload because we are not using it here.
182509 iter.SetKeepPinned(true);
182510 }
Here is the caller graph for this function:

◆ ResolveSimpleJoin()

void duckdb::AsOfProbeBuffer::ResolveSimpleJoin ( ExecutionContext & context,
DataChunk & chunk )
182804 {
182805 // perform the actual join
182806 (this->*resolve_join_func)(nullptr);
182807
182808 // Scan the lhs values (after comparing keys) and filter out the LHS NULLs
182809 ScanLeft();
182810
182811 // Convert the match selection to simple join mask
182812 bool found_match[STANDARD_VECTOR_SIZE] = {false};
182813 for (idx_t i = 0; i < lhs_match_count; ++i) {
182814 found_match[lhs_match_sel.get_index(i)] = true;
182815 }
182816
182817 // now construct the result based on the join result
182818 switch (op.join_type) {
182819 case JoinType::SEMI:
182820 PhysicalJoin::ConstructSemiJoinResult(lhs_payload, chunk, found_match);
182821 break;
182822 case JoinType::ANTI:
182823 PhysicalJoin::ConstructAntiJoinResult(lhs_payload, chunk, found_match);
182824 break;
182825 default:
182826 throw NotImplementedException("Unimplemented join type for AsOf join");
182827 }
182828}

◆ ResolveComplexJoin()

void duckdb::AsOfProbeBuffer::ResolveComplexJoin ( ExecutionContext & context,
DataChunk & chunk )
182830 {
182831 // perform the actual join
182832 idx_t matches[STANDARD_VECTOR_SIZE];
182833 (this->*resolve_join_func)(matches);
182834
182835 // Scan the lhs values (after comparing keys) and filter out the LHS NULLs
182836 ScanLeft();
182837
182838 // Extract the rhs input columns from the match
182839 rhs_input.Reset();
182840 idx_t rhs_match_count = 0;
182841 for (idx_t i = 0; i < lhs_match_count; ++i) {
182842 const auto idx = lhs_match_sel[i];
182843 const auto match_pos = matches[idx];
182844 // Skip to the range containing the match
182845 if (match_pos >= rhs_scanner->Scanned()) {
182846 if (rhs_match_count) {
182847 rhs_input.Append(rhs_payload, false, &rhs_match_sel, rhs_match_count);
182848 rhs_match_count = 0;
182849 }
182850 rhs_payload.Reset();
182851 rhs_scanner->SeekRow(match_pos);
182852 rhs_scanner->Scan(rhs_payload);
182853 }
182854 // Select the individual values
182855 const auto source_offset = match_pos - rhs_scanner->Base();
182856 rhs_match_sel.set_index(rhs_match_count++, source_offset);
182857 }
182858 rhs_input.Append(rhs_payload, false, &rhs_match_sel, rhs_match_count);
182859
182860 // Slice the left payload into the result
182861 for (column_t i = 0; i < lhs_payload.ColumnCount(); ++i) {
182862 chunk.data[i].Slice(lhs_payload.data[i], lhs_match_sel, lhs_match_count);
182863 }
182864
182865 // Reference the projected right payload into the result
182866 for (column_t col_idx = 0; col_idx < op.right_projection_map.size(); ++col_idx) {
182867 const auto rhs_idx = op.right_projection_map[col_idx];
182868 auto &source = rhs_input.data[rhs_idx];
182869 auto &target = chunk.data[lhs_payload.ColumnCount() + col_idx];
182870 target.Reference(source);
182871 }
182872 chunk.SetCardinality(lhs_match_count);
182873
182874 // Update the match masks for the rows we ended up with
182875 left_outer.Reset();
182876 for (idx_t i = 0; i < lhs_match_count; ++i) {
182877 const auto idx = lhs_match_sel.get_index(i);
182878 left_outer.SetMatch(idx);
182879 const auto first = matches[idx];
182880 right_outer->SetMatch(first);
182881 }
182882
182883 // If we are doing a left join, come back for the NULLs
182884 fetch_next_left = !left_outer.Enabled();
182885}
vector< Vector > data
The vectors owned by the DataChunk.
Definition duckdb.hpp:11445
idx_t column_t
Type used for column identifiers.
Definition duckdb.hpp:259

◆ GetData()

void duckdb::AsOfProbeBuffer::GetData ( ExecutionContext & context,
DataChunk & chunk )
182887 {
182888 // Handle dangling left join results from current chunk
182889 if (!fetch_next_left) {
182890 fetch_next_left = true;
182891 if (left_outer.Enabled()) {
182892 // left join: before we move to the next chunk, see if we need to output any vectors that didn't
182893 // have a match found
182894 left_outer.ConstructLeftJoinResult(lhs_payload, chunk);
182895 left_outer.Reset();
182896 }
182897 return;
182898 }
182899
182900 // Stop if there is no more data
182901 if (!NextLeft()) {
182902 return;
182903 }
182904
182905 switch (op.join_type) {
182906 case JoinType::SEMI:
182907 case JoinType::ANTI:
182908 case JoinType::MARK:
182909 // simple joins can have max STANDARD_VECTOR_SIZE matches per chunk
182910 ResolveSimpleJoin(context, chunk);
182911 break;
182912 case JoinType::LEFT:
182913 case JoinType::INNER:
182914 case JoinType::RIGHT:
182915 case JoinType::OUTER:
182916 ResolveComplexJoin(context, chunk);
182917 break;
182918 default:
182919 throw NotImplementedException("Unimplemented type for as-of join!");
182920 }
182921}

◆ HasMoreData()

bool duckdb::AsOfProbeBuffer::HasMoreData ( ) const
inline
182517 {
182518 return !fetch_next_left || (task->begin_idx < task->end_idx);
182519 }

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