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

Public Types

using Task = IEJoinSourceTask
using TaskPtr = optional_ptr<Task>
using SortedTable = PhysicalRangeJoin::GlobalSortedTable

Public Member Functions

 IEJoinGlobalSourceState (const PhysicalIEJoin &op, ClientContext &client, IEJoinGlobalState &gsink)
idx_t GetStageCount (IEJoinSourceStage stage) const
atomic< idx_t > & GetStageNext (IEJoinSourceStage stage)
IEJoinSourceStage GetStage () const
 The processing stage for this group.
idx_t GetTaskCount () const
 The total number of tasks we will execute.
bool HasMoreTasks () const
 Are there any more tasks?
bool HasUnfinishedTasks () const
bool TryPrepareNextStage ()
bool TryNextTask (TaskPtr &task, Task &task_local)
void ExecuteLiTask (ClientContext &client)
void ExecutePermutationTask (ClientContext &client)
idx_t MaxThreads () override
ProgressData GetProgress () const
Public Member Functions inherited from duckdb::GlobalSourceState
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const
Public Member Functions inherited from duckdb::StateWithBlockableTasks
unique_lock< mutex > Lock ()
void PreventBlocking (const unique_lock< mutex > &guard)
bool BlockTask (const unique_lock< mutex > &guard, const InterruptState &interrupt_state)
 Add a task to 'blocked_tasks' before returning SourceResultType::BLOCKED (must hold the lock).
bool CanBlock (const unique_lock< mutex > &guard) const
bool UnblockTasks (const unique_lock< mutex > &guard)
 Unblock all tasks (must hold the lock).
SinkResultType BlockSink (const unique_lock< mutex > &guard, const InterruptState &interrupt_state)
SourceResultType BlockSource (const unique_lock< mutex > &guard, const InterruptState &interrupt_state)
void VerifyLock (const unique_lock< mutex > &guard) const

Static Public Member Functions

template<typename T>
static T BinValue (T n, T val)

Public Attributes

const PhysicalIEJoin & op
IEJoinGlobalState & gsink
atomic< IEJoinSourceStage > stage
 The processing stage.
vector< idx_t > stage_tasks
 The the number of tasks per stage.
vector< idx_t > stage_begin
 The the first task in the stage.
idx_t next_task = 0
 The next task to process.
idx_t total_tasks = 0
 The total number of tasks.
atomic< idx_t > started
 The number of started tasks.
atomic< idx_t > finished
 The number of tasks finished.
atomic< bool > stopped
 Stop producing tasks.
array< atomic< idx_t >, static_cast< size_t >(IEJoinSourceStage::DONE)> completed
 The number of completed tasks for each stage.
unique_ptr< SortedTable > l1
 L1.
unique_ptr< SortedTable > l2
 L2.
unsafe_vector< int64_t > li
 Li.
unsafe_vector< idx_t > p
 P.
idx_t l2_blocks = 0
idx_t per_thread = 0
idx_t left_blocks = 0
idx_t right_blocks = 0
idx_t left_outers = 0
idx_t right_outers = 0

Protected Member Functions

void Initialize ()
void FinishTask (TaskPtr task)
bool TryNextTask (Task &task)

Constructor & Destructor Documentation

◆ IEJoinGlobalSourceState()

