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::PipelineExecutor Class Reference

The Pipeline class represents an execution pipeline. More...

Collaboration diagram for duckdb::PipelineExecutor:

Public Member Functions

 PipelineExecutor (ClientContext &context, Pipeline &pipeline)
PipelineExecuteResult Execute ()
 Fully execute a pipeline with a source and a sink until the source is completely exhausted.
PipelineExecuteResult Execute (idx_t max_chunks)
PipelineExecuteResult PushFinalize ()
bool RemainingSinkChunk () const
void InitializeChunk (DataChunk &chunk)
 Initializes a chunk with the types that will flow out of the chunk.
void SetTaskForInterrupts (weak_ptr< Task > current_task)
 Registers the task in the interrupt_state to allow Source/Sink operators to block the task.

Private Member Functions

void StartOperator (PhysicalOperator &op)
void EndOperator (PhysicalOperator &op, optional_ptr< DataChunk > chunk)
void GoToSource (idx_t &current_idx, idx_t initial_idx)
 Reset the operator index to the first operator.
SourceResultType FetchFromSource (DataChunk &result)
void FinishProcessing (int32_t operator_idx=-1)
bool IsFinished ()
SourceResultType GetData (DataChunk &chunk, OperatorSourceInput &input)
 Wrappers for sink/source calls to respective operators.
SinkResultType Sink (DataChunk &chunk, OperatorSinkInput &input)
OperatorResultType ExecutePushInternal (DataChunk &input, ExecutionBudget &chunk_budget, idx_t initial_idx=0)
OperatorResultType Execute (DataChunk &input, DataChunk &result, idx_t initial_index=0)
SinkNextBatchType NextBatch (DataChunk &source_chunk, const bool have_more_output)
 Notifies the sink that a new batch has started.
bool TryFlushCachingOperators (ExecutionBudget &chunk_budget)
void CacheChunk (DataChunk &input, idx_t operator_idx)

Static Private Member Functions

static bool CanCacheType (const LogicalType &type)

Private Attributes

Pipeline & pipeline
 The pipeline to process.
ThreadContext thread
 The thread context of this executor.
ExecutionContext context
 The total execution context of this executor.
vector< unique_ptr< DataChunk > > intermediate_chunks
 Intermediate chunks for the operators.
vector< unique_ptr< OperatorState > > intermediate_states
 Intermediate states for the operators.
unique_ptr< LocalSourceState > local_source_state
 The local source state.
unique_ptr< LocalSinkState > local_sink_state
 The local sink state (if any).
InterruptState interrupt_state
 The interrupt state, holding required information for sink/source operators to block.
DataChunk final_chunk
 The final chunk used for moving data into the sink.
stack< idx_t > in_process_operators
bool finalized = false
 Whether or not the pipeline has been finalized (used for verification only).
int32_t finished_processing_idx = -1
 Whether or not the pipeline has finished processing.
OperatorPartitionInfo required_partition_info
 Partition info that is used by this executor.
bool exhausted_pipeline = false
 Source or intermediate operator indicated that there is no more output possible.
bool started_flushing = false
 Flushing of intermediate operators has started.
bool done_flushing = false
 Flushing of caching operators is done.
bool remaining_sink_chunk = false
 This flag is set when the pipeline gets interrupted by the Sink -> the final_chunk should be re-sink-ed.
bool next_batch_blocked = false
idx_t flushing_idx
 Current operator being flushed.
bool should_flush_current_idx = true
 Whether the current flushing_idx should be flushed: this needs to be stored to make flushing code re-entrant.

Detailed Description

The Pipeline class represents an execution pipeline.

Constructor & Destructor Documentation

◆ PipelineExecutor()

