Skip to content

Commit 33ddddd

Browse files
committed
Address review: use std::get_if and capture Validate status once
1 parent e25c74e commit 33ddddd

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

src/iceberg/schema_field.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,14 @@ Status ValidateDefault(const SchemaField& field, const Literal& value,
174174
// so a value that does not fit the field precision would pass the type check above yet
175175
// be rejected when the metadata is parsed back. Reject it here to keep the default
176176
// consistent with what the reader accepts.
177-
if (field.type()->type_id() == TypeId::kDecimal &&
178-
std::holds_alternative<Decimal>(value.value())) {
179-
const auto& decimal_type = internal::checked_cast<const DecimalType&>(*field.type());
180-
if (!std::get<Decimal>(value.value()).FitsInPrecision(decimal_type.precision())) {
181-
return InvalidSchema("{} of field {} does not fit precision {}", kind, field.name(),
182-
decimal_type.precision());
177+
if (field.type()->type_id() == TypeId::kDecimal) {
178+
if (const auto* dec = std::get_if<Decimal>(&value.value())) {
179+
const auto& decimal_type =
180+
internal::checked_cast<const DecimalType&>(*field.type());
181+
if (!dec->FitsInPrecision(decimal_type.precision())) {
182+
return InvalidSchema("{} of field {} does not fit precision {}", kind,
183+
field.name(), decimal_type.precision());
184+
}
183185
}
184186
}
185187
return {};

src/iceberg/test/schema_field_test.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,9 @@ TEST(SchemaFieldTest, ValidateRejectsDecimalDefaultExceedingPrecision) {
179179
SchemaField field(/*field_id=*/1, /*name=*/"d", decimal(2, 1),
180180
/*optional=*/true, /*doc=*/"",
181181
std::make_shared<const Literal>(Literal::Decimal(999, 2, 1)));
182-
EXPECT_THAT(field.Validate(), IsError(ErrorKind::kInvalidSchema));
183-
EXPECT_THAT(field.Validate(), HasErrorMessage("does not fit precision"));
182+
auto status = field.Validate();
183+
EXPECT_THAT(status, IsError(ErrorKind::kInvalidSchema));
184+
EXPECT_THAT(status, HasErrorMessage("does not fit precision"));
184185
}
185186

186187
TEST(SchemaFieldTest, ValidateAcceptsDecimalDefaultWithinPrecision) {

0 commit comments

Comments
 (0)