duckdb::IEJoinGlobalSourceState::IEJoinGlobalSourceState ( const PhysicalIEJoin & op,
ClientContext & client,
IEJoinGlobalState & gsink )
186867 : op(op), gsink(gsink), stage(IEJoinSourceStage::INIT), started(0), finished(0), stopped(false) {
186868 auto &left_table = *gsink.tables[0];
186869 auto &right_table = *gsink.tables[1];
186870
186871 left_blocks = left_table.BlockCount();
186872 if (left_table.found_match) {
186873 left_outers = left_blocks;
186874 }
186875
186876 right_blocks = right_table.BlockCount();
186877 if (right_table.found_match) {
186878 right_outers = right_blocks;
186879 }
186880
186881 // input : query Q with 2 join predicates t1.X op1 t2.X' and t1.Y op2 t2.Y', tables T, T' of sizes m and n resp.
186882 // output: a list of tuple pairs (ti , tj)
186883 // Note that T/T' are already sorted on X/X' and contain the payload data
186884 // We only join the two block numbers and use the sizes of the blocks as the counts
186885
186886 // 1. let L1 (resp. L2) be the array of column X (resp. Y )
186887 const auto &order1 = op.lhs_orders[0];
186888 const auto &order2 = op.lhs_orders[1];
186889
186890 // 2. if (op1 ∈ {>, ≥}) sort L1 in descending order
186891 // 3. else if (op1 ∈ {<, ≤}) sort L1 in ascending order
186892
186893 // For the union algorithm, we make a unified table with the keys and the rids as the payload:
186894 // X/X', Y/Y', R/R'/Li
186895 // The first position is the sort key.
186896 vector<LogicalType> types;
186897 types.emplace_back(order2.expression->return_type);
186898 types.emplace_back(LogicalType::BIGINT);
186899
186900 // Sort on the first expression
186901 auto ref = make_uniq<BoundReferenceExpression>(order1.expression->return_type, 0U);
186902 vector<BoundOrderByNode> orders;
186903 orders.emplace_back(order1.type, order1.null_order, std::move(ref));
186904 // The goal is to make i (from the left table) < j (from the right table),
186905 // if value[i] and value[j] match the condition 1.
186906 // Add a column from_left to solve the problem when there exist multiple equal values in l1.
186907 // If the operator is loose inequality, make t1.from_left (== true) sort BEFORE t2.from_left (== false).
186908 // Otherwise, make t1.from_left sort (== true) sort AFTER t2.from_left (== false).
186909 // For example, if t1.time <= t2.time
186910 // | value | 1 | 1 | 1 | 1 |
186911 // | --------- | ----- | ----- | ----- | ----- |
186912 // | from_left | T(l2) | T(l2) | F(r1) | F(r2) |
186913 // if t1.time < t2.time
186914 // | value | 1 | 1 | 1 | 1 |
186915 // | --------- | ----- | ----- | ----- | ----- |
186916 // | from_left | F(r2) | F(r1) | T(l2) | T(l1) |
186917 // Using this OrderType, if i < j then value[i] (from left table) and value[j] (from right table) match
186918 // the condition (t1.time <= t2.time or t1.time < t2.time), then from_left will force them into the correct order.
186919 auto from_left = make_uniq<BoundConstantExpression>(Value::BOOLEAN(true));
186920 const auto strict1 = IEJoinUnion::IsStrictComparison(op.conditions[0].comparison);
186921 orders.emplace_back(!strict1 ? OrderType::DESCENDING : OrderType::ASCENDING, OrderByNullType::ORDER_DEFAULT,
186922 std::move(from_left));
186923
186924 l1 = make_uniq<SortedTable>(client, orders, types, op);
186925
186926 // 4. if (op2 ∈ {>, ≥}) sort L2 in ascending order
186927 // 5. else if (op2 ∈ {<, ≤}) sort L2 in descending order
186928
186929 // We sort on Y/Y' to obtain the sort keys and the permutation array.
186930 // For this we just need a two-column table of Y, P
186931 types.clear();
186932 types.emplace_back(LogicalType::BIGINT);
186933
186934 // Sort on the first expression
186935 orders.clear();
186936 ref = make_uniq<BoundReferenceExpression>(order2.expression->return_type, 0U);
186937 orders.emplace_back(order2.type, order2.null_order, std::move(ref));
186938
186939 l2 = make_uniq<SortedTable>(client, orders, types, op);
186940
186941 // The number of blocks in L2 is not quite the sum of the blocks in the two tables...
186942 const auto join_count = left_table.count.load() + right_table.count.load();
186943 l2_blocks = BinValue<idx_t>(join_count, STANDARD_VECTOR_SIZE);
186944
186945 // Schedule the largest group on as many threads as possible
186946 auto &ts = TaskScheduler::GetScheduler(client);
186947 const auto threads = NumericCast<idx_t>(ts.NumberOfThreads());
186948 per_thread = BinValue<idx_t>(l2_blocks, threads);
186949
186950 Initialize();
186951}
unique_ptr< SortedTable > l2
L2.
Definition duckdb.cpp:186413
atomic< IEJoinSourceStage > stage
The processing stage.
Definition duckdb.cpp:186392
atomic< idx_t > finished
The number of tasks finished.
Definition duckdb.cpp:186404
atomic< idx_t > started
The number of started tasks.
Definition duckdb.cpp:186402
atomic< bool > stopped
Stop producing tasks.
Definition duckdb.cpp:186406
unique_ptr< SortedTable > l1
L1.
Definition duckdb.cpp:186411
static DUCKDB_API Value BOOLEAN(bool value)
Create a boolean Value from a specified value.
Definition duckdb.cpp:132118

