@@ -77,8 +77,8 @@ Result<CatalogConfig> FetchServerConfig(const ResourcePaths& paths,
7777
7878RestCatalog::~RestCatalog () = default ;
7979
80- Result<std::unique_ptr <RestCatalog>> RestCatalog::Make (
81- const RestCatalogProperties& config) {
80+ Result<std::shared_ptr <RestCatalog>> RestCatalog::Make (
81+ const RestCatalogProperties& config, std::shared_ptr<FileIO> file_io ) {
8282 ICEBERG_ASSIGN_OR_RAISE (auto uri, config.Uri ());
8383 ICEBERG_ASSIGN_OR_RAISE (
8484 auto paths, ResourcePaths::Make (std::string (TrimTrailingSlash (uri)),
@@ -103,18 +103,21 @@ Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(
103103 ICEBERG_ASSIGN_OR_RAISE (auto final_uri, final_config->Uri ());
104104 ICEBERG_RETURN_UNEXPECTED (paths->SetBaseUri (std::string (TrimTrailingSlash (final_uri))));
105105
106- return std::unique_ptr<RestCatalog>(
107- new RestCatalog (std::move (final_config), std::move (paths), std::move (endpoints)));
106+ return std::shared_ptr<RestCatalog>(
107+ new RestCatalog (std::move (final_config), std::move (paths), std::move (endpoints),
108+ std::move (file_io)));
108109}
109110
110111RestCatalog::RestCatalog (std::unique_ptr<RestCatalogProperties> config,
111112 std::unique_ptr<ResourcePaths> paths,
112- std::unordered_set<Endpoint> endpoints)
113+ std::unordered_set<Endpoint> endpoints,
114+ std::shared_ptr<FileIO> file_io)
113115 : config_(std::move(config)),
114116 client_ (std::make_unique<HttpClient>(config_->ExtractHeaders ())),
115117 paths_(std::move(paths)),
116118 name_(config_->Get (RestCatalogProperties::kName )),
117- supported_endpoints_(std::move(endpoints)) {}
119+ supported_endpoints_(std::move(endpoints)),
120+ file_io_(std::move(file_io)) {}
118121
119122std::string_view RestCatalog::name () const { return name_; }
120123
@@ -241,11 +244,50 @@ Result<std::vector<TableIdentifier>> RestCatalog::ListTables(
241244}
242245
243246Result<std::unique_ptr<Table>> RestCatalog::CreateTable (
244- [[maybe_unused]] const TableIdentifier& identifier,
245- [[maybe_unused]] const Schema& schema, [[maybe_unused]] const PartitionSpec& spec,
246- [[maybe_unused]] const std::string& location,
247- [[maybe_unused]] const std::unordered_map<std::string, std::string>& properties) {
248- return NotImplemented (" Not implemented" );
247+ const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec,
248+ const SortOrder& order, const std::string& location,
249+ const std::unordered_map<std::string, std::string>& properties) {
250+ ICEBERG_RETURN_UNEXPECTED (CheckEndpoint (supported_endpoints_, Endpoint::CreateTable ()));
251+ ICEBERG_ASSIGN_OR_RAISE (auto path, paths_->Tables (identifier.ns ));
252+
253+ // Schema, partition spec, and sort order are not copyable due to Lazy<> members,
254+ // recreate them
255+ auto new_schema = std::make_shared<Schema>(
256+ std::vector<SchemaField>(schema.fields ().begin (), schema.fields ().end ()),
257+ schema.schema_id ());
258+
259+ ICEBERG_ASSIGN_OR_RAISE (
260+ std::shared_ptr<PartitionSpec> partition_spec,
261+ PartitionSpec::Make (
262+ spec.spec_id (),
263+ std::vector<PartitionField>(spec.fields ().begin (), spec.fields ().end ()),
264+ spec.last_assigned_field_id ()));
265+
266+ ICEBERG_ASSIGN_OR_RAISE (
267+ std::shared_ptr<SortOrder> sort_order,
268+ SortOrder::Make (order.order_id (), std::vector<SortField>(order.fields ().begin (),
269+ order.fields ().end ())));
270+
271+ CreateTableRequest request{
272+ .name = identifier.name ,
273+ .location = location,
274+ .schema = new_schema,
275+ .partition_spec = partition_spec,
276+ .write_order = sort_order,
277+ .stage_create = false ,
278+ .properties = properties,
279+ };
280+
281+ ICEBERG_ASSIGN_OR_RAISE (auto json_request, ToJsonString (ToJson (request)));
282+ ICEBERG_ASSIGN_OR_RAISE (
283+ const auto response,
284+ client_->Post (path, json_request, /* headers=*/ {}, *TableErrorHandler::Instance ()));
285+
286+ ICEBERG_ASSIGN_OR_RAISE (auto json, FromJsonString (response.body ()));
287+ ICEBERG_ASSIGN_OR_RAISE (auto load_result, LoadTableResultFromJson (json));
288+ return std::make_unique<Table>(identifier, std::move (load_result.metadata ),
289+ std::move (load_result.metadata_location ), file_io_,
290+ std::static_pointer_cast<Catalog>(shared_from_this ()));
249291}
250292
251293Result<std::unique_ptr<Table>> RestCatalog::UpdateTable (
@@ -258,7 +300,7 @@ Result<std::unique_ptr<Table>> RestCatalog::UpdateTable(
258300Result<std::shared_ptr<Transaction>> RestCatalog::StageCreateTable (
259301 [[maybe_unused]] const TableIdentifier& identifier,
260302 [[maybe_unused]] const Schema& schema, [[maybe_unused]] const PartitionSpec& spec,
261- [[maybe_unused]] const std::string& location,
303+ [[maybe_unused]] const SortOrder& order, [[maybe_unused]] const std::string& location,
262304 [[maybe_unused]] const std::unordered_map<std::string, std::string>& properties) {
263305 return NotImplemented (" Not implemented" );
264306}
0 commit comments