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

Public Member Functions

 DuckDBReader (ClientContext &context, OpenFileInfo file, const DuckDBFileReaderOptions &options)
bool TryInitializeScan (ClientContext &context, GlobalTableFunctionState &gstate, LocalTableFunctionState &lstate) override
 Try to initialize a scan over the reader - this is done while the global lock is held.
AsyncResult Scan (ClientContext &context, GlobalTableFunctionState &global_state, LocalTableFunctionState &local_state, DataChunk &chunk) override
 Scan a chunk from the read state.
shared_ptr< BaseUnionData > GetUnionData (idx_t file_idx) override
void FinishFile (ClientContext &context, GlobalTableFunctionState &gstate) override
 Finish scanning a given file.
double GetProgressInFile (ClientContext &context) override
 Get progress within a given file.
unique_ptr< BaseStatistics > GetStatistics (ClientContext &context, const string &name) override
 Get statistics for a specific column.
void AddVirtualColumn (column_t virtual_column_id) override
 Adds a virtual column to be projected at the end.
string GetReaderType () const override
optional_idx NumRows ()
AttachedDatabase & GetAttachedDatabase ()
TableCatalogEntry & GetTableEntry ()
Public Member Functions inherited from duckdb::BaseFileReader
 BaseFileReader (OpenFileInfo file_p)
const vector< MultiFileColumnDefinition > & GetColumns () const
const string & GetFileName () const
virtual bool UseCastMap () const
virtual DUCKDB_API void PrepareReader (ClientContext &context, GlobalTableFunctionState &)
 Prepare reader for scanning.
virtual void PrepareScan (ClientContext &context, GlobalTableFunctionState &gstate, LocalTableFunctionState &lstate)
 Prepare a scan - called after TryInitializeScan succeeds - this is done without any lock held.
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const
Public Member Functions inherited from duckdb::enable_shared_from_this< BaseFileReader >
shared_ptr< BaseFileReader > shared_from_this ()

Private Attributes

ClientContext & context
shared_ptr< AttachedDatabaseWrapper > db_wrapper
TableFunction scan_function
unique_ptr< FunctionData > bind_data
unique_ptr< GlobalTableFunctionState > global_state
atomic< bool > finished
idx_t column_count
string schema_name
string table_name

Additional Inherited Members

Public Attributes inherited from duckdb::BaseFileReader
OpenFileInfo file
 The file we are reading.
optional_idx file_list_idx
 (Optionally) The file index (generated by the multi file reader)
vector< MultiFileColumnDefinition > columns
 The set of columns for the current file.
MultiFileLocalColumnIds< MultiFileLocalColumnId > column_ids
 The column ids to read from the file.
vector< ColumnIndex > column_indexes
 The column indexes to read from the file.
unique_ptr< TableFilterSet > filters
 The set of table filters (adjusted to local indexes).
unordered_map< column_t, unique_ptr< Expression > > expression_map
unordered_map< column_t, LogicalType > cast_map
 The final types for various expressions - this is ONLY used if UseCastMap() is explicitly enabled.
unique_ptr< DeleteFilter > deletion_filter
 (Optionally) The deletion filter (generated by the multi file reader)
Protected Member Functions inherited from duckdb::enable_shared_from_this< BaseFileReader >
enable_shared_from_this & operator= (enable_shared_from_this const &) noexcept

Constructor & Destructor Documentation

◆ DuckDBReader()

duckdb::DuckDBReader::DuckDBReader ( ClientContext & context,
OpenFileInfo file,
const DuckDBFileReaderOptions & options )
243498 : BaseFileReader(std::move(file_p)), context(context_p), finished(false) {
243499 auto &attached = GetAttachedDatabase();
243500 auto &catalog = attached.GetCatalog();
243501 if (!catalog.IsDuckCatalog()) {
243502 throw NotImplementedException("read_duckdb can only be used to read DuckDB files - \"%s\" is of type \"%s\"",
243503 catalog.GetDBPath(), catalog.GetCatalogType());
243504 }
243505 vector<reference<TableCatalogEntry>> tables;
243506 vector<reference<TableCatalogEntry>> candidate_tables;
243507 catalog.ScanSchemas(context, [&](SchemaCatalogEntry &schema) {
243508 schema.Scan(CatalogType::TABLE_ENTRY, [&](CatalogEntry &entry) {
243509 if (entry.type != CatalogType::TABLE_ENTRY) {
243510 return;
243511 }
243512 auto &table = entry.Cast<TableCatalogEntry>();
243513 if (options.Matches(table)) {
243514 tables.push_back(table);
243515 }
243516 candidate_tables.push_back(table);
243517 });
243518 });
243519 if (tables.size() != 1) {
243520 string error_msg = tables.empty() ? "does not have any tables" : "has multiple tables";
243521 string extra_info;
243522 if (!options.HasSelection()) {
243523 extra_info = "\nSelect a table using `table_name='<name>'";
243524 } else {
243525 extra_info = " matching " + options.PrintOptions();
243526 }
243527 string candidate_str = options.GetCandidates(candidate_tables);
243528 throw BinderException("Database \"%s\" %s%s%s", file.path, error_msg, extra_info, candidate_str);
243529 }
243530 auto &table = tables[0].get();
243531 for (auto &col : table.GetColumns().Logical()) {
243532 columns.emplace_back(col.Name(), col.Type());
243533 }
243534 column_count = columns.size();
243535 schema_name = table.ParentSchema().name;
243536 table_name = table.name;
243537 db_wrapper->table_entry = table;
243538}
vector< MultiFileColumnDefinition > columns
The set of columns for the current file.
Definition duckdb.cpp:80258
OpenFileInfo file
The file we are reading.
Definition duckdb.cpp:80254

