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

Static Public Member Functions

static CSVSchema SchemaDiscovery (ClientContext &context, shared_ptr< CSVBufferManager > &buffer_manager, CSVReaderOptions &options, const MultiFileOptions &file_options, vector< LogicalType > &return_types, vector< string > &names, MultiFileList &multi_file_list)
 Function to do schema discovery over one CSV file or a list/glob of CSV files.

Member Function Documentation

◆ SchemaDiscovery()

CSVSchema duckdb::CSVSchemaDiscovery::SchemaDiscovery ( ClientContext & context,
shared_ptr< CSVBufferManager > & buffer_manager,
CSVReaderOptions & options,
const MultiFileOptions & file_options,
vector< LogicalType > & return_types,
vector< string > & names,
MultiFileList & multi_file_list )
static

Function to do schema discovery over one CSV file or a list/glob of CSV files.

174969 {
174970 vector<CSVSchema> schemas;
174971 const auto option_og = options;
174972
174973 const auto file_paths = multi_file_list.GetAllFiles();
174974
174975 // Here what we want to do is to sniff a given number of lines, if we have many files, we might go through them
174976 // to reach the number of lines.
174977 const idx_t required_number_of_lines = options.sniff_size * options.sample_size_chunks;
174978
174979 idx_t total_number_of_rows = 0;
174980 idx_t current_file = 0;
174981 options.file_path = file_paths[current_file].path;
174982
174983 buffer_manager = make_shared_ptr<CSVBufferManager>(context, options, options.file_path, false);
174984 idx_t only_header_or_empty_files = 0;
174985
174986 {
174987 CSVSniffer sniffer(options, file_options, buffer_manager, CSVStateMachineCache::Get(context));
174988 auto sniffer_result = sniffer.SniffCSV();
174989 idx_t rows_read = sniffer.LinesSniffed() -
174990 (options.dialect_options.skip_rows.GetValue() + options.dialect_options.header.GetValue());
174991
174992 schemas.emplace_back(sniffer_result.names, sniffer_result.return_types, file_paths[0].path, rows_read,
174993 buffer_manager->GetBuffer(0)->actual_size == 0);
174994 total_number_of_rows += sniffer.LinesSniffed();
174995 current_file++;
174996 if (sniffer.EmptyOrOnlyHeader()) {
174997 only_header_or_empty_files++;
174998 }
174999 }
175000
175001 // We do a copy of the options to not pollute the options of the first file.
175002 idx_t max_files_to_sniff = static_cast<idx_t>(options.files_to_sniff == -1)
175003 ? NumericLimits<idx_t>::Maximum()
175004 : static_cast<idx_t>(options.files_to_sniff);
175005 idx_t files_to_sniff = file_paths.size() > max_files_to_sniff ? max_files_to_sniff : file_paths.size();
175006 while (total_number_of_rows < required_number_of_lines && current_file < files_to_sniff) {
175007 auto option_copy = option_og;
175008 option_copy.file_path = file_paths[current_file].path;
175009 auto file_buffer_manager =
175010 make_shared_ptr<CSVBufferManager>(context, option_copy, option_copy.file_path, false);
175011 // TODO: We could cache the sniffer to be reused during scanning. Currently that's an exercise left to the
175012 // reader
175013 CSVSniffer sniffer(option_copy, file_options, file_buffer_manager, CSVStateMachineCache::Get(context));
175014 auto sniffer_result = sniffer.SniffCSV();
175015 idx_t rows_read = sniffer.LinesSniffed() - (option_copy.dialect_options.skip_rows.GetValue() +
175016 option_copy.dialect_options.header.GetValue());
175017 if (file_buffer_manager->GetBuffer(0)->actual_size == 0) {
175018 schemas.emplace_back(true);
175019 } else {
175020 schemas.emplace_back(sniffer_result.names, sniffer_result.return_types, option_copy.file_path, rows_read);
175021 }
175022 total_number_of_rows += sniffer.LinesSniffed();
175023 if (sniffer.EmptyOrOnlyHeader()) {
175024 only_header_or_empty_files++;
175025 }
175026 current_file++;
175027 }
175028
175029 // We might now have multiple schemas, we need to go through them to define the one true schema
175030 CSVSchema best_schema;
175031 for (auto &schema : schemas) {
175032 if (best_schema.Empty()) {
175033 // A schema is bettah than no schema
175034 best_schema = schema;
175035 } else if (best_schema.GetRowsRead() == 0) {
175036 // If the best-schema has no data-rows, that's easy; we just take the new schema
175037 best_schema = schema;
175038 } else if (schema.GetRowsRead() != 0) {
175039 // We might have conflicting-schemas, we must merge them
175040 best_schema.MergeSchemas(schema, options.null_padding);
175041 }
175042 }
175043
175044 // At this point, replace a sqlnull with varchar for the type
175045 best_schema.ReplaceNullWithVarchar();
175046
175047 if (names.empty()) {
175048 names = best_schema.GetNames();
175049 return_types = best_schema.GetTypes();
175050 }
175051 if (only_header_or_empty_files == current_file && !options.columns_set) {
175052 for (idx_t i = 0; i < return_types.size(); i++) {
175053 if (!options.sql_types_per_column.empty()) {
175054 if (options.sql_types_per_column.find(names[i]) != options.sql_types_per_column.end()) {
175055 continue;
175056 }
175057 } else if (i < options.sql_type_list.size()) {
175058 continue;
175059 }
175060 // we default to varchar if all files are empty or only have a header after all the sniffing
175061 return_types[i] = LogicalType::VARCHAR;
175062 }
175063 }
175064 return best_schema;
175065}
static CSVStateMachineCache & Get(ClientContext &context)
Definition duckdb.cpp:174537
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:

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