Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/duckdb/extension/parquet/parquet_multi_file_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ shared_ptr<BaseUnionData> ParquetReader::GetUnionData(idx_t file_idx) {
result->names.push_back(column.name.GetIdentifierName());
result->types.push_back(column.type);
}
result->column_name_index.Build(result->names);

result->options = parquet_options;
result->metadata = metadata;
Expand Down
27 changes: 7 additions & 20 deletions src/duckdb/extension/parquet/parquet_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,17 +704,11 @@ unique_ptr<BaseStatistics> ParquetReader::ReadStatistics(const Identifier &name)
if (!can_use_metadata_statistics) {
return nullptr;
}
idx_t file_col_idx;
for (file_col_idx = 0; file_col_idx < columns.size(); file_col_idx++) {
if (columns[file_col_idx].name == name) {
break;
}
}
if (file_col_idx == columns.size()) {
auto file_col_idx = FindColumn(name);
if (!file_col_idx.IsValid()) {
return nullptr;
}

return ReadStatisticsInternal(*GetFileMetadata(), *root_schema, parquet_options, file_col_idx);
return ReadStatisticsInternal(*GetFileMetadata(), *root_schema, parquet_options, file_col_idx.GetIndex());
}

unique_ptr<BaseStatistics> ParquetReader::ReadStatistics(ClientContext &context, ParquetOptions parquet_options,
Expand Down Expand Up @@ -749,20 +743,12 @@ unique_ptr<BaseStatistics> ParquetReader::ReadStatistics(ClientContext &context,
if (!CanUseParquetMetadataStatistics(context, union_data.metadata, union_data.options)) {
return nullptr;
}
const auto &col_names = union_data.names;

idx_t file_col_idx;
for (file_col_idx = 0; file_col_idx < col_names.size(); file_col_idx++) {
if (col_names[file_col_idx] == name) {
break;
}
}
if (file_col_idx == col_names.size()) {
auto file_col_idx = union_data.FindColumn(name);
if (!file_col_idx.IsValid()) {
return nullptr;
}

return ReadStatisticsInternal(*union_data.metadata->metadata, *union_data.root_schema, union_data.options,
file_col_idx);
file_col_idx.GetIndex());
}

static bool IsFullyShredded(const BaseStatistics &variant_stats, const ColumnIndex &column_index) {
Expand Down Expand Up @@ -1395,6 +1381,7 @@ void ParquetReader::InitializeSchema(ClientContext &context) {
auto &element = root_schema->children[i];
columns.push_back(ParseColumnDefinition(*file_meta_data, element));
}
column_name_index.Build(columns);
}

void ParquetReader::AddVirtualColumn(column_t virtual_column_id) {
Expand Down
1 change: 1 addition & 0 deletions src/duckdb/extension/parquet/read_single_parquet_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ static unique_ptr<GlobalTableFunctionState> ReadSingleParquetFileInitGlobal(Clie
reader.column_ids.push_back(MultiFileLocalColumnId(local_id));
column_indexes.push_back(column_index.RemapRootIndex(local_id));
reader.columns.emplace_back(virtual_entry->second.name.GetIdentifierName(), virtual_entry->second.type);
reader.column_name_index.Build(reader.columns);
reader.AddVirtualColumn(virtual_id);
}
reader.column_indexes = std::move(column_indexes);
Expand Down
8 changes: 4 additions & 4 deletions src/duckdb/generated_extension_loader_package_build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int32_t duckdb_extension_core_functions_describe(duckdb_extension_descriptor *de
descriptor->version = 1;
descriptor->name = "core_functions";
descriptor->extension_version = EXT_VERSION_CORE_FUNCTIONS;
descriptor->api_version = "v2.0.1-alpha54735";
descriptor->api_version = "v2.0.1-alpha54836";
descriptor->entry_cpp = (void (*)(void))core_functions_duckdb_cpp_init;
return 0;
}
Expand Down Expand Up @@ -78,7 +78,7 @@ int32_t duckdb_extension_parquet_describe(duckdb_extension_descriptor *descripto
descriptor->version = 1;
descriptor->name = "parquet";
descriptor->extension_version = EXT_VERSION_PARQUET;
descriptor->api_version = "v2.0.1-alpha54735";
descriptor->api_version = "v2.0.1-alpha54836";
descriptor->entry_cpp = (void (*)(void))parquet_duckdb_cpp_init;
return 0;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ int32_t duckdb_extension_icu_describe(duckdb_extension_descriptor *descriptor) {
descriptor->version = 1;
descriptor->name = "icu";
descriptor->extension_version = EXT_VERSION_ICU;
descriptor->api_version = "v2.0.1-alpha54735";
descriptor->api_version = "v2.0.1-alpha54836";
descriptor->entry_cpp = (void (*)(void))icu_duckdb_cpp_init;
return 0;
}
Expand Down Expand Up @@ -144,7 +144,7 @@ int32_t duckdb_extension_json_describe(duckdb_extension_descriptor *descriptor)
descriptor->version = 1;
descriptor->name = "json";
descriptor->extension_version = EXT_VERSION_JSON;
descriptor->api_version = "v2.0.1-alpha54735";
descriptor->api_version = "v2.0.1-alpha54836";
descriptor->entry_cpp = (void (*)(void))json_duckdb_cpp_init;
return 0;
}
Expand Down
43 changes: 43 additions & 0 deletions src/duckdb/src/common/multi_file/base_file_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,47 @@

namespace duckdb {

void ColumnNameIndex::Build(const vector<MultiFileColumnDefinition> &columns) {
positions.clear();
for (idx_t i = 0; i < columns.size(); i++) {
positions.emplace(columns[i].name, i);
}
}

void ColumnNameIndex::Build(const vector<Identifier> &names) {
positions.clear();
for (idx_t i = 0; i < names.size(); i++) {
positions.emplace(names[i], i);
}
}

void ColumnNameIndex::Build(const vector<string> &names) {
positions.clear();
for (idx_t i = 0; i < names.size(); i++) {
positions.emplace(Identifier(names[i]), i);
}
}

optional_idx ColumnNameIndex::Find(const Identifier &name) const {
auto entry = positions.find(name);
if (entry == positions.end()) {
return optional_idx();
}
return entry->second;
}

unique_ptr<BaseStatistics> BaseFileReader::GetStatistics(ClientContext &context, const Identifier &name) {
return nullptr;
}

optional_idx BaseFileReader::FindColumn(const Identifier &name) const {
auto position = column_name_index.Find(name);
// a stale index would point at another column
D_ASSERT(!position.IsValid() ||
(position.GetIndex() < columns.size() && columns[position.GetIndex()].name == name));
return position;
}

unique_ptr<BaseStatistics> BaseFileReader::GetVirtualColumnStatistics(ClientContext &context,
column_t virtual_column_id) {
return nullptr;
Expand Down Expand Up @@ -45,4 +82,10 @@ unique_ptr<BaseStatistics> BaseUnionData::GetStatistics(ClientContext &context,
return nullptr;
}

optional_idx BaseUnionData::FindColumn(const Identifier &name) const {
auto position = column_name_index.Find(name);
D_ASSERT(!position.IsValid() || (position.GetIndex() < names.size() && names[position.GetIndex()] == name));
return position;
}

} // namespace duckdb
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ ResultColumnMapping MultiFileColumnMapper::CreateColumnMappingByMapper(const Col
auto local_index = global_id.RemapRootIndex(local_id.GetId());

reader.columns.emplace_back(virtual_entry->second.name.GetIdentifierName(), virtual_column_type);
reader.column_name_index.Build(reader.columns);
reader.column_ids.push_back(local_id);
reader.column_indexes.push_back(std::move(local_index));
reader.AddVirtualColumn(id);
Expand Down
22 changes: 12 additions & 10 deletions src/duckdb/src/common/multi_file/table_function_multi_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ void TableFunctionFileReader::BindFunction(ClientContext &context, const TableFu
names.clear();
types.clear();
bind_data = function.bind(context, bind_input, types, names);
name_index.Build(names);
// a function can describe what it bound in more detail than its names and types - e.g. with the field ids of its
// columns, and options that describe the file, which are exposed as the metadata of the reader of the file
optional<BindInfo> bind_info;
Expand Down Expand Up @@ -137,6 +138,7 @@ void TableFunctionFileReader::BindFunction(ClientContext &context, const TableFu
} else {
columns = MultiFileColumnDefinition::ColumnsFromNamesAndTypes(names, types);
}
column_name_index.Build(columns);
metadata.clear();
if (bind_info) {
// the options are kept unordered - sort them, so the same file always has the same metadata
Expand All @@ -163,18 +165,18 @@ unique_ptr<BaseStatistics> TableFunctionUnionData::GetStatistics(ClientContext &
if (!statistics || !bind_data) {
return nullptr;
}
for (idx_t col_idx = 0; col_idx < names.size(); col_idx++) {
if (Identifier(names[col_idx]) == name) {
return statistics(context, bind_data.get(), col_idx);
}
auto col_idx = FindColumn(name);
if (!col_idx.IsValid()) {
return nullptr;
}
return nullptr;
return statistics(context, bind_data.get(), col_idx.GetIndex());
}

shared_ptr<BaseUnionData> TableFunctionFileReader::GetUnionData(idx_t file_idx) {
auto result = make_shared_ptr<TableFunctionUnionData>(file);
result->names = IdentifiersToStrings(names);
result->types = types;
result->column_name_index.Build(result->names);
result->cardinality = cardinality;
result->bind_data = bind_data;
result->statistics = function.statistics;
Expand All @@ -189,12 +191,12 @@ unique_ptr<BaseStatistics> TableFunctionFileReader::GetStatistics(ClientContext
if (!function.statistics || !bind_data) {
return nullptr;
}
for (idx_t col_idx = 0; col_idx < names.size(); col_idx++) {
if (names[col_idx] == name) {
return function.statistics(context, bind_data.get(), col_idx);
}
auto col_idx = name_index.Find(name);
if (!col_idx.IsValid()) {
return nullptr;
}
return nullptr;
D_ASSERT(names[col_idx.GetIndex()] == name);
return function.statistics(context, bind_data.get(), col_idx.GetIndex());
}

unique_ptr<BaseStatistics> TableFunctionFileReader::GetVirtualColumnStatistics(ClientContext &context,
Expand Down
1 change: 1 addition & 0 deletions src/duckdb/src/function/table/direct_file_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ DirectFileReader::DirectFileReader(OpenFileInfo file_p, const LogicalType &type)
columns.push_back(MultiFileColumnDefinition("content", type));
columns.push_back(MultiFileColumnDefinition("size", LogicalType::BIGINT));
columns.push_back(MultiFileColumnDefinition("last_modified", LogicalType::TIMESTAMP_TZ));
column_name_index.Build(columns);
}

DirectFileReader::~DirectFileReader() {
Expand Down
2 changes: 2 additions & 0 deletions src/duckdb/src/function/table/read_duckdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ DuckDBReader::DuckDBReader(ClientContext &context_p, OpenFileInfo file_p, const
// create the child column definitions as well, so that STRUCT children are mapped by name
columns.push_back(MultiFileColumnDefinition::CreateFromNameAndType(col.Name(), col.Type()));
}
column_name_index.Build(columns);
column_count = columns.size();
schema_name = table.ParentSchema().name;
table_name = table.name;
Expand Down Expand Up @@ -489,6 +490,7 @@ shared_ptr<BaseUnionData> DuckDBReader::GetUnionData(idx_t file_idx) {
result->names.push_back(column.name.GetIdentifierName());
result->types.push_back(column.type);
}
result->column_name_index.Build(result->names);
result->reader = shared_from_this();
return std::move(result);
}
Expand Down
6 changes: 3 additions & 3 deletions src/duckdb/src/function/table/version/pragma_version.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DUCKDB_PATCH_VERSION
#define DUCKDB_PATCH_VERSION "1-alpha54735"
#define DUCKDB_PATCH_VERSION "1-alpha54836"
#endif
#ifndef DUCKDB_MINOR_VERSION
#define DUCKDB_MINOR_VERSION 0
Expand All @@ -8,10 +8,10 @@
#define DUCKDB_MAJOR_VERSION 2
#endif
#ifndef DUCKDB_VERSION
#define DUCKDB_VERSION "v2.0.1-alpha54735"
#define DUCKDB_VERSION "v2.0.1-alpha54836"
#endif
#ifndef DUCKDB_SOURCE_ID
#define DUCKDB_SOURCE_ID "a7e6fc10d2"
#define DUCKDB_SOURCE_ID "925bcdf357"
#endif
#include "duckdb/function/table/system_functions.hpp"
#include "duckdb/main/database.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include "duckdb/common/common.hpp"
#include "duckdb/common/identifier.hpp"
#include "duckdb/common/insertion_order_preserving_map.hpp"
#include "duckdb/common/types/value.hpp"
#include "duckdb/common/column_index.hpp"
Expand Down Expand Up @@ -40,6 +41,22 @@ struct BaseFileReaderExpression {
vector<ColumnIndex> column_indexes;
};

//! The position of every column of a file by name. The optimizer asks for the statistics of each column of a scan
//! separately and has to find that column in every file, so the names are indexed rather than scanned per request.
//! Names compare case-insensitively, as identifiers do, and of two columns with the same name the first wins
class ColumnNameIndex {
public:
//! Index the given names - called again whenever the names change
DUCKDB_API void Build(const vector<MultiFileColumnDefinition> &columns);
DUCKDB_API void Build(const vector<Identifier> &names);
DUCKDB_API void Build(const vector<string> &names);
//! The position of the column with the given name, or an invalid index when there is none
DUCKDB_API optional_idx Find(const Identifier &name) const;

private:
identifier_map_t<idx_t> positions;
};

//! Parent class of single-file readers - this must be inherited from for readers implementing the MultiFileReader
//! interface
class BaseFileReader : public enable_shared_from_this<BaseFileReader> {
Expand All @@ -52,7 +69,7 @@ class BaseFileReader : public enable_shared_from_this<BaseFileReader> {
OpenFileInfo file;
//! (Optionally) The file index (generated by the multi file reader)
optional_idx file_list_idx;
//! The set of columns for the current file
//! The set of columns for the current file - follow any change with column_name_index.Build(columns)
vector<MultiFileColumnDefinition> columns;
//! The column ids to read from the file
MultiFileLocalColumnIds<MultiFileLocalColumnId> column_ids;
Expand All @@ -72,11 +89,15 @@ class BaseFileReader : public enable_shared_from_this<BaseFileReader> {
vector<MultiFileGlobalIndex> filter_global_indices;
//! Counts the file against the open file budget of read-ahead until the reader closes it
shared_ptr<ReadAheadOpenFile> read_ahead_open_file;
//! The position of each column by name - rebuilt by whoever changes "columns"
ColumnNameIndex column_name_index;

public:
const vector<MultiFileColumnDefinition> &GetColumns() const {
return columns;
}
//! The position of the column with the given name in "columns", or an invalid index when there is none
DUCKDB_API optional_idx FindColumn(const Identifier &name) const;
const string &GetFileName() const {
return file.path;
}
Expand Down Expand Up @@ -162,12 +183,17 @@ class BaseUnionData {

OpenFileInfo file;
shared_ptr<BaseFileReader> reader;
//! The names of the columns of the file - follow any change with column_name_index.Build(names)
vector<string> names;
vector<LogicalType> types;
//! The position of each column by name - built by GetUnionData once the names are filled
ColumnNameIndex column_name_index;

const string &GetFileName() {
return file.path;
}
//! The position of the column with the given name in "names", or an invalid index when there is none
DUCKDB_API optional_idx FindColumn(const Identifier &name) const;

virtual optional_idx TryGetCardinalityEstimate() const {
return optional_idx();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,12 @@ class TableFunctionFileReader : public BaseFileReader {
TableFunction function;
//! The bind data of the wrapped function for this file
shared_ptr<FunctionData> bind_data;
//! The names/types the wrapped function bound to for this file
//! The names/types the wrapped function bound to for this file - follow any change to the names with
//! name_index.Build(names)
vector<Identifier> names;
vector<LogicalType> types;
//! The position of each of the names, for the statistics of a column - built with them in BindFunction
ColumnNameIndex name_index;
//! The cardinality estimate of the wrapped function for this file (if it has one)
optional_idx cardinality;
//! The virtual columns that are read, as a map of the index they are projected in to their virtual column id
Expand Down
4 changes: 4 additions & 0 deletions src/duckdb/src/include/duckdb/main/extension_entries.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,7 @@ static constexpr ExtensionEntry EXTENSION_SETTINGS[] = {
{"pg_connection_limit", "postgres_scanner"},
{"pg_debug_show_queries", "postgres_scanner"},
{"pg_enable_filter_pushdown", "postgres_scanner"},
{"pg_enable_remote_pushdown", "postgres_scanner"},
{"pg_idle_in_transaction_timeout_millis", "postgres_scanner"},
{"pg_null_byte_replacement", "postgres_scanner"},
{"pg_numeric_as_varchar", "postgres_scanner"},
Expand Down Expand Up @@ -1527,6 +1528,9 @@ static constexpr ExtensionEntry EXTENSION_SETTINGS[] = {
{"sqlite_all_varchar", "sqlite_scanner"},
{"sqlite_debug_show_queries", "sqlite_scanner"},
{"sqlite_disable_multithreaded_scans", "sqlite_scanner"},
{"sqlite_enable_filter_pushdown", "sqlite_scanner"},
{"sqlite_enable_remote_pushdown", "sqlite_scanner"},
{"sqlite_keep_database_open", "sqlite_scanner"},
{"timezone", "icu"},
{"ui_local_port", "ui"},
{"ui_polling_interval", "ui"},
Expand Down
Loading