Skip to content

Commit 8ba4f37

Browse files
committed
resolve some comments
1 parent a92d1ac commit 8ba4f37

3 files changed

Lines changed: 34 additions & 30 deletions

File tree

src/iceberg/update/update_schema.cc

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ std::vector<SchemaField> ApplyChangesVisitor::MoveFields(
243243

244244
switch (move.type) {
245245
case UpdateSchema::Move::MoveType::kFirst:
246-
reordered.insert(reordered.begin(), to_move);
246+
reordered.insert(reordered.begin(), std::move(to_move));
247247
break;
248248

249249
case UpdateSchema::Move::MoveType::kBefore: {
@@ -252,7 +252,7 @@ std::vector<SchemaField> ApplyChangesVisitor::MoveFields(
252252
return field.field_id() == move.reference_field_id;
253253
});
254254
if (before_it != reordered.end()) {
255-
reordered.insert(before_it, to_move);
255+
reordered.insert(before_it, std::move(to_move));
256256
}
257257
break;
258258
}
@@ -263,7 +263,7 @@ std::vector<SchemaField> ApplyChangesVisitor::MoveFields(
263263
return field.field_id() == move.reference_field_id;
264264
});
265265
if (after_it != reordered.end()) {
266-
reordered.insert(after_it + 1, to_move);
266+
reordered.insert(after_it + 1, std::move(to_move));
267267
}
268268
break;
269269
}
@@ -503,38 +503,33 @@ UpdateSchema& UpdateSchema::DeleteColumn(std::string_view name) {
503503
}
504504

505505
UpdateSchema& UpdateSchema::MoveFirst(std::string_view name) {
506-
auto field_id = FindFieldIdForMove(name);
507-
ICEBERG_BUILDER_CHECK(field_id.has_value(), "Cannot move missing column: {}", name);
506+
ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto field_id, FindFieldIdForMove(name));
508507

509-
return MoveInternal(name, Move::First(*field_id));
508+
return MoveInternal(name, Move::First(field_id));
510509
}
511510

512511
UpdateSchema& UpdateSchema::MoveBefore(std::string_view name,
513512
std::string_view before_name) {
514-
auto field_id = FindFieldIdForMove(name);
515-
ICEBERG_BUILDER_CHECK(field_id.has_value(), "Cannot move missing column: {}", name);
513+
ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto field_id, FindFieldIdForMove(name));
514+
ICEBERG_BUILDER_ASSIGN_OR_RETURN_WITH_ERROR(
515+
auto before_id, FindFieldIdForMove(before_name),
516+
"Cannot move {} before missing column: {}", name, before_name);
516517

517-
auto before_id = FindFieldIdForMove(before_name);
518-
ICEBERG_BUILDER_CHECK(before_id.has_value(), "Cannot move {} before missing column: {}",
519-
name, before_name);
518+
ICEBERG_BUILDER_CHECK(field_id != before_id, "Cannot move {} before itself", name);
520519

521-
ICEBERG_BUILDER_CHECK(*field_id != *before_id, "Cannot move {} before itself", name);
522-
523-
return MoveInternal(name, Move::Before(*field_id, *before_id));
520+
return MoveInternal(name, Move::Before(field_id, before_id));
524521
}
525522

526523
UpdateSchema& UpdateSchema::MoveAfter(std::string_view name,
527524
std::string_view after_name) {
528-
auto field_id = FindFieldIdForMove(name);
529-
ICEBERG_BUILDER_CHECK(field_id.has_value(), "Cannot move missing column: {}", name);
530-
531-
auto after_id = FindFieldIdForMove(after_name);
532-
ICEBERG_BUILDER_CHECK(after_id.has_value(), "Cannot move {} after missing column: {}",
533-
name, after_name);
525+
ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto field_id, FindFieldIdForMove(name));
526+
ICEBERG_BUILDER_ASSIGN_OR_RETURN_WITH_ERROR(
527+
auto after_id, FindFieldIdForMove(after_name),
528+
"Cannot move {} after missing column: {}", name, after_name);
534529

535-
ICEBERG_BUILDER_CHECK(*field_id != *after_id, "Cannot move {} after itself", name);
530+
ICEBERG_BUILDER_CHECK(field_id != after_id, "Cannot move {} after itself", name);
536531

537-
return MoveInternal(name, Move::After(*field_id, *after_id));
532+
return MoveInternal(name, Move::After(field_id, after_id));
538533
}
539534

540535
UpdateSchema& UpdateSchema::UnionByNameWith(std::shared_ptr<Schema> new_schema) {
@@ -726,20 +721,18 @@ std::string UpdateSchema::CaseSensitivityAwareName(std::string_view name) const
726721
return StringUtils::ToLower(name);
727722
}
728723

729-
std::optional<int32_t> UpdateSchema::FindFieldIdForMove(std::string_view name) const {
730-
// First check if it's a newly added field
724+
Result<int32_t> UpdateSchema::FindFieldIdForMove(std::string_view name) const {
731725
auto added_it = added_name_to_id_.find(CaseSensitivityAwareName(name));
732726
if (added_it != added_name_to_id_.end()) {
733727
return added_it->second;
734728
}
735729

736-
// Then check existing schema fields
737-
auto field_result = FindField(name);
738-
if (field_result.has_value() && field_result.value().has_value()) {
739-
return field_result.value()->get().field_id();
730+
ICEBERG_ASSIGN_OR_RAISE(auto field, FindField(name));
731+
if (field.has_value()) {
732+
return field->get().field_id();
740733
}
741734

742-
return std::nullopt;
735+
return InvalidArgument("Cannot move missing column: {}", name);
743736
}
744737

745738
UpdateSchema& UpdateSchema::MoveInternal(std::string_view name, const Move& move) {

src/iceberg/update/update_schema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ class ICEBERG_EXPORT UpdateSchema : public PendingUpdate {
396396
std::string CaseSensitivityAwareName(std::string_view name) const;
397397

398398
/// \brief Find a field ID for move operations.
399-
std::optional<int32_t> FindFieldIdForMove(std::string_view name) const;
399+
Result<int32_t> FindFieldIdForMove(std::string_view name) const;
400400

401401
/// \brief Internal implementation for recording a move operation.
402402
UpdateSchema& MoveInternal(std::string_view name, const Move& move);

src/iceberg/util/error_collector.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ namespace iceberg {
4545
ICEBERG_BUILDER_ASSIGN_OR_RETURN_IMPL( \
4646
ICEBERG_ASSIGN_OR_RAISE_NAME(result_, __COUNTER__), lhs, rexpr)
4747

48+
#define ICEBERG_BUILDER_ASSIGN_OR_RETURN_WITH_ERROR_IMPL(result_name, lhs, rexpr, ...) \
49+
auto&& result_name = (rexpr); \
50+
if (!result_name) [[unlikely]] { \
51+
return AddError(ErrorKind::kInvalidArgument, __VA_ARGS__); \
52+
} \
53+
lhs = std::move(result_name.value());
54+
55+
#define ICEBERG_BUILDER_ASSIGN_OR_RETURN_WITH_ERROR(lhs, rexpr, ...) \
56+
ICEBERG_BUILDER_ASSIGN_OR_RETURN_WITH_ERROR_IMPL( \
57+
ICEBERG_ASSIGN_OR_RAISE_NAME(result_, __COUNTER__), lhs, rexpr, __VA_ARGS__)
58+
4859
#define ICEBERG_BUILDER_CHECK(expr, ...) \
4960
do { \
5061
if (!(expr)) [[unlikely]] { \

0 commit comments

Comments
 (0)