duckdb::PipelineExecutor::PipelineExecutor ( ClientContext & context,
Pipeline & pipeline )
329963 : pipeline(pipeline_p), thread(context_p), context(context_p, thread, &pipeline_p) {
329964 D_ASSERT(pipeline.source_state);
329965 if (pipeline.sink) {
329966 local_sink_state = pipeline.sink->GetLocalSinkState(context);
329967 required_partition_info = pipeline.sink->RequiredPartitionInfo();
329968 if (required_partition_info.AnyRequired()) {
329969 D_ASSERT(pipeline.source->SupportsPartitioning(OperatorPartitionInfo::BatchIndex()));
329970 auto &partition_info = local_sink_state->partition_info;
329971 D_ASSERT(!partition_info.batch_index.IsValid());
329972 // batch index is not set yet - initialize before fetching anything
329973 partition_info.batch_index = pipeline.RegisterNewBatchIndex();
329974 partition_info.min_batch_index = partition_info.batch_index;
329975 }
329976 }
329977 local_source_state = pipeline.source->GetLocalSourceState(context, *pipeline.source_state);
329978
329979 intermediate_chunks.reserve(pipeline.operators.size());
329980 intermediate_states.reserve(pipeline.operators.size());
329981 for (idx_t i = 0; i < pipeline.operators.size(); i++) {
329982 auto &prev_operator = i == 0 ? *pipeline.source : pipeline.operators[i - 1].get();
329983 auto &current_operator = pipeline.operators[i].get();
329984
329985 auto chunk = make_uniq<DataChunk>();
329986 chunk->Initialize(BufferAllocator::Get(context.client), prev_operator.GetTypes());
329987 intermediate_chunks.push_back(std::move(chunk));
329988
329989 auto op_state = current_operator.GetOperatorState(context);
329990 intermediate_states.push_back(std::move(op_state));
329991
329992 if (current_operator.IsSink() && current_operator.sink_state->state == SinkFinalizeType::NO_OUTPUT_POSSIBLE) {
329993 // one of the operators has already figured out no output is possible
329994 // we can skip executing the pipeline
329995 FinishProcessing();
329996 }
329997 }
329999}
Pipeline & pipeline
The pipeline to process.
Definition duckdb.cpp:328237
ThreadContext thread
The thread context of this executor.
Definition duckdb.cpp:328239
unique_ptr< LocalSinkState > local_sink_state
The local sink state (if any).
Definition duckdb.cpp:328251
unique_ptr< LocalSourceState > local_source_state
The local source state.
Definition duckdb.cpp:328249
void InitializeChunk(DataChunk &chunk)
Initializes a chunk with the types that will flow out of the chunk.
Definition duckdb.cpp:330492
DataChunk final_chunk
The final chunk used for moving data into the sink.
Definition duckdb.cpp:328256
OperatorPartitionInfo required_partition_info
Partition info that is used by this executor.
Definition duckdb.cpp:328266
ExecutionContext context
The total execution context of this executor.
Definition duckdb.cpp:328241
vector< unique_ptr< DataChunk > > intermediate_chunks
Intermediate chunks for the operators.
Definition duckdb.cpp:328244
vector< unique_ptr< OperatorState > > intermediate_states
Intermediate states for the operators.
Definition duckdb.cpp:328246
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

Member Function Documentation

◆ Execute() [1/3]

PipelineExecuteResult duckdb::PipelineExecutor::Execute ( )

Fully execute a pipeline with a source and a sink until the source is completely exhausted.

330228 {
330229 return Execute(NumericLimits<idx_t>::Maximum());
330230}
PipelineExecuteResult Execute()
Fully execute a pipeline with a source and a sink until the source is completely exhausted.
Definition duckdb.cpp:330228
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Execute() [2/3]

PipelineExecuteResult duckdb::PipelineExecutor::Execute ( idx_t max_chunks)

Execute a pipeline with a source and a sink until finished, or until max_chunks were processed from the source Returns true if execution is finished, false if Execute should be called again