Member Function Documentation

◆ BinValue()

template<typename T>
T duckdb::IEJoinGlobalSourceState::BinValue ( T n,
T val )
inlinestatic
186347 {
186348 return ((n + (val - 1)) / val);
186349 }

◆ GetStageCount()

idx_t duckdb::IEJoinGlobalSourceState::GetStageCount ( IEJoinSourceStage stage) const
inline
186351 {
186352 return stage_tasks[size_t(stage)];
186353 }
vector< idx_t > stage_tasks
The the number of tasks per stage.
Definition duckdb.cpp:186394

◆ GetStageNext()

atomic< idx_t > & duckdb::IEJoinGlobalSourceState::GetStageNext ( IEJoinSourceStage stage)
inline
186355 {
186356 return completed[size_t(stage)];
186357 }
array< atomic< idx_t >, static_cast< size_t >(IEJoinSourceStage::DONE)> completed
The number of completed tasks for each stage.
Definition duckdb.cpp:186408

◆ GetStage()

IEJoinSourceStage duckdb::IEJoinGlobalSourceState::GetStage ( ) const
inline

The processing stage for this group.

186360 {
186361 return stage;
186362 }

◆ GetTaskCount()

idx_t duckdb::IEJoinGlobalSourceState::GetTaskCount ( ) const
inline

The total number of tasks we will execute.

186365 {
186366 return stage_begin[size_t(IEJoinSourceStage::DONE)];
186367 }
vector< idx_t > stage_begin
The the first task in the stage.
Definition duckdb.cpp:186396

◆ HasMoreTasks()

bool duckdb::IEJoinGlobalSourceState::HasMoreTasks ( ) const
inline

Are there any more tasks?

186370 {
186371 return !stopped && started < total_tasks;
186372 }
idx_t total_tasks
The total number of tasks.
Definition duckdb.cpp:186400

◆ HasUnfinishedTasks()

bool duckdb::IEJoinGlobalSourceState::HasUnfinishedTasks ( ) const
inline
186373 {
186374 return !stopped && finished < total_tasks;
186375 }

◆ TryPrepareNextStage()

bool duckdb::IEJoinGlobalSourceState::TryPrepareNextStage ( )
187445 {
187446 // Inside lock
187447 const auto stage_count = GetStageCount(stage);
187448 const auto stage_next = GetStageNext(stage).load();
187449 if (stage_next >= stage_count) {
187450 auto stage_curr = stage.load();
187451 while (stage_curr < IEJoinSourceStage::DONE) {
187452 stage_curr = IEJoinSourceStage(size_t(stage_curr) + 1);
187453 if (GetStageCount(stage_curr)) {
187454 break;
187455 }
187456 }
187457 stage = stage_curr;
187458 return true;
187459 }
187460
187461 return false;
187462}

◆ TryNextTask() [1/2]

