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

Public Member Functions

 PhysicalMergeInto (PhysicalPlan &physical_plan, vector< LogicalType > types, map< MergeActionCondition, vector< unique_ptr< MergeIntoOperator > > > actions, idx_t row_id_index, optional_idx source_marker, bool parallel, bool return_chunk)
unique_ptr< GlobalSourceState > GetGlobalSourceState (ClientContext &context) const override
unique_ptr< LocalSourceState > GetLocalSourceState (ExecutionContext &context, GlobalSourceState &gstate) const override
SourceResultType GetDataInternal (ExecutionContext &context, DataChunk &chunk, OperatorSourceInput &input) const override
bool IsSource () const override
unique_ptr< GlobalSinkState > GetGlobalSinkState (ClientContext &context) const override
unique_ptr< LocalSinkState > GetLocalSinkState (ExecutionContext &context) const override
SinkResultType Sink (ExecutionContext &context, DataChunk &chunk, OperatorSinkInput &input) const override
SinkCombineResultType Combine (ExecutionContext &context, OperatorSinkCombineInput &input) const override
SinkFinalizeType Finalize (Pipeline &pipeline, Event &event, ClientContext &context, OperatorSinkFinalizeInput &input) const override
bool IsSink () const override
bool ParallelSink () const override
Public Member Functions inherited from duckdb::PhysicalOperator
 PhysicalOperator (PhysicalPlan &physical_plan, PhysicalOperatorType type, vector< LogicalType > types, idx_t estimated_cardinality)
 PhysicalOperator (const PhysicalOperator &other)=delete
 Deleted copy constructors.
PhysicalOperator & operator= (const PhysicalOperator &)=delete
virtual string GetName () const
virtual InsertionOrderPreservingMap< string > ParamsToString () const
virtual string ToString (ExplainFormat format=ExplainFormat::DEFAULT) const
void Print () const
virtual vector< const_reference< PhysicalOperator > > GetChildren () const
const vector< LogicalType > & GetTypes () const
 Return a vector of the types that will be returned by this operator.
virtual bool Equals (const PhysicalOperator &other) const
idx_t EstimatedThreadCount () const
 Functions to help decide how to set up pipeline dependencies.
bool CanSaturateThreads (ClientContext &context) const
virtual void Verify ()
virtual unique_ptr< OperatorState > GetOperatorState (ExecutionContext &context) const
virtual unique_ptr< GlobalOperatorState > GetGlobalOperatorState (ClientContext &context) const
virtual OperatorResultType Execute (ExecutionContext &context, DataChunk &input, DataChunk &chunk, GlobalOperatorState &gstate, OperatorState &state) const
virtual OperatorFinalizeResultType FinalExecute (ExecutionContext &context, DataChunk &chunk, GlobalOperatorState &gstate, OperatorState &state) const
virtual OperatorFinalResultType OperatorFinalize (Pipeline &pipeline, Event &event, ClientContext &context, OperatorFinalizeInput &input) const
virtual bool ParallelOperator () const
virtual bool RequiresFinalExecute () const
virtual bool RequiresOperatorFinalize () const
virtual OrderPreservationType OperatorOrder () const
 The influence the operator has on order (insertion order means no influence).
SourceResultType GetData (ExecutionContext &context, DataChunk &chunk, OperatorSourceInput &input) const
virtual OperatorPartitionData GetPartitionData (ExecutionContext &context, DataChunk &chunk, GlobalSourceState &gstate, LocalSourceState &lstate, const OperatorPartitionInfo &partition_info) const
virtual bool ParallelSource () const
virtual bool SupportsPartitioning (const OperatorPartitionInfo &partition_info) const
virtual OrderPreservationType SourceOrder () const
 The type of order emitted by the operator (as a source).
virtual ProgressData GetProgress (ClientContext &context, GlobalSourceState &gstate) const
 Returns the current progress percentage, or a negative value if progress bars are not supported.
virtual ProgressData GetSinkProgress (ClientContext &context, GlobalSinkState &gstate, const ProgressData source_progress) const
 Returns the current progress percentage, or a negative value if progress bars are not supported.
