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

Public Member Functions

 PhysicalRecursiveCTE (PhysicalPlan &physical_plan, string ctename, idx_t table_index, vector< LogicalType > types, bool union_all, PhysicalOperator &top, PhysicalOperator &bottom, idx_t estimated_cardinality)
SourceResultType GetDataInternal (ExecutionContext &context, DataChunk &chunk, OperatorSourceInput &input) const override
bool IsSource () const override
SinkResultType Sink (ExecutionContext &context, DataChunk &chunk, OperatorSinkInput &input) const override
unique_ptr< GlobalSinkState > GetGlobalSinkState (ClientContext &context) const override
bool IsSink () const override
bool ParallelSink () const override
InsertionOrderPreservingMap< string > ParamsToString () const override
void BuildPipelines (Pipeline &current, MetaPipeline &meta_pipeline) override
vector< const_reference< PhysicalOperator > > GetSources () 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 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).
virtual unique_ptr< LocalSourceState > GetLocalSourceState (ExecutionContext &context, GlobalSourceState &gstate) const
virtual unique_ptr< GlobalSourceState > GetGlobalSourceState (ClientContext &context) const
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 SinkCombineResultType Combine (ExecutionContext &context, OperatorSinkCombineInput &input) const
virtual void PrepareFinalize (ClientContext &context, GlobalSinkState &sink_state) const
virtual SinkFinalizeType Finalize (Pipeline &pipeline, Event &event, ClientContext &context, OperatorSinkFinalizeInput &input) const
virtual SinkNextBatchType NextBatch (ExecutionContext &context, OperatorSinkNextBatchInput &input) const
virtual unique_ptr< LocalSinkState > GetLocalSinkState (ExecutionContext &context) const
virtual OperatorPartitionInfo RequiredPartitionInfo () const
virtual bool SinkOrderDependent () const
bool AllSourcesSupportBatchIndex () const
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const

Public Attributes

string ctename
idx_t table_index
bool ref_recurring
bool union_all
shared_ptr< ColumnDataCollection > working_table
shared_ptr< MetaPipeline > recursive_meta_pipeline
bool using_key = false
shared_ptr< ColumnDataCollection > recurring_table
vector< LogicalType > payload_types
vector< LogicalType > distinct_types
vector< idx_t > payload_idx
vector< idx_t > distinct_idx
vector< unique_ptr< BoundAggregateExpression > > payload_aggregates
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::RECURSIVE_CTE
Static Public Attributes inherited from duckdb::PhysicalOperator
static constexpr const PhysicalOperatorType TYPE = PhysicalOperatorType::INVALID

Private Member Functions

idx_t ProbeHT (DataChunk &chunk, RecursiveCTEState &state) const
 Probe Hash Table and eliminate duplicate rows.
void ExecuteRecursivePipelines (ExecutionContext &context) 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

◆ PhysicalRecursiveCTE()

duckdb::PhysicalRecursiveCTE::PhysicalRecursiveCTE ( PhysicalPlan & physical_plan,
string ctename,
idx_t table_index,
vector< LogicalType > types,
bool union_all,
PhysicalOperator & top,
PhysicalOperator & bottom,
idx_t estimated_cardinality )
200625 : PhysicalOperator(physical_plan, PhysicalOperatorType::RECURSIVE_CTE, std::move(types), estimated_cardinality),
200626 ctename(std::move(ctename)), table_index(table_index), union_all(union_all) {
200627 children.push_back(top);
200628 children.push_back(bottom);
200629}
vector< LogicalType > types
The return types.
Definition duckdb.hpp:22152
idx_t estimated_cardinality
The estimated cardinality.
Definition duckdb.hpp:22154
ArenaLinkedList< reference< PhysicalOperator > > children
The child operators.
Definition duckdb.hpp:22148

◆ ~PhysicalRecursiveCTE()

duckdb::PhysicalRecursiveCTE::~PhysicalRecursiveCTE ( )
override
200631 {
200632}

Member Function Documentation

◆ GetDataInternal()

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

Reimplemented from duckdb::PhysicalOperator.

