#include "processor/operator/ddl/drop.h" #include "catalog/catalog.h" #include "catalog/catalog_entry/index_catalog_entry.h" #include "catalog/catalog_entry/node_table_catalog_entry.h" #include "catalog/catalog_entry/rel_group_catalog_entry.h" #include "catalog/catalog_entry/sequence_catalog_entry.h" #include "common/exception/binder.h" #include "common/exception/catalog.h" #include "common/string_utils.h" #include "function/sequence/sequence_functions.h" #include "main/client_context.h" #include "main/database.h" #include "main/database_manager.h" #include "parser/expression/parsed_function_expression.h" #include "parser/expression/parsed_literal_expression.h" #include "processor/execution_context.h" #include "storage/buffer_manager/memory_manager.h" #include "storage/storage_manager.h" #include "storage/table/node_table.h" #include "transaction/transaction.h" #include "transaction/transaction_manager.h" #include using namespace lbug::catalog; using namespace lbug::common; namespace lbug { namespace processor { void Drop::executeInternal(ExecutionContext* context) { auto clientContext = context->clientContext; switch (dropInfo.dropType) { case DropType::SEQUENCE: { dropSequence(clientContext); } break; case DropType::TABLE: { dropTable(clientContext); } break; case DropType::MACRO: { dropMacro(clientContext); } break; case DropType::GRAPH: { dropGraph(clientContext); } break; case DropType::INDEX: { dropIndex(clientContext); } break; default: UNREACHABLE_CODE; } } namespace { // The table property whose SERIAL column uses the sequence, as ".", or an empty // string if no SERIAL column does. A column uses a sequence if it derives the sequence's name // (
__serial), or if its default calls nextval on it: the default keeps the old // name after a rename of the table or the property. The sequence of a column that was dropped is // not used by any column and can be dropped. std::string findSerialOwner(const Catalog& catalog, const transaction::Transaction* transaction, const std::string& sequenceName) { for (const auto* table : catalog.getTableEntries(transaction)) { for (idx_t i = 0; i < table->getNumProperties(); i++) { const auto& property = table->getProperty(i); if (property.getType().getLogicalTypeID() != LogicalTypeID::SERIAL) { continue; } auto owns = SequenceCatalogEntry::getSerialName(table->getName(), property.getName()) == sequenceName; const auto* defaultExpr = property.defaultExpr.get(); if (!owns && defaultExpr != nullptr && defaultExpr->getExpressionType() == ExpressionType::FUNCTION && defaultExpr->getNumChildren() == 1 && defaultExpr->getChild(0)->getExpressionType() == ExpressionType::LITERAL && StringUtils::getUpper( defaultExpr->constCast().getFunctionName()) == function::NextValFunction::name) { const auto& value = defaultExpr->getChild(0) ->constCast() .getValue(); owns = value.getDataType().getLogicalTypeID() == LogicalTypeID::STRING && value.getValue() == sequenceName; } if (owns) { return std::format("{}.{}", table->getName(), property.getName()); } } } return ""; } } // namespace void Drop::dropSequence(const main::ClientContext* context) { auto catalog = Catalog::Get(*context); auto transaction = transaction::Transaction::Get(*context); auto memoryManager = storage::MemoryManager::Get(*context); if (!catalog->containsSequence(transaction, dropInfo.name)) { auto message = std::format("Sequence {} does not exist.", dropInfo.name); switch (dropInfo.conflictAction) { case ConflictAction::ON_CONFLICT_DO_NOTHING: { appendMessage(message, memoryManager); return; } case ConflictAction::ON_CONFLICT_THROW: { throw BinderException(message); } default: UNREACHABLE_CODE; } } if (auto owner = findSerialOwner(*catalog, transaction, dropInfo.name); !owner.empty()) { throw BinderException(std::format("Cannot drop sequence {} because it is used by the " "SERIAL column {}. Drop the column or its table instead.", dropInfo.name, owner)); } catalog->dropSequence(transaction, dropInfo.name); appendMessage(std::format("Sequence {} has been dropped.", dropInfo.name), memoryManager); } void Drop::dropTable(const main::ClientContext* context) { auto catalog = Catalog::Get(*context); auto transaction = transaction::Transaction::Get(*context); auto memoryManager = storage::MemoryManager::Get(*context); if (!catalog->containsTable(transaction, dropInfo.name, context->useInternalCatalogEntry())) { auto message = std::format("Table {} does not exist.", dropInfo.name); switch (dropInfo.conflictAction) { case ConflictAction::ON_CONFLICT_DO_NOTHING: { appendMessage(message, memoryManager); return; } case ConflictAction::ON_CONFLICT_THROW: { throw BinderException(message); } default: UNREACHABLE_CODE; } } auto entry = catalog->getTableCatalogEntry(transaction, dropInfo.name); // Storage tables this DROP removes, keyed as LocalStorage keys them. const auto ownerCatalogName = entry->getOwningCatalogName(); std::vector storageTableIDs; switch (entry->getType()) { case CatalogEntryType::NODE_TABLE_ENTRY: { auto* nodeEntry = entry->ptrCast(); if (nodeEntry->isPartitionChild()) { // A partition subgraph is owned by its partitioned parent and is dropped with it. // Dropping it individually would leave the parent pointing at a missing table. auto* parent = catalog->getTableCatalogEntry(transaction, nodeEntry->getParentTableID()); throw BinderException(std::format( "Cannot drop table {} because it is a partition of partitioned table {}. " "Drop {} instead.", entry->getName(), parent->getName(), parent->getName())); } // A partitioned parent cascades to its partition subgraphs, so every table that this // DROP would remove must satisfy the reference checks below. This refuses the drop // atomically (before anything is dropped) when any partition is still referenced. std::vector droppingTables{nodeEntry}; if (nodeEntry->isPartitioned()) { droppingTables.reserve(nodeEntry->getChildTableIDs().size() + 1); for (auto childID : nodeEntry->getChildTableIDs()) { droppingTables.push_back(catalog->getTableCatalogEntry(transaction, childID) ->ptrCast()); } } for (auto* droppingEntry : droppingTables) { storageTableIDs.push_back(droppingEntry->getTableID()); for (auto& indexEntry : catalog->getIndexEntries(transaction)) { if (indexEntry->getTableID() == droppingEntry->getTableID()) { if (StringUtils::caseInsensitiveEquals(indexEntry->getIndexType(), "HASH") || StringUtils::caseInsensitiveEquals(indexEntry->getIndexType(), "ART")) { continue; } throw BinderException(std::format( "Cannot delete node table {} because it is referenced by index {}.", droppingEntry->getName(), indexEntry->getIndexName())); } } for (auto& relEntry : catalog->getRelGroupEntries(transaction)) { if (relEntry->isParent(droppingEntry->getTableID())) { throw BinderException(std::format("Cannot delete node table {} because it is " "referenced by relationship table {}.", droppingEntry->getName(), relEntry->getName())); } } } } break; case CatalogEntryType::REL_GROUP_ENTRY: { for (auto& relEntryInfo : entry->ptrCast()->getRelEntryInfos()) { storageTableIDs.push_back(relEntryInfo.oid); } } break; default: UNREACHABLE_CODE; } catalog->dropTableEntryAndIndex(transaction, dropInfo.name); // Checked after the catalog change, as TransactionManager::addLocalWriter requires. auto* transactionManager = transaction::TransactionManager::Get(*context); for (auto tableID : storageTableIDs) { if (transactionManager->hasLocalWriterOtherThan(ownerCatalogName, tableID, transaction->getLocalStorage())) { throw CatalogException(std::format("Write-write conflict on dropping table {}: " "another transaction has uncommitted inserts " "into it.", dropInfo.name)); } } appendMessage(std::format("Table {} has been dropped.", dropInfo.name), memoryManager); } void Drop::dropMacro(const main::ClientContext* context) { auto catalog = Catalog::Get(*context); auto transaction = transaction::Transaction::Get(*context); auto memoryManager = storage::MemoryManager::Get(*context); if (!handleMacroExistence(context)) { return; } catalog->dropMacro(transaction, dropInfo.name); appendMessage(std::format("Macro {} has been dropped.", dropInfo.name), memoryManager); } void Drop::dropGraph(const main::ClientContext* context) { auto dbManager = main::DatabaseManager::Get(*context); auto memoryManager = storage::MemoryManager::Get(*context); if (StringUtils::caseInsensitiveEquals(dropInfo.name, "main")) { throw BinderException{"Cannot drop the main graph."}; } // Every node table is registered as a subgraph (partition subgraphs included). Those // subgraphs are owned by their table: dropping one would desynchronize it from its table // (and, for a partition, from its partitioned parent), so refuse and point at the owner. const auto catalog = Catalog::Get(*context); const auto transaction = transaction::Transaction::Get(*context); if (catalog->containsGraph(transaction, dropInfo.name) && catalog->containsTable(transaction, dropInfo.name, context->useInternalCatalogEntry())) { throw BinderException(std::format( "Cannot drop graph {}: it is a node-table subgraph. Drop the node table instead.", dropInfo.name)); } if (!dbManager->hasGraph(dropInfo.name)) { auto message = std::format("Graph {} does not exist.", dropInfo.name); switch (dropInfo.conflictAction) { case ConflictAction::ON_CONFLICT_DO_NOTHING: { appendMessage(message, memoryManager); return; } case ConflictAction::ON_CONFLICT_THROW: { throw BinderException(message); } default: UNREACHABLE_CODE; } } if (dbManager->hasDefaultGraph() && StringUtils::getUpper(dbManager->getDefaultGraphName()) == StringUtils::getUpper(dropInfo.name)) { dbManager->clearDefaultGraph(); } dbManager->dropGraph(dropInfo.name, const_cast(context)); appendMessage(std::format("Graph {} has been dropped.", dropInfo.name), memoryManager); } void Drop::dropIndex(const main::ClientContext* context) { auto catalog = Catalog::Get(*context); auto transaction = transaction::Transaction::Get(*context); auto memoryManager = storage::MemoryManager::Get(*context); if (!catalog->containsTable(transaction, dropInfo.name, context->useInternalCatalogEntry())) { auto message = std::format("Table {} does not exist.", dropInfo.name); switch (dropInfo.conflictAction) { case ConflictAction::ON_CONFLICT_DO_NOTHING: { appendMessage(message, memoryManager); return; } case ConflictAction::ON_CONFLICT_THROW: { throw BinderException(message); } default: UNREACHABLE_CODE; } } auto tableEntry = catalog->getTableCatalogEntry(transaction, dropInfo.name); if (tableEntry->getType() != CatalogEntryType::NODE_TABLE_ENTRY) { throw BinderException( std::format("Table {} is not a node table; cannot drop index.", dropInfo.name)); } auto tableID = tableEntry->getTableID(); auto storageManager = storage::StorageManager::Get(*context); auto* nodeTable = storageManager->getTable(tableID)->ptrCast(); // An index may live in the catalog (user-created indexes), in storage only (the default // built-in PK hash index), or in both. Drop whichever are present. const auto inCatalog = catalog->containsIndex(transaction, tableID, dropInfo.indexName); // Use getIndexHolder (not getIndex) so an unloaded/orphan holder — whose catalog entry // is already gone — is still recognized as present and can be dropped, instead of // throwing "Index ... not loaded yet". const auto inStorage = nodeTable->getIndexHolder(dropInfo.indexName).has_value(); if (!inCatalog && !inStorage) { auto message = std::format("Index {} does not exist in table {}.", dropInfo.indexName, dropInfo.name); switch (dropInfo.conflictAction) { case ConflictAction::ON_CONFLICT_DO_NOTHING: { appendMessage(message, memoryManager); return; } case ConflictAction::ON_CONFLICT_THROW: { throw BinderException(message); } default: UNREACHABLE_CODE; } } if (inCatalog) { // Marks the IndexCatalogEntry deleted; the commit path emits the WAL drop record. catalog->dropIndex(transaction, tableID, dropInfo.indexName); } if (inStorage) { // Removes the in-memory index; its pages are reclaimed on the next checkpoint. nodeTable->dropIndex(dropInfo.indexName); // A storage-only index (the built-in PK hash index) has no catalog entry to record the // drop, but cached plans may rely on it (e.g. COUNT of the key bound as the internal id). transaction->recordCatalogChange(catalog); } appendMessage(std::format("Index {} has been dropped.", dropInfo.indexName), memoryManager); } bool Drop::handleMacroExistence(const main::ClientContext* context) { auto catalog = Catalog::Get(*context); auto transaction = transaction::Transaction::Get(*context); auto memoryManager = storage::MemoryManager::Get(*context); if (!catalog->containsMacro(transaction, dropInfo.name)) { auto message = std::format("Macro {} does not exist.", dropInfo.name); switch (dropInfo.conflictAction) { case ConflictAction::ON_CONFLICT_DO_NOTHING: { appendMessage(message, memoryManager); return false; } case ConflictAction::ON_CONFLICT_THROW: { throw BinderException(message); } default: UNREACHABLE_CODE; } } return true; } } // namespace processor } // namespace lbug