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

Public Member Functions

 PhysicalBatchCopyToFile (PhysicalPlan &physical_plan, vector< LogicalType > types, CopyFunction function, unique_ptr< FunctionData > bind_data, 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
SinkCombineResultType Combine (ExecutionContext &context, OperatorSinkCombineInput &input) const override
SinkFinalizeType Finalize (Pipeline &pipeline, Event &event, ClientContext &context, OperatorSinkFinalizeInput &input) const override
unique_ptr< LocalSinkState > GetLocalSinkState (ExecutionContext &context) const override
unique_ptr< GlobalSinkState > GetGlobalSinkState (ClientContext &context) const override
SinkNextBatchType NextBatch (ExecutionContext &context, OperatorSinkNextBatchInput &input) const override
OperatorPartitionInfo RequiredPartitionInfo () const override
bool IsSink () const override
bool ParallelSink () const override
void AddLocalBatch (ClientContext &context, GlobalSinkState &gstate, LocalSinkState &state) const
void AddRawBatchData (ClientContext &context, GlobalSinkState &gstate_p, idx_t batch_index, unique_ptr< FixedRawBatchData > collection) const
void RepartitionBatches (ClientContext &context, GlobalSinkState &gstate_p, idx_t min_index, bool final=false) const
void FlushBatchData (ClientContext &context, GlobalSinkState &gstate_p) const
bool ExecuteTask (ClientContext &context, GlobalSinkState &gstate_p) const
void ExecuteTasks (ClientContext &context, GlobalSinkState &gstate_p) const
SinkFinalizeType FinalFlush (ClientContext &context, GlobalSinkState &gstate_p) const
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).
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 void PrepareFinalize (ClientContext &context, GlobalSinkState &sink_state) 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

CopyFunction function
unique_ptr< FunctionData > bind_data
string file_path
bool use_tmp_file
CopyFunctionReturnType return_type
bool write_empty_file
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::BATCH_COPY_TO_FILE
Static Public Attributes inherited from duckdb::PhysicalOperator
static constexpr const PhysicalOperatorType TYPE = PhysicalOperatorType::INVALID

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

◆ PhysicalBatchCopyToFile()

