Skip to content

Commit 31a3cf5

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

File tree

10 files changed

+67
-138
lines changed

10 files changed

+67
-138
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/json_internal.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ Status ParseSortOrders(const nlohmann::json& json, int8_t format_version,
10591059
if (format_version > 1) {
10601060
return JsonParseError("{} must exist in format v{}", kSortOrders, format_version);
10611061
}
1062-
return NotImplementedError("Assign a default sort order");
1062+
return NotImplemented("Assign a default sort order");
10631063
}
10641064
return {};
10651065
}
@@ -1115,7 +1115,7 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataFromJson(const nlohmann::jso
11151115
// TODO(gangwu): iterate all partition specs to find the largest partition
11161116
// field id or assign a default value for unpartitioned tables. However,
11171117
// PartitionSpec::lastAssignedFieldId() is not implemented yet.
1118-
return NotImplementedError("Find the largest partition field id");
1118+
return NotImplemented("Find the largest partition field id");
11191119
}
11201120

11211121
ICEBERG_RETURN_UNEXPECTED(ParseSortOrders(json, table_metadata->format_version,

src/iceberg/result.h

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

6262
using Status = Result<void>;
6363

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

15988
} // namespace iceberg

src/iceberg/schema_internal.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,13 @@ ArrowErrorCode ToArrowSchema(const Type& type, bool optional, std::string_view n
173173

174174
Status ToArrowSchema(const Schema& schema, ArrowSchema* out) {
175175
if (out == nullptr) [[unlikely]] {
176-
return InvalidArgumentError("Output Arrow schema cannot be null");
176+
return InvalidArgument("Output Arrow schema cannot be null");
177177
}
178178

179179
if (ArrowErrorCode errorCode = ToArrowSchema(schema, /*optional=*/false, /*name=*/"",
180180
/*field_id=*/std::nullopt, out);
181181
errorCode != NANOARROW_OK) {
182-
return InvalidSchemaError(
182+
return InvalidSchema(
183183
"Failed to convert Iceberg schema to Arrow schema, error code: {}", errorCode);
184184
}
185185

@@ -221,8 +221,8 @@ Result<std::shared_ptr<Type>> FromArrowSchema(const ArrowSchema& schema) {
221221
ArrowSchemaView schema_view;
222222
if (auto error_code = ArrowSchemaViewInit(&schema_view, &schema, &arrow_error);
223223
error_code != NANOARROW_OK) {
224-
return InvalidSchemaError("Failed to read Arrow schema, code: {}, message: {}",
225-
error_code, arrow_error.message);
224+
return InvalidSchema("Failed to read Arrow schema, code: {}, message: {}", error_code,
225+
arrow_error.message);
226226
}
227227

228228
switch (schema_view.type) {
@@ -267,16 +267,16 @@ Result<std::shared_ptr<Type>> FromArrowSchema(const ArrowSchema& schema) {
267267
return std::make_shared<DateType>();
268268
case NANOARROW_TYPE_TIME64:
269269
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));
270+
return InvalidSchema("Unsupported time unit for Arrow time type: {}",
271+
static_cast<int>(schema_view.time_unit));
272272
}
273273
return std::make_shared<TimeType>();
274274
case NANOARROW_TYPE_TIMESTAMP: {
275275
bool with_timezone =
276276
schema_view.timezone != nullptr && std::strlen(schema_view.timezone) > 0;
277277
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));
278+
return InvalidSchema("Unsupported time unit for Arrow timestamp type: {}",
279+
static_cast<int>(schema_view.time_unit));
280280
}
281281
if (with_timezone) {
282282
return std::make_shared<TimestampTzType>();
@@ -293,15 +293,15 @@ Result<std::shared_ptr<Type>> FromArrowSchema(const ArrowSchema& schema) {
293293
schema_view.extension_name.size_bytes);
294294
extension_name == kArrowUuidExtensionName) {
295295
if (schema_view.fixed_size != 16) {
296-
return InvalidSchemaError("UUID type must have a fixed size of 16");
296+
return InvalidSchema("UUID type must have a fixed size of 16");
297297
}
298298
return std::make_shared<UuidType>();
299299
}
300300
return std::make_shared<FixedType>(schema_view.fixed_size);
301301
}
302302
default:
303-
return InvalidSchemaError("Unsupported Arrow type: {}",
304-
ArrowTypeString(schema_view.type));
303+
return InvalidSchema("Unsupported Arrow type: {}",
304+
ArrowTypeString(schema_view.type));
305305
}
306306
}
307307

@@ -321,7 +321,7 @@ Result<std::unique_ptr<Schema>> FromArrowSchema(const ArrowSchema& schema,
321321
ICEBERG_ASSIGN_OR_RAISE(auto type, FromArrowSchema(schema));
322322

323323
if (type->type_id() != TypeId::kStruct) {
324-
return InvalidSchemaError("Arrow schema must be a struct type for Iceberg schema");
324+
return InvalidSchema("Arrow schema must be a struct type for Iceberg schema");
325325
}
326326

327327
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ Result<std::unique_ptr<TransformFunction>> Transform::Bind(
121121
if (auto param = std::get_if<int32_t>(&param_)) {
122122
return std::make_unique<BucketTransform>(source_type, *param);
123123
}
124-
return InvalidArgumentError(
125-
"Bucket requires int32 param, none found in transform '{}'", type_str);
124+
return InvalidArgument("Bucket requires int32 param, none found in transform '{}'",
125+
type_str);
126126
}
127127

128128
case TransformType::kTruncate: {
129129
if (auto param = std::get_if<int32_t>(&param_)) {
130130
return std::make_unique<TruncateTransform>(source_type, *param);
131131
}
132-
return InvalidArgumentError(
132+
return InvalidArgument(
133133
"Truncate requires int32 param, none found in transform '{}'", type_str);
134134
}
135135

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

147147
default:
148-
return NotSupportedError("Unsupported transform type: '{}'", type_str);
148+
return NotSupported("Unsupported transform type: '{}'", type_str);
149149
}
150150
}
151151

@@ -209,7 +209,7 @@ Result<std::shared_ptr<Transform>> TransformFromString(std::string_view transfor
209209
}
210210
}
211211

212-
return InvalidArgumentError("Invalid Transform string: {}", transform_str);
212+
return InvalidArgument("Invalid Transform string: {}", transform_str);
213213
}
214214

215215
} // namespace iceberg

0 commit comments

Comments
 (0)