330137 {
330138 D_ASSERT(pipeline.sink);
330139 auto &source_chunk = pipeline.operators.empty() ? final_chunk : *intermediate_chunks[0];
330140 ExecutionBudget chunk_budget(max_chunks);
330141 do {
330142 if (context.client.interrupted) {
330143 throw InterruptException();
330144 }
330145
330146 OperatorResultType result;
330148 in_process_operators.empty()) {
330149 break;
330150 } else if (remaining_sink_chunk) {
330151 // The pipeline was interrupted by the Sink. We should retry sinking the final chunk.
330152 result = ExecutePushInternal(final_chunk, chunk_budget);
330153 D_ASSERT(result != OperatorResultType::HAVE_MORE_OUTPUT);
330154 remaining_sink_chunk = false;
330155 } else if (!in_process_operators.empty() && !started_flushing) {
330156 // Operator(s) in the pipeline have returned `HAVE_MORE_OUTPUT` in the last Execute call
330157 // the operators have to be called with the same input chunk to produce the rest of the output
330158 D_ASSERT(source_chunk.size() > 0);
330159 result = ExecutePushInternal(source_chunk, chunk_budget);
330161 // The pipeline was exhausted, try flushing all operators
330162 auto flush_completed = TryFlushCachingOperators(chunk_budget);
330163 if (flush_completed) {
330164 done_flushing = true;
330165 break;
330166 } else {
330167 if (remaining_sink_chunk) {
330169 } else {
330170 D_ASSERT(chunk_budget.IsDepleted());
330172 }
330173 }
330174 } else if (!exhausted_pipeline || next_batch_blocked) {
330175 SourceResultType source_result = SourceResultType::BLOCKED;
330176 if (!next_batch_blocked) {
330177 // "Regular" path: fetch a chunk from the source and push it through the pipeline
330178 source_chunk.Reset();
330179 source_result = FetchFromSource(source_chunk);
330180 if (source_result == SourceResultType::BLOCKED) {
330182 }
330183 if (source_result == SourceResultType::FINISHED) {
330184 exhausted_pipeline = true;
330185 }
330186 }
330187
330188 if (required_partition_info.AnyRequired()) {
330189 auto next_batch_result = NextBatch(source_chunk, source_result == SourceResultType::HAVE_MORE_OUTPUT);
330190 next_batch_blocked = next_batch_result == SinkNextBatchType::BLOCKED;
330191 if (next_batch_blocked) {
330193 }
330194 }
330195
330196 if (exhausted_pipeline && source_chunk.size() == 0) {
330197 continue;
330198 }
330199
330200 result = ExecutePushInternal(source_chunk, chunk_budget);
330201 } else {
330202 throw InternalException("Unexpected state reached in pipeline executor");
330203 }
330204
330205 // SINK INTERRUPT
330206 if (result == OperatorResultType::BLOCKED) {
330207 remaining_sink_chunk = true;
330209 }
330210
330211 if (result == OperatorResultType::FINISHED) {
330212 D_ASSERT(in_process_operators.empty());
330213 exhausted_pipeline = true;
330214 }
330215 } while (chunk_budget.Next());
330216
330217 if ((!exhausted_pipeline || !done_flushing) && !IsFinished()) {
330219 }
330220
330221 return PushFinalize();
330222}
PipelineExecuteResult PushFinalize()
Definition duckdb.cpp:330301
SinkNextBatchType NextBatch(DataChunk &source_chunk, const bool have_more_output)
Notifies the sink that a new batch has started.
Definition duckdb.cpp:330073
stack< idx_t > in_process_operators
Definition duckdb.cpp:328260
bool remaining_sink_chunk
This flag is set when the pipeline gets interrupted by the Sink -> the final_chunk should be re-sink-...
Definition duckdb.cpp:328276
bool next_batch_blocked
Definition duckdb.cpp:328280
bool done_flushing
Flushing of caching operators is done.
Definition duckdb.cpp:328273
bool exhausted_pipeline
Source or intermediate operator indicated that there is no more output possible.
Definition duckdb.cpp:328269
bool started_flushing
Flushing of intermediate operators has started.
Definition duckdb.cpp:328271
bool TryFlushCachingOperators(ExecutionBudget &chunk_budget)
Definition duckdb.cpp:330001
@ INTERRUPTED
Definition duckdb.cpp:328185
@ NOT_FINISHED
PipelineExecutor is not yet fully executed and can be called again immediately.
Definition duckdb.cpp:328182
OperatorResultType
Definition duckdb.hpp:20185
SourceResultType
Definition duckdb.hpp:20201
Here is the call graph for this function:

◆ PushFinalize()

PipelineExecuteResult duckdb::PipelineExecutor::PushFinalize ( )

Called after depleting the source: finalizes the execution of this pipeline executor This should only be called once per PipelineExecutor.

