Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/iceberg/arrow/arrow_error_transform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include <arrow/result.h>
#include <arrow/status.h>

#include "iceberg/error.h"
#include "iceberg/expected.h"
#include "iceberg/result.h"

namespace iceberg::arrow {

Expand Down
11 changes: 5 additions & 6 deletions src/iceberg/arrow/arrow_fs_file_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
namespace iceberg::arrow {

/// \brief Read the content of the file at the given location.
expected<std::string, Error> ArrowFileSystemFileIO::ReadFile(
const std::string& file_location, std::optional<size_t> length) {
Result<std::string> ArrowFileSystemFileIO::ReadFile(const std::string& file_location,
std::optional<size_t> length) {
::arrow::fs::FileInfo file_info(file_location);
if (length.has_value()) {
file_info.set_size(length.value());
Expand All @@ -52,8 +52,8 @@ expected<std::string, Error> ArrowFileSystemFileIO::ReadFile(
}

/// \brief Write the given content to the file at the given location.
expected<void, Error> ArrowFileSystemFileIO::WriteFile(const std::string& file_location,
std::string_view content) {
Status ArrowFileSystemFileIO::WriteFile(const std::string& file_location,
std::string_view content) {
ICEBERG_ARROW_ASSIGN_OR_RETURN(auto file, arrow_fs_->OpenOutputStream(file_location));
ICEBERG_ARROW_RETURN_NOT_OK(file->Write(content.data(), content.size()));
ICEBERG_ARROW_RETURN_NOT_OK(file->Flush());
Expand All @@ -62,8 +62,7 @@ expected<void, Error> ArrowFileSystemFileIO::WriteFile(const std::string& file_l
}

/// \brief Delete a file at the given location.
expected<void, Error> ArrowFileSystemFileIO::DeleteFile(
const std::string& file_location) {
Status ArrowFileSystemFileIO::DeleteFile(const std::string& file_location) {
ICEBERG_ARROW_RETURN_NOT_OK(arrow_fs_->DeleteFile(file_location));
return {};
}
Expand Down
9 changes: 4 additions & 5 deletions src/iceberg/arrow/arrow_fs_file_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ class ICEBERG_BUNDLE_EXPORT ArrowFileSystemFileIO : public FileIO {
~ArrowFileSystemFileIO() override = default;

/// \brief Read the content of the file at the given location.
expected<std::string, Error> ReadFile(const std::string& file_location,
std::optional<size_t> length) override;
Result<std::string> ReadFile(const std::string& file_location,
std::optional<size_t> length) override;

/// \brief Write the given content to the file at the given location.
expected<void, Error> WriteFile(const std::string& file_location,
std::string_view content) override;
Status WriteFile(const std::string& file_location, std::string_view content) override;

/// \brief Delete a file at the given location.
expected<void, Error> DeleteFile(const std::string& file_location) override;
Status DeleteFile(const std::string& file_location) override;

private:
std::shared_ptr<::arrow::fs::FileSystem> arrow_fs_;
Expand Down
29 changes: 14 additions & 15 deletions src/iceberg/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

#pragma once

#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

#include "iceberg/error.h"
#include "iceberg/expected.h"
#include "iceberg/result.h"
#include "iceberg/table_identifier.h"
#include "iceberg/type_fwd.h"

Expand All @@ -48,8 +47,7 @@ class ICEBERG_EXPORT Catalog {
/// \param ns a namespace
/// \return a list of identifiers for tables or ErrorKind::kNoSuchNamespace
/// if the namespace does not exist
virtual expected<std::vector<TableIdentifier>, Error> ListTables(
const Namespace& ns) const = 0;
virtual Result<std::vector<TableIdentifier>> ListTables(const Namespace& ns) const = 0;

/// \brief Create a table
///
Expand All @@ -59,18 +57,18 @@ class ICEBERG_EXPORT Catalog {
/// \param location a location for the table; leave empty if unspecified
/// \param properties a string map of table properties
/// \return a Table instance or ErrorKind::kAlreadyExists if the table already exists
virtual expected<std::unique_ptr<Table>, Error> CreateTable(
virtual Result<std::unique_ptr<Table>> CreateTable(
const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec,
const std::string& location,
const std::map<std::string, std::string>& properties) = 0;
const std::unordered_map<std::string, std::string>& properties) = 0;

/// \brief Update a table
///
/// \param identifier a table identifier
/// \param requirements a list of table requirements
/// \param updates a list of table updates
/// \return a Table instance or ErrorKind::kAlreadyExists if the table already exists
virtual expected<std::unique_ptr<Table>, Error> UpdateTable(
virtual Result<std::unique_ptr<Table>> UpdateTable(
const TableIdentifier& identifier,
const std::vector<std::unique_ptr<UpdateRequirement>>& requirements,
const std::vector<std::unique_ptr<MetadataUpdate>>& updates) = 0;
Expand All @@ -84,10 +82,10 @@ class ICEBERG_EXPORT Catalog {
/// \param properties a string map of table properties
/// \return a Transaction to create the table or ErrorKind::kAlreadyExists if the table
/// already exists
virtual expected<std::shared_ptr<Transaction>, Error> StageCreateTable(
virtual Result<std::shared_ptr<Transaction>> StageCreateTable(
const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec,
const std::string& location,
const std::map<std::string, std::string>& properties) = 0;
const std::unordered_map<std::string, std::string>& properties) = 0;

/// \brief Check whether table exists
///
Expand All @@ -110,15 +108,15 @@ class ICEBERG_EXPORT Catalog {
/// \param identifier a table identifier
/// \return instance of Table implementation referred to by identifier or
/// ErrorKind::kNoSuchTable if the table does not exist
virtual expected<std::shared_ptr<Table>, Error> LoadTable(
virtual Result<std::shared_ptr<Table>> LoadTable(
const TableIdentifier& identifier) const = 0;

/// \brief Register a table with the catalog if it does not exist
///
/// \param identifier a table identifier
/// \param metadata_file_location the location of a metadata file
/// \return a Table instance or ErrorKind::kAlreadyExists if the table already exists
virtual expected<std::shared_ptr<Table>, Error> RegisterTable(
virtual Result<std::shared_ptr<Table>> RegisterTable(
const TableIdentifier& identifier, const std::string& metadata_file_location) = 0;

/// \brief Initialize a catalog given a custom name and a map of catalog properties
Expand All @@ -129,8 +127,9 @@ class ICEBERG_EXPORT Catalog {
///
/// \param name a custom name for the catalog
/// \param properties catalog properties
virtual void Initialize(const std::string& name,
const std::map<std::string, std::string>& properties) = 0;
virtual void Initialize(
const std::string& name,
const std::unordered_map<std::string, std::string>& properties) = 0;

/// \brief Instantiate a builder to either create a table or start a create/replace
/// transaction
Expand Down Expand Up @@ -169,7 +168,7 @@ class ICEBERG_EXPORT Catalog {
/// \param properties key/value properties
/// \return this for method chaining
virtual TableBuilder& WithProperties(
const std::map<std::string, std::string>& properties) = 0;
const std::unordered_map<std::string, std::string>& properties) = 0;

/// \brief Adds a key/value property to the table
///
Expand Down
12 changes: 5 additions & 7 deletions src/iceberg/file_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
#include <string>
#include <string_view>

#include "iceberg/error.h"
#include "iceberg/expected.h"
#include "iceberg/iceberg_export.h"
#include "iceberg/result.h"

namespace iceberg {

Expand All @@ -50,8 +49,8 @@ class ICEBERG_EXPORT FileIO {
/// the length to read, e.g. S3 `GetObject` has a Range parameter.
/// \return The content of the file if the read succeeded, an error code if the read
/// failed.
virtual expected<std::string, Error> ReadFile(const std::string& file_location,
std::optional<size_t> length) {
virtual Result<std::string> ReadFile(const std::string& file_location,
std::optional<size_t> length) {
// We provide a default implementation to avoid Windows linker error LNK2019.
return unexpected<Error>{
{.kind = ErrorKind::kNotImplemented, .message = "ReadFile not implemented"}};
Expand All @@ -64,8 +63,7 @@ class ICEBERG_EXPORT FileIO {
/// \param overwrite If true, overwrite the file if it exists. If false, fail if the
/// file exists.
/// \return void if the write succeeded, an error code if the write failed.
virtual expected<void, Error> WriteFile(const std::string& file_location,
std::string_view content) {
virtual Status WriteFile(const std::string& file_location, std::string_view content) {
return unexpected<Error>{
{.kind = ErrorKind::kNotImplemented, .message = "WriteFile not implemented"}};
}
Expand All @@ -74,7 +72,7 @@ class ICEBERG_EXPORT FileIO {
///
/// \param file_location The location of the file to delete.
/// \return void if the delete succeeded, an error code if the delete failed.
virtual expected<void, Error> DeleteFile(const std::string& file_location) {
virtual Status DeleteFile(const std::string& file_location) {
return unexpected<Error>{
{.kind = ErrorKind::kNotImplemented, .message = "DeleteFile not implemented"}};
}
Expand Down
22 changes: 10 additions & 12 deletions src/iceberg/json_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

#include <nlohmann/json.hpp>

#include "iceberg/error.h"
#include "iceberg/expected.h"
#include "iceberg/result.h"
#include "iceberg/schema.h"
#include "iceberg/schema_internal.h"
#include "iceberg/sort_order.h"
Expand Down Expand Up @@ -69,7 +69,7 @@ constexpr std::string_view kElementRequired = "element-required";
constexpr std::string_view kValueRequired = "value-required";

template <typename T>
expected<T, Error> GetJsonValue(const nlohmann::json& json, std::string_view key) {
Result<T> GetJsonValue(const nlohmann::json& json, std::string_view key) {
if (!json.contains(key)) {
return unexpected<Error>({
.kind = ErrorKind::kJsonParseError,
Expand Down Expand Up @@ -109,8 +109,7 @@ nlohmann::json ToJson(const SortOrder& sort_order) {
return json;
}

expected<std::unique_ptr<SortField>, Error> SortFieldFromJson(
const nlohmann::json& json) {
Result<std::unique_ptr<SortField>> SortFieldFromJson(const nlohmann::json& json) {
ICEBERG_ASSIGN_OR_RAISE(auto source_id, GetJsonValue<int32_t>(json, kSourceId));
ICEBERG_ASSIGN_OR_RAISE(
auto transform,
Expand All @@ -125,8 +124,7 @@ expected<std::unique_ptr<SortField>, Error> SortFieldFromJson(
null_order);
}

expected<std::unique_ptr<SortOrder>, Error> SortOrderFromJson(
const nlohmann::json& json) {
Result<std::unique_ptr<SortOrder>> SortOrderFromJson(const nlohmann::json& json) {
ICEBERG_ASSIGN_OR_RAISE(auto order_id, GetJsonValue<int32_t>(json, kOrderId));
ICEBERG_ASSIGN_OR_RAISE(auto fields, GetJsonValue<nlohmann::json>(json, kFields));

Expand Down Expand Up @@ -232,7 +230,7 @@ nlohmann::json SchemaToJson(const Schema& schema) {

namespace {

expected<std::unique_ptr<Type>, Error> StructTypeFromJson(const nlohmann::json& json) {
Result<std::unique_ptr<Type>> StructTypeFromJson(const nlohmann::json& json) {
ICEBERG_ASSIGN_OR_RAISE(auto json_fields, GetJsonValue<nlohmann::json>(json, kFields));

std::vector<SchemaField> fields;
Expand All @@ -244,7 +242,7 @@ expected<std::unique_ptr<Type>, Error> StructTypeFromJson(const nlohmann::json&
return std::make_unique<StructType>(std::move(fields));
}

expected<std::unique_ptr<Type>, Error> ListTypeFromJson(const nlohmann::json& json) {
Result<std::unique_ptr<Type>> ListTypeFromJson(const nlohmann::json& json) {
ICEBERG_ASSIGN_OR_RAISE(auto element_type, TypeFromJson(json[kElement]));
ICEBERG_ASSIGN_OR_RAISE(auto element_id, GetJsonValue<int32_t>(json, kElementId));
ICEBERG_ASSIGN_OR_RAISE(auto element_required,
Expand All @@ -255,7 +253,7 @@ expected<std::unique_ptr<Type>, Error> ListTypeFromJson(const nlohmann::json& js
std::move(element_type), !element_required));
}

expected<std::unique_ptr<Type>, Error> MapTypeFromJson(const nlohmann::json& json) {
Result<std::unique_ptr<Type>> MapTypeFromJson(const nlohmann::json& json) {
ICEBERG_ASSIGN_OR_RAISE(
auto key_type, GetJsonValue<nlohmann::json>(json, kKey).and_then(TypeFromJson));
ICEBERG_ASSIGN_OR_RAISE(
Expand All @@ -274,7 +272,7 @@ expected<std::unique_ptr<Type>, Error> MapTypeFromJson(const nlohmann::json& jso

} // namespace

expected<std::unique_ptr<Type>, Error> TypeFromJson(const nlohmann::json& json) {
Result<std::unique_ptr<Type>> TypeFromJson(const nlohmann::json& json) {
if (json.is_string()) {
std::string type_str = json.get<std::string>();
if (type_str == "boolean") {
Expand Down Expand Up @@ -346,7 +344,7 @@ expected<std::unique_ptr<Type>, Error> TypeFromJson(const nlohmann::json& json)
}
}

expected<std::unique_ptr<SchemaField>, Error> FieldFromJson(const nlohmann::json& json) {
Result<std::unique_ptr<SchemaField>> FieldFromJson(const nlohmann::json& json) {
ICEBERG_ASSIGN_OR_RAISE(
auto type, GetJsonValue<nlohmann::json>(json, kType).and_then(TypeFromJson));
ICEBERG_ASSIGN_OR_RAISE(auto field_id, GetJsonValue<int32_t>(json, kId));
Expand All @@ -357,7 +355,7 @@ expected<std::unique_ptr<SchemaField>, Error> FieldFromJson(const nlohmann::json
!required);
}

expected<std::unique_ptr<Schema>, Error> SchemaFromJson(const nlohmann::json& json) {
Result<std::unique_ptr<Schema>> SchemaFromJson(const nlohmann::json& json) {
ICEBERG_ASSIGN_OR_RAISE(auto schema_id, GetJsonValue<int32_t>(json, kSchemaId));
ICEBERG_ASSIGN_OR_RAISE(auto type, TypeFromJson(json));

Expand Down
12 changes: 6 additions & 6 deletions src/iceberg/json_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

#include <nlohmann/json_fwd.hpp>

#include "iceberg/error.h"
#include "iceberg/expected.h"
#include "iceberg/result.h"
#include "iceberg/type_fwd.h"

namespace iceberg {
Expand Down Expand Up @@ -59,7 +59,7 @@ nlohmann::json ToJson(const SortOrder& sort_order);
/// \param json The JSON object representing a `SortField`.
/// \return An `expected` value containing either a `SortField` object or an error. If the
/// JSON is malformed or missing expected fields, an error will be returned.
expected<std::unique_ptr<SortField>, Error> SortFieldFromJson(const nlohmann::json& json);
Result<std::unique_ptr<SortField>> SortFieldFromJson(const nlohmann::json& json);

/// \brief Deserializes a JSON object into a `SortOrder` object.
///
Expand All @@ -70,7 +70,7 @@ expected<std::unique_ptr<SortField>, Error> SortFieldFromJson(const nlohmann::js
/// \param json The JSON object representing a `SortOrder`.
/// \return An `expected` value containing either a `SortOrder` object or an error. If the
/// JSON is malformed or missing expected fields, an error will be returned.
expected<std::unique_ptr<SortOrder>, Error> SortOrderFromJson(const nlohmann::json& json);
Result<std::unique_ptr<SortOrder>> SortOrderFromJson(const nlohmann::json& json);

/// \brief Convert an Iceberg Schema to JSON.
///
Expand All @@ -94,18 +94,18 @@ nlohmann::json FieldToJson(const SchemaField& field);
///
/// \param[in] json The JSON representation of the schema.
/// \return The Iceberg schema or an error if the conversion fails.
expected<std::unique_ptr<Schema>, Error> SchemaFromJson(const nlohmann::json& json);
Result<std::unique_ptr<Schema>> SchemaFromJson(const nlohmann::json& json);

/// \brief Convert JSON to an Iceberg Type.
///
/// \param[in] json The JSON representation of the type.
/// \return The Iceberg type or an error if the conversion fails.
expected<std::unique_ptr<Type>, Error> TypeFromJson(const nlohmann::json& json);
Result<std::unique_ptr<Type>> TypeFromJson(const nlohmann::json& json);

/// \brief Convert JSON to an Iceberg SchemaField.
///
/// \param[in] json The JSON representation of the field.
/// \return The Iceberg field or an error if the conversion fails.
expected<std::unique_ptr<SchemaField>, Error> FieldFromJson(const nlohmann::json& json);
Result<std::unique_ptr<SchemaField>> FieldFromJson(const nlohmann::json& json);

} // namespace iceberg
13 changes: 13 additions & 0 deletions src/iceberg/error.h → src/iceberg/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <string>

#include "iceberg/expected.h"
#include "iceberg/iceberg_export.h"

namespace iceberg {
Expand All @@ -47,4 +48,16 @@ struct ICEBERG_EXPORT [[nodiscard]] Error {
std::string message;
};

/// /brief Default error trait
template <typename T>
struct DefaultError {
using type = Error;
};

/// \brief Result alias
template <typename T, typename E = typename DefaultError<T>::type>
using Result = expected<T, E>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we default T to void so we can simply use Result as the return type if nothing to return?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also add using Status = expected<void, Error>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I will uniformly modify the places where expected<void, Error> is used


using Status = Result<void>;

} // namespace iceberg
Loading
Loading