bool duckdb::IEJoinGlobalSourceState::TryNextTask ( TaskPtr & task,
Task & task_local )
187478 {
187479 auto guard = Lock();
187480 FinishTask(task);
187481
187482 if (!HasMoreTasks()) {
187483 task = nullptr;
187484 return false;
187485 }
187486
187487 if (TryPrepareNextStage()) {
187488 UnblockTasks(guard);
187489 }
187490
187491 if (TryNextTask(task_local)) {
187492 task = task_local;
187493 ++started;
187494 return true;
187495 }
187496
187497 task = nullptr;
187498
187499 return false;
187500}
bool HasMoreTasks() const
Are there any more tasks?
Definition duckdb.cpp:186370
bool UnblockTasks(const unique_lock< mutex > &guard)
Unblock all tasks (must hold the lock).
Definition duckdb.hpp:21906

◆ ExecuteLiTask()

void duckdb::IEJoinGlobalSourceState::ExecuteLiTask ( ClientContext & client)
186953 {
186954 // We don't actually need the L1 column, just its sort key, which is in the sort blocks
186955 l1->GetSortedRun(client);
186956
186957 IEJoinUnion::ExtractColumn<int64_t>(*l1, 1, li);
186958}
unsafe_vector< int64_t > li
Li.
Definition duckdb.cpp:186415

◆ ExecutePermutationTask()

void duckdb::IEJoinGlobalSourceState::ExecutePermutationTask ( ClientContext & client)
186960 {
186961 // We don't actually need the L2 column, just its sort key, which is in the sort blocks
186962 l2->GetSortedRun(client);
186963
186964 // 6. compute the permutation array P of L2 w.r.t. L1
186965 IEJoinUnion::ExtractColumn<idx_t, int64_t>(*l2, 0, p);
186966}
unsafe_vector< idx_t > p
P.
Definition duckdb.cpp:186417

◆ MaxThreads()

idx_t duckdb::IEJoinGlobalSourceState::MaxThreads ( )
overridevirtual

Reimplemented from duckdb::GlobalSourceState.

187464 {
187465 // We can't leverage any more threads than tasks.
187466 return *max_element(stage_tasks.begin(), stage_tasks.end());
187467}

◆ GetProgress()

ProgressData duckdb::IEJoinGlobalSourceState::GetProgress ( ) const
187564 {
187565 const auto count = GetTaskCount();
187566
187567 const auto returned = finished.load();
187568
187569 ProgressData res;
187570 if (count) {
187571 res.done = double(returned);
187572 res.total = double(count);
187573 } else {
187574 res.SetInvalid();
187575 }
187576 return res;
187577}
idx_t GetTaskCount() const
The total number of tasks we will execute.
Definition duckdb.cpp:186365

◆ Initialize()

void duckdb::IEJoinGlobalSourceState::Initialize ( )
protected
187382 {
187383 // INIT
187384 stage_tasks.emplace_back(0);
187385
187386 // SINK_L1
187387 idx_t l1_tasks = 0;
187388 if (per_thread) {
187389 l1_tasks = BinValue<idx_t>(left_blocks + right_blocks, per_thread);
187390 }
187391 stage_tasks.emplace_back(l1_tasks);
187392
187393 // FINALIZE_L1
187394 stage_tasks.emplace_back(1);
187395
187396 // MATERIALIZE_L1
187397 stage_tasks.emplace_back(MaxValue<idx_t>(l1_tasks, 1));
187398
187399 // EXTRACT_LI
187400 stage_tasks.emplace_back(1);
187401
187402 // SINK_L2
187403 idx_t l2_tasks = 0;
187404 if (per_thread) {
187405 l2_tasks = BinValue<idx_t>(l2_blocks, per_thread);
187406 }
187407 stage_tasks.emplace_back(l2_tasks);
187408
187409 // FINALIZE_L2
187410 stage_tasks.emplace_back(1);
187411
187412 // MATERIALIZE_L2
187413 stage_tasks.emplace_back(MaxValue<idx_t>(l2_tasks, 1));
187414
187415 // EXTRACT_P
187416 stage_tasks.emplace_back(1);
187417
187418 // INNER
187419 stage_tasks.emplace_back(l2_tasks);
187420
187421 // OUTER
187422 stage_tasks.emplace_back(left_outers + right_outers);
187423
187424 // DONE
187425 stage_tasks.emplace_back(0);
187426
187427 // Accumulate task counts so we can find boundaries reliably
187428 idx_t begin = 0;
187429 for (const auto &stage_task : stage_tasks) {
187430 stage_begin.emplace_back(begin);
187431 begin += stage_task;
187432 }
187433
187434 total_tasks = stage_begin.back();
187435
187436 // Set all the stage atomic counts to 0
187437 for (auto &stage_next : completed) {
187438 stage_next = 0;
187439 }
187440
187441 // Ready for action
187442 stage = IEJoinSourceStage(1);
187443}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ FinishTask()