330301 {
330302 if (finalized) {
330303 throw InternalException("Calling PushFinalize on a pipeline that has been finalized already");
330304 }
330305
330306 D_ASSERT(local_sink_state);
330307
330308 // Run the combine for the sink
330309 OperatorSinkCombineInput combine_input {*pipeline.sink->sink_state, *local_sink_state, interrupt_state};
330310
330311#ifdef DUCKDB_DEBUG_ASYNC_SINK_SOURCE
330312 if (debug_blocked_combine_count < debug_blocked_target_count) {
330313 debug_blocked_combine_count++;
330314
330315 auto &callback_state = combine_input.interrupt_state;
330316 std::thread rewake_thread([callback_state] {
330317 std::this_thread::sleep_for(std::chrono::milliseconds(1));
330318 callback_state.Callback();
330319 });
330320 rewake_thread.detach();
330321
330323 }
330324#endif
330325 auto result = pipeline.sink->Combine(context, combine_input);
330326
330327 if (result == SinkCombineResultType::BLOCKED) {
330329 }
330330
330331 finalized = true;
330332 // flush all query profiler info
330333 for (idx_t i = 0; i < intermediate_states.size(); i++) {
330334 intermediate_states[i]->Finalize(pipeline.operators[i].get(), context);
330335 }
330336 pipeline.executor.Flush(thread);
330337 local_sink_state.reset();
330338
330340}
bool finalized
Whether or not the pipeline has been finalized (used for verification only).
Definition duckdb.cpp:328262
InterruptState interrupt_state
The interrupt state, holding required information for sink/source operators to block.
Definition duckdb.cpp:328253
@ FINISHED
PipelineExecutor is fully executed: the source is completely exhausted.
Definition duckdb.cpp:328180
Here is the caller graph for this function:

◆ RemainingSinkChunk()

bool duckdb::PipelineExecutor::RemainingSinkChunk ( ) const
330224 {
330225 return remaining_sink_chunk;
330226}

◆ InitializeChunk()

void duckdb::PipelineExecutor::InitializeChunk ( DataChunk & chunk)

Initializes a chunk with the types that will flow out of the chunk.

330492 {
330493 auto &last_op = pipeline.operators.empty() ? *pipeline.source : pipeline.operators.back().get();
330494 chunk.Initialize(BufferAllocator::Get(context.client), last_op.GetTypes());
330495}
DUCKDB_API void Initialize(ClientContext &context, const vector< LogicalType > &types, idx_t capacity=STANDARD_VECTOR_SIZE)
Definition duckdb.cpp:116791
Here is the call graph for this function:

◆ SetTaskForInterrupts()

void duckdb::PipelineExecutor::SetTaskForInterrupts ( weak_ptr< Task > current_task)

Registers the task in the interrupt_state to allow Source/Sink operators to block the task.

Execute a pipeline without a sink, and retrieve a single DataChunk Returns an empty chunk when finished.

330432 {
330433 interrupt_state = InterruptState(std::move(current_task));
330434}

◆ StartOperator()

void duckdb::PipelineExecutor::StartOperator ( PhysicalOperator & op)
private
330497 {
330498 if (context.client.interrupted) {
330499 throw InterruptException();
330500 }
330501 context.thread.profiler.StartOperator(&op);
330502}

◆ EndOperator()

void duckdb::PipelineExecutor::EndOperator ( PhysicalOperator & op,
optional_ptr< DataChunk > chunk )
private
330504 {
330505 context.thread.profiler.EndOperator(chunk);
330506
330507 if (chunk) {
330508 chunk->Verify();
330509 }
330510}

◆ GoToSource()

void duckdb::PipelineExecutor::GoToSource ( idx_t & current_idx,
idx_t initial_idx )
private

Reset the operator index to the first operator.

330342 {
330343 // we go back to the first operator (the source)
330344 current_idx = initial_idx;
330345 if (!in_process_operators.empty()) {
330346 // ... UNLESS there is an in process operator
330347 // if there is an in-process operator, we start executing at the latest one
330348 // for example, if we have a join operator that has tuples left, we first need to emit those tuples
330349 current_idx = in_process_operators.top();
330350 in_process_operators.pop();
330351 }
330352 D_ASSERT(current_idx >= initial_idx);
330353}
Here is the caller graph for this function:

◆ FetchFromSource()

