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

Basic CSV Schema. More...

Collaboration diagram for duckdb::CSVSchema:

Public Member Functions

 CSVSchema (const bool empty=false)
 CSVSchema (const vector< string > &names, const vector< LogicalType > &types, const string &file_path, idx_t rows_read, const bool empty=false)
void Initialize (const vector< string > &names, const vector< LogicalType > &types, const string &file_path)
 Initializes the schema based on names and types.
bool Empty () const
 If the schema is empty.
bool MatchColumns (const CSVSchema &other) const
 If the columns of the schema match.
void MergeSchemas (CSVSchema &other, bool null_padding)
 We merge two schemas by ensuring that the column types are compatible between both.
string GetPath () const
 What's the file path for the file that generated this schema.
idx_t GetColumnCount () const
 How many columns we have in this schema.
bool SchemasMatch (string &error_message, SnifferResult &sniffer_result, const string &cur_file_path, bool is_minimal_sniffer) const
 Check if two schemas match.
idx_t GetRowsRead () const
 How many rows were read when generating this schema, this is only used for sniffing during the binder.
vector< string > GetNames () const
 Get a vector with names.
vector< LogicalType > GetTypes () const
 Get a vector with types.
void ReplaceNullWithVarchar ()
 Replace any SQLNull types with Varchar.

Static Private Member Functions

static bool CanWeCastIt (LogicalTypeId source, LogicalTypeId destination)
 If a type can be cast to another.

Private Attributes

vector< CSVColumnInfo > columns
unordered_map< string, idx_t > name_idx_map
string file_path
idx_t rows_read = 0
bool empty = false

Detailed Description

Basic CSV Schema.

Constructor & Destructor Documentation

◆ CSVSchema() [1/2]

duckdb::CSVSchema::CSVSchema ( const bool empty = false)
inlineexplicit
166378 : empty(empty) {
166379 }

◆ CSVSchema() [2/2]

duckdb::CSVSchema::CSVSchema ( const vector< string > & names,
const vector< LogicalType > & types,
const string & file_path,
idx_t rows_read,
const bool empty = false )
168740 : rows_read(rows_read_p), empty(empty_p) {
168741 Initialize(names, types, file_path);
168742}
void Initialize(const vector< string > &names, const vector< LogicalType > &types, const string &file_path)
Initializes the schema based on names and types.
Definition duckdb.cpp:168744

Member Function Documentation

◆ Initialize()

void duckdb::CSVSchema::Initialize ( const vector< string > & names,
const vector< LogicalType > & types,
const string & file_path )

Initializes the schema based on names and types.

168744 {
168745 if (!columns.empty()) {
168746 throw InternalException("CSV Schema is already populated, this should not happen.");
168747 }
168748 file_path = file_path_p;
168749 D_ASSERT(names.size() == types.size() && !names.empty());
168750 for (idx_t i = 0; i < names.size(); i++) {
168751 // Populate our little schema
168752 const auto &name = names.at(i);
168753 const auto &type = types.at(i);
168754 columns.push_back({name, type});
168755 name_idx_map[names[i]] = i;
168756 }
168757}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ Empty()

bool duckdb::CSVSchema::Empty ( ) const

If the schema is empty.

168783 {
168784 return columns.empty();
168785}
Here is the caller graph for this function:

◆ MatchColumns()

bool duckdb::CSVSchema::MatchColumns ( const CSVSchema & other) const

If the columns of the schema match.

168787 {
168788 return other.columns.size() == columns.size() || empty || other.empty;
168789}

◆ MergeSchemas()

void duckdb::CSVSchema::MergeSchemas ( CSVSchema & other,
bool null_padding )

We merge two schemas by ensuring that the column types are compatible between both.

