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

Struct holding information over a CSV File we will scan. More...

Inheritance diagram for duckdb::CSVFileScan:
Collaboration diagram for duckdb::CSVFileScan:

Public Member Functions

 CSVFileScan (ClientContext &context, const OpenFileInfo &file, CSVReaderOptions options, const MultiFileOptions &file_options, const vector< string > &names, const vector< LogicalType > &types, CSVSchema &file_schema, bool per_file_single_threaded, shared_ptr< CSVBufferManager > buffer_manager=nullptr, bool fixed_schema=false)
 CSVFileScan (ClientContext &context, const OpenFileInfo &file, const CSVReaderOptions &options, const MultiFileOptions &file_options)
void SetStart ()
void SetNamesAndTypes (const vector< string > &names, const vector< LogicalType > &types)
string GetReaderType () const override
bool UseCastMap () const override
shared_ptr< BaseUnionData > GetUnionData (idx_t file_idx) override
void PrepareReader (ClientContext &context, GlobalTableFunctionState &) override
 Prepare reader for scanning.
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.
void FinishFile (ClientContext &context, GlobalTableFunctionState &gstate_p) override
 Finish scanning a given file.
double GetProgressInFile (ClientContext &context) override
 Get progress within a given file.
idx_t GetFileIndex () const
const vector< string > & GetNames ()
const vector< LogicalType > & GetTypes ()
void InitializeProjection ()
void Finish ()
void InitializeFileNamesTypes ()
 Initialize the actual names and types to be scanned from the file.
Public Member Functions inherited from duckdb::BaseFileReader
 BaseFileReader (OpenFileInfo file_p)
const vector< MultiFileColumnDefinition > & GetColumns () const
const string & GetFileName () const
virtual void AddVirtualColumn (column_t virtual_column_id)
 Adds a virtual column to be projected at the end.
virtual DUCKDB_API unique_ptr< BaseStatistics > GetStatistics (ClientContext &context, const string &name)
 Get statistics for a specific column.
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 ()

Public Attributes

shared_ptr< CSVBufferManager > buffer_manager
 Buffer Manager for the CSV File.
shared_ptr< CSVStateMachine > state_machine
 State Machine for this file.
atomic< idx_t > bytes_read {0}
 How many bytes were read up to this point.
idx_t file_size
 Size of this file.
shared_ptr< CSVErrorHandler > error_handler
 Line Info used in error messages.
bool on_disk_file = true
 Whether or not this is an on-disk file.
vector< LogicalType > file_types
set< idx_t > projected_columns
 Variables to handle projection pushdown.
std::vector< std::pair< idx_t, idx_t > > projection_ids
CSVReaderOptions options
 Options for this CSV Reader.
CSVIterator start_iterator
CSVValidator validator
idx_t skipped_rows = 0
atomic< idx_t > started_tasks {1}
atomic< idx_t > finished_tasks {0}
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)

Private Attributes

vector< string > names
vector< LogicalType > types

Additional Inherited Members

Protected Member Functions inherited from duckdb::enable_shared_from_this< BaseFileReader >
enable_shared_from_this & operator= (enable_shared_from_this const &) noexcept

Detailed Description

Struct holding information over a CSV File we will scan.

Constructor & Destructor Documentation

◆ CSVFileScan() [1/2]

duckdb::CSVFileScan::CSVFileScan ( ClientContext & context,
const OpenFileInfo & file,
CSVReaderOptions options,
const MultiFileOptions & file_options,
const vector< string > & names,
const vector< LogicalType > & types,
CSVSchema & file_schema,
bool per_file_single_threaded,
shared_ptr< CSVBufferManager > buffer_manager = nullptr,
bool fixed_schema = false )

Constructor for new CSV Files, we must initialize the buffer manager and the state machine Path to this file