virtual InsertionOrderPreservingMap< string > ExtraSourceParams (GlobalSourceState &gstate, LocalSourceState &lstate) const
virtual void PrepareFinalize (ClientContext &context, GlobalSinkState &sink_state) const
virtual SinkNextBatchType NextBatch (ExecutionContext &context, OperatorSinkNextBatchInput &input) const
virtual OperatorPartitionInfo RequiredPartitionInfo () const
virtual bool SinkOrderDependent () const
virtual vector< const_reference< PhysicalOperator > > GetSources () const
bool AllSourcesSupportBatchIndex () const
virtual void BuildPipelines (Pipeline &current, MetaPipeline &meta_pipeline)
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const

Public Attributes

vector< unique_ptr< MergeIntoOperator > > actions
 List of all actions.
vector< MergeActionCondition > match_actions
 Sequence of match actions.
vector< MergeActionRange > action_ranges
 List of all actions that apply to a given action condition.
idx_t row_id_index
optional_idx source_marker
bool parallel
bool return_chunk
Public Attributes inherited from duckdb::PhysicalOperator
ArenaLinkedList< reference< PhysicalOperator > > children
 The child operators.
PhysicalOperatorType type
 The physical operator type.
vector< LogicalType > types
 The return types.
idx_t estimated_cardinality
 The estimated cardinality.
unique_ptr< GlobalSinkState > sink_state
 The global sink state.
unique_ptr< GlobalOperatorState > op_state
 The global operator state.
mutex lock
 Lock for (re)setting any of the operator states.

Static Public Attributes

static constexpr const PhysicalOperatorType TYPE = PhysicalOperatorType::MERGE_INTO
Static Public Attributes inherited from duckdb::PhysicalOperator
static constexpr const PhysicalOperatorType TYPE = PhysicalOperatorType::INVALID

Private Member Functions

idx_t GetIndex (MergeActionCondition condition) const
void ComputeMatches (MergeIntoLocalState &local_state, DataChunk &chunk) const

Additional Inherited Members

Static Public Member Functions inherited from duckdb::PhysicalOperator
static void SetEstimatedCardinality (InsertionOrderPreservingMap< string > &result, idx_t estimated_cardinality)
static idx_t GetMaxThreadMemory (ClientContext &context)
 The maximum amount of memory the operator should use per thread.
static OperatorCachingMode SelectOperatorCachingMode (ExecutionContext &context)
 Whether operator caching is allowed in the current execution context.

Constructor & Destructor Documentation

◆ PhysicalMergeInto()

duckdb::PhysicalMergeInto::PhysicalMergeInto ( PhysicalPlan & physical_plan,
vector< LogicalType > types,
map< MergeActionCondition, vector< unique_ptr< MergeIntoOperator > > > actions,
idx_t row_id_index,
optional_idx source_marker,
bool parallel,
bool return_chunk )
196348 : PhysicalOperator(physical_plan, PhysicalOperatorType::MERGE_INTO, std::move(types), 1),
196349 row_id_index(row_id_index), source_marker(source_marker), parallel(parallel_p), return_chunk(return_chunk_p) {
196350 map<MergeActionCondition, MergeActionRange> ranges;
196351 for (auto &entry : actions_p) {
196352 MergeActionRange range;
196353 range.condition = entry.first;
196354 range.start = actions.size();
196355 for (auto &action : entry.second) {
196356 actions.push_back(std::move(action));
196357 }
196358 range.end = actions.size();
196359 ranges.emplace(entry.first, range);
196360 }
196361 match_actions = {MergeActionCondition::WHEN_MATCHED, MergeActionCondition::WHEN_NOT_MATCHED_BY_TARGET,
196362 MergeActionCondition::WHEN_NOT_MATCHED_BY_SOURCE};
196363 for (idx_t i = 0; i < match_actions.size(); i++) {
196364 auto entry = ranges.find(match_actions[i]);
196365 MergeActionRange range;
196366 if (entry != ranges.end()) {
196367 range = entry->second;
196368 }
196369 range.condition = match_actions[i];
196370 action_ranges.push_back(range);
196371 }
196372}
vector< MergeActionRange > action_ranges
List of all actions that apply to a given action condition.
Definition duckdb.cpp:196226
vector< MergeActionCondition > match_actions
Sequence of match actions.
Definition duckdb.cpp:196224
vector< unique_ptr< MergeIntoOperator > > actions
List of all actions.
Definition duckdb.cpp:196222
vector< LogicalType > types
The return types.
Definition duckdb.hpp:22152
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