◆ ~DuckDBReader()

duckdb::DuckDBReader::~DuckDBReader ( )
override
243540 {
243541}

Member Function Documentation

◆ TryInitializeScan()

bool duckdb::DuckDBReader::TryInitializeScan ( ClientContext & context,
GlobalTableFunctionState & gstate,
LocalTableFunctionState & lstate )
overridevirtual

Try to initialize a scan over the reader - this is done while the global lock is held.

Implements duckdb::BaseFileReader.

243573 {
243574 auto &lstate = lstate_p.Cast<DuckDBReadLocalState>();
243575 if (finished) {
243576 lstate.attached_database.reset();
243577 return false;
243578 }
243579 if (!global_state) {
243580 lstate.attached_database.reset();
243581 auto &table_entry = GetTableEntry();
243582 scan_function = table_entry.GetScanFunction(context, bind_data);
243583 for (auto &col : column_indexes) {
243584 if (col.GetPrimaryIndex() >= column_count) {
243585 col = ColumnIndex(COLUMN_IDENTIFIER_ROW_ID);
243586 } else {
243587 auto &column = table_entry.GetColumn(LogicalIndex(col.GetPrimaryIndex()));
243588 if (column.Generated()) {
243589 throw NotImplementedException("Unsupported: read_duckdb cannot read generated column %s",
243590 column.Name());
243591 }
243592 }
243593 }
243594
243595 // initialize the scan over this table
243596 TableFunctionInitInput input(bind_data.get(), column_indexes, vector<idx_t>(), filters.get());
243597 global_state = scan_function.init_global(context, input);
243598 }
243599 AssignSharedPointer(lstate.attached_database, db_wrapper);
243600 // initialize the local scan
243601 ThreadContext thread(context);
243602 ExecutionContext exec_context(context, thread, nullptr);
243603 TableFunctionInitInput input(bind_data.get(), column_indexes, vector<idx_t>(), filters.get());
243604 lstate.local_state = scan_function.init_local(exec_context, input, global_state.get());
243605 return true;
243606}
unique_ptr< TableFilterSet > filters
The set of table filters (adjusted to local indexes).
Definition duckdb.cpp:80264
vector< ColumnIndex > column_indexes
The column indexes to read from the file.
Definition duckdb.cpp:80262
const column_t COLUMN_IDENTIFIER_ROW_ID
Special value used to signify the ROW ID of a table.
Definition duckdb.cpp:50303
void AssignSharedPointer(shared_ptr< T > &target, const shared_ptr< T > &source)
Definition duckdb.hpp:2180
Here is the call graph for this function:

◆ Scan()

AsyncResult duckdb::DuckDBReader::Scan ( ClientContext & context,
GlobalTableFunctionState & global_state,
LocalTableFunctionState & local_state,
DataChunk & chunk )
overridevirtual

Scan a chunk from the read state.

Implements duckdb::BaseFileReader.

243609 {
243610 chunk.Reset();
243611 auto &lstate = lstate_p.Cast<DuckDBReadLocalState>();
243612 TableFunctionInput input(bind_data.get(), lstate.local_state, global_state);
243613
243614 if (!scan_function.function) {
243615 throw InternalException("DuckDBReader works only with simple table functions");
243616 } else {
243617 input.async_result = AsyncResultType::IMPLICIT;
243618 input.results_execution_mode = AsyncResultsExecutionMode::TASK_EXECUTOR;
243619 scan_function.function(context, input, chunk);
243620
243621 switch (input.async_result.GetResultType()) {
243622 case AsyncResultType::BLOCKED:
243623 return std::move(input.async_result);
243624 case AsyncResultType::HAVE_MORE_OUTPUT:
243625 return SourceResultType::HAVE_MORE_OUTPUT;
243626 case AsyncResultType::IMPLICIT:
243627 if (chunk.size() > 0) {
243628 return SourceResultType::HAVE_MORE_OUTPUT;
243629 }
243630 finished = true;
243631 return SourceResultType::FINISHED;
243632 case AsyncResultType::FINISHED:
243633 finished = true;
243634 return SourceResultType::FINISHED;
243635 default:
243636 throw InternalException("DuckDBReader call of scan_function.function returned unexpected return '%'",
243637 EnumUtil::ToChars(input.async_result.GetResultType()));
243638 }
243639 throw InternalException("DuckDBReader hasn't handled a scan_function.function return");
243640 }
243641}
DUCKDB_API void Reset()
Definition duckdb.cpp:116840
Here is the call graph for this function:

◆ GetUnionData()

shared_ptr< BaseUnionData > duckdb::DuckDBReader::GetUnionData ( idx_t file_idx)
overridevirtual

Reimplemented from duckdb::BaseFileReader.

243771 {
243772 auto result = make_uniq<DuckDBReaderUnionData>(file);
243773 for (auto &column : columns) {
243774 result->names.push_back(column.name);
243775 result->types.push_back(column.type);
243776 }
243777 result->reader = shared_from_this();
243778 return std::move(result);
243779}

◆ FinishFile()

void duckdb::DuckDBReader::FinishFile ( ClientContext & context,
GlobalTableFunctionState & gstate )
overridevirtual

Finish scanning a given file.

Reimplemented from duckdb::BaseFileReader.

243643 {
243644 db_wrapper.reset();
243645}

◆ GetProgressInFile()

double duckdb::DuckDBReader::GetProgressInFile ( ClientContext & context)
overridevirtual

Get progress within a given file.

Reimplemented from duckdb::BaseFileReader.

243663 {
243664 if (!scan_function.table_scan_progress || !global_state) {
243665 return BaseFileReader::GetProgressInFile(context);
243666 }
243667 return scan_function.table_scan_progress(context, bind_data.get(), global_state.get());
243668}
virtual DUCKDB_API double GetProgressInFile(ClientContext &context)
Get progress within a given file.
Definition duckdb.cpp:80401
Here is the call graph for this function:

◆ GetStatistics()

unique_ptr< BaseStatistics > duckdb::DuckDBReader::GetStatistics ( ClientContext & context,
const string & name )
overridevirtual

Get statistics for a specific column.

Reimplemented from duckdb::BaseFileReader.

243652 {
243653 if (!scan_function.statistics) {
243654 return BaseFileReader::GetStatistics(context, name);
243655 }
243656 auto &table_entry = GetTableEntry();
243657 if (!table_entry.ColumnExists(name)) {
243658 return nullptr;
243659 }
243660 return scan_function.statistics(context, bind_data.get(), table_entry.GetColumn(name).Logical().index);
243661}
virtual DUCKDB_API unique_ptr< BaseStatistics > GetStatistics(ClientContext &context, const string &name)
Get statistics for a specific column.
Definition duckdb.cpp:80384
Here is the call graph for this function:

◆ AddVirtualColumn()

void duckdb::DuckDBReader::AddVirtualColumn ( column_t virtual_column_id)
overridevirtual

Adds a virtual column to be projected at the end.

Reimplemented from duckdb::BaseFileReader.

243670 {
243671 if (virtual_column_id != COLUMN_IDENTIFIER_ROW_ID) {
243672 throw InternalException("Unsupported virtual column id %d for duckdb reader", virtual_column_id);
243673 }
243674}

◆ GetReaderType()

string duckdb::DuckDBReader::GetReaderType ( ) const
inlineoverridevirtual

Implements duckdb::BaseFileReader.

243397 {
243398 return "duckdb";
243399 }

◆ NumRows()

optional_idx duckdb::DuckDBReader::NumRows ( )
243647 {
243648 auto &table_entry = GetTableEntry();
243649 return table_entry.GetStorage().GetTotalRows();
243650}

◆ GetAttachedDatabase()

AttachedDatabase & duckdb::DuckDBReader::GetAttachedDatabase ( )
243543 {
243544 if (!db_wrapper) {
243545 auto &db_manager = DatabaseManager::Get(context);
243546 AttachInfo info;
243547 info.path = file.path;
243548 // use invalid UTF-8 so that a conflicting database name cannot be attached by a user
243549 info.name = "\x80__duckdb_reader_" + info.path;
243550
243551 info.on_conflict = OnCreateConflict::IGNORE_ON_CONFLICT;
243552 unordered_map<string, Value> attach_kv;
243553 AttachOptions attach_options(attach_kv, AccessMode::READ_ONLY);
243554 attach_options.visibility = AttachVisibility::HIDDEN;
243555
243556 auto attached = db_manager.AttachDatabase(context, info, attach_options);
243557 db_wrapper = make_shared_ptr<AttachedDatabaseWrapper>(context, std::move(attached));
243558 }
243559 return *db_wrapper->attached_database;
243560}

◆ GetTableEntry()

TableCatalogEntry & duckdb::DuckDBReader::GetTableEntry ( )
243562 {
243563 auto &attached = GetAttachedDatabase();
243564 if (!db_wrapper->table_entry) {
243565 auto &catalog = attached.GetCatalog();
243566 db_wrapper->table_entry =
243567 catalog.GetEntry<TableCatalogEntry>(context, schema_name, table_name, OnEntryNotFound::THROW_EXCEPTION);
243568 }
243569 return *db_wrapper->table_entry;
243570}

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