Skip to content

Commit 33d1fd7

Browse files
committed
fix: use macro to define the Error wrappers
Signed-off-by: Junwang Zhao <[email protected]>
1 parent ef0e808 commit 33d1fd7

File tree

11 files changed

+68
-144
lines changed

11 files changed

+68
-144
lines changed

src/iceberg/expression/expression.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ std::string And::ToString() const {
5151

5252
Result<std::shared_ptr<Expression>> And::Negate() const {
5353
// TODO(yingcai-cy): Implement Or expression
54-
return InvalidExpressionError("And negation not yet implemented");
54+
return InvalidExpression("And negation not yet implemented");
5555
}
5656

5757
bool And::Equals(const Expression& expr) const {

src/iceberg/expression/expression.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class ICEBERG_EXPORT Expression {
6868

6969
/// \brief Returns the negation of this expression, equivalent to not(this).
7070
virtual Result<std::shared_ptr<Expression>> Negate() const {
71-
return InvalidExpressionError("Expression cannot be negated");
71+
return InvalidExpression("Expression cannot be negated");
7272
}
7373

7474
/// \brief Returns whether this expression will accept the same values as another.

src/iceberg/file_io.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ICEBERG_EXPORT FileIO {
5252
virtual Result<std::string> ReadFile(const std::string& file_location,
5353
std::optional<size_t> length) {
5454
// We provide a default implementation to avoid Windows linker error LNK2019.
55-
return NotImplementedError("ReadFile not implemented");
55+
return NotImplemented("ReadFile not implemented");
5656
}
5757

5858
/// \brief Write the given content to the file at the given location.
@@ -63,15 +63,15 @@ class ICEBERG_EXPORT FileIO {
6363
/// file exists.
6464
/// \return void if the write succeeded, an error code if the write failed.
6565
virtual Status WriteFile(const std::string& file_location, std::string_view content) {
66-
return NotImplementedError("WriteFile not implemented");
66+
return NotImplemented("WriteFile not implemented");
6767
}
6868

6969
/// \brief Delete a file at the given location.
7070
///
7171
/// \param file_location The location of the file to delete.
7272
/// \return void if the delete succeeded, an error code if the delete failed.
7373
virtual Status DeleteFile(const std::string& file_location) {
74-
return NotImplementedError("DeleteFile not implemented");
74+
return NotImplemented("DeleteFile not implemented");
7575
}
7676
};
7777

src/iceberg/result.h

Lines changed: 23 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -62,99 +62,28 @@ using Result = expected<T, E>;
6262

6363
using Status = Result<void>;
6464

65-
/// \brief Create an unexpected error with kAlreadyExists
66-
template <typename... Args>
67-
auto AlreadyExistsError(const std::format_string<Args...> fmt, Args&&... args)
68-
-> unexpected<Error> {
69-
return unexpected<Error>({.kind = ErrorKind::kAlreadyExists,
70-
.message = std::format(fmt, std::forward<Args>(args)...)});
71-
}
72-
73-
/// \brief Create an unexpected error with kCommitStateUnknown
74-
template <typename... Args>
75-
auto CommitStateUnknownError(const std::format_string<Args...> fmt, Args&&... args)
76-
-> unexpected<Error> {
77-
return unexpected<Error>({.kind = ErrorKind::kCommitStateUnknown,
78-
.message = std::format(fmt, std::forward<Args>(args)...)});
79-
}
80-
81-
/// \brief Create an unexpected error with kInvalidArgument
82-
template <typename... Args>
83-
auto InvalidArgumentError(const std::format_string<Args...> fmt, Args&&... args)
84-
-> unexpected<Error> {
85-
return unexpected<Error>({.kind = ErrorKind::kInvalidArgument,
86-
.message = std::format(fmt, std::forward<Args>(args)...)});
87-
}
88-
89-
/// \brief Create an unexpected error with kInvalidExpression
90-
template <typename... Args>
91-
auto InvalidExpressionError(const std::format_string<Args...> fmt, Args&&... args)
92-
-> unexpected<Error> {
93-
return unexpected<Error>({.kind = ErrorKind::kInvalidExpression,
94-
.message = std::format(fmt, std::forward<Args>(args)...)});
95-
}
96-
97-
/// \brief Create an unexpected error with kInvalidSchema
98-
template <typename... Args>
99-
auto InvalidSchemaError(const std::format_string<Args...> fmt, Args&&... args)
100-
-> unexpected<Error> {
101-
return unexpected<Error>({.kind = ErrorKind::kInvalidSchema,
102-
.message = std::format(fmt, std::forward<Args>(args)...)});
103-
}
104-
105-
/// \brief Create an unexpected error with kIOError
106-
template <typename... Args>
107-
auto IOError(const std::format_string<Args...> fmt, Args&&... args) -> unexpected<Error> {
108-
return unexpected<Error>({.kind = ErrorKind::kIOError,
109-
.message = std::format(fmt, std::forward<Args>(args)...)});
110-
}
111-
112-
/// \brief Create an unexpected error with kJsonParseError
113-
template <typename... Args>
114-
auto JsonParseError(const std::format_string<Args...> fmt, Args&&... args)
115-
-> unexpected<Error> {
116-
return unexpected<Error>({.kind = ErrorKind::kJsonParseError,
117-
.message = std::format(fmt, std::forward<Args>(args)...)});
118-
}
119-
120-
/// \brief Create an unexpected error with kNoSuchNamespace
121-
template <typename... Args>
122-
auto NoSuchNamespaceError(const std::format_string<Args...> fmt, Args&&... args)
123-
-> unexpected<Error> {
124-
return unexpected<Error>({.kind = ErrorKind::kNoSuchNamespace,
125-
.message = std::format(fmt, std::forward<Args>(args)...)});
126-
}
127-
128-
/// \brief Create an unexpected error with kNoSuchTable
129-
template <typename... Args>
130-
auto NoSuchTableError(const std::format_string<Args...> fmt, Args&&... args)
131-
-> unexpected<Error> {
132-
return unexpected<Error>({.kind = ErrorKind::kNoSuchTable,
133-
.message = std::format(fmt, std::forward<Args>(args)...)});
134-
}
135-
136-
/// \brief Create an unexpected error with kNotImplemented
137-
template <typename... Args>
138-
auto NotImplementedError(const std::format_string<Args...> fmt, Args&&... args)
139-
-> unexpected<Error> {
140-
return unexpected<Error>({.kind = ErrorKind::kNotImplemented,
141-
.message = std::format(fmt, std::forward<Args>(args)...)});
142-
}
143-
144-
/// \brief Create an unexpected error with kNotSupported
145-
template <typename... Args>
146-
auto NotSupportedError(const std::format_string<Args...> fmt, Args&&... args)
147-
-> unexpected<Error> {
148-
return unexpected<Error>({.kind = ErrorKind::kNotSupported,
149-
.message = std::format(fmt, std::forward<Args>(args)...)});
150-
}
151-
152-
/// \brief Create an unexpected error with kUnknownError
153-
template <typename... Args>
154-
auto UnknownError(const std::format_string<Args...> fmt, Args&&... args)
155-
-> unexpected<Error> {
156-
return unexpected<Error>({.kind = ErrorKind::kUnknownError,
157-
.message = std::format(fmt, std::forward<Args>(args)...)});
158-
}
65+
/// \brief Macro to define error creation functions
66+
#define DEFINE_ERROR_FUNCTION(name) \
67+
template <typename... Args> \
68+
auto name(const std::format_string<Args...> fmt, Args&&... args) \
69+
-> unexpected<Error> { \
70+
return unexpected<Error>( \
71+
{ErrorKind::k##name, std::format(fmt, std::forward<Args>(args)...)}); \
72+
}
73+
74+
DEFINE_ERROR_FUNCTION(AlreadyExists)
75+
DEFINE_ERROR_FUNCTION(CommitStateUnknown)
76+
DEFINE_ERROR_FUNCTION(InvalidArgument)
77+
DEFINE_ERROR_FUNCTION(InvalidExpression)
78+
DEFINE_ERROR_FUNCTION(InvalidSchema)
79+
DEFINE_ERROR_FUNCTION(IOError)
80+
DEFINE_ERROR_FUNCTION(JsonParseError)
81+
DEFINE_ERROR_FUNCTION(NoSuchNamespace)
82+
DEFINE_ERROR_FUNCTION(NoSuchTable)
83+
DEFINE_ERROR_FUNCTION(NotImplemented)
84+
DEFINE_ERROR_FUNCTION(NotSupported)
85+
DEFINE_ERROR_FUNCTION(UnknownError)
86+
87+
#undef DEFINE_ERROR_FUNCTION
15988

16089
} // namespace iceberg

src/iceberg/schema_internal.cc

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@
2020
#include "iceberg/schema_internal.h"
2121

2222
#include <cstring>
23-
#include <format>
2423
#include <optional>
2524
#include <string>
2625

27-
#include <iceberg/result.h>
28-
2926
#include "iceberg/schema.h"
3027
#include "iceberg/type.h"
3128
#include "iceberg/util/macros.h"
@@ -173,13 +170,13 @@ ArrowErrorCode ToArrowSchema(const Type& type, bool optional, std::string_view n
173170

174171
Status ToArrowSchema(const Schema& schema, ArrowSchema* out) {
175172
if (out == nullptr) [[unlikely]] {
176-
return InvalidArgumentError("Output Arrow schema cannot be null");
173+
return InvalidArgument("Output Arrow schema cannot be null");
177174
}
178175

179176
if (ArrowErrorCode errorCode = ToArrowSchema(schema, /*optional=*/false, /*name=*/"",
180177
/*field_id=*/std::nullopt, out);
181178
errorCode != NANOARROW_OK) {
182-
return InvalidSchemaError(
179+
return InvalidSchema(
183180
"Failed to convert Iceberg schema to Arrow schema, error code: {}", errorCode);
184181
}
185182

@@ -221,8 +218,8 @@ Result<std::shared_ptr<Type>> FromArrowSchema(const ArrowSchema& schema) {
221218
ArrowSchemaView schema_view;
222219
if (auto error_code = ArrowSchemaViewInit(&schema_view, &schema, &arrow_error);
223220
error_code != NANOARROW_OK) {
224-
return InvalidSchemaError("Failed to read Arrow schema, code: {}, message: {}",
225-
error_code, arrow_error.message);
221+
return InvalidSchema("Failed to read Arrow schema, code: {}, message: {}", error_code,
222+
arrow_error.message);
226223
}
227224

228225
switch (schema_view.type) {
@@ -267,16 +264,16 @@ Result<std::shared_ptr<Type>> FromArrowSchema(const ArrowSchema& schema) {
267264
return std::make_shared<DateType>();
268265
case NANOARROW_TYPE_TIME64:
269266
if (schema_view.time_unit != NANOARROW_TIME_UNIT_MICRO) {
270-
return InvalidSchemaError("Unsupported time unit for Arrow time type: {}",
271-
static_cast<int>(schema_view.time_unit));
267+
return InvalidSchema("Unsupported time unit for Arrow time type: {}",
268+
static_cast<int>(schema_view.time_unit));
272269
}
273270
return std::make_shared<TimeType>();
274271
case NANOARROW_TYPE_TIMESTAMP: {
275272
bool with_timezone =
276273
schema_view.timezone != nullptr && std::strlen(schema_view.timezone) > 0;
277274
if (schema_view.time_unit != NANOARROW_TIME_UNIT_MICRO) {
278-
return InvalidSchemaError("Unsupported time unit for Arrow timestamp type: {}",
279-
static_cast<int>(schema_view.time_unit));
275+
return InvalidSchema("Unsupported time unit for Arrow timestamp type: {}",
276+
static_cast<int>(schema_view.time_unit));
280277
}
281278
if (with_timezone) {
282279
return std::make_shared<TimestampTzType>();
@@ -293,15 +290,15 @@ Result<std::shared_ptr<Type>> FromArrowSchema(const ArrowSchema& schema) {
293290
schema_view.extension_name.size_bytes);
294291
extension_name == kArrowUuidExtensionName) {
295292
if (schema_view.fixed_size != 16) {
296-
return InvalidSchemaError("UUID type must have a fixed size of 16");
293+
return InvalidSchema("UUID type must have a fixed size of 16");
297294
}
298295
return std::make_shared<UuidType>();
299296
}
300297
return std::make_shared<FixedType>(schema_view.fixed_size);
301298
}
302299
default:
303-
return InvalidSchemaError("Unsupported Arrow type: {}",
304-
ArrowTypeString(schema_view.type));
300+
return InvalidSchema("Unsupported Arrow type: {}",
301+
ArrowTypeString(schema_view.type));
305302
}
306303
}
307304

@@ -322,7 +319,7 @@ Result<std::unique_ptr<Schema>> FromArrowSchema(const ArrowSchema& schema,
322319
ICEBERG_ASSIGN_OR_RAISE(auto type, FromArrowSchema(schema));
323320

324321
if (type->type_id() != TypeId::kStruct) {
325-
return InvalidSchemaError("Arrow schema must be a struct type for Iceberg schema");
322+
return InvalidSchema("Arrow schema must be a struct type for Iceberg schema");
326323
}
327324

328325
auto& struct_type = static_cast<StructType&>(*type);

src/iceberg/snapshot.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ ICEBERG_EXPORT constexpr Result<SnapshotRefType> SnapshotRefTypeFromString(
5656
std::string_view str) noexcept {
5757
if (str == "branch") return SnapshotRefType::kBranch;
5858
if (str == "tag") return SnapshotRefType::kTag;
59-
return InvalidArgumentError("Invalid snapshot reference type: {}", str);
59+
return InvalidArgument("Invalid snapshot reference type: {}", str);
6060
}
6161

6262
/// \brief A reference to a snapshot, either a branch or a tag.

src/iceberg/sort_field.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ ICEBERG_EXPORT constexpr Result<SortDirection> SortDirectionFromString(
5757
std::string_view str) {
5858
if (str == "asc") return SortDirection::kAscending;
5959
if (str == "desc") return SortDirection::kDescending;
60-
return InvalidArgumentError("Invalid SortDirection string: {}", str);
60+
return InvalidArgument("Invalid SortDirection string: {}", str);
6161
}
6262

6363
enum class NullOrder {
@@ -81,7 +81,7 @@ ICEBERG_EXPORT constexpr std::string_view NullOrderToString(NullOrder null_order
8181
ICEBERG_EXPORT constexpr Result<NullOrder> NullOrderFromString(std::string_view str) {
8282
if (str == "nulls-first") return NullOrder::kFirst;
8383
if (str == "nulls-last") return NullOrder::kLast;
84-
return InvalidArgumentError("Invalid NullOrder string: {}", str);
84+
return InvalidArgument("Invalid NullOrder string: {}", str);
8585
}
8686

8787
/// \brief a field with its transform.

src/iceberg/transform.cc

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
#include <format>
2323
#include <regex>
2424

25-
#include <iceberg/result.h>
26-
2725
#include "iceberg/transform_function.h"
2826
#include "iceberg/type.h"
2927

@@ -121,15 +119,15 @@ Result<std::unique_ptr<TransformFunction>> Transform::Bind(
121119
if (auto param = std::get_if<int32_t>(&param_)) {
122120
return std::make_unique<BucketTransform>(source_type, *param);
123121
}
124-
return InvalidArgumentError(
125-
"Bucket requires int32 param, none found in transform '{}'", type_str);
122+
return InvalidArgument("Bucket requires int32 param, none found in transform '{}'",
123+
type_str);
126124
}
127125

128126
case TransformType::kTruncate: {
129127
if (auto param = std::get_if<int32_t>(&param_)) {
130128
return std::make_unique<TruncateTransform>(source_type, *param);
131129
}
132-
return InvalidArgumentError(
130+
return InvalidArgument(
133131
"Truncate requires int32 param, none found in transform '{}'", type_str);
134132
}
135133

@@ -145,7 +143,7 @@ Result<std::unique_ptr<TransformFunction>> Transform::Bind(
145143
return std::make_unique<VoidTransform>(source_type);
146144

147145
default:
148-
return NotSupportedError("Unsupported transform type: '{}'", type_str);
146+
return NotSupported("Unsupported transform type: '{}'", type_str);
149147
}
150148
}
151149

@@ -209,7 +207,7 @@ Result<std::shared_ptr<Transform>> TransformFromString(std::string_view transfor
209207
}
210208
}
211209

212-
return InvalidArgumentError("Invalid Transform string: {}", transform_str);
210+
return InvalidArgument("Invalid Transform string: {}", transform_str);
213211
}
214212

215213
} // namespace iceberg

0 commit comments

Comments
 (0)