174555 : BaseFileReader(file_p), buffer_manager(std::move(buffer_manager_p)),
174556 error_handler(make_shared_ptr<CSVErrorHandler>(options_p.ignore_errors.GetValue())),
174557 options(std::move(options_p)) {
174558 // Initialize Buffer Manager
174559 if (!buffer_manager) {
174560 buffer_manager = make_shared_ptr<CSVBufferManager>(context, options, file, per_file_single_threaded);
174561 }
174562 // Initialize On Disk and Size of file
174563 on_disk_file = buffer_manager->file_handle->OnDiskFile();
174564 file_size = buffer_manager->file_handle->FileSize();
174565 // Initialize State Machine
174566 auto &state_machine_cache = CSVStateMachineCache::Get(context);
174567
174568 SetNamesAndTypes(names, types);
174569 if (options.auto_detect) {
174570 if (fixed_schema) {
174571 // schema of the file is fixed - only run the sniffer
174572 CSVSniffer sniffer(options, file_options, buffer_manager, state_machine_cache);
174573 sniffer.SniffCSV();
174574 } else if (file_schema.Empty()) {
174575 throw InternalException("CSV File Scanner cannot be created without a schema");
174576 } else if (buffer_manager->file_handle->FileSize() > 0) {
174577 options.file_path = file.path;
174578 CSVSniffer sniffer(options, file_options, buffer_manager, state_machine_cache, false);
174579 auto result = sniffer.AdaptiveSniff(file_schema);
174580 SetNamesAndTypes(result.names, result.return_types);
174581 }
174582 }
174583 if (options.dialect_options.num_cols == 0) {
174584 // We need to define the number of columns, if the sniffer is not running this must be in the sql_type_list
174585 options.dialect_options.num_cols = options.sql_type_list.size();
174586 }
174587 if (options.dialect_options.state_machine_options.new_line == NewLineIdentifier::NOT_SET) {
174588 options.dialect_options.state_machine_options.new_line = CSVSniffer::DetectNewLineDelimiter(*buffer_manager);
174589 }
174590 state_machine = make_shared_ptr<CSVStateMachine>(
174591 state_machine_cache.Get(options.dialect_options.state_machine_options), options);
174592}
OpenFileInfo file
The file we are reading.
Definition duckdb.cpp:80254
idx_t file_size
Size of this file.
Definition duckdb.cpp:166558
shared_ptr< CSVBufferManager > buffer_manager
Buffer Manager for the CSV File.
Definition duckdb.cpp:166552
shared_ptr< CSVErrorHandler > error_handler
Line Info used in error messages.
Definition duckdb.cpp:166560
CSVReaderOptions options
Options for this CSV Reader.
Definition duckdb.cpp:166571
bool on_disk_file
Whether or not this is an on-disk file.
Definition duckdb.cpp:166562
shared_ptr< CSVStateMachine > state_machine
State Machine for this file.
Definition duckdb.cpp:166554
static CSVStateMachineCache & Get(ClientContext &context)
Definition duckdb.cpp:174537
Here is the call graph for this function:

◆ CSVFileScan() [2/2]

duckdb::CSVFileScan::CSVFileScan ( ClientContext & context,
const OpenFileInfo & file,
const CSVReaderOptions & options,
const MultiFileOptions & file_options )
174596 : BaseFileReader(file_p), error_handler(make_shared_ptr<CSVErrorHandler>(options_p.ignore_errors.GetValue())),
174597 options(options_p) {
174598 buffer_manager = make_shared_ptr<CSVBufferManager>(context, options, file);
174599 // Initialize On Disk and Size of file
174600 on_disk_file = buffer_manager->file_handle->OnDiskFile();
174601 file_size = buffer_manager->file_handle->FileSize();
174602 // Sniff it (We only really care about dialect detection, if types or number of columns are different this will
174603 // error out during scanning)
174604 auto &state_machine_cache = CSVStateMachineCache::Get(context);
174605 // We sniff file if it has not been sniffed yet and either auto-detect is on, or union by name is on
174606 if ((options.auto_detect || file_options.union_by_name) && options.dialect_options.num_cols == 0) {
174607 CSVSniffer sniffer(options, file_options, buffer_manager, state_machine_cache);
174608 auto sniffer_result = sniffer.SniffCSV();
174609 if (names.empty()) {
174610 SetNamesAndTypes(sniffer_result.names, sniffer_result.return_types);
174611 }
174612 }
174613 if (options.dialect_options.num_cols == 0) {
174614 // We need to define the number of columns, if the sniffer is not running this must be in the sql_type_list
174615 options.dialect_options.num_cols = options.sql_type_list.size();
174616 }
174617 // Initialize State Machine
174618 state_machine = make_shared_ptr<CSVStateMachine>(
174619 state_machine_cache.Get(options.dialect_options.state_machine_options), options);
174620 SetStart();
174621}

Member Function Documentation

◆ SetStart()