200729 {
200730 auto &gstate = sink_state->Cast<RecursiveCTEState>();
200731 if (!gstate.initialized) {
200732 if (!using_key) {
200733 gstate.intermediate_table.InitializeScan(gstate.scan_state);
200734 } else {
200735 gstate.ht->InitializeScan(gstate.ht_scan_state);
200736 recurring_table->InitializeScan(gstate.scan_state);
200737 }
200738 gstate.finished_scan = false;
200739 gstate.initialized = true;
200740 }
200741 while (chunk.size() == 0) {
200742 if (!gstate.finished_scan) {
200743 if (!using_key) {
200744 // scan any chunks we have collected so far
200745 gstate.intermediate_table.Scan(gstate.scan_state, chunk);
200746 }
200747 if (chunk.size() == 0) {
200748 gstate.finished_scan = true;
200749 } else {
200750 break;
200751 }
200752 } else {
200753 // we have run out of chunks
200754 // now we need to recurse
200755 // we set up the working table as the data we gathered in this iteration of the recursion
200756
200757 // After an iteration, we reset the recurring table
200758 // and fill it up with the new hash table rows for the next iteration.
200759 if (using_key && ref_recurring && gstate.intermediate_table.Count() != 0) {
200760 recurring_table->Reset();
200761 AggregateHTScanState scan_state;
200762 gstate.ht->InitializeScan(scan_state);
200763
200764 // Initialise the DataChunks to read the resulting rows.
200765 // One DataChunk for the payload, one for the keys.
200766 // Create a new DataChunk to store the result.
200767 DataChunk result;
200768 DataChunk payload_rows;
200769 DataChunk distinct_rows;
200770 distinct_rows.Initialize(Allocator::DefaultAllocator(), distinct_types);
200771 if (!payload_types.empty()) {
200772 payload_rows.Initialize(Allocator::DefaultAllocator(), payload_types);
200773 }
200774 result.Initialize(Allocator::DefaultAllocator(), chunk.GetTypes());
200775
200776 while (gstate.ht->Scan(scan_state, distinct_rows, payload_rows)) {
200777 // Populate the result DataChunk with the keys and the payload.
200778 PopulateChunk(result, distinct_rows, distinct_idx, false);
200779 PopulateChunk(result, payload_rows, payload_idx, false);
200780 // Append the result to the recurring table.
200781 recurring_table->Append(result);
200782 }
200783 } else if (ref_recurring && gstate.intermediate_table.Count() != 0) {
200784 // we need to populate the recurring table from the intermediate table
200785 // careful: we can not just use Combine here, because this destroys the intermediate table
200786 // instead we need to scan and append to create a copy
200787 // Note: as we are in the "normal" recursion case here, not the USING KEY case,
200788 // we can just scan the intermediate table directly, instead of going through the HT
200789 ColumnDataScanState scan_state;
200790 gstate.intermediate_table.InitializeScan(scan_state);
200791 DataChunk result;
200792 result.Initialize(Allocator::DefaultAllocator(), chunk.GetTypes());
200793
200794 while (gstate.intermediate_table.Scan(scan_state, result)) {
200795 recurring_table->Append(result);
200796 }
200797 }
200798
200799 working_table->Reset();
200800 working_table->Combine(gstate.intermediate_table);
200801 // and we clear the intermediate table
200802 gstate.finished_scan = false;
200803 gstate.intermediate_table.Reset();
200804 // now we need to re-execute all of the pipelines that depend on the recursion
200805 ExecuteRecursivePipelines(context);
200806
200807 // check if we obtained any results
200808 // if not, we are done
200809 if (gstate.intermediate_table.Count() == 0) {
200810 gstate.finished_scan = true;
200811 if (using_key) {
200812 // Initialise the DataChunks to read the ht.
200813 // One DataChunk for payload, one for keys.
200814 DataChunk payload_rows;
200815 DataChunk distinct_rows;
200816 distinct_rows.Initialize(Allocator::DefaultAllocator(), distinct_types);
200817 if (!payload_types.empty()) {
200818 payload_rows.Initialize(Allocator::DefaultAllocator(), payload_types);
200819 }
200820
200821 gstate.ht->Scan(gstate.ht_scan_state, distinct_rows, payload_rows);
200822 PopulateChunk(chunk, distinct_rows, distinct_idx, false);
200823 PopulateChunk(chunk, payload_rows, payload_idx, false);
200824 }
200825 break;
200826 }
200827 if (!using_key) {
200828 // set up the scan again
200829 gstate.intermediate_table.InitializeScan(gstate.scan_state);
200830 }
200831 }
200832 }
200833
200834 return chunk.size() == 0 ? SourceResultType::FINISHED : SourceResultType::HAVE_MORE_OUTPUT;
200835}
DUCKDB_API void Initialize(ClientContext &context, const vector< LogicalType > &types, idx_t capacity=STANDARD_VECTOR_SIZE)
Definition duckdb.cpp:116791
DUCKDB_API vector< LogicalType > GetTypes() const
Returns a list of types of the vectors of this data chunk.
Definition duckdb.cpp:116995
unique_ptr< GlobalSinkState > sink_state
The global sink state.
Definition duckdb.hpp:22157