SourceResultType duckdb::PipelineExecutor::FetchFromSource ( DataChunk & result)
private
330475 {
330476 StartOperator(*pipeline.source);
330477
330478 OperatorSourceInput source_input = {*pipeline.source_state, *local_source_state, interrupt_state};
330479 auto res = GetData(result, source_input);
330480
330481 // Ensures sources only return empty results when Blocking or Finished
330482 D_ASSERT(res != SourceResultType::BLOCKED || result.size() == 0);
330483 if (res == SourceResultType::FINISHED) {
330484 // final call into the source - finish source execution
330485 context.thread.profiler.FinishSource(*pipeline.source_state, *local_source_state);
330486 }
330487 EndOperator(*pipeline.source, &result);
330488
330489 return res;
330490}
SourceResultType GetData(DataChunk &chunk, OperatorSourceInput &input)
Wrappers for sink/source calls to respective operators.
Definition duckdb.cpp:330436

◆ FinishProcessing()

void duckdb::PipelineExecutor::FinishProcessing ( int32_t operator_idx = -1)
private
330232 {
330233 finished_processing_idx = operator_idx < 0 ? NumericLimits<int32_t>::Maximum() : operator_idx;
330234 in_process_operators = stack<idx_t>();
330235
330236 if (pipeline.GetSource()) {
330237 auto guard = pipeline.source_state->Lock();
330238 pipeline.source_state->PreventBlocking(guard);
330239 pipeline.source_state->UnblockTasks(guard);
330240 }
330241 if (pipeline.GetSink()) {
330242 auto guard = pipeline.GetSink()->sink_state->Lock();
330243 pipeline.GetSink()->sink_state->PreventBlocking(guard);
330244 pipeline.GetSink()->sink_state->UnblockTasks(guard);
330245 }
330246}
int32_t finished_processing_idx
Whether or not the pipeline has finished processing.
Definition duckdb.cpp:328264

◆ IsFinished()

bool duckdb::PipelineExecutor::IsFinished ( )
private
330248 {
330249 return finished_processing_idx >= 0;
330250}

◆ GetData()

SourceResultType duckdb::PipelineExecutor::GetData ( DataChunk & chunk,
OperatorSourceInput & input )
private

Wrappers for sink/source calls to respective operators.

Testing feature to enable async source on every operator

330436 {
330438#ifdef DUCKDB_DEBUG_ASYNC_SINK_SOURCE
330439 if (debug_blocked_source_count < debug_blocked_target_count) {
330440 debug_blocked_source_count++;
330441
330442 auto &callback_state = input.interrupt_state;
330443 std::thread rewake_thread([callback_state] {
330444 std::this_thread::sleep_for(std::chrono::milliseconds(1));
330445 callback_state.Callback();
330446 });
330447 rewake_thread.detach();
330448
330449 return SourceResultType::BLOCKED;
330450 }
330451#endif
330452
330453 return pipeline.source->GetData(context, chunk, input);
330454}

◆ Sink()

SinkResultType duckdb::PipelineExecutor::Sink ( DataChunk & chunk,
OperatorSinkInput & input )
private

Testing feature to enable async sink on every operator

330456 {
330458#ifdef DUCKDB_DEBUG_ASYNC_SINK_SOURCE
330459 if (debug_blocked_sink_count < debug_blocked_target_count) {
330460 debug_blocked_sink_count++;
330461
330462 auto &callback_state = input.interrupt_state;
330463 std::thread rewake_thread([callback_state] {
330464 std::this_thread::sleep_for(std::chrono::milliseconds(1));
330465 callback_state.Callback();
330466 });
330467 rewake_thread.detach();
330468
330469 return SinkResultType::BLOCKED;
330470 }
330471#endif
330472 return pipeline.sink->Sink(context, chunk, input);
330473}

◆ ExecutePushInternal()