void duckdb::IEJoinGlobalSourceState::FinishTask ( TaskPtr task)
protected
187469 {
187470 // Inside the lock
187471 if (!task) {
187472 return;
187473 }
187474
187475 ++finished;
187476}

◆ TryNextTask() [2/2]

bool duckdb::IEJoinGlobalSourceState::TryNextTask ( Task & task)
protected
187502 {
187503 // Inside lock
187504 if (next_task >= GetTaskCount()) {
187505 return false;
187506 }
187507
187508 // Search for where we are in the task list
187509 for (idx_t stage = idx_t(IEJoinSourceStage::INIT); stage <= idx_t(IEJoinSourceStage::DONE); ++stage) {
187510 if (next_task < stage_begin[stage]) {
187511 task.stage = IEJoinSourceStage(stage - 1);
187512 task.thread_idx = next_task - stage_begin[size_t(task.stage)];
187513 break;
187514 }
187515 }
187516
187517 if (task.stage != stage) {
187518 return false;
187519 }
187520
187521 switch (stage.load()) {
187522 case IEJoinSourceStage::SINK_L1:
187523 task.range.first = MinValue(task.thread_idx * per_thread, left_blocks + right_blocks);
187524 task.range.second = MinValue(task.range.first + per_thread, left_blocks + right_blocks);
187525 break;
187526 case IEJoinSourceStage::FINALIZE_L1:
187527 case IEJoinSourceStage::MATERIALIZE_L1:
187528 case IEJoinSourceStage::EXTRACT_LI:
187529 break;
187530 case IEJoinSourceStage::SINK_L2:
187531 task.range.first = MinValue(task.thread_idx * per_thread, l1->BlockCount());
187532 task.range.second = MinValue(task.range.first + per_thread, l1->BlockCount());
187533 break;
187534 case IEJoinSourceStage::FINALIZE_L2:
187535 case IEJoinSourceStage::MATERIALIZE_L2:
187536 case IEJoinSourceStage::EXTRACT_P:
187537 break;
187538 case IEJoinSourceStage::INNER: {
187539 task.range.first = task.thread_idx * per_thread;
187540 task.range.second = MinValue(task.range.first + per_thread, l2_blocks);
187541 break;
187542 }
187543 case IEJoinSourceStage::OUTER:
187544 if (task.thread_idx < left_outers) {
187545 // Left outer blocks
187546 const auto left_task = task.thread_idx;
187547 task.range = {left_task, left_task + 1};
187548 } else {
187549 // Right outer blocks
187550 const auto right_task = task.thread_idx - left_outers;
187551 task.range = {right_task, right_task + 1};
187552 }
187553 break;
187554 case IEJoinSourceStage::INIT:
187555 case IEJoinSourceStage::DONE:
187556 break;
187557 }
187558
187559 ++next_task;
187560
187561 return true;
187562}
idx_t next_task
The next task to process.
Definition duckdb.cpp:186398

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