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::ArrowTableFunction Struct Reference

Static Public Member Functions

static void RegisterFunction (BuiltinFunctions &set)
static unique_ptr< FunctionData > ArrowScanBind (ClientContext &context, TableFunctionBindInput &input, vector< LogicalType > &return_types, vector< string > &names)
 Binds an arrow table.
static unique_ptr< FunctionData > ArrowScanBindDumb (ClientContext &context, TableFunctionBindInput &input, vector< LogicalType > &return_types, vector< string > &names)
static void ArrowToDuckDB (ArrowScanLocalState &scan_state, const arrow_column_map_t &arrow_convert_data, DataChunk &output, bool arrow_scan_is_projected=true, idx_t rowid_column_index=COLUMN_IDENTIFIER_ROW_ID)
 Actual conversion from Arrow to DuckDB.
static bool ArrowScanParallelStateNext (ClientContext &context, const FunctionData *bind_data_p, ArrowScanLocalState &state, ArrowScanGlobalState &parallel_state)
 Get next scan state.
static unique_ptr< GlobalTableFunctionState > ArrowScanInitGlobal (ClientContext &context, TableFunctionInitInput &input)
 Initialize Global State.
static unique_ptr< LocalTableFunctionState > ArrowScanInitLocalInternal (ClientContext &context, TableFunctionInitInput &input, GlobalTableFunctionState *global_state)
 Initialize Local State.
static unique_ptr< LocalTableFunctionState > ArrowScanInitLocal (ExecutionContext &context, TableFunctionInitInput &input, GlobalTableFunctionState *global_state)
static void ArrowScanFunction (ClientContext &context, TableFunctionInput &data, DataChunk &output)
 Scan Function.
static void PopulateArrowTableSchema (ClientContext &context, ArrowTableSchema &arrow_table, const ArrowSchema &arrow_schema)

Static Protected Member Functions

static idx_t ArrowScanMaxThreads (ClientContext &context, const FunctionData *bind_data)
 Defines Maximum Number of Threads.
static OperatorPartitionData ArrowGetPartitionData (ClientContext &context, TableFunctionGetPartitionInput &input)
 Allows parallel Create Table / Insertion.
static bool ArrowPushdownType (const FunctionData &bind_data, idx_t col_idx)
 Specify if a given type can be pushed-down by the arrow engine.
static unique_ptr< NodeStatistics > ArrowScanCardinality (ClientContext &context, const FunctionData *bind_data)

Member Function Documentation

◆ RegisterFunction()

void duckdb::ArrowTableFunction::RegisterFunction ( BuiltinFunctions & set)
static
239973 {
239974 TableFunction arrow("arrow_scan", {LogicalType::POINTER, LogicalType::POINTER, LogicalType::POINTER},
239975 ArrowScanFunction, ArrowScanBind, ArrowScanInitGlobal, ArrowScanInitLocal);
239976 arrow.cardinality = ArrowScanCardinality;
239977 arrow.get_partition_data = ArrowGetPartitionData;
239978 arrow.projection_pushdown = true;
239979 arrow.filter_pushdown = true;
239980 arrow.filter_prune = true;
239981 arrow.supports_pushdown_type = ArrowPushdownType;
239982 set.AddFunction(arrow);
239983
239984 TableFunction arrow_dumb("arrow_scan_dumb", {LogicalType::POINTER, LogicalType::POINTER, LogicalType::POINTER},
239985 ArrowScanFunction, ArrowScanBindDumb, ArrowScanInitGlobal, ArrowScanInitLocal);
239986 arrow_dumb.cardinality = ArrowScanCardinality;
239987 arrow_dumb.get_partition_data = ArrowGetPartitionData;
239988 arrow_dumb.projection_pushdown = false;
239989 arrow_dumb.filter_pushdown = false;
239990 arrow_dumb.filter_prune = false;
239991 set.AddFunction(arrow_dumb);
239992}
static unique_ptr< FunctionData > ArrowScanBind(ClientContext &context, TableFunctionBindInput &input, vector< LogicalType > &return_types, vector< string > &names)
Binds an arrow table.
Definition duckdb.cpp:239699
static void ArrowScanFunction(ClientContext &context, TableFunctionInput &data, DataChunk &output)
Scan Function.
Definition duckdb.cpp:239824
static OperatorPartitionData ArrowGetPartitionData(ClientContext &context, TableFunctionGetPartitionInput &input)
Allows parallel Create Table / Insertion.
Definition duckdb.cpp:239859
static bool ArrowPushdownType(const FunctionData &bind_data, idx_t col_idx)
Specify if a given type can be pushed-down by the arrow engine.
Definition duckdb.cpp:239959
static unique_ptr< GlobalTableFunctionState > ArrowScanInitGlobal(ClientContext &context, TableFunctionInitInput &input)
Initialize Global State.
Definition duckdb.cpp:239778
static unique_ptr< NodeStatistics > ArrowScanCardinality(ClientContext &context, const FunctionData *bind_data)
Definition duckdb.cpp:239855