duckdb::PhysicalBatchCopyToFile::PhysicalBatchCopyToFile ( PhysicalPlan & physical_plan,
vector< LogicalType > types,
CopyFunction function,
unique_ptr< FunctionData > bind_data,
idx_t estimated_cardinality )
192026 : PhysicalOperator(physical_plan, PhysicalOperatorType::BATCH_COPY_TO_FILE, std::move(types),
192028 function(std::move(function_p)), bind_data(std::move(bind_data_p)) {
192029 if (!function.flush_batch || !function.prepare_batch) {
192030 throw InternalException("PhysicalFixedBatchCopy created for copy function that does not have "
192031 "prepare_batch/flush_batch defined");
192032 }
192033}
vector< LogicalType > types
The return types.
Definition duckdb.hpp:22152
idx_t estimated_cardinality
The estimated cardinality.
Definition duckdb.hpp:22154

Member Function Documentation

◆ GetDataInternal()

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

Reimplemented from duckdb::PhysicalOperator.

192642 {
192643 auto &g = sink_state->Cast<FixedBatchCopyGlobalState>();
192644 auto fp = use_tmp_file ? PhysicalCopyToFile::GetNonTmpFile(context.client, file_path) : file_path;
192645 switch (return_type) {
192646 case CopyFunctionReturnType::CHANGED_ROWS:
192647 chunk.SetValue(0, 0, Value::BIGINT(NumericCast<int64_t>(g.rows_copied.load())));
192648 chunk.SetCardinality(1);
192649 break;
192650 case CopyFunctionReturnType::CHANGED_ROWS_AND_FILE_LIST: {
192651 vector<Value> file_list;
192652 if (g.global_state) {
192653 file_list.emplace_back(std::move(fp));
192654 }
192655 chunk.SetValue(0, 0, Value::BIGINT(NumericCast<int64_t>(g.rows_copied.load())));
192656 chunk.SetValue(1, 0, Value::LIST(LogicalType::VARCHAR, std::move(file_list)));
192657 chunk.SetCardinality(1);
192658 break;
192659 }
192660 case CopyFunctionReturnType::WRITTEN_FILE_STATISTICS: {
192661 if (g.written_file_info) {
192662 g.written_file_info->file_path = std::move(fp);
192663 PhysicalCopyToFile::ReturnStatistics(chunk, 0, *g.written_file_info);
192664 chunk.SetCardinality(1);
192665 }
192666 break;
192667 }
192668 default:
192669 throw NotImplementedException("Unknown CopyFunctionReturnType");
192670 }
192671
192672 return SourceResultType::FINISHED;
192673}
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
static DUCKDB_API Value LIST(const LogicalType &child_type, vector< Value > values)
Create a list value with the given entries.
Definition duckdb.cpp:132555

◆ IsSource()

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

Reimplemented from duckdb::PhysicalOperator.

191670 {
191671 return true;
191672 }

◆ Sink()

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

192178 {
192179 auto &state = input.local_state.Cast<FixedBatchCopyLocalState>();
192180 auto &gstate = input.global_state.Cast<FixedBatchCopyGlobalState>();
192181 auto &memory_manager = gstate.memory_manager;
192182 auto batch_index = state.partition_info.batch_index.GetIndex();
192183 if (state.current_task == FixedBatchCopyState::PROCESSING_TASKS) {
192184 ExecuteTasks(context.client, gstate);
192185 FlushBatchData(context.client, gstate);
192186
192187 if (!memory_manager.IsMinimumBatchIndex(batch_index) && memory_manager.OutOfMemory(batch_index)) {
192188 auto guard = memory_manager.Lock();
192189 if (!memory_manager.IsMinimumBatchIndex(batch_index)) {
192190 // no tasks to process, we are not the minimum batch index and we have no memory available to buffer
192191 // block the task for now
192192 return memory_manager.BlockSink(guard, input.interrupt_state);
192193 }
192194 }
192195 state.current_task = FixedBatchCopyState::SINKING_DATA;
192196 }
192197 if (!memory_manager.IsMinimumBatchIndex(batch_index)) {
192198 memory_manager.UpdateMinBatchIndex(state.partition_info.min_batch_index.GetIndex());
192199
192200 // we are not processing the current min batch index
192201 // check if we have exceeded the maximum number of unflushed rows
192202 if (memory_manager.OutOfMemory(batch_index)) {
192203 // out-of-memory - stop sinking chunks and instead assist in processing tasks for the minimum batch index
192204 state.current_task = FixedBatchCopyState::PROCESSING_TASKS;
192205 return Sink(context, chunk, input);
192206 }
192207 }
192208 if (!write_empty_file) {
192209 // if we are not writing empty files - initialize after we have received rows
192210 gstate.Initialize(context.client, *this);
192211 }
192212 if (!state.collection) {
192213 state.InitializeCollection(context.client, *this);
192214 state.batch_index = batch_index;
192215 }
192216 state.rows_copied += chunk.size();
192217 state.collection->Append(state.append_state, chunk);
192218 auto new_memory_usage = state.collection->AllocationSize();
192219 if (new_memory_usage > state.local_memory_usage) {
192220 // memory usage increased - add to global state
192221 memory_manager.IncreaseUnflushedMemory(new_memory_usage - state.local_memory_usage);
192222 state.local_memory_usage = new_memory_usage;
192223 } else if (new_memory_usage < state.local_memory_usage) {
192224 throw InternalException("PhysicalFixedBatchCopy - memory usage decreased somehow?");
192225 }
192226 return SinkResultType::NEED_MORE_INPUT;
192227}
SinkResultType Sink(ExecutionContext &context, DataChunk &chunk, OperatorSinkInput &input) const override
Definition duckdb.cpp:192177
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Combine()

SinkCombineResultType duckdb::PhysicalBatchCopyToFile::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.

192230 {
192231 auto &state = input.local_state.Cast<FixedBatchCopyLocalState>();
192232 auto &gstate = input.global_state.Cast<FixedBatchCopyGlobalState>();
192233 auto &memory_manager = gstate.memory_manager;
192234 gstate.rows_copied += state.rows_copied;
192235
192236 // add any final remaining local batches
192237 AddLocalBatch(context.client, gstate, state);
192238
192239 if (!gstate.any_finished) {
192240 // signal that this thread is finished processing batches and that we should move on to Finalize
192241 lock_guard<mutex> l(gstate.lock);
192242 gstate.any_finished = true;
192243 }
192244 memory_manager.UpdateMinBatchIndex(state.partition_info.min_batch_index.GetIndex());
192245 ExecuteTasks(context.client, gstate);
192246
192247 return SinkCombineResultType::FINISHED;
192248}
void AddLocalBatch(ClientContext &context, GlobalSinkState &gstate, LocalSinkState &state) const
Definition duckdb.cpp:192571
Here is the call graph for this function:

◆ Finalize()

SinkFinalizeType duckdb::PhysicalBatchCopyToFile::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.

192330 {
192331 auto &gstate = input.global_state.Cast<FixedBatchCopyGlobalState>();
192332 auto min_batch_index = idx_t(NumericLimits<int64_t>::Maximum());
192333 // repartition any remaining batches
192334 RepartitionBatches(context, input.global_state, min_batch_index, true);
192335 // check if we have multiple tasks to execute
192336 if (gstate.task_manager.TaskCount() <= 1) {
192337 // we don't - just execute the remaining task and finish flushing to disk
192338 ExecuteTasks(context, input.global_state);
192339 FinalFlush(context, input.global_state);
192340 } else {
192341 // we have multiple tasks remaining - launch an event to execute the tasks in parallel
192342 auto new_event = make_shared_ptr<ProcessRemainingBatchesEvent>(*this, gstate, pipeline, context);
192343 event.InsertEvent(std::move(new_event));
192344 }
192345 return SinkFinalizeType::READY;
192346}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ GetLocalSinkState()

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

Reimplemented from duckdb::PhysicalOperator.

192621 {
192622 return make_uniq<FixedBatchCopyLocalState>(function.copy_to_initialize_local(context, *bind_data));
192623}

◆ GetGlobalSinkState()

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

Reimplemented from duckdb::PhysicalOperator.

192625 {
192626 // request memory based on the minimum amount of memory per column
192627 auto minimum_memory_per_thread =
192628 FixedBatchCopyGlobalState::MINIMUM_MEMORY_PER_COLUMN_PER_THREAD * children[0].get().GetTypes().size();
192629 auto result = make_uniq<FixedBatchCopyGlobalState>(context, minimum_memory_per_thread);
192630 if (write_empty_file) {
192631 // if we are writing the file also if it is empty - initialize now
192632 result->Initialize(context, *this);
192633 }
192634 result->batch_size = function.desired_batch_size ? function.desired_batch_size(context, *bind_data) : 0;
192635 return std::move(result);
192636}
ArenaLinkedList< reference< PhysicalOperator > > children
The child operators.
Definition duckdb.hpp:22148

◆ NextBatch()

SinkNextBatchType duckdb::PhysicalBatchCopyToFile::NextBatch ( ExecutionContext & context,
OperatorSinkNextBatchInput & input ) const
overridevirtual

For sinks with RequiresBatchIndex set to true, when a new batch starts being processed this method is called This allows flushing of the current batch (e.g. to disk)

Reimplemented from duckdb::PhysicalOperator.

192604 {
192605 auto &lstate = input.local_state;
192606 auto &state = lstate.Cast<FixedBatchCopyLocalState>();
192607 auto &gstate = input.global_state.Cast<FixedBatchCopyGlobalState>();
192608 auto &memory_manager = gstate.memory_manager;
192609
192610 // add the previously finished batch (if any) to the state
192611 AddLocalBatch(context.client, gstate, state);
192612
192613 // update the minimum batch index
192614 memory_manager.UpdateMinBatchIndex(state.partition_info.min_batch_index.GetIndex());
192615 state.batch_index = lstate.partition_info.batch_index.GetIndex();
192616
192617 state.InitializeCollection(context.client, *this);
192618 return SinkNextBatchType::READY;
192619}
Here is the call graph for this function:

◆ RequiredPartitionInfo()

OperatorPartitionInfo duckdb::PhysicalBatchCopyToFile::RequiredPartitionInfo ( ) const
inlineoverridevirtual

Reimplemented from duckdb::PhysicalOperator.

191684 {
191685 return OperatorPartitionInfo::BatchIndex();
191686 }

◆ IsSink()

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

Reimplemented from duckdb::PhysicalOperator.

191688 {
191689 return true;
191690 }

◆ ParallelSink()

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

Reimplemented from duckdb::PhysicalOperator.

191692 {
191693 return true;
191694 }

◆ AddLocalBatch()

void duckdb::PhysicalBatchCopyToFile::AddLocalBatch ( ClientContext & context,
GlobalSinkState & gstate,
LocalSinkState & state ) const

Execute a single repartition task

Flush batch data to disk (if any is ready)

192572 {
192573 auto &state = lstate.Cast<FixedBatchCopyLocalState>();
192574 auto &gstate = gstate_p.Cast<FixedBatchCopyGlobalState>();
192575 auto &memory_manager = gstate.memory_manager;
192576 if (!state.collection || state.collection->Count() == 0) {
192577 return;
192578 }
192579 // we finished processing this batch
192580 // start flushing data
192581 auto min_batch_index = state.partition_info.min_batch_index.GetIndex();
192582 // push the raw batch data into the set of unprocessed batches
192583 auto raw_batch = make_uniq<FixedRawBatchData>(state.local_memory_usage, std::move(state.collection));
192584 AddRawBatchData(context, gstate, state.batch_index.GetIndex(), std::move(raw_batch));
192585 // attempt to repartition to our desired batch size
192586 RepartitionBatches(context, gstate, min_batch_index);
192587 // unblock tasks so they can help process batches (if any are blocked)
192588 bool any_unblocked;
192589 {
192590 auto guard = memory_manager.Lock();
192591 any_unblocked = memory_manager.UnblockTasks(guard);
192592 }
192593 // if any threads were unblocked they can pick up execution of the tasks
192594 // otherwise we will execute a task and flush here
192595 if (!any_unblocked) {
192597 ExecuteTask(context, gstate);
192599 FlushBatchData(context, gstate);
192600 }
192601}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ AddRawBatchData()

void duckdb::PhysicalBatchCopyToFile::AddRawBatchData ( ClientContext & context,
GlobalSinkState & gstate_p,
idx_t batch_index,
unique_ptr< FixedRawBatchData > collection ) const
192386 {
192387 auto &gstate = gstate_p.Cast<FixedBatchCopyGlobalState>();
192388
192389 // add the batch index to the set of raw batches
192390 lock_guard<mutex> l(gstate.lock);
192391 auto entry = gstate.raw_batches.insert(make_pair(batch_index, std::move(raw_batch)));
192392 if (!entry.second) {
192393 throw InternalException("Duplicate batch index %llu encountered in PhysicalFixedBatchCopy", batch_index);
192394 }
192395}

◆ RepartitionBatches()

void duckdb::PhysicalBatchCopyToFile::RepartitionBatches ( ClientContext & context,
GlobalSinkState & gstate_p,
idx_t min_index,
bool final = false ) const
192406 {
192407 auto &gstate = gstate_p.Cast<FixedBatchCopyGlobalState>();
192408 auto &task_manager = gstate.task_manager;
192409
192410 // repartition batches until the min index is reached
192411 lock_guard<mutex> l(gstate.lock);
192412 if (gstate.raw_batches.empty()) {
192413 return;
192414 }
192415 if (!final) {
192416 if (gstate.any_finished) {
192417 // we only repartition in ::NextBatch if all threads are still busy processing batches
192418 // otherwise we might end up repartitioning a lot of data with only a few threads remaining
192419 // which causes erratic performance
192420 return;
192421 }
192422 // if this is not the final flush we first check if we have enough data to merge past the batch threshold
192423 idx_t candidate_rows = 0;
192424 for (auto entry = gstate.raw_batches.begin(); entry != gstate.raw_batches.end(); entry++) {
192425 if (entry->first >= min_index) {
192426 // we have exceeded the minimum batch
192427 break;
192428 }
192429 candidate_rows += entry->second->collection->Count();
192430 }
192431 if (candidate_rows < gstate.batch_size) {
192432 // not enough rows - cancel!
192433 return;
192434 }
192435 }
192436 // gather all collections we can repartition
192437 idx_t max_batch_index = 0;
192438 vector<unique_ptr<FixedRawBatchData>> raw_batches;
192439 for (auto entry = gstate.raw_batches.begin(); entry != gstate.raw_batches.end();) {
192440 if (entry->first >= min_index) {
192441 break;
192442 }
192443 max_batch_index = entry->first;
192444 raw_batches.push_back(std::move(entry->second));
192445 entry = gstate.raw_batches.erase(entry);
192446 }
192447 unique_ptr<FixedRawBatchData> append_batch;
192448 ColumnDataAppendState append_state;
192449 // now perform the actual repartitioning
192450 for (auto &current_batch : raw_batches) {
192451 if (!append_batch) {
192452 auto current_count = current_batch->collection->Count();
192453 if (CorrectSizeForBatch(current_count, gstate.batch_size)) {
192454 // the collection is ~approximately equal to the batch size (off by at most one vector)
192455 // use it directly
192456 task_manager.AddTask(
192457 make_uniq<PrepareBatchTask>(gstate.scheduled_batch_index++, std::move(current_batch)));
192458 current_batch.reset();
192459 } else if (current_count < gstate.batch_size) {
192460 // the collection is smaller than the batch size - use it as a starting point
192461 append_batch = std::move(current_batch);
192462 current_batch.reset();
192463 } else {
192464 // the collection is too large for a batch - we need to repartition
192465 // create an empty collection
192466 auto new_collection = make_uniq<ColumnDataCollection>(context, children[0].get().GetTypes());
192467 new_collection->SetPartitionIndex(0); // Makes the buffer manager less likely to spill this data
192468 append_batch = make_uniq<FixedRawBatchData>(0U, std::move(new_collection));
192469 }
192470 if (append_batch) {
192471 append_batch->collection->InitializeAppend(append_state);
192472 }
192473 }
192474 if (!current_batch) {
192475 // we have consumed the collection already - no need to append
192476 continue;
192477 }
192478 auto &current_collection = *current_batch->collection;
192479 append_batch->memory_usage += current_batch->memory_usage;
192480 // iterate the collection while appending
192481 for (auto &chunk : current_collection.Chunks()) {
192482 // append the chunk to the collection
192483 append_batch->collection->Append(append_state, chunk);
192484 if (append_batch->collection->Count() < gstate.batch_size) {
192485 // the collection is still under the batch size - continue
192486 continue;
192487 }
192488 // the collection is full - move it to the result and create a new one
192489 task_manager.AddTask(make_uniq<PrepareBatchTask>(gstate.scheduled_batch_index++, std::move(append_batch)));
192490
192491 auto new_collection = make_uniq<ColumnDataCollection>(context, children[0].get().GetTypes());
192492 new_collection->SetPartitionIndex(0); // Makes the buffer manager less likely to spill this data
192493 append_batch = make_uniq<FixedRawBatchData>(0U, std::move(new_collection));
192494 append_batch->collection->InitializeAppend(append_state);
192495 }
192496 }
192497 if (append_batch && append_batch->collection->Count() > 0) {
192498 // if there are any remaining batches that are not filled up to the batch size
192499 // AND this is not the final collection
192500 // re-add it to the set of raw (to-be-merged) batches
192501 if (final || CorrectSizeForBatch(append_batch->collection->Count(), gstate.batch_size)) {
192502 task_manager.AddTask(make_uniq<PrepareBatchTask>(gstate.scheduled_batch_index++, std::move(append_batch)));
192503 } else {
192504 gstate.raw_batches[max_batch_index] = std::move(append_batch);
192505 }
192506 }
192507}
const vector< LogicalType > & GetTypes() const
Return a vector of the types that will be returned by this operator.
Definition duckdb.hpp:22174
__host__ cudaTextureObject_t get() const noexcept

◆ FlushBatchData()

void duckdb::PhysicalBatchCopyToFile::FlushBatchData ( ClientContext & context,
GlobalSinkState & gstate_p ) const
192509 {
192510 auto &gstate = gstate_p.Cast<FixedBatchCopyGlobalState>();
192511 auto &memory_manager = gstate.memory_manager;
192512
192513 // flush batch data to disk (if there are any to flush)
192514 // grab the flush lock - we can only call flush_batch with this lock
192515 // otherwise the data might end up in the wrong order
192516 {
192517 lock_guard<mutex> l(gstate.flush_lock);
192518 if (gstate.any_flushing) {
192519 return;
192520 }
192521 gstate.any_flushing = true;
192522 }
192523 ActiveFlushGuard active_flush(gstate.any_flushing);
192524 while (true) {
192525 unique_ptr<FixedPreparedBatchData> batch_data;
192526 {
192527 lock_guard<mutex> l(gstate.lock);
192528 if (gstate.batch_data.empty()) {
192529 // no batch data left to flush
192530 break;
192531 }
192532 auto entry = gstate.batch_data.begin();
192533 if (entry->first != gstate.flushed_batch_index) {
192534 // this entry is not yet ready to be flushed
192535 break;
192536 }
192537 if (entry->first < gstate.flushed_batch_index) {
192538 throw InternalException("Batch index was out of order!?");
192539 }
192540 batch_data = std::move(entry->second);
192541 gstate.batch_data.erase(entry);
192542 }
192543 function.flush_batch(context, *bind_data, *gstate.global_state, *batch_data->prepared_data);
192544 batch_data->prepared_data.reset();
192545 memory_manager.ReduceUnflushedMemory(batch_data->memory_usage);
192546 gstate.flushed_batch_index++;
192547 }
192548}

◆ ExecuteTask()

bool duckdb::PhysicalBatchCopyToFile::ExecuteTask ( ClientContext & context,
GlobalSinkState & gstate_p ) const
192553 {
192554 auto &gstate = gstate_p.Cast<FixedBatchCopyGlobalState>();
192555 auto task = gstate.task_manager.GetTask();
192556 if (!task) {
192557 return false;
192558 }
192559 task->Execute(*this, context, gstate_p);
192560 return true;
192561}

◆ ExecuteTasks()

void duckdb::PhysicalBatchCopyToFile::ExecuteTasks ( ClientContext & context,
GlobalSinkState & gstate_p ) const
192563 {
192564 while (ExecuteTask(context, gstate_p)) {
192565 }
192566}

◆ FinalFlush()

SinkFinalizeType duckdb::PhysicalBatchCopyToFile::FinalFlush ( ClientContext & context,
GlobalSinkState & gstate_p ) const
192308 {
192309 auto &gstate = gstate_p.Cast<FixedBatchCopyGlobalState>();
192310 if (gstate.task_manager.TaskCount() != 0) {
192311 throw InternalException("Unexecuted tasks are remaining in PhysicalFixedBatchCopy::FinalFlush!?");
192312 }
192313
192314 FlushBatchData(context, gstate_p);
192315 if (gstate.scheduled_batch_index != gstate.flushed_batch_index) {
192316 throw InternalException("Not all batches were flushed to disk - incomplete file?");
192317 }
192318 if (function.copy_to_finalize && gstate.global_state) {
192319 function.copy_to_finalize(context, *bind_data, *gstate.global_state);
192320
192321 if (use_tmp_file) {
192322 PhysicalCopyToFile::MoveTmpFile(context, file_path);
192323 }
192324 }
192325 gstate.memory_manager.FinalCheck();
192326 return SinkFinalizeType::READY;
192327}

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