void duckdb::CSVFileScan::SetStart ( )
174626 {
174627 idx_t rows_to_skip = options.GetSkipRows() + state_machine->dialect_options.header.GetValue();
174628
174629 if (rows_to_skip == 0) {
174630 start_iterator.first_one = true;
174631 return;
174632 }
174633 SkipScanner skip_scanner(buffer_manager, state_machine, error_handler, rows_to_skip);
174634 skip_scanner.ParseChunk();
174635 skipped_rows = skip_scanner.GetLinesRead() - rows_to_skip;
174636 start_iterator = skip_scanner.GetIterator();
174637}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ SetNamesAndTypes()

void duckdb::CSVFileScan::SetNamesAndTypes ( const vector< string > & names,
const vector< LogicalType > & types )
174639 {
174640 names = names_p;
174641 types = types_p;
174642 columns = MultiFileColumnDefinition::ColumnsFromNamesAndTypes(names, types);
174643}
vector< MultiFileColumnDefinition > columns
The set of columns for the current file.
Definition duckdb.cpp:80258

◆ GetReaderType()

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

Implements duckdb::BaseFileReader.

166520 {
166521 return "CSV";
166522 }

◆ UseCastMap()

bool duckdb::CSVFileScan::UseCastMap ( ) const
inlineoverridevirtual

Whether or not to push casts into the cast map

Reimplemented from duckdb::BaseFileReader.

166524 {
166526 return true;
166527 }

◆ GetUnionData()

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

Reimplemented from duckdb::BaseFileReader.

175240 {
175241 auto data = make_shared_ptr<CSVUnionData>(file);
175242 data->names = GetNames();
175243 data->types = GetTypes();
175244 if (file_idx == 0) {
175245 data->options = options;
175246 data->reader = shared_from_this();
175247 } else {
175248 data->options = std::move(options);
175249 }
175250 data->options.auto_detect = false;
175251 return data;
175252}

◆ PrepareReader()

void duckdb::CSVFileScan::PrepareReader ( ClientContext & context,
GlobalTableFunctionState &  )
overridevirtual

Prepare reader for scanning.

Reimplemented from duckdb::BaseFileReader.

175254 {
175256 SetStart();
175257}
void InitializeFileNamesTypes()
Initialize the actual names and types to be scanned from the file.
Definition duckdb.cpp:174645
Here is the call graph for this function:

◆ TryInitializeScan()

bool duckdb::CSVFileScan::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.

175260 {
175261 auto &gstate = gstate_p.Cast<CSVGlobalState>();
175262 auto &lstate = lstate_p.Cast<CSVLocalState>();
175263 auto csv_reader_ptr = shared_ptr_cast<BaseFileReader, CSVFileScan>(shared_from_this());
175264 gstate.FinishScan(std::move(lstate.csv_reader));
175265 lstate.csv_reader = gstate.Next(csv_reader_ptr);
175266 if (!lstate.csv_reader) {
175267 // exhausted the scan
175268 return false;
175269 }
175270 return true;
175271}

◆ Scan()

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

Scan a chunk from the read state.

Implements duckdb::BaseFileReader.

175274 {
175275 auto &lstate = local_state.Cast<CSVLocalState>();
175276 if (lstate.csv_reader->FinishedIterator()) {
175277 return AsyncResult(SourceResultType::FINISHED);
175278 }
175279 lstate.csv_reader->Flush(chunk);
175280 return chunk.size() == 0 ? AsyncResult(SourceResultType::FINISHED)
175281 : AsyncResult(SourceResultType::HAVE_MORE_OUTPUT);
175282}
SourceResultType
Definition duckdb.hpp:20201

◆ FinishFile()

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

Finish scanning a given file.

Reimplemented from duckdb::BaseFileReader.

175284 {
175285 auto &gstate = global_state.Cast<CSVGlobalState>();
175286 gstate.FinishLaunchingTasks(*this);
175287}

◆ GetProgressInFile()

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

Get progress within a given file.

Reimplemented from duckdb::BaseFileReader.