168705 {
168706 // TODO: We could also merge names, maybe by giving preference to non-generated names?
168707 const vector<LogicalType> candidates_by_specificity = {LogicalType::BOOLEAN, LogicalType::BIGINT,
168708 LogicalType::DOUBLE, LogicalType::VARCHAR};
168709 for (idx_t i = 0; i < columns.size() && i < other.columns.size(); i++) {
168710 auto this_type = columns[i].type.id();
168711 auto other_type = other.columns[i].type.id();
168712 if (columns[i].type != other.columns[i].type) {
168713 if (CanWeCastIt(this_type, other_type)) {
168714 // If we can cast this to other, this becomes other
168715 columns[i].type = other.columns[i].type;
168716 } else if (!CanWeCastIt(other_type, this_type)) {
168717 // If we can't cast this to other or other to this, we see which parent they can be both cast to
168718 for (const auto &type : candidates_by_specificity) {
168719 if (CanWeCastIt(this_type, type.id()) && CanWeCastIt(other_type, type.id())) {
168720 columns[i].type = type;
168721 break;
168722 }
168723 }
168724 }
168725 }
168726 }
168727
168728 if (null_padding && other.columns.size() > columns.size()) {
168729 for (idx_t i = columns.size(); i < other.columns.size(); i++) {
168730 auto name = other.columns[i].name;
168731 auto type = other.columns[i].type;
168732 columns.push_back({name, type});
168733 name_idx_map[name] = i;
168734 }
168735 }
168736}
static bool CanWeCastIt(LogicalTypeId source, LogicalTypeId destination)
If a type can be cast to another.
Definition duckdb.cpp:168675
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetPath()

string duckdb::CSVSchema::GetPath ( ) const

What's the file path for the file that generated this schema.

168791 {
168792 return file_path;
168793}

◆ GetColumnCount()

idx_t duckdb::CSVSchema::GetColumnCount ( ) const

How many columns we have in this schema.

168795 {
168796 return columns.size();
168797}

◆ SchemasMatch()

bool duckdb::CSVSchema::SchemasMatch ( string & error_message,
SnifferResult & sniffer_result,
const string & cur_file_path,
bool is_minimal_sniffer ) const

Check if two schemas match.

168804 {
168805 D_ASSERT(sniffer_result.names.size() == sniffer_result.return_types.size());
168806 bool match = true;
168807 unordered_map<string, TypeIdxPair> current_schema;
168808
168809 for (idx_t i = 0; i < sniffer_result.names.size(); i++) {
168810 // Populate our little schema
168811 current_schema[sniffer_result.names[i]] = {sniffer_result.return_types[i], i};
168812 }
168813 if (is_minimal_sniffer) {
168814 auto min_sniffer = static_cast<AdaptiveSnifferResult &>(sniffer_result);
168815 if (!min_sniffer.more_than_one_row) {
168816 bool min_sniff_match = true;
168817 // If we don't have more than one row, either the names must match or the types must match.
168818 for (auto &column : columns) {
168819 if (current_schema.find(column.name) == current_schema.end()) {
168820 min_sniff_match = false;
168821 break;
168822 }
168823 }
168824 if (min_sniff_match) {
168825 return true;
168826 }
168827 // Otherwise, the types must match.
168828 min_sniff_match = true;
168829 if (sniffer_result.return_types.size() == columns.size()) {
168830 idx_t return_type_idx = 0;
168831 for (auto &column : columns) {
168832 if (column.type != sniffer_result.return_types[return_type_idx++]) {
168833 min_sniff_match = false;
168834 break;
168835 }
168836 }
168837 } else {
168838 min_sniff_match = false;
168839 }
168840 if (min_sniff_match) {
168841 // If we got here, we have the right types but the wrong names, lets fix the names
168842 idx_t sniff_name_idx = 0;
168843 for (auto &column : columns) {
168844 sniffer_result.names[sniff_name_idx++] = column.name;
168845 }
168846 return true;
168847 }
168848 }
168849 // If we got to this point, the minimal sniffer doesn't match, we throw an error.
168850 }
168851 // Here we check if the schema of a given file matched our original schema
168852 // We consider it's not a match if:
168853 // 1. The file misses columns that were defined in the original schema.
168854 // 2. They have a column match, but the types do not match.
168855 std::ostringstream error;
168856 error << "Schema mismatch between globbed files."
168857 << "\n";
168858 error << "Main file schema: " << file_path << "\n";
168859 error << "Current file: " << cur_file_path << "\n";
168860
168861 for (auto &column : columns) {
168862 if (current_schema.find(column.name) == current_schema.end()) {
168863 error << "Column with name: \"" << column.name << "\" is missing"
168864 << "\n";
168865 match = false;
168866 } else {
168867 if (!CanWeCastIt(current_schema[column.name].type.id(), column.type.id())) {
168868 error << "Column with name: \"" << column.name
168869 << "\" is expected to have type: " << column.type.ToString();
168870 error << " But has type: " << current_schema[column.name].type.ToString() << "\n";
168871 match = false;
168872 }
168873 }
168874 }
168875
168876 // Lets suggest some potential fixes
168877 error << "Potential Fixes "
168878 << "\n";
168879 error << "* Consider setting union_by_name=true."
168880 << "\n";
168881 error << "* Consider setting files_to_sniff to a higher value (e.g., files_to_sniff = -1)"
168882 << "\n";
168883 if (!match) {
168884 error_message = error.str();
168885 }
168886 return match;
168887}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetRowsRead()

