huan233usc commented on code in PR #805:
URL: https://github.com/apache/iceberg-cpp/pull/805#discussion_r3567146145
##########
src/iceberg/expression/literal.cc:
##########
@@ -95,6 +104,71 @@ Literal
LiteralCaster::AboveMaxLiteral(std::shared_ptr<PrimitiveType> type) {
return Literal(Literal::AboveMax{}, std::move(type));
}
+Result<Literal> LiteralCaster::CastIntegerToDecimal(
+ int64_t value, const std::shared_ptr<PrimitiveType>& target_type) {
+ const auto& decimal_type = internal::checked_cast<const
DecimalType&>(*target_type);
+ const int32_t scale = decimal_type.scale();
+ // DecimalType does not bound its scale, but Rescale indexes a powers-of-ten
table sized
+ // for [0, kMaxScale]; reject an out-of-range scale here rather than reading
past it.
+ if (scale < 0 || scale > Decimal::kMaxScale) {
+ return InvalidArgument("Cannot cast {} as a {} value", value,
+ target_type->ToString());
+ }
+ // An integer has scale 0, so scaling it to the target scale multiplies the
unscaled
+ // value by 10^scale; Rescale rejects a target scale that would overflow the
value.
+ ICEBERG_ASSIGN_OR_RAISE(auto decimal, Decimal(value).Rescale(0, scale));
+ if (!decimal.FitsInPrecision(decimal_type.precision())) {
+ return InvalidArgument("Cannot cast {} as a {} value", value,
+ target_type->ToString());
+ }
+ return Literal::Decimal(decimal.value(), decimal_type.precision(),
+ decimal_type.scale());
+}
+
+Result<Literal> LiteralCaster::CastRealToDecimal(
+ double value, const std::shared_ptr<PrimitiveType>& target_type) {
+ const auto& decimal_type = internal::checked_cast<const
DecimalType&>(*target_type);
+ const int32_t target_scale = decimal_type.scale();
+ if (target_scale < 0 || target_scale > Decimal::kMaxScale) {
+ return InvalidArgument("Cannot cast {} as a {} value", value,
+ target_type->ToString());
+ }
+ if (!std::isfinite(value)) {
+ return InvalidArgument("Cannot cast {} as a {} value", value,
+ target_type->ToString());
+ }
+
+ // Parse the shortest round-tripping decimal representation of the value
(matching
+ // Java's BigDecimal.valueOf(double), which goes through Double.toString)
into a
+ // full-precision decimal, then round to the target scale below.
+ int32_t parsed_scale = 0;
+ ICEBERG_ASSIGN_OR_RAISE(
+ auto parsed, Decimal::FromString(std::format("{}", value), nullptr,
&parsed_scale));
+
+ Decimal unscaled = parsed;
+ if (parsed_scale > target_scale) {
+ // Drop excess fractional digits with HALF_UP rounding (round half away
from zero, as
+ // Java does), since Rescale itself only truncates and rejects any dropped
remainder.
+ const int32_t drop = parsed_scale - target_scale;
+ ICEBERG_ASSIGN_OR_RAISE(auto divisor, Decimal(1).Rescale(0, drop));
Review Comment:
Fixed. Rounding now goes through a shared `RescaleHalfUp` helper that
returns `0` when the drop exceeds the max scale (so `1e-100` -> `0.00` instead
of indexing past the table). Added a regression test.
##########
src/iceberg/expression/literal.cc:
##########
@@ -95,6 +104,71 @@ Literal
LiteralCaster::AboveMaxLiteral(std::shared_ptr<PrimitiveType> type) {
return Literal(Literal::AboveMax{}, std::move(type));
}
+Result<Literal> LiteralCaster::CastIntegerToDecimal(
+ int64_t value, const std::shared_ptr<PrimitiveType>& target_type) {
+ const auto& decimal_type = internal::checked_cast<const
DecimalType&>(*target_type);
+ const int32_t scale = decimal_type.scale();
+ // DecimalType does not bound its scale, but Rescale indexes a powers-of-ten
table sized
+ // for [0, kMaxScale]; reject an out-of-range scale here rather than reading
past it.
+ if (scale < 0 || scale > Decimal::kMaxScale) {
Review Comment:
Done — both the integer and real paths now allow negative scales and round
HALF_UP, so `149 -> decimal(9,-2)` gives `1E+2`. Tested for both int and double
sources.
--
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]