175307 {
175308 auto manager = buffer_manager;
175309 if (!manager) {
175310 // We are done with this file, so it's 100%
175311 return 100.0;
175312 }
175313 double total_bytes_read;
175314 if (manager->file_handle->compression_type == FileCompressionType::GZIP ||
175315 manager->file_handle->compression_type == FileCompressionType::ZSTD) {
175316 // compressed file: we care about the progress made in the *underlying* file handle
175317 // the bytes read from the uncompressed file are skewed
175318 total_bytes_read = manager->file_handle->GetProgress();
175319 } else {
175320 total_bytes_read = static_cast<double>(bytes_read);
175321 }
175322 double file_progress = total_bytes_read / static_cast<double>(file_size);
175323 return file_progress * 100.0;
175324}
atomic< idx_t > bytes_read
How many bytes were read up to this point.
Definition duckdb.cpp:166556

◆ GetFileIndex()

idx_t duckdb::CSVFileScan::GetFileIndex ( ) const
inline
166539 {
166540 return file_list_idx.GetIndex();
166541 }
optional_idx file_list_idx
(Optionally) The file index (generated by the multi file reader)
Definition duckdb.cpp:80256

◆ GetNames()

const vector< string > & duckdb::CSVFileScan::GetNames ( )
174687 {
174688 return names;
174689}

◆ GetTypes()

const vector< LogicalType > & duckdb::CSVFileScan::GetTypes ( )
174690 {
174691 return types;
174692}

◆ InitializeProjection()

void duckdb::CSVFileScan::InitializeProjection ( )
174694 {
174695 for (idx_t i = 0; i < options.dialect_options.num_cols; i++) {
174696 column_ids.push_back(MultiFileLocalColumnId(i));
174697 }
174698}
MultiFileLocalColumnIds< MultiFileLocalColumnId > column_ids
The column ids to read from the file.
Definition duckdb.cpp:80260

◆ Finish()

void duckdb::CSVFileScan::Finish ( )
174700 {
174701 buffer_manager.reset();
174702}

◆ InitializeFileNamesTypes()

void duckdb::CSVFileScan::InitializeFileNamesTypes ( )

Initialize the actual names and types to be scanned from the file.

174645 {
174646 if (column_ids.empty()) {
174647 // This means that the columns from this file are irrelevant.
174648 // just read the first column
174649 file_types.emplace_back(LogicalType::VARCHAR);
174650 projected_columns.insert(0);
174651 projection_ids.emplace_back(0, 0);
174652 return;
174653 }
174654
174655 for (idx_t i = 0; i < column_ids.size(); i++) {
174656 auto col_idx = MultiFileLocalIndex(i);
174657 auto column_id = column_ids[col_idx];
174658 file_types.emplace_back(types[column_id.GetId()]);
174659 projected_columns.insert(column_id.GetId());
174660 projection_ids.emplace_back(column_id.GetId(), col_idx);
174661 }
174662
174663 if (column_ids.empty()) {
174664 file_types = types;
174665 }
174666
174667 // We need to be sure that our types are also following the cast_map
174668 if (!cast_map.empty()) {
174669 for (idx_t i = 0; i < column_ids.size(); i++) {
174670 auto local_idx = MultiFileLocalIndex(i);
174671 auto entry = cast_map.find(column_ids[local_idx].GetId());
174672 if (entry != cast_map.end()) {
174673 file_types[i] = entry->second;
174674 }
174675 }
174676 }
174677
174678 // We sort the types on the order of the parsed chunk
174679 std::sort(projection_ids.begin(), projection_ids.end());
174680 vector<LogicalType> sorted_types;
174681 for (idx_t i = 0; i < projection_ids.size(); ++i) {
174682 sorted_types.push_back(file_types[projection_ids[i].second]);
174683 }
174684 file_types = sorted_types;
174685}
unordered_map< column_t, LogicalType > cast_map
The final types for various expressions - this is ONLY used if UseCastMap() is explicitly enabled.
Definition duckdb.cpp:80269
set< idx_t > projected_columns
Variables to handle projection pushdown.
Definition duckdb.cpp:166567
Here is the caller graph for this function:

Member Data Documentation

◆ bytes_read

atomic<idx_t> duckdb::CSVFileScan::bytes_read {0}

How many bytes were read up to this point.

166556{0};

◆ started_tasks

atomic<idx_t> duckdb::CSVFileScan::started_tasks {1}

The started tasks and finished tasks allow us to track if all reads of the CSV file have completed Note that the "started_tasks" starts at one - this is so we can track when the scheduling of all tasks for this file has completed When the scheduling is finished we increment finished_tasks by one as well

166581{1};

◆ finished_tasks

atomic<idx_t> duckdb::CSVFileScan::finished_tasks {0}
166582{0};

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