◆ IsSource()

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

Reimplemented from duckdb::PhysicalOperator.

200572 {
200573 return true;
200574 }

◆ Sink()

SinkResultType duckdb::PhysicalRecursiveCTE::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.

200693 {
200694 auto &gstate = input.global_state.Cast<RecursiveCTEState>();
200695
200696 lock_guard<mutex> guard(gstate.intermediate_table_lock);
200697 if (!using_key) {
200698 if (!union_all) {
200699 idx_t match_count = ProbeHT(chunk, gstate);
200700 if (match_count > 0) {
200701 gstate.intermediate_table.Append(chunk);
200702 }
200703 } else {
200704 gstate.intermediate_table.Append(chunk);
200705 }
200706 } else {
200707 // Split incoming DataChunk into payload and keys
200708 DataChunk distinct_rows;
200709 distinct_rows.Initialize(Allocator::DefaultAllocator(), distinct_types);
200710 PopulateChunk(distinct_rows, chunk, distinct_idx, true);
200711 DataChunk payload_rows;
200712 if (!payload_types.empty()) {
200713 payload_rows.Initialize(Allocator::DefaultAllocator(), payload_types);
200714 }
200715 PopulateChunk(payload_rows, chunk, payload_idx, true);
200716
200717 // Add the chunk to the hash table and append it to the intermediate table
200718 gstate.ht->AddChunk(distinct_rows, payload_rows, AggregateType::NON_DISTINCT);
200719 gstate.intermediate_table.Append(chunk);
200720 }
200721
200722 return SinkResultType::NEED_MORE_INPUT;
200723}
idx_t ProbeHT(DataChunk &chunk, RecursiveCTEState &state) const
Probe Hash Table and eliminate duplicate rows.
Definition duckdb.cpp:200666
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:

◆ GetGlobalSinkState()

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

Reimplemented from duckdb::PhysicalOperator.

200662 {
200663 return make_uniq<RecursiveCTEState>(context, *this);
200664}

◆ IsSink()

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

Reimplemented from duckdb::PhysicalOperator.

200582 {
200583 return true;
200584 }

◆ ParallelSink()

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

Reimplemented from duckdb::PhysicalOperator.

200586 {
200587 return true;
200588 }

◆ ParamsToString()

InsertionOrderPreservingMap< string > duckdb::PhysicalRecursiveCTE::ParamsToString ( ) const
overridevirtual

Reimplemented from duckdb::PhysicalOperator.

200936 {
200937 InsertionOrderPreservingMap<string> result;
200938 result["CTE Name"] = ctename;
200939 result["Table Index"] = StringUtil::Format("%llu", table_index);
200940 SetEstimatedCardinality(result, estimated_cardinality);
200941 return result;
200942}
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002

◆ BuildPipelines()

void duckdb::PhysicalRecursiveCTE::BuildPipelines ( Pipeline & current,
MetaPipeline & meta_pipeline )
overridevirtual

Reimplemented from duckdb::PhysicalOperator.