OperatorResultType duckdb::PipelineExecutor::ExecutePushInternal ( DataChunk & input,
ExecutionBudget & chunk_budget,
idx_t initial_idx = 0 )
private
330253 {
330254 D_ASSERT(pipeline.sink);
330255 if (input.size() == 0) { // LCOV_EXCL_START
330256 return OperatorResultType::NEED_MORE_INPUT;
330257 } // LCOV_EXCL_STOP
330258
330259 // this loop will continuously push the input chunk through the pipeline as long as:
330260 // - the OperatorResultType for the Execute is HAVE_MORE_OUTPUT
330261 // - the Sink doesn't block
330262 // - the ExecutionBudget has not been depleted
330263 OperatorResultType result = OperatorResultType::HAVE_MORE_OUTPUT;
330264 do {
330265 // Note: if input is the final_chunk, we don't do any executing, the chunk just needs to be sinked
330266 if (&input != &final_chunk) {
330267 final_chunk.Reset();
330268 // Execute and put the result into 'final_chunk'
330269 result = Execute(input, final_chunk, initial_idx);
330270 if (result == OperatorResultType::FINISHED) {
330271 return OperatorResultType::FINISHED;
330272 }
330273 } else {
330274 result = OperatorResultType::NEED_MORE_INPUT;
330275 }
330276 auto &sink_chunk = final_chunk;
330277 if (sink_chunk.size() > 0) {
330278 StartOperator(*pipeline.sink);
330279 D_ASSERT(pipeline.sink);
330280 D_ASSERT(pipeline.sink->sink_state);
330281 OperatorSinkInput sink_input {*pipeline.sink->sink_state, *local_sink_state, interrupt_state};
330282
330283 auto sink_result = Sink(sink_chunk, sink_input);
330284
330285 EndOperator(*pipeline.sink, nullptr);
330286
330287 if (sink_result == SinkResultType::BLOCKED) {
330288 return OperatorResultType::BLOCKED;
330289 } else if (sink_result == SinkResultType::FINISHED) {
330290 FinishProcessing();
330291 return OperatorResultType::FINISHED;
330292 }
330293 }
330294 if (result == OperatorResultType::NEED_MORE_INPUT) {
330295 return OperatorResultType::NEED_MORE_INPUT;
330296 }
330297 } while (chunk_budget.Next());
330298 return result;
330299}
SinkResultType Sink(DataChunk &chunk, OperatorSinkInput &input)
Definition duckdb.cpp:330456

◆ Execute() [3/3]

OperatorResultType duckdb::PipelineExecutor::Execute ( DataChunk & input,
DataChunk & result,
idx_t initial_index = 0 )
private

Pushes a chunk through the pipeline and returns a single result chunk Returns whether or not a new input chunk is needed, or whether or not we are finished

330355 {
330356 if (input.size() == 0) { // LCOV_EXCL_START
330357 return OperatorResultType::NEED_MORE_INPUT;
330358 } // LCOV_EXCL_STOP
330359 D_ASSERT(!pipeline.operators.empty());
330360
330361 idx_t current_idx;
330362 GoToSource(current_idx, initial_idx);
330363 if (current_idx == initial_idx) {
330364 current_idx++;
330365 }
330366 if (current_idx > pipeline.operators.size()) {
330367 result.Reference(input);
330368 return OperatorResultType::NEED_MORE_INPUT;
330369 }
330370 while (true) {
330371 if (context.client.interrupted) {
330372 throw InterruptException();
330373 }
330374 // now figure out where to put the chunk
330375 // if current_idx is the last possible index (>= operators.size()) we write to the result
330376 // otherwise we write to an intermediate chunk
330377 auto current_intermediate = current_idx;
330378 auto &current_chunk =
330379 current_intermediate >= intermediate_chunks.size() ? result : *intermediate_chunks[current_intermediate];
330380 current_chunk.Reset();
330381 if (current_idx == initial_idx) {
330382 // we went back to the source: we need more input
330383 return OperatorResultType::NEED_MORE_INPUT;
330384 } else {
330385 auto &prev_chunk =
330386 current_intermediate == initial_idx + 1 ? input : *intermediate_chunks[current_intermediate - 1];
330387 auto operator_idx = current_idx - 1;
330388 auto &current_operator = pipeline.operators[operator_idx].get();
330389
330390 // if current_idx > source_idx, we pass the previous operators' output through the Execute of the current
330391 // operator
330392 StartOperator(current_operator);
330393 auto result = current_operator.Execute(context, prev_chunk, current_chunk, *current_operator.op_state,
330394 *intermediate_states[current_intermediate - 1]);
330395 EndOperator(current_operator, &current_chunk);
330396 if (result == OperatorResultType::HAVE_MORE_OUTPUT) {
330397 // more data remains in this operator
330398 // push in-process marker
330399 in_process_operators.push(current_idx);
330400 } else if (result == OperatorResultType::FINISHED) {
330401 D_ASSERT(current_chunk.size() == 0);
330402 FinishProcessing(NumericCast<int32_t>(current_idx));
330403 return OperatorResultType::FINISHED;
330404 }
330405 current_chunk.Verify();
330406 }
330407
330408 if (current_chunk.size() == 0) {
330409 // no output from this operator!
330410 if (current_idx == initial_idx) {
330411 // if we got no output from the scan, we are done
330412 break;
330413 } else {
330414 // if we got no output from an intermediate op
330415 // we go back and try to pull data from the source again
330416 GoToSource(current_idx, initial_idx);
330417 continue;
330418 }
330419 } else {
330420 // we got output! continue to the next operator
330421 current_idx++;
330422 if (current_idx > pipeline.operators.size()) {
330423 // if we got output and are at the last operator, we are finished executing for this output chunk
330424 // return the data and push it into the chunk
330425 break;
330426 }
330427 }
330428 }
330429 return in_process_operators.empty() ? OperatorResultType::NEED_MORE_INPUT : OperatorResultType::HAVE_MORE_OUTPUT;
330430}
void GoToSource(idx_t &current_idx, idx_t initial_idx)
Reset the operator index to the first operator.
Definition duckdb.cpp:330342
Here is the call graph for this function:

