4040#include " iceberg/partition_spec.h"
4141#include " iceberg/result.h"
4242#include " iceberg/schema.h"
43+ #include " iceberg/sort_order.h"
4344#include " iceberg/table.h"
4445#include " iceberg/util/macros.h"
4546
@@ -77,9 +78,12 @@ Result<CatalogConfig> FetchServerConfig(const ResourcePaths& paths,
7778
7879RestCatalog::~RestCatalog () = default ;
7980
80- Result<std::unique_ptr <RestCatalog>> RestCatalog::Make (
81- const RestCatalogProperties& config) {
81+ Result<std::shared_ptr <RestCatalog>> RestCatalog::Make (
82+ const RestCatalogProperties& config, std::shared_ptr<FileIO> file_io ) {
8283 ICEBERG_ASSIGN_OR_RAISE (auto uri, config.Uri ());
84+ if (!file_io) {
85+ return InvalidArgument (" FileIO is required to create RestCatalog" );
86+ }
8387 ICEBERG_ASSIGN_OR_RAISE (
8488 auto paths, ResourcePaths::Make (std::string (TrimTrailingSlash (uri)),
8589 config.Get (RestCatalogProperties::kPrefix )));
@@ -103,14 +107,17 @@ Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(
103107 ICEBERG_ASSIGN_OR_RAISE (auto final_uri, final_config->Uri ());
104108 ICEBERG_RETURN_UNEXPECTED (paths->SetBaseUri (std::string (TrimTrailingSlash (final_uri))));
105109
106- return std::unique_ptr<RestCatalog>(
107- new RestCatalog (std::move (final_config), std::move (paths), std::move (endpoints)));
110+ return std::shared_ptr<RestCatalog>(
111+ new RestCatalog (std::move (final_config), std::move (file_io), std::move (paths),
112+ std::move (endpoints)));
108113}
109114
110115RestCatalog::RestCatalog (std::unique_ptr<RestCatalogProperties> config,
116+ std::shared_ptr<FileIO> file_io,
111117 std::unique_ptr<ResourcePaths> paths,
112118 std::unordered_set<Endpoint> endpoints)
113119 : config_(std::move(config)),
120+ file_io_ (std::move(file_io)),
114121 client_(std::make_unique<HttpClient>(config_->ExtractHeaders ())),
115122 paths_(std::move(paths)),
116123 name_(config_->Get (RestCatalogProperties::kName )),
@@ -241,11 +248,33 @@ Result<std::vector<TableIdentifier>> RestCatalog::ListTables(
241248}
242249
243250Result<std::shared_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" );
251+ const TableIdentifier& identifier, const std::shared_ptr<Schema>& schema,
252+ const std::shared_ptr<PartitionSpec>& spec, const std::shared_ptr<SortOrder>& order,
253+ const std::string& location,
254+ const std::unordered_map<std::string, std::string>& properties) {
255+ ICEBERG_RETURN_UNEXPECTED (CheckEndpoint (supported_endpoints_, Endpoint::CreateTable ()));
256+ ICEBERG_ASSIGN_OR_RAISE (auto path, paths_->Tables (identifier.ns ));
257+
258+ CreateTableRequest request{
259+ .name = identifier.name ,
260+ .location = location,
261+ .schema = schema,
262+ .partition_spec = spec,
263+ .write_order = order,
264+ .stage_create = false ,
265+ .properties = properties,
266+ };
267+
268+ ICEBERG_ASSIGN_OR_RAISE (auto json_request, ToJsonString (ToJson (request)));
269+ ICEBERG_ASSIGN_OR_RAISE (
270+ const auto response,
271+ client_->Post (path, json_request, /* headers=*/ {}, *TableErrorHandler::Instance ()));
272+
273+ ICEBERG_ASSIGN_OR_RAISE (auto json, FromJsonString (response.body ()));
274+ ICEBERG_ASSIGN_OR_RAISE (auto load_result, LoadTableResultFromJson (json));
275+ return Table::Make (identifier, load_result.metadata ,
276+ std::move (load_result.metadata_location ), file_io_,
277+ shared_from_this ());
249278}
250279
251280Result<std::shared_ptr<Table>> RestCatalog::UpdateTable (
@@ -257,7 +286,9 @@ Result<std::shared_ptr<Table>> RestCatalog::UpdateTable(
257286
258287Result<std::shared_ptr<Transaction>> RestCatalog::StageCreateTable (
259288 [[maybe_unused]] const TableIdentifier& identifier,
260- [[maybe_unused]] const Schema& schema, [[maybe_unused]] const PartitionSpec& spec,
289+ [[maybe_unused]] const std::shared_ptr<Schema>& schema,
290+ [[maybe_unused]] const std::shared_ptr<PartitionSpec>& spec,
291+ [[maybe_unused]] const std::shared_ptr<SortOrder>& order,
261292 [[maybe_unused]] const std::string& location,
262293 [[maybe_unused]] const std::unordered_map<std::string, std::string>& properties) {
263294 return NotImplemented (" Not implemented" );
0 commit comments