wgtmac commented on code in PR #793:
URL: https://github.com/apache/iceberg-cpp/pull/793#discussion_r3510509098


##########
src/iceberg/update/update_schema.cc:
##########
@@ -282,6 +289,24 @@ std::vector<SchemaField> ApplyChangesVisitor::MoveFields(
   return reordered;
 }
 
+/// \brief Cast a default value literal to a target primitive type.
+///
+/// `Literal::CastTo` only returns a value for identical decimal types, but 
schema
+/// evolution permits widening a decimal to a larger precision at the same 
scale. Such a
+/// promotion leaves the unscaled value unchanged, so the literal is rebuilt 
with the
+/// target type instead of going through `CastTo`. All other conversions 
delegate to
+/// `CastTo`, which reports narrowing via AboveMax/BelowMin sentinels that 
callers reject.
+Result<Literal> CastDefaultToType(const Literal& value,
+                                  const std::shared_ptr<PrimitiveType>& 
target_type) {
+  if (value.type()->type_id() == TypeId::kDecimal &&
+      target_type->type_id() == TypeId::kDecimal) {
+    const auto& decimal_type = internal::checked_cast<const 
DecimalType&>(*target_type);
+    return Literal::Decimal(std::get<Decimal>(value.value()).value(),
+                            decimal_type.precision(), decimal_type.scale());

Review Comment:
   Could we guard this decimal fast path with a same-scale check before 
rebuilding the literal? `Decimal` stores only the unscaled value, so accepting 
`decimal(9,3)` as a default for `decimal(18,2)` would reinterpret `1234` from 
`1.234` to `12.34` instead of rejecting it. Java default parsing/serialization 
requires the decimal default scale to match the column type, so only same-scale 
precision widening should be accepted here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to