200897 {
200898 op_state.reset();
200899 sink_state.reset();
200900 recursive_meta_pipeline.reset();
200901
200902 auto &state = meta_pipeline.GetState();
200903 state.SetPipelineSource(current, *this);
200904
200905 auto &executor = meta_pipeline.GetExecutor();
200906 executor.AddRecursiveCTE(*this);
200907
200908 // the LHS of the recursive CTE is our initial state
200909 auto &initial_state_pipeline = meta_pipeline.CreateChildMetaPipeline(current, *this);
200910 initial_state_pipeline.Build(children[0]);
200911
200912 // the RHS is the recursive pipeline
200913 recursive_meta_pipeline = make_shared_ptr<MetaPipeline>(executor, state, this);
200914 recursive_meta_pipeline->SetRecursiveCTE();
200915 recursive_meta_pipeline->Build(children[1]);
200916
200917 vector<const_reference<PhysicalOperator>> ops;
200918 GatherColumnDataScans(children[1], ops);
200919
200920 for (auto op : ops) {
200921 auto entry = state.cte_dependencies.find(op);
200922 if (entry == state.cte_dependencies.end()) {
200923 continue;
200924 }
200925 // this chunk scan introduces a dependency to the current pipeline
200926 // namely a dependency on the CTE pipeline to finish
200927 auto cte_dependency = entry->second.get().shared_from_this();
200928 current.AddDependency(cte_dependency);
200929 }
200930}
unique_ptr< GlobalOperatorState > op_state
The global operator state.
Definition duckdb.hpp:22159

◆ GetSources()

vector< const_reference< PhysicalOperator > > duckdb::PhysicalRecursiveCTE::GetSources ( ) const
overridevirtual

Reimplemented from duckdb::PhysicalOperator.

200932 {
200933 return {*this};
200934}

◆ ProbeHT()

idx_t duckdb::PhysicalRecursiveCTE::ProbeHT ( DataChunk & chunk,
RecursiveCTEState & state ) const
private

Probe Hash Table and eliminate duplicate rows.

200666 {
200667 Vector dummy_addresses(LogicalType::POINTER);
200668
200669 // Use the HT to eliminate duplicate rows
200670 idx_t new_group_count = state.ht->FindOrCreateGroups(chunk, dummy_addresses, state.new_groups);
200671
200672 // we only return entries we have not seen before (i.e. new groups)
200673 chunk.Slice(state.new_groups, new_group_count);
200674
200675 return new_group_count;
200676}
Here is the caller graph for this function:

◆ ExecuteRecursivePipelines()

void duckdb::PhysicalRecursiveCTE::ExecuteRecursivePipelines ( ExecutionContext & context) const
private
200837 {
200838 if (!recursive_meta_pipeline) {
200839 throw InternalException("Missing meta pipeline for recursive CTE");
200840 }
200841 D_ASSERT(recursive_meta_pipeline->HasRecursiveCTE());
200842
200843 // get and reset pipelines
200844 vector<shared_ptr<Pipeline>> pipelines;
200845 recursive_meta_pipeline->GetPipelines(pipelines, true);
200846 for (auto &pipeline : pipelines) {
200847 auto sink = pipeline->GetSink();
200848 if (sink.get() != this) {
200849 sink->sink_state.reset();
200850 }
200851 for (auto &op_ref : pipeline->GetOperators()) {
200852 auto &op = op_ref.get();
200853 op.op_state.reset();
200854 }
200855 pipeline->ClearSource();
200856 }
200857
200858 // get the MetaPipelines in the recursive_meta_pipeline and reschedule them
200859 vector<shared_ptr<MetaPipeline>> meta_pipelines;
200860 recursive_meta_pipeline->GetMetaPipelines(meta_pipelines, true, false);
200861 auto &executor = recursive_meta_pipeline->GetExecutor();
200862 vector<shared_ptr<Event>> events;
200863 executor.ReschedulePipelines(meta_pipelines, events);
200864
200865 while (true) {
200866 executor.WorkOnTasks();
200867 if (executor.HasError()) {
200868 executor.ThrowException();
200869 }
200870 bool finished = true;
200871 for (auto &event : events) {
200872 if (!event->IsFinished()) {
200873 finished = false;
200874 break;
200875 }
200876 }
200877 if (finished) {
200878 // all pipelines finished: done!
200879 break;
200880 }
200881 }
200882}

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