◆ ArrowScanBind()

unique_ptr< FunctionData > duckdb::ArrowTableFunction::ArrowScanBind ( ClientContext & context,
TableFunctionBindInput & input,
vector< LogicalType > & return_types,
vector< string > & names )
static

Binds an arrow table.

239700 {
239701 if (input.inputs[0].IsNull() || input.inputs[1].IsNull() || input.inputs[2].IsNull()) {
239702 throw BinderException("arrow_scan: pointers cannot be null");
239703 }
239704 auto &ref = input.ref;
239705
239706 shared_ptr<DependencyItem> dependency;
239707 if (ref.external_dependency) {
239708 // This was created during the replacement scan for Python (see python_replacement_scan.cpp)
239709 // this object is the owning reference to 'stream_factory_ptr' and has to be kept alive.
239710 dependency = ref.external_dependency->GetDependency("replacement_cache");
239711 D_ASSERT(dependency);
239712 }
239713
239714 auto stream_factory_ptr = input.inputs[0].GetPointer();
239715 auto stream_factory_produce = (stream_factory_produce_t)input.inputs[1].GetPointer(); // NOLINT
239716 auto stream_factory_get_schema = (stream_factory_get_schema_t)input.inputs[2].GetPointer(); // NOLINT
239717
239718 auto res = make_uniq<ArrowScanFunctionData>(stream_factory_produce, stream_factory_ptr, std::move(dependency));
239719
239720 auto &data = *res;
239721 stream_factory_get_schema(reinterpret_cast<ArrowArrayStream *>(stream_factory_ptr), data.schema_root.arrow_schema);
239722 PopulateArrowTableSchema(context, res->arrow_table, data.schema_root.arrow_schema);
239723 names = res->arrow_table.GetNames();
239724 return_types = res->arrow_table.GetTypes();
239725 res->all_types = return_types;
239726 if (return_types.empty()) {
239727 throw InvalidInputException("Provided table/dataframe must have at least one column");
239728 }
239729 return std::move(res);
239730}

◆ ArrowScanBindDumb()

unique_ptr< FunctionData > duckdb::ArrowTableFunction::ArrowScanBindDumb ( ClientContext & context,
TableFunctionBindInput & input,
vector< LogicalType > & return_types,
vector< string > & names )
static
239692 {
239693 auto bind_data = ArrowScanBind(context, input, return_types, names);
239694 auto &arrow_bind_data = bind_data->Cast<ArrowScanFunctionData>();
239695 arrow_bind_data.projection_pushdown_enabled = false;
239696 return bind_data;
239697}

◆ ArrowToDuckDB()

void duckdb::ArrowTableFunction::ArrowToDuckDB ( ArrowScanLocalState & scan_state,
const arrow_column_map_t & arrow_convert_data,
DataChunk & output,
bool arrow_scan_is_projected = true,
idx_t rowid_column_index = COLUMN_IDENTIFIER_ROW_ID )
static

Actual conversion from Arrow to DuckDB.

