Skip to content

Commit

Permalink
get rid of the need for a ClientContext in every StandardEntry constr…
Browse files Browse the repository at this point in the history
…uctor
  • Loading branch information
Tishj committed Oct 20, 2023
1 parent b2bcd57 commit bea69f7
Show file tree
Hide file tree
Showing 46 changed files with 138 additions and 122 deletions.
5 changes: 2 additions & 3 deletions src/catalog/catalog_entry/duck_index_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace duckdb {

DuckIndexEntry::DuckIndexEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateIndexInfo &info,
optional_ptr<ClientContext> context)
: IndexCatalogEntry(catalog, schema, info, context) {
DuckIndexEntry::DuckIndexEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateIndexInfo &info)
: IndexCatalogEntry(catalog, schema, info) {
}

DuckIndexEntry::~DuckIndexEntry() {
Expand Down
39 changes: 19 additions & 20 deletions src/catalog/catalog_entry/duck_schema_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ DuckSchemaEntry::DuckSchemaEntry(Catalog &catalog, string name_p, bool is_intern
optional_ptr<CatalogEntry> DuckSchemaEntry::AddEntryInternal(CatalogTransaction transaction,
unique_ptr<StandardEntry> entry,
OnCreateConflict on_conflict,
PhysicalDependencyList dependencies) {
LogicalDependencyList logical_dependencies) {
auto entry_name = entry->name;
auto entry_type = entry->type;
auto result = entry.get();

// first find the set for this entry
auto &set = GetCatalogSet(entry_type);
dependencies.AddDependency(*this);
logical_dependencies.AddDependency(*this);
if (on_conflict == OnCreateConflict::REPLACE_ON_CONFLICT) {
// CREATE OR REPLACE: first try to drop the entry
auto old_entry = set.GetEntry(transaction, entry_name);
if (old_entry) {
if (dependencies.Contains(*old_entry)) {
if (logical_dependencies.Contains(*old_entry)) {
throw CatalogException("CREATE OR REPLACE is not allowed to depend on itself");
}
if (old_entry->type != entry_type) {
Expand All @@ -96,8 +96,9 @@ optional_ptr<CatalogEntry> DuckSchemaEntry::AddEntryInternal(CatalogTransaction
(void)set.DropEntry(transaction, entry_name, false, entry->internal);
}
}

// now try to add the entry
if (!set.CreateEntry(transaction, entry_name, std::move(entry), dependencies)) {
if (!set.CreateEntry(transaction, entry_name, std::move(entry), logical_dependencies)) {
// entry already exists!
if (on_conflict == OnCreateConflict::ERROR_ON_CONFLICT) {
throw CatalogException("%s with name \"%s\" already exists!", CatalogTypeToString(entry_type), entry_name);
Expand Down Expand Up @@ -148,29 +149,27 @@ optional_ptr<CatalogEntry> DuckSchemaEntry::CreateFunction(CatalogTransaction tr
unique_ptr<StandardEntry> function;
switch (info.type) {
case CatalogType::SCALAR_FUNCTION_ENTRY:
function = make_uniq_base<StandardEntry, ScalarFunctionCatalogEntry>(
catalog, *this, info.Cast<CreateScalarFunctionInfo>(), transaction.context);
function = make_uniq_base<StandardEntry, ScalarFunctionCatalogEntry>(catalog, *this,
info.Cast<CreateScalarFunctionInfo>());
break;
case CatalogType::TABLE_FUNCTION_ENTRY:
function = make_uniq_base<StandardEntry, TableFunctionCatalogEntry>(
catalog, *this, info.Cast<CreateTableFunctionInfo>(), transaction.context);
function = make_uniq_base<StandardEntry, TableFunctionCatalogEntry>(catalog, *this,
info.Cast<CreateTableFunctionInfo>());
break;
case CatalogType::MACRO_ENTRY:
// create a macro function
function = make_uniq_base<StandardEntry, ScalarMacroCatalogEntry>(catalog, *this, info.Cast<CreateMacroInfo>(),
transaction.context);
function = make_uniq_base<StandardEntry, ScalarMacroCatalogEntry>(catalog, *this, info.Cast<CreateMacroInfo>());
break;

case CatalogType::TABLE_MACRO_ENTRY:
// create a macro table function
function = make_uniq_base<StandardEntry, TableMacroCatalogEntry>(catalog, *this, info.Cast<CreateMacroInfo>(),
transaction.context);
function = make_uniq_base<StandardEntry, TableMacroCatalogEntry>(catalog, *this, info.Cast<CreateMacroInfo>());
break;
case CatalogType::AGGREGATE_FUNCTION_ENTRY:
D_ASSERT(info.type == CatalogType::AGGREGATE_FUNCTION_ENTRY);
// create an aggregate function
function = make_uniq_base<StandardEntry, AggregateFunctionCatalogEntry>(
catalog, *this, info.Cast<CreateAggregateFunctionInfo>(), transaction.context);
catalog, *this, info.Cast<CreateAggregateFunctionInfo>());
break;
default:
throw InternalException("Unknown function type \"%s\"", CatalogTypeToString(info.type));
Expand All @@ -181,7 +180,7 @@ optional_ptr<CatalogEntry> DuckSchemaEntry::CreateFunction(CatalogTransaction tr

optional_ptr<CatalogEntry> DuckSchemaEntry::AddEntry(CatalogTransaction transaction, unique_ptr<StandardEntry> entry,
OnCreateConflict on_conflict) {
PhysicalDependencyList dependencies = entry->dependencies;
LogicalDependencyList dependencies = entry->dependencies;
return AddEntryInternal(transaction, std::move(entry), on_conflict, dependencies);
}

Expand All @@ -191,21 +190,21 @@ optional_ptr<CatalogEntry> DuckSchemaEntry::CreateSequence(CatalogTransaction tr
}

optional_ptr<CatalogEntry> DuckSchemaEntry::CreateType(CatalogTransaction transaction, CreateTypeInfo &info) {
auto type_entry = make_uniq<TypeCatalogEntry>(catalog, *this, info, transaction.context);
auto type_entry = make_uniq<TypeCatalogEntry>(catalog, *this, info);
return AddEntry(transaction, std::move(type_entry), info.on_conflict);
}

optional_ptr<CatalogEntry> DuckSchemaEntry::CreateView(CatalogTransaction transaction, CreateViewInfo &info) {
auto view = make_uniq<ViewCatalogEntry>(catalog, *this, info, transaction.context);
auto view = make_uniq<ViewCatalogEntry>(catalog, *this, info);
return AddEntry(transaction, std::move(view), info.on_conflict);
}

optional_ptr<CatalogEntry> DuckSchemaEntry::CreateIndex(ClientContext &context, CreateIndexInfo &info,
TableCatalogEntry &table) {
info.dependencies.AddDependency(table);
auto index = make_uniq<DuckIndexEntry>(catalog, *this, info, &context);
auto index = make_uniq<DuckIndexEntry>(catalog, *this, info);

PhysicalDependencyList dependencies = index->dependencies;
LogicalDependencyList dependencies = index->dependencies;
return AddEntryInternal(GetCatalogTransaction(context), std::move(index), info.on_conflict, dependencies);
}

Expand All @@ -217,7 +216,7 @@ optional_ptr<CatalogEntry> DuckSchemaEntry::CreateCollation(CatalogTransaction t

optional_ptr<CatalogEntry> DuckSchemaEntry::CreateTableFunction(CatalogTransaction transaction,
CreateTableFunctionInfo &info) {
auto table_function = make_uniq<TableFunctionCatalogEntry>(catalog, *this, info, transaction.context);
auto table_function = make_uniq<TableFunctionCatalogEntry>(catalog, *this, info);
table_function->internal = info.internal;
return AddEntry(transaction, std::move(table_function), info.on_conflict);
}
Expand All @@ -231,7 +230,7 @@ optional_ptr<CatalogEntry> DuckSchemaEntry::CreateCopyFunction(CatalogTransactio

optional_ptr<CatalogEntry> DuckSchemaEntry::CreatePragmaFunction(CatalogTransaction transaction,
CreatePragmaFunctionInfo &info) {
auto pragma_function = make_uniq<PragmaFunctionCatalogEntry>(catalog, *this, info, transaction.context);
auto pragma_function = make_uniq<PragmaFunctionCatalogEntry>(catalog, *this, info);
pragma_function->internal = info.internal;
return AddEntry(transaction, std::move(pragma_function), info.on_conflict);
}
Expand Down
2 changes: 1 addition & 1 deletion src/catalog/catalog_entry/duck_table_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void AddDataTableIndex(DataTable &storage, const ColumnList &columns, vector<Log

DuckTableEntry::DuckTableEntry(Catalog &catalog, SchemaCatalogEntry &schema, BoundCreateTableInfo &info,
optional_ptr<ClientContext> context, std::shared_ptr<DataTable> inherited_storage)
: TableCatalogEntry(catalog, schema, info.Base(), context), storage(std::move(inherited_storage)),
: TableCatalogEntry(catalog, schema, info.Base()), storage(std::move(inherited_storage)),
bound_constraints(std::move(info.bound_constraints)),
column_dependency_manager(std::move(info.column_dependency_manager)) {
if (!storage) {
Expand Down
7 changes: 3 additions & 4 deletions src/catalog/catalog_entry/index_catalog_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

namespace duckdb {

IndexCatalogEntry::IndexCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateIndexInfo &info,
optional_ptr<ClientContext> context)
IndexCatalogEntry::IndexCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateIndexInfo &info)
: StandardEntry(CatalogType::INDEX_ENTRY, schema, catalog, info.index_name), index(nullptr), sql(info.sql) {
this->temporary = info.temporary;
this->dependencies = info.dependencies.GetPhysical(catalog, context);
this->dependencies = info.dependencies;
}

string IndexCatalogEntry::ToSQL() const {
Expand All @@ -28,7 +27,7 @@ unique_ptr<CreateInfo> IndexCatalogEntry::GetInfo() const {
result->sql = sql;
result->index_type = index->type;
result->constraint_type = index->constraint_type;
result->dependencies = dependencies.GetLogical();
result->dependencies = dependencies;
for (auto &expr : expressions) {
result->expressions.push_back(expr->Copy());
}
Expand Down
17 changes: 7 additions & 10 deletions src/catalog/catalog_entry/macro_catalog_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@

namespace duckdb {

MacroCatalogEntry::MacroCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateMacroInfo &info,
optional_ptr<ClientContext> context)
MacroCatalogEntry::MacroCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateMacroInfo &info)
: FunctionEntry(
(info.function->type == MacroType::SCALAR_MACRO ? CatalogType::MACRO_ENTRY : CatalogType::TABLE_MACRO_ENTRY),
catalog, schema, info, context),
catalog, schema, info),
function(std::move(info.function)) {
this->temporary = info.temporary;
this->internal = info.internal;
}

ScalarMacroCatalogEntry::ScalarMacroCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateMacroInfo &info,
optional_ptr<ClientContext> context)
: MacroCatalogEntry(catalog, schema, info, context) {
ScalarMacroCatalogEntry::ScalarMacroCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateMacroInfo &info)
: MacroCatalogEntry(catalog, schema, info) {
}

TableMacroCatalogEntry::TableMacroCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateMacroInfo &info,
optional_ptr<ClientContext> context)
: MacroCatalogEntry(catalog, schema, info, context) {
TableMacroCatalogEntry::TableMacroCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateMacroInfo &info)
: MacroCatalogEntry(catalog, schema, info) {
}

unique_ptr<CreateInfo> MacroCatalogEntry::GetInfo() const {
Expand All @@ -30,7 +27,7 @@ unique_ptr<CreateInfo> MacroCatalogEntry::GetInfo() const {
info->schema = schema.name;
info->name = name;
info->function = function->Copy();
info->dependencies = dependencies.GetLogical();
info->dependencies = dependencies;
return std::move(info);
}

Expand Down
6 changes: 2 additions & 4 deletions src/catalog/catalog_entry/pragma_function_catalog_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
namespace duckdb {

PragmaFunctionCatalogEntry::PragmaFunctionCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema,
CreatePragmaFunctionInfo &info,
optional_ptr<ClientContext> context)
: FunctionEntry(CatalogType::PRAGMA_FUNCTION_ENTRY, catalog, schema, info, context),
functions(std::move(info.functions)) {
CreatePragmaFunctionInfo &info)
: FunctionEntry(CatalogType::PRAGMA_FUNCTION_ENTRY, catalog, schema, info), functions(std::move(info.functions)) {
}

} // namespace duckdb
7 changes: 3 additions & 4 deletions src/catalog/catalog_entry/scalar_function_catalog_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
namespace duckdb {

ScalarFunctionCatalogEntry::ScalarFunctionCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema,
CreateScalarFunctionInfo &info,
optional_ptr<ClientContext> context)
: FunctionEntry(CatalogType::SCALAR_FUNCTION_ENTRY, catalog, schema, info, context), functions(info.functions) {
CreateScalarFunctionInfo &info)
: FunctionEntry(CatalogType::SCALAR_FUNCTION_ENTRY, catalog, schema, info), functions(info.functions) {
}

unique_ptr<CatalogEntry> ScalarFunctionCatalogEntry::AlterEntry(ClientContext &context, AlterInfo &info) {
Expand All @@ -25,7 +24,7 @@ unique_ptr<CatalogEntry> ScalarFunctionCatalogEntry::AlterEntry(ClientContext &c
throw BinderException("Failed to add new function overloads to function \"%s\": function already exists", name);
}
CreateScalarFunctionInfo new_info(std::move(new_set));
return make_uniq<ScalarFunctionCatalogEntry>(catalog, schema, new_info, &context);
return make_uniq<ScalarFunctionCatalogEntry>(catalog, schema, new_info);
}

} // namespace duckdb
2 changes: 1 addition & 1 deletion src/catalog/catalog_entry/sequence_catalog_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ unique_ptr<CreateInfo> SequenceCatalogEntry::GetInfo() const {
result->max_value = max_value;
result->start_value = counter;
result->cycle = cycle;
result->dependencies = dependencies.GetLogical();
result->dependencies = dependencies;
return std::move(result);
}

Expand Down
7 changes: 3 additions & 4 deletions src/catalog/catalog_entry/table_catalog_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@

namespace duckdb {

TableCatalogEntry::TableCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateTableInfo &info,
optional_ptr<ClientContext> context)
TableCatalogEntry::TableCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateTableInfo &info)
: StandardEntry(CatalogType::TABLE_ENTRY, schema, catalog, info.table), columns(std::move(info.columns)),
constraints(std::move(info.constraints)) {
this->temporary = info.temporary;
this->dependencies = info.dependencies.GetPhysical(catalog, context);
this->dependencies = info.dependencies;
}

bool TableCatalogEntry::HasGeneratedColumns() const {
Expand Down Expand Up @@ -63,7 +62,7 @@ unique_ptr<CreateInfo> TableCatalogEntry::GetInfo() const {
result->table = name;
result->columns = columns.Copy();
result->constraints.reserve(constraints.size());
result->dependencies = dependencies.GetLogical();
result->dependencies = dependencies;
std::for_each(constraints.begin(), constraints.end(),
[&result](const unique_ptr<Constraint> &c) { result->constraints.emplace_back(c->Copy()); });
return std::move(result);
Expand Down
7 changes: 3 additions & 4 deletions src/catalog/catalog_entry/table_function_catalog_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
namespace duckdb {

TableFunctionCatalogEntry::TableFunctionCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema,
CreateTableFunctionInfo &info, optional_ptr<ClientContext> context)
: FunctionEntry(CatalogType::TABLE_FUNCTION_ENTRY, catalog, schema, info, context),
functions(std::move(info.functions)) {
CreateTableFunctionInfo &info)
: FunctionEntry(CatalogType::TABLE_FUNCTION_ENTRY, catalog, schema, info), functions(std::move(info.functions)) {
D_ASSERT(this->functions.Size() > 0);
}

Expand All @@ -26,7 +25,7 @@ unique_ptr<CatalogEntry> TableFunctionCatalogEntry::AlterEntry(ClientContext &co
throw BinderException("Failed to add new function overloads to function \"%s\": function already exists", name);
}
CreateTableFunctionInfo new_info(std::move(new_set));
return make_uniq<TableFunctionCatalogEntry>(catalog, schema, new_info, &context);
return make_uniq<TableFunctionCatalogEntry>(catalog, schema, new_info);
}

} // namespace duckdb
7 changes: 3 additions & 4 deletions src/catalog/catalog_entry/type_catalog_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

namespace duckdb {

TypeCatalogEntry::TypeCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateTypeInfo &info,
optional_ptr<ClientContext> context)
TypeCatalogEntry::TypeCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateTypeInfo &info)
: StandardEntry(CatalogType::TYPE_ENTRY, schema, catalog, info.name), user_type(info.type) {
this->temporary = info.temporary;
this->internal = info.internal;
this->dependencies = info.dependencies.GetPhysical(catalog, context);
this->dependencies = info.dependencies;
}

unique_ptr<CreateInfo> TypeCatalogEntry::GetInfo() const {
Expand All @@ -23,7 +22,7 @@ unique_ptr<CreateInfo> TypeCatalogEntry::GetInfo() const {
result->schema = schema.name;
result->name = name;
result->type = user_type;
result->dependencies = dependencies.GetLogical();
result->dependencies = dependencies;
return std::move(result);
}

Expand Down
13 changes: 6 additions & 7 deletions src/catalog/catalog_entry/view_catalog_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@

namespace duckdb {

void ViewCatalogEntry::Initialize(CreateViewInfo &info, optional_ptr<ClientContext> context, Catalog &catalog) {
void ViewCatalogEntry::Initialize(CreateViewInfo &info, Catalog &catalog) {
query = std::move(info.query);
this->aliases = info.aliases;
this->types = info.types;
this->temporary = info.temporary;
this->sql = info.sql;
this->internal = info.internal;
this->dependencies = info.dependencies.GetPhysical(catalog, context);
this->dependencies = info.dependencies;
}

ViewCatalogEntry::ViewCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateViewInfo &info,
optional_ptr<ClientContext> context)
ViewCatalogEntry::ViewCatalogEntry(Catalog &catalog, SchemaCatalogEntry &schema, CreateViewInfo &info)
: StandardEntry(CatalogType::VIEW_ENTRY, schema, catalog, info.view_name) {
Initialize(info, context, catalog);
Initialize(info, catalog);
}

unique_ptr<CreateInfo> ViewCatalogEntry::GetInfo() const {
Expand All @@ -34,7 +33,7 @@ unique_ptr<CreateInfo> ViewCatalogEntry::GetInfo() const {
result->query = unique_ptr_cast<SQLStatement, SelectStatement>(query->Copy());
result->aliases = aliases;
result->types = types;
result->dependencies = dependencies.GetLogical();
result->dependencies = dependencies;
return std::move(result);
}

Expand Down Expand Up @@ -77,7 +76,7 @@ unique_ptr<CatalogEntry> ViewCatalogEntry::Copy(ClientContext &context) const {
create_info.temporary = temporary;
create_info.sql = sql;

return make_uniq<ViewCatalogEntry>(catalog, schema, create_info, &context);
return make_uniq<ViewCatalogEntry>(catalog, schema, create_info);
}

} // namespace duckdb
5 changes: 2 additions & 3 deletions src/catalog/catalog_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void CatalogSet::PutEntry(EntryIndex index, unique_ptr<CatalogEntry> catalog_ent
}

bool CatalogSet::CreateEntry(CatalogTransaction transaction, const string &name, unique_ptr<CatalogEntry> value,
PhysicalDependencyList &dependencies) {
LogicalDependencyList &dependencies) {
if (value->internal && !catalog.IsSystemCatalog() && name != DEFAULT_SCHEMA) {
throw InternalException("Attempting to create internal entry \"%s\" in non-system catalog - internal entries "
"can only be created in the system catalog",
Expand Down Expand Up @@ -153,8 +153,7 @@ bool CatalogSet::CreateEntry(CatalogTransaction transaction, const string &name,
bool CatalogSet::CreateEntry(ClientContext &context, const string &name, unique_ptr<CatalogEntry> value,
LogicalDependencyList &dependencies) {
auto &catalog = GetCatalog();
auto physical_dependencies = dependencies.GetPhysical(catalog, &context);
return CreateEntry(catalog.GetCatalogTransaction(context), name, std::move(value), physical_dependencies);
return CreateEntry(catalog.GetCatalogTransaction(context), name, std::move(value), dependencies);
}

optional_ptr<CatalogEntry> CatalogSet::GetEntryInternal(CatalogTransaction transaction, EntryIndex &entry_index) {
Expand Down
2 changes: 1 addition & 1 deletion src/catalog/default/default_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ unique_ptr<CatalogEntry> DefaultFunctionGenerator::CreateDefaultEntry(ClientCont
const string &entry_name) {
auto info = GetDefaultFunction(schema.name, entry_name);
if (info) {
return make_uniq_base<CatalogEntry, ScalarMacroCatalogEntry>(catalog, schema, info->Cast<CreateMacroInfo>(), &context);
return make_uniq_base<CatalogEntry, ScalarMacroCatalogEntry>(catalog, schema, info->Cast<CreateMacroInfo>());
}
return nullptr;
}
Expand Down
Loading

0 comments on commit bea69f7

Please sign in to comment.