◆ NextBatch()

SinkNextBatchType duckdb::PipelineExecutor::NextBatch ( DataChunk & source_chunk,
const bool have_more_output )
private

Notifies the sink that a new batch has started.

330073 {
330074 D_ASSERT(required_partition_info.AnyRequired());
330075 auto max_batch_index = pipeline.base_batch_index + PipelineBuildState::BATCH_INCREMENT - 1;
330076 // by default set it to the maximum valid batch index value for the current pipeline
330077 auto &partition_info = local_sink_state->partition_info;
330078 OperatorPartitionData next_data(max_batch_index);
330079 if ((source_chunk.size() > 0)) {
330080 D_ASSERT(local_source_state);
330081 D_ASSERT(pipeline.source_state);
330082 // if we retrieved data - initialize the next batch index
330083 auto partition_data = pipeline.source->GetPartitionData(context, source_chunk, *pipeline.source_state,
330085 auto batch_index = partition_data.batch_index;
330086 // we start with the base_batch_index as a valid starting value. Make sure that next batch is called below
330087 next_data = std::move(partition_data);
330088 next_data.batch_index = pipeline.base_batch_index + batch_index + 1;
330089 if (next_data.batch_index >= max_batch_index) {
330090 throw InternalException("Pipeline batch index - invalid batch index %llu returned by source operator",
330091 batch_index);
330092 }
330093 } else if (have_more_output) {
330094 next_data.batch_index = partition_info.batch_index.GetIndex();
330095 }
330096 if (next_data.batch_index == partition_info.batch_index.GetIndex()) {
330097 // no changes, return
330098 return SinkNextBatchType::READY;
330099 }
330100 // batch index has changed - update it
330101 if (partition_info.batch_index.GetIndex() > next_data.batch_index) {
330102 throw InternalException(
330103 "Pipeline batch index - gotten lower batch index %llu (down from previous batch index of %llu)",
330104 next_data.batch_index, partition_info.batch_index.GetIndex());
330105 }
330106#ifdef DUCKDB_DEBUG_ASYNC_SINK_SOURCE
330107 if (debug_blocked_next_batch_count < debug_blocked_target_count) {
330108 debug_blocked_next_batch_count++;
330109
330110 auto &callback_state = interrupt_state;
330111 std::thread rewake_thread([callback_state] {
330112 std::this_thread::sleep_for(std::chrono::milliseconds(1));
330113 callback_state.Callback();
330114 });
330115 rewake_thread.detach();
330116
330117 return SinkNextBatchType::BLOCKED;
330118 }
330119#endif
330120 auto current_batch = partition_info.batch_index.GetIndex();
330121 partition_info.batch_index = next_data.batch_index;
330122 partition_info.partition_data = std::move(next_data.partition_data);
330123 OperatorSinkNextBatchInput next_batch_input {*pipeline.sink->sink_state, *local_sink_state, interrupt_state};
330124 // call NextBatch before updating min_batch_index to provide the opportunity to flush the previous batch
330125 auto next_batch_result = pipeline.sink->NextBatch(context, next_batch_input);
330126
330127 if (next_batch_result == SinkNextBatchType::BLOCKED) {
330128 partition_info.batch_index = current_batch; // set batch_index back to what it was before
330129 return SinkNextBatchType::BLOCKED;
330130 }
330131
330132 partition_info.min_batch_index = pipeline.UpdateBatchIndex(current_batch, next_data.batch_index);
330133
330134 return SinkNextBatchType::READY;
330135}
static constexpr idx_t BATCH_INCREMENT
How much to increment batch indexes when multiple pipelines share the same source.
Definition duckdb.hpp:26848
Here is the caller graph for this function:

◆ TryFlushCachingOperators()

bool duckdb::PipelineExecutor::TryFlushCachingOperators ( ExecutionBudget & chunk_budget)
private

Tries to flush all state from intermediate operators. Will return true if all state is flushed, false in the case of a blocked sink.

330001 {
330002 if (!started_flushing) {
330003 // Remainder of this method assumes any in process operators are from flushing
330004 D_ASSERT(in_process_operators.empty());
330005 started_flushing = true;
330006 flushing_idx = IsFinished() ? idx_t(finished_processing_idx) : 0;
330007 }
330008
330009 // For each operator that supports FinalExecute,
330010 // extract every chunk from it and push it through the rest of the pipeline
330011 // before moving onto the next operators' FinalExecute
330012 while (flushing_idx < pipeline.operators.size()) {
330013 if (!pipeline.operators[flushing_idx].get().RequiresFinalExecute()) {
330014 flushing_idx++;
330015 continue;
330016 }
330017
330018 // This slightly awkward way of increasing the flushing idx is to make the code re-entrant: We need to call this
330019 // method again in the case of a Sink returning BLOCKED.
330022 flushing_idx++;
330023 continue;
330024 }
330025
330026 auto &curr_chunk =
330028 auto &current_operator = pipeline.operators[flushing_idx].get();
330029
330030 OperatorFinalizeResultType finalize_result;
330031
330032 if (in_process_operators.empty()) {
330033 curr_chunk.Reset();
330034 StartOperator(current_operator);
330035 finalize_result = current_operator.FinalExecute(context, curr_chunk, *current_operator.op_state,
330037 EndOperator(current_operator, &curr_chunk);
330038 } else {
330039 // Reset flag and reflush the last chunk we were flushing.
330040 finalize_result = OperatorFinalizeResultType::HAVE_MORE_OUTPUT;
330041 }
330042
330043 auto push_result = ExecutePushInternal(curr_chunk, chunk_budget, flushing_idx + 1);
330044
330045 if (finalize_result == OperatorFinalizeResultType::HAVE_MORE_OUTPUT) {
330047 } else {
330048 should_flush_current_idx = false;
330049 }
330050
330051 switch (push_result) {
330052 case OperatorResultType::BLOCKED: {
330053 remaining_sink_chunk = true;
330054 return false;
330055 }
330056 case OperatorResultType::HAVE_MORE_OUTPUT: {
330057 D_ASSERT(chunk_budget.IsDepleted());
330058 // The chunk budget was used up, pushing the chunk through the pipeline created more chunks
330059 // we need to continue this the next time Execute is called.
330060 return false;
330061 }
330062 case OperatorResultType::NEED_MORE_INPUT:
330063 case OperatorResultType::FINISHED:
330064 break;
330065 default:
330066 throw InternalException("Unexpected OperatorResultType (%s) in TryFlushCachingOperators",
330067 EnumUtil::ToString(push_result));
330068 }
330069 }
330070 return true;
330071}
bool should_flush_current_idx
Whether the current flushing_idx should be flushed: this needs to be stored to make flushing code re-...
Definition duckdb.cpp:328285
idx_t flushing_idx
Current operator being flushed.
Definition duckdb.cpp:328283
OperatorFinalizeResultType
Definition duckdb.hpp:20190
Here is the caller graph for this function:

Member Data Documentation

◆ in_process_operators

stack<idx_t> duckdb::PipelineExecutor::in_process_operators
private

The operators that are not yet finished executing and have data remaining If the stack of in_process_operators is empty, we fetch from the source instead

◆ next_batch_blocked

bool duckdb::PipelineExecutor::next_batch_blocked = false
private

This flag is set when the pipeline gets interrupted by NextBatch -> NextBatch should be called again and the source_chunk should be sent through the pipeline


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