241456 {
241457 for (idx_t idx = 0; idx < output.ColumnCount(); idx++) {
241458 auto col_idx = scan_state.column_ids.empty() ? idx : scan_state.column_ids[idx];
241459
241460 // If projection was not pushed down into the arrow scanner, but projection pushdown is enabled on the
241461 // table function, we need to use original column ids here.
241462 auto arrow_array_idx = arrow_scan_is_projected ? idx : col_idx;
241463
241464 if (rowid_column_index != COLUMN_IDENTIFIER_ROW_ID) {
241465 if (col_idx == COLUMN_IDENTIFIER_ROW_ID) {
241466 arrow_array_idx = rowid_column_index;
241467 } else if (col_idx >= rowid_column_index) {
241468 // Since the rowid column is skipped when the table is bound (its not a named column),
241469 // we need to shift references forward in the Arrow array by one to match the alignment
241470 // that DuckDB believes the Arrow array is in.
241471 col_idx += 1;
241472 arrow_array_idx += 1;
241473 }
241474 } else {
241475 // If there isn't any defined row_id_index, and we're asked for it, skip the column.
241476 // This is the incumbent behavior.
241477 if (col_idx == COLUMN_IDENTIFIER_ROW_ID) {
241478 continue;
241479 }
241480 }
241481
241482 auto &parent_array = scan_state.chunk->arrow_array;
241483 auto &array = *scan_state.chunk->arrow_array.children[arrow_array_idx];
241484 if (!array.release) {
241485 throw InvalidInputException("arrow_scan: released array passed");
241486 }
241487 if (array.length != scan_state.chunk->arrow_array.length) {
241488 throw InvalidInputException("arrow_scan: array length mismatch");
241489 }
241490
241491 D_ASSERT(arrow_convert_data.find(col_idx) != arrow_convert_data.end());
241492 auto &arrow_type = *arrow_convert_data.at(col_idx);
241493 auto &array_state = scan_state.GetState(col_idx);
241494
241495 // Make sure this Vector keeps the Arrow chunk alive in case we can zero-copy the data
241496 if (!array_state.owned_data) {
241497 array_state.owned_data = scan_state.chunk;
241498 }
241499 auto array_physical_type = arrow_type.GetPhysicalType();
241500
241501 switch (array_physical_type) {
241502 case ArrowArrayPhysicalType::DICTIONARY_ENCODED:
241503 ArrowToDuckDBConversion::ColumnArrowToDuckDBDictionary(output.data[idx], array, scan_state.chunk_offset,
241504 array_state, output.size(), arrow_type);
241505 break;
241506 case ArrowArrayPhysicalType::RUN_END_ENCODED:
241507 ArrowToDuckDBConversion::ColumnArrowToDuckDBRunEndEncoded(output.data[idx], array, scan_state.chunk_offset,
241508 array_state, output.size(), arrow_type);
241509 break;
241510 case ArrowArrayPhysicalType::DEFAULT:
241511 ArrowToDuckDBConversion::SetValidityMask(output.data[idx], array, scan_state.chunk_offset, output.size(),
241512 parent_array.offset, -1);
241513 ArrowToDuckDBConversion::ColumnArrowToDuckDB(output.data[idx], array, scan_state.chunk_offset, array_state,
241514 output.size(), arrow_type);
241515 break;
241516 default:
241517 throw NotImplementedException("ArrowArrayPhysicalType not recognized");
241518 }
241519 }
241520}
const column_t COLUMN_IDENTIFIER_ROW_ID
Special value used to signify the ROW ID of a table.
Definition duckdb.cpp:50303
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
static void ColumnArrowToDuckDBDictionary(Vector &vector, ArrowArray &array, idx_t chunk_offset, ArrowArrayScanState &array_state, idx_t size, const ArrowType &arrow_type, int64_t nested_offset=-1, const ValidityMask *parent_mask=nullptr, uint64_t parent_offset=0)
Definition duckdb.cpp:241388
static void ColumnArrowToDuckDB(Vector &vector, ArrowArray &array, idx_t chunk_offset, ArrowArrayScanState &array_state, idx_t size, const ArrowType &arrow_type, int64_t nested_offset=-1, ValidityMask *parent_mask=nullptr, uint64_t parent_offset=0, bool ignore_extensions=false)
Definition duckdb.cpp:240819
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ArrowScanParallelStateNext()

bool duckdb::ArrowTableFunction::ArrowScanParallelStateNext ( ClientContext & context,
const FunctionData * bind_data_p,
ArrowScanLocalState & state,
ArrowScanGlobalState & parallel_state )
static

Get next scan state.

have we run out of chunks? we are done

239757 {
239758 lock_guard<mutex> parallel_lock(parallel_state.main_mutex);
239759 if (parallel_state.done) {
239760 return false;
239761 }
239762 state.Reset();
239763 state.batch_index = ++parallel_state.batch_index;
239764
239765 auto current_chunk = parallel_state.stream->GetNextChunk();
239766 while (current_chunk->arrow_array.length == 0 && current_chunk->arrow_array.release) {
239767 current_chunk = parallel_state.stream->GetNextChunk();
239768 }
239769 state.chunk = std::move(current_chunk);
239771 if (!state.chunk->arrow_array.release) {
239772 parallel_state.done = true;
239773 return false;
239774 }
239775 return true;
239776}
Here is the caller graph for this function:

◆ ArrowScanInitGlobal()

unique_ptr< GlobalTableFunctionState > duckdb::ArrowTableFunction::ArrowScanInitGlobal ( ClientContext & context,
TableFunctionInitInput & input )
static

Initialize Global State.