idx_t duckdb::CSVSchema::GetRowsRead ( ) const

How many rows were read when generating this schema, this is only used for sniffing during the binder.

168799 {
168800 return rows_read;
168801}
Here is the caller graph for this function:

◆ GetNames()

vector< string > duckdb::CSVSchema::GetNames ( ) const

Get a vector with names.

168759 {
168760 vector<string> names;
168761 for (auto &column : columns) {
168762 names.push_back(column.name);
168763 }
168764 return names;
168765}
Here is the caller graph for this function:

◆ GetTypes()

vector< LogicalType > duckdb::CSVSchema::GetTypes ( ) const

Get a vector with types.

168767 {
168768 vector<LogicalType> types;
168769 for (auto &column : columns) {
168770 types.push_back(column.type);
168771 }
168772 return types;
168773}
Here is the caller graph for this function:

◆ ReplaceNullWithVarchar()

void duckdb::CSVSchema::ReplaceNullWithVarchar ( )

Replace any SQLNull types with Varchar.

168775 {
168776 for (auto &column : columns) {
168777 if (column.type.id() == LogicalTypeId::SQLNULL) {
168778 column.type = LogicalType::VARCHAR;
168779 }
168780 }
168781}
Here is the caller graph for this function:

◆ CanWeCastIt()

bool duckdb::CSVSchema::CanWeCastIt ( LogicalTypeId source,
LogicalTypeId destination )
staticprivate

If a type can be cast to another.

168675 {
168676 if (destination == LogicalTypeId::VARCHAR || source == destination) {
168677 // We can always cast to varchar
168678 // And obviously don't have to do anything if they are equal.
168679 return true;
168680 }
168681 switch (source) {
168682 case LogicalTypeId::SQLNULL:
168683 return true;
168684 case LogicalTypeId::TINYINT:
168685 return destination == LogicalTypeId::SMALLINT || destination == LogicalTypeId::INTEGER ||
168686 destination == LogicalTypeId::BIGINT || destination == LogicalTypeId::DECIMAL ||
168687 destination == LogicalTypeId::FLOAT || destination == LogicalTypeId::DOUBLE;
168688 case LogicalTypeId::SMALLINT:
168689 return destination == LogicalTypeId::INTEGER || destination == LogicalTypeId::BIGINT ||
168690 destination == LogicalTypeId::DECIMAL || destination == LogicalTypeId::FLOAT ||
168691 destination == LogicalTypeId::DOUBLE;
168692 case LogicalTypeId::INTEGER:
168693 return destination == LogicalTypeId::BIGINT || destination == LogicalTypeId::DECIMAL ||
168694 destination == LogicalTypeId::FLOAT || destination == LogicalTypeId::DOUBLE;
168695 case LogicalTypeId::BIGINT:
168696 return destination == LogicalTypeId::DECIMAL || destination == LogicalTypeId::FLOAT ||
168697 destination == LogicalTypeId::DOUBLE;
168698 case LogicalTypeId::FLOAT:
168699 return destination == LogicalTypeId::DOUBLE;
168700 default:
168701 return false;
168702 }
168703}
Here is the caller graph for this function:

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