Member Function Documentation

◆ GetGlobalSourceState()

unique_ptr< GlobalSourceState > duckdb::PhysicalMergeInto::GetGlobalSourceState ( ClientContext & context) const
overridevirtual

Reimplemented from duckdb::PhysicalOperator.

196786 {
196787 return make_uniq<MergeGlobalSourceState>(context, *this);
196788}

◆ GetLocalSourceState()

unique_ptr< LocalSourceState > duckdb::PhysicalMergeInto::GetLocalSourceState ( ExecutionContext & context,
GlobalSourceState & gstate ) const
overridevirtual

Reimplemented from duckdb::PhysicalOperator.

196791 {
196792 return make_uniq<MergeLocalSourceState>(context, *this, gstate.Cast<MergeGlobalSourceState>());
196793}

◆ GetDataInternal()

SourceResultType duckdb::PhysicalMergeInto::GetDataInternal ( ExecutionContext & context,
DataChunk & chunk,
OperatorSourceInput & input ) const
overridevirtual

Reimplemented from duckdb::PhysicalOperator.

196796 {
196797 auto &g = sink_state->Cast<MergeIntoGlobalState>();
196798 if (!return_chunk) {
196799 chunk.SetCardinality(1);
196800 chunk.SetValue(0, 0, Value::BIGINT(NumericCast<int64_t>(g.merged_count.load())));
196801 return SourceResultType::FINISHED;
196802 }
196803 auto &gstate = input.global_state.Cast<MergeGlobalSourceState>();
196804 auto &lstate = input.local_state.Cast<MergeLocalSourceState>();
196805 chunk.Reset();
196806 for (; lstate.index < actions.size(); lstate.index++) {
196807 auto &action = *actions[lstate.index];
196808 if (!action.op) {
196809 // no action to scan from
196810 continue;
196811 }
196812 // found a good one
196813 break;
196814 }
196815 if (lstate.index < actions.size()) {
196816 auto &action = *actions[lstate.index];
196817
196818 auto &child_gstate = *gstate.global_states[lstate.index];
196819 auto &child_lstate = *lstate.local_states[lstate.index];
196820 OperatorSourceInput source_input {child_gstate, child_lstate, input.interrupt_state};
196821
196822 lstate.scan_chunk.Reset();
196823 auto result = action.op->GetData(context, lstate.scan_chunk, source_input);
196824 if (lstate.scan_chunk.size() > 0) {
196825 // construct the result chunk
196826 for (idx_t c = 0; c < lstate.scan_chunk.ColumnCount(); c++) {
196827 chunk.data[c].Reference(lstate.scan_chunk.data[c]);
196828 }
196829 // set the merge action
196830 string merge_action_name;
196831 switch (action.action_type) {
196832 case MergeActionType::MERGE_UPDATE:
196833 merge_action_name = "UPDATE";
196834 break;
196835 case MergeActionType::MERGE_INSERT:
196836 merge_action_name = "INSERT";
196837 break;
196838 case MergeActionType::MERGE_DELETE:
196839 merge_action_name = "DELETE";
196840 break;
196841 default:
196842 throw InternalException("Unsupported merge action for RETURNING");
196843 }
196844 Value merge_action(merge_action_name);
196845 chunk.data.back().Reference(merge_action);
196846 chunk.SetCardinality(lstate.scan_chunk.size());
196847 }
196848
196849 if (result != SourceResultType::FINISHED) {
196850 return result;
196851 } else {
196852 lstate.index++;
196853 if (lstate.index < actions.size()) {
196854 return SourceResultType::HAVE_MORE_OUTPUT;
196855 } else {
196856 return SourceResultType::FINISHED;
196857 }
196858 }
196859 }
196860 return SourceResultType::FINISHED;
196861}
DUCKDB_API void Reset()
Definition duckdb.cpp:116840
vector< Vector > data
The vectors owned by the DataChunk.
Definition duckdb.hpp:11445
unique_ptr< GlobalSinkState > sink_state
The global sink state.
Definition duckdb.hpp:22157
static DUCKDB_API Value BIGINT(int64_t value)
Create a bigint Value from a specified value.
Definition duckdb.cpp:132146