239779 {
239780 auto &bind_data = input.bind_data->Cast<ArrowScanFunctionData>();
239781 auto result = make_uniq<ArrowScanGlobalState>();
239782 result->stream = ProduceArrowScan(bind_data, input.column_ids, input.filters.get());
239783 result->max_threads = ArrowScanMaxThreads(context, input.bind_data.get());
239784 if (!input.projection_ids.empty()) {
239785 result->projection_ids = input.projection_ids;
239786 for (const auto &col_idx : input.column_ids) {
239787 if (col_idx == COLUMN_IDENTIFIER_ROW_ID) {
239788 result->scanned_types.emplace_back(LogicalType::ROW_TYPE);
239789 } else {
239790 result->scanned_types.push_back(bind_data.all_types[col_idx]);
239791 }
239792 }
239793 }
239794 return std::move(result);
239795}
unique_ptr< ArrowArrayStreamWrapper > ProduceArrowScan(const ArrowScanFunctionData &function, const vector< column_t > &column_ids, TableFilterSet *filters)
Definition duckdb.cpp:239732
static idx_t ArrowScanMaxThreads(ClientContext &context, const FunctionData *bind_data)
Defines Maximum Number of Threads.
Definition duckdb.cpp:239752
Here is the call graph for this function:

◆ ArrowScanInitLocalInternal()

unique_ptr< LocalTableFunctionState > duckdb::ArrowTableFunction::ArrowScanInitLocalInternal ( ClientContext & context,
TableFunctionInitInput & input,
GlobalTableFunctionState * global_state )
static

Initialize Local State.

239799 {
239800 auto &global_state = global_state_p->Cast<ArrowScanGlobalState>();
239801 auto current_chunk = make_uniq<ArrowArrayWrapper>();
239802 auto result = make_uniq<ArrowScanLocalState>(std::move(current_chunk), context);
239803 result->column_ids = input.column_ids;
239804 result->filters = input.filters.get();
239805 auto &bind_data = input.bind_data->Cast<ArrowScanFunctionData>();
239806 if (!bind_data.projection_pushdown_enabled) {
239807 result->column_ids.clear();
239808 } else if (!input.projection_ids.empty()) {
239809 auto &asgs = global_state_p->Cast<ArrowScanGlobalState>();
239810 result->all_columns.Initialize(context, asgs.scanned_types);
239811 }
239812 if (!ArrowScanParallelStateNext(context, input.bind_data.get(), *result, global_state)) {
239813 return nullptr;
239814 }
239815 return std::move(result);
239816}
static bool ArrowScanParallelStateNext(ClientContext &context, const FunctionData *bind_data_p, ArrowScanLocalState &state, ArrowScanGlobalState &parallel_state)
Get next scan state.
Definition duckdb.cpp:239756
Here is the call graph for this function:

◆ ArrowScanInitLocal()

unique_ptr< LocalTableFunctionState > duckdb::ArrowTableFunction::ArrowScanInitLocal ( ExecutionContext & context,
TableFunctionInitInput & input,
GlobalTableFunctionState * global_state )
static
239820 {
239821 return ArrowScanInitLocalInternal(context.client, input, global_state_p);
239822}
static unique_ptr< LocalTableFunctionState > ArrowScanInitLocalInternal(ClientContext &context, TableFunctionInitInput &input, GlobalTableFunctionState *global_state)
Initialize Local State.
Definition duckdb.cpp:239798

◆ ArrowScanFunction()

void duckdb::ArrowTableFunction::ArrowScanFunction ( ClientContext & context,
TableFunctionInput & data,
DataChunk & output )
static

Scan Function.

Out of tuples in this chunk

239824 {
239825 if (!data_p.local_state) {
239826 return;
239827 }
239828 auto &data = data_p.bind_data->CastNoConst<ArrowScanFunctionData>(); // FIXME
239829 auto &state = data_p.local_state->Cast<ArrowScanLocalState>();
239830 auto &global_state = data_p.global_state->Cast<ArrowScanGlobalState>();
239831
239833 if (state.chunk_offset >= static_cast<idx_t>(state.chunk->arrow_array.length)) {
239834 if (!ArrowScanParallelStateNext(context, data_p.bind_data.get(), state, global_state)) {
239835 return;
239836 }
239837 }
239838 auto output_size =
239839 MinValue<idx_t>(STANDARD_VECTOR_SIZE, NumericCast<idx_t>(state.chunk->arrow_array.length) - state.chunk_offset);
239840 data.lines_read += output_size;
239841 if (global_state.CanRemoveFilterColumns()) {
239842 state.all_columns.Reset();
239843 state.all_columns.SetCardinality(output_size);
239844 ArrowToDuckDB(state, data.arrow_table.GetColumns(), state.all_columns);
239845 output.ReferenceColumns(state.all_columns, global_state.projection_ids);
239846 } else {
239847 output.SetCardinality(output_size);
239848 ArrowToDuckDB(state, data.arrow_table.GetColumns(), output);
239849 }
239850
239851 output.Verify();
239852 state.chunk_offset += output.size();
239853}
static void ArrowToDuckDB(ArrowScanLocalState &scan_state, const arrow_column_map_t &arrow_convert_data, DataChunk &output, bool arrow_scan_is_projected=true, idx_t rowid_column_index=COLUMN_IDENTIFIER_ROW_ID)
Actual conversion from Arrow to DuckDB.
Definition duckdb.cpp:241455
Here is the call graph for this function:

◆ PopulateArrowTableSchema()

void duckdb::ArrowTableFunction::PopulateArrowTableSchema ( ClientContext & context,
ArrowTableSchema & arrow_table,
const ArrowSchema & arrow_schema )
static
239663 {
239664 vector<string> names;
239665 // We first gather the column names and deduplicate them
239666 for (idx_t col_idx = 0; col_idx < static_cast<idx_t>(arrow_schema.n_children); col_idx++) {
239667 const auto &schema = *arrow_schema.children[col_idx];
239668 if (!schema.release) {
239669 throw InvalidInputException("arrow_scan: released schema passed");
239670 }
239671 auto name = string(schema.name);
239672 if (name.empty()) {
239673 name = string("v") + to_string(col_idx);
239674 }
239675 names.push_back(name);
239676 }
239678
239679 // We do a second iteration to figure out the arrow types and already set their deduplicated names
239680 for (idx_t col_idx = 0; col_idx < static_cast<idx_t>(arrow_schema.n_children); col_idx++) {
239681 auto &schema = *arrow_schema.children[col_idx];
239682 if (!schema.release) {
239683 throw InvalidInputException("arrow_scan: released schema passed");
239684 }
239685 auto arrow_type = ArrowType::GetArrowLogicalType(context, schema);
239686 arrow_table.AddColumn(col_idx, std::move(arrow_type), names[col_idx]);
239687 }
239688}
static void DeduplicateColumns(vector< string > &names)
Deduplicate column names for interop with external libraries.
Definition duckdb.cpp:296062
std::string to_string(const T &value)
Definition duckdb.cpp:19425

◆ ArrowScanMaxThreads()

idx_t duckdb::ArrowTableFunction::ArrowScanMaxThreads ( ClientContext & context,
const FunctionData * bind_data )
staticprotected

Defines Maximum Number of Threads.

239752 {
239753 return context.db->NumberOfThreads();
239754}
Here is the caller graph for this function:

◆ ArrowGetPartitionData()

OperatorPartitionData duckdb::ArrowTableFunction::ArrowGetPartitionData ( ClientContext & context,
TableFunctionGetPartitionInput & input )
staticprotected

Allows parallel Create Table / Insertion.

239860 {
239861 if (input.partition_info.RequiresPartitionColumns()) {
239862 throw InternalException("ArrowTableFunction::GetPartitionData: partition columns not supported");
239863 }
239864 auto &state = input.local_state->Cast<ArrowScanLocalState>();
239865 return OperatorPartitionData(state.batch_index);
239866}

◆ ArrowPushdownType()

bool duckdb::ArrowTableFunction::ArrowPushdownType ( const FunctionData & bind_data,
idx_t col_idx )
staticprotected

Specify if a given type can be pushed-down by the arrow engine.

239959 {
239960 auto &arrow_bind_data = bind_data.Cast<ArrowScanFunctionData>();
239961 const auto &column_info = arrow_bind_data.arrow_table.GetColumns();
239962 // PyArrow's array_filter kernel doesn't support string_view/binary_view types.
239963 // The filter is applied to ALL columns in a record batch, so if any column has a
239964 // view type, we must disable filter pushdown for the entire table.
239965 // See https://github.com/duckdb/duckdb-python/issues/227
239966 if (TableHasViewTypes(column_info)) {
239967 return false;
239968 }
239969 const auto column_type = column_info.at(col_idx);
239970 return CanPushdown(*column_type);
239971}

◆ ArrowScanCardinality()

unique_ptr< NodeStatistics > duckdb::ArrowTableFunction::ArrowScanCardinality ( ClientContext & context,
const FunctionData * bind_data )
staticprotected

--—Utility Functions:--— Gets Arrow Table's Cardinality

239855 {
239856 return make_uniq<NodeStatistics>();
239857}

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