|
22 | 22 | #include <algorithm> |
23 | 23 | #include <iterator> |
24 | 24 |
|
| 25 | +#include "iceberg/sort_order.h" |
25 | 26 | #include "iceberg/table.h" |
26 | 27 | #include "iceberg/table_identifier.h" |
27 | 28 | #include "iceberg/table_metadata.h" |
@@ -318,7 +319,7 @@ Result<std::string> InMemoryNamespace::GetTableMetadataLocation( |
318 | 319 | ICEBERG_RETURN_UNEXPECTED(ns); |
319 | 320 | const auto it = ns.value()->table_metadata_locations_.find(table_ident.name); |
320 | 321 | if (it == ns.value()->table_metadata_locations_.end()) { |
321 | | - return NotFound("{} does not exist", table_ident.name); |
| 322 | + return NotFound("Table does not exist: {}", table_ident); |
322 | 323 | } |
323 | 324 | return it->second; |
324 | 325 | } |
@@ -400,11 +401,25 @@ Result<std::vector<TableIdentifier>> InMemoryCatalog::ListTables( |
400 | 401 | } |
401 | 402 |
|
402 | 403 | Result<std::unique_ptr<Table>> InMemoryCatalog::CreateTable( |
403 | | - const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, |
404 | | - const std::string& location, |
| 404 | + const TableIdentifier& identifier, const std::string& location, const Schema& schema, |
| 405 | + const PartitionSpec& spec, const SortOrder& sort_order, |
405 | 406 | const std::unordered_map<std::string, std::string>& properties) { |
406 | 407 | std::unique_lock lock(mutex_); |
407 | | - return NotImplemented("create table"); |
| 408 | + if (root_namespace_->TableExists(identifier).value_or(false)) { |
| 409 | + return AlreadyExists("Table already exists: {}", identifier); |
| 410 | + } |
| 411 | + |
| 412 | + std::string base_location = |
| 413 | + location.empty() ? warehouse_location_ + "/" + identifier.ToString() : location; |
| 414 | + ICEBERG_ASSIGN_OR_RAISE(auto metadata, TableMetadata::Make(base_location, schema, spec, |
| 415 | + sort_order, properties)); |
| 416 | + ICEBERG_ASSIGN_OR_RAISE(auto metadata_file_location, |
| 417 | + TableMetadataUtil::Write(*file_io_, nullptr, "", *metadata)); |
| 418 | + ICEBERG_RETURN_UNEXPECTED( |
| 419 | + root_namespace_->UpdateTableMetadataLocation(identifier, metadata_file_location)); |
| 420 | + return std::make_unique<Table>(identifier, std::move(metadata), |
| 421 | + std::move(metadata_file_location), file_io_, |
| 422 | + std::static_pointer_cast<Catalog>(shared_from_this())); |
408 | 423 | } |
409 | 424 |
|
410 | 425 | Result<std::unique_ptr<Table>> InMemoryCatalog::UpdateTable( |
@@ -440,11 +455,20 @@ Result<std::unique_ptr<Table>> InMemoryCatalog::UpdateTable( |
440 | 455 | } |
441 | 456 |
|
442 | 457 | Result<std::shared_ptr<Transaction>> InMemoryCatalog::StageCreateTable( |
443 | | - const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, |
444 | | - const std::string& location, |
| 458 | + const TableIdentifier& identifier, const std::string& location, const Schema& schema, |
| 459 | + const PartitionSpec& spec, const SortOrder& sort_order, |
445 | 460 | const std::unordered_map<std::string, std::string>& properties) { |
446 | 461 | std::unique_lock lock(mutex_); |
447 | | - return NotImplemented("stage create table"); |
| 462 | + if (root_namespace_->TableExists(identifier).value_or(false)) { |
| 463 | + return AlreadyExists("Table already exists: {}", identifier); |
| 464 | + } |
| 465 | + |
| 466 | + std::string base_location = |
| 467 | + location.empty() ? warehouse_location_ + "/" + identifier.ToString() : location; |
| 468 | + ICEBERG_ASSIGN_OR_RAISE(auto metadata, TableMetadata::Make(base_location, schema, spec, |
| 469 | + sort_order, properties)); |
| 470 | + // TODO(zhuo.wang) Init transaction with metadata |
| 471 | + return nullptr; |
448 | 472 | } |
449 | 473 |
|
450 | 474 | Result<bool> InMemoryCatalog::TableExists(const TableIdentifier& identifier) const { |
@@ -495,7 +519,7 @@ Result<std::shared_ptr<Table>> InMemoryCatalog::RegisterTable( |
495 | 519 |
|
496 | 520 | std::unique_lock lock(mutex_); |
497 | 521 | if (!root_namespace_->NamespaceExists(identifier.ns)) { |
498 | | - return NoSuchNamespace("table namespace does not exist."); |
| 522 | + return NoSuchNamespace("Table namespace does not exist: {}", identifier.ns); |
499 | 523 | } |
500 | 524 | if (!root_namespace_->RegisterTable(identifier, metadata_file_location)) { |
501 | 525 | return UnknownError("The registry failed."); |
|
0 commit comments