◆ IsSource()

bool duckdb::PhysicalMergeInto::IsSource ( ) const
inlineoverridevirtual

Reimplemented from duckdb::PhysicalOperator.

196240 {
196241 return true;
196242 }

◆ GetGlobalSinkState()

unique_ptr< GlobalSinkState > duckdb::PhysicalMergeInto::GetGlobalSinkState ( ClientContext & context) const
overridevirtual

Reimplemented from duckdb::PhysicalOperator.

196597 {
196598 return make_uniq<MergeIntoGlobalState>(context, *this);
196599}

◆ GetLocalSinkState()

unique_ptr< LocalSinkState > duckdb::PhysicalMergeInto::GetLocalSinkState ( ExecutionContext & context) const
overridevirtual

Reimplemented from duckdb::PhysicalOperator.

196601 {
196602 return make_uniq<MergeIntoLocalState>(context, *this);
196603}

◆ Sink()

SinkResultType duckdb::PhysicalMergeInto::Sink ( ExecutionContext & context,
DataChunk & chunk,
OperatorSinkInput & input ) const
overridevirtual

The sink method is called constantly with new input, as long as new input is available. Note that this method CAN be called in parallel, proper locking is needed when accessing dat a inside the GlobalSinkState.

Reimplemented from duckdb::PhysicalOperator.

196668 {
196669 auto &global_state = input.global_state.Cast<MergeIntoGlobalState>();
196670 auto &local_state = input.local_state.Cast<MergeIntoLocalState>();
196671
196672 auto &match_results = local_state.match_results;
196673 auto &computed_matches = local_state.sink_state.computed_matches;
196674 auto &match_idx = local_state.sink_state.match_idx;
196675 auto &index_in_match = local_state.sink_state.index_in_match;
196676 auto &match_initialized = local_state.sink_state.match_initialized;
196677 auto &current_sel = local_state.sink_state.current_sel;
196678 auto &current_count = local_state.sink_state.current_count;
196679 if (!computed_matches) {
196680 // we haven't figured out which rows have which types of matches - compute them
196681 ComputeMatches(local_state, chunk);
196682
196683 // set up the state so we can prepare sinking into the relevant operators
196684 computed_matches = true;
196685 match_idx = 0;
196686 index_in_match = 0;
196687 match_initialized = false;
196688 }
196689 // now slice and call sink for each of the match conditions
196690 for (; match_idx < 3; match_idx++) {
196691 auto &match_result = match_results[match_idx];
196692 if (match_result.count == 0) {
196693 // no matches for this action
196694 continue;
196695 }
196696 if (!match_initialized) {
196697 current_sel = SelectionVector();
196698 current_count = match_result.count;
196699 match_initialized = true;
196700 }
196701 auto match_range_index = GetIndex(match_actions[match_idx]);
196702 auto result = global_state.Sink(context, *match_result.chunk, local_state, input,
196703 action_ranges[match_range_index], index_in_match);
196704 if (result == SinkResultType::BLOCKED) {
196705 return SinkResultType::BLOCKED;
196706 }
196707 // move to next match action
196708 index_in_match = 0;
196709 match_initialized = false;
196710 }
196711 // finished - prepare for next match
196712 computed_matches = false;
196713 return SinkResultType::NEED_MORE_INPUT;
196714}

◆ Combine()

SinkCombineResultType duckdb::PhysicalMergeInto::Combine ( ExecutionContext & context,
OperatorSinkCombineInput & input ) const
overridevirtual

The combine is called when a single thread has completed execution of its part of the pipeline, it is the final time that a specific LocalSinkState is accessible. This method can be called in parallel while other Sink() or Combine() calls are active on the same GlobalSinkState.

Reimplemented from duckdb::PhysicalOperator.

