Skip to content

Commit

Permalink
throw if no default value is provided for this column when it could n…
Browse files Browse the repository at this point in the history
…ot be found in the file we're scanning
  • Loading branch information
Tishj committed Dec 18, 2024
1 parent e9393ea commit 8726323
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/common/multi_file_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,24 @@ void MultiFileReader::CreateColumnMappingByName(const string &file_name,
auto entry = name_map.find(identifier);
if (entry == name_map.end()) {
// identiier not present in file, use default value
auto &default_val = global_column.default_expression;
D_ASSERT(default_val);
if (default_val->type != ExpressionType::VALUE_CONSTANT) {
throw NotImplementedException("Default expression that isn't constant is not supported yet");
if (global_column.default_expression) {
reader_data.constant_map.emplace_back(i, global_column.GetDefaultValue());
continue;
} else {
string candidate_names;
for (auto &column : local_columns) {
if (!candidate_names.empty()) {
candidate_names += ", ";
}
candidate_names += column.name;
}
throw IOException(StringUtil::Format(
"Failed to read file \"%s\": schema mismatch in glob: column \"%s\" was read from "
"the original file \"%s\", but could not be found in file \"%s\".\nCandidate names: "
"%s\nIf you are trying to "
"read files with different schemas, try setting union_by_name=True",
file_name, identifier, initial_file, file_name, candidate_names));
}
auto &constant_expr = default_val->Cast<ConstantExpression>();
reader_data.constant_map.emplace_back(i, constant_expr.value);
continue;
}
// we found the column in the local file - check if the types are the same
auto local_id = entry->second;
Expand Down
10 changes: 10 additions & 0 deletions src/include/duckdb/common/multi_file_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "duckdb/common/optional_ptr.hpp"
#include "duckdb/common/types/value.hpp"
#include "duckdb/common/union_by_name.hpp"
#include "duckdb/parser/expression/constant_expression.hpp"

namespace duckdb {
class TableFunction;
Expand Down Expand Up @@ -97,6 +98,15 @@ struct MultiFileReaderColumnDefinition {
return identifier.GetValue<string>();
}

Value GetDefaultValue() const {
D_ASSERT(default_expression);
if (default_expression->type != ExpressionType::VALUE_CONSTANT) {
throw NotImplementedException("Default expression that isn't constant is not supported yet");
}
auto &constant_expr = default_expression->Cast<ConstantExpression>();
return constant_expr.value;
}

public:
string name;
LogicalType type;
Expand Down

0 comments on commit 8726323

Please sign in to comment.