196716 {
196717 auto &global_state = input.global_state.Cast<MergeIntoGlobalState>();
196718 auto &local_state = input.local_state.Cast<MergeIntoLocalState>();
196719
196720 auto result = global_state.Combine(context, local_state, input);
196721 if (result == SinkCombineResultType::FINISHED) {
196722 global_state.merged_count += local_state.merged_count;
196723 }
196724 return result;
196725}

◆ Finalize()

SinkFinalizeType duckdb::PhysicalMergeInto::Finalize ( Pipeline & pipeline,
Event & event,
ClientContext & context,
OperatorSinkFinalizeInput & input ) const
overridevirtual

The finalize is called when ALL threads are finished execution. It is called only once per pipeline, and is entirely single threaded. If Finalize returns SinkResultType::Finished, the sink is marked as finished

Reimplemented from duckdb::PhysicalOperator.

196728 {
196729 auto &global_state = input.global_state.Cast<MergeIntoGlobalState>();
196730 return global_state.Finalize(pipeline, event, context, input);
196731}

◆ IsSink()

bool duckdb::PhysicalMergeInto::IsSink ( ) const
inlineoverridevirtual

Reimplemented from duckdb::PhysicalOperator.

196253 {
196254 return true;
196255 }

◆ ParallelSink()

bool duckdb::PhysicalMergeInto::ParallelSink ( ) const
inlineoverridevirtual

Reimplemented from duckdb::PhysicalOperator.

196256 {
196257 return parallel;
196258 }

◆ GetIndex()

idx_t duckdb::PhysicalMergeInto::GetIndex ( MergeActionCondition condition) const
private
196605 {
196606 for (idx_t i = 0; i < match_actions.size(); ++i) {
196607 if (match_actions[i] == condition) {
196608 return i;
196609 }
196610 }
196611 throw InternalException("Unsupported match action condition");
196612}

◆ ComputeMatches()

void duckdb::PhysicalMergeInto::ComputeMatches ( MergeIntoLocalState & local_state,
DataChunk & chunk ) const
private
196614 {
196615 auto &match_results = local_state.match_results;
196616
196617 // for each row, figure out if we have generated a match or not
196618 auto &matched = match_results[0];
196619 auto &not_matched = match_results[1];
196620 auto &not_matched_by_source = match_results[2];
196621
196622 matched.count = 0;
196623 not_matched.count = 0;
196624 not_matched_by_source.count = 0;
196625
196626 UnifiedVectorFormat row_id_data;
196627 chunk.data[row_id_index].ToUnifiedFormat(chunk.size(), row_id_data);
196628 if (source_marker.IsValid()) {
196629 // source marker - check both row id and source marker
196630 UnifiedVectorFormat source_marker_data;
196631 chunk.data[source_marker.GetIndex()].ToUnifiedFormat(chunk.size(), source_marker_data);
196632 for (idx_t i = 0; i < chunk.size(); i++) {
196633 if (!source_marker_data.validity.RowIsValid(source_marker_data.sel->get_index(i))) {
196634 // source marker is NULL - no source match
196635 not_matched_by_source.sel.set_index(not_matched_by_source.count++, i);
196636 } else if (!row_id_data.validity.RowIsValid(row_id_data.sel->get_index(i))) {
196637 // target marker is NULL - no target match
196638 not_matched.sel.set_index(not_matched.count++, i);
196639 } else {
196640 // match
196641 matched.sel.set_index(matched.count++, i);
196642 }
196643 }
196644 } else {
196645 // no source marker - only check row-ids
196646 for (idx_t i = 0; i < chunk.size(); i++) {
196647 auto idx = row_id_data.sel->get_index(i);
196648 if (row_id_data.validity.RowIsValid(idx)) {
196649 // match
196650 matched.sel.set_index(matched.count++, i);
196651 } else {
196652 // no match
196653 not_matched.sel.set_index(not_matched.count++, i);
196654 }
196655 }
196656 }
196657
196658 // reset and slice chunks
196659 for (auto &match : match_results) {
196660 if (match.count == 0) {
196661 continue;
196662 }
196663 match.chunk->Reset();
196664 match.chunk->Slice(chunk, match.sel, match.count);
196665 }
196666}

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