zhjwpku commented on code in PR #182:
URL: https://github.com/apache/iceberg-cpp/pull/182#discussion_r2319396290


##########
src/iceberg/util/decimal.cc:
##########
@@ -0,0 +1,1044 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/// \file iceberg/util/decimal.cc
+/// \brief 128-bit fixed-point decimal numbers.
+/// Adapted from Apache Arrow with only Decimal128 support.
+/// https://github.com/apache/arrow/blob/main/cpp/src/arrow/util/decimal.cc
+
+#include "iceberg/util/decimal.h"
+
+#include <array>
+#include <bit>
+#include <cassert>
+#include <charconv>
+#include <climits>
+#include <cmath>
+#include <cstdint>
+#include <cstring>
+#include <format>
+#include <iomanip>
+#include <limits>
+#include <sstream>
+#include <string>
+#include <string_view>
+#include <utility>
+
+#include "iceberg/result.h"
+#include "iceberg/util/int128.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+
+// Signed left shift with well-defined behaviour on negative numbers or 
overflow
+template <typename SignedInt, typename Shift>
+  requires std::is_signed_v<SignedInt> && std::is_integral_v<Shift>
+constexpr SignedInt SafeLeftShift(SignedInt u, Shift shift) {
+  using UnsignedInt = std::make_unsigned_t<SignedInt>;
+  return static_cast<SignedInt>(static_cast<UnsignedInt>(u) << shift);
+}
+
+struct DecimalComponents {
+  std::string_view while_digits;
+  std::string_view fractional_digits;
+  int32_t exponent;
+  char sign{0};
+  bool has_exponent{false};
+};
+
+inline bool IsSign(char c) { return c == '+' || c == '-'; }
+
+inline bool IsDigit(char c) { return c >= '0' && c <= '9'; }
+
+inline bool IsDot(char c) { return c == '.'; }
+
+inline bool StartsExponent(char c) { return c == 'e' || c == 'E'; }
+
+inline size_t ParseDigitsRun(std::string_view str, size_t pos, 
std::string_view* out) {
+  size_t start = pos;
+  while (pos < str.size() && IsDigit(str[pos])) {
+    ++pos;
+  }
+  *out = str.substr(start, pos - start);
+  return pos;
+}
+
+bool ParseDecimalComponents(std::string_view str, DecimalComponents* out) {
+  size_t pos = 0;
+
+  if (str.empty()) {
+    return false;
+  }
+
+  // Sign of the number
+  if (IsSign(str[pos])) {
+    out->sign = str[pos++];
+  }
+  // First run of digits
+  pos = ParseDigitsRun(str, pos, &out->while_digits);
+  if (pos == str.size()) {
+    return !out->while_digits.empty();
+  }
+
+  // Optional dot
+  if (IsDot(str[pos])) {
+    ++pos;
+    // Second run of digits after the dot
+    pos = ParseDigitsRun(str, pos, &out->fractional_digits);
+  }
+  if (out->fractional_digits.empty() && out->while_digits.empty()) {
+    // Need at least some digits (whole or fractional)
+    return false;
+  }
+  if (pos == str.size()) {
+    return true;
+  }
+
+  // Optional exponent part
+  if (StartsExponent(str[pos])) {
+    ++pos;
+    // Skip '+' sign, '-' sign will be handled by from_chars
+    if (pos < str.size() && str[pos] == '+') {
+      ++pos;
+    }
+    out->has_exponent = true;
+    auto [ptr, ec] =
+        std::from_chars(str.data() + pos, str.data() + str.size(), 
out->exponent);
+    if (ec != std::errc()) {
+      return false;  // Failed to parse exponent
+    }
+    pos = ptr - str.data();
+  }
+
+  return pos == str.size();
+}
+
+constexpr auto kInt64DecimalDigits =
+    static_cast<size_t>(std::numeric_limits<int64_t>::digits10);
+
+constexpr std::array<uint64_t, kInt64DecimalDigits + 1> kUInt64PowersOfTen = {
+    // clang-format off
+    1ULL,
+    10ULL,
+    100ULL,
+    1000ULL,
+    10000ULL,
+    100000ULL,
+    1000000ULL,
+    10000000ULL,
+    100000000ULL,
+    1000000000ULL,
+    10000000000ULL,
+    100000000000ULL,
+    1000000000000ULL,
+    10000000000000ULL,
+    100000000000000ULL,
+    1000000000000000ULL,
+    10000000000000000ULL,
+    100000000000000000ULL,
+    1000000000000000000ULL
+    // clang-format on
+};
+
+/// \brief Powers of ten for Decimal with scale from 0 to 38.
+constexpr std::array<Decimal, Decimal::kMaxScale + 1> kDecimal128PowersOfTen = 
{
+    Decimal(1LL),
+    Decimal(10LL),
+    Decimal(100LL),
+    Decimal(1000LL),
+    Decimal(10000LL),
+    Decimal(100000LL),
+    Decimal(1000000LL),
+    Decimal(10000000LL),
+    Decimal(100000000LL),
+    Decimal(1000000000LL),
+    Decimal(10000000000LL),
+    Decimal(100000000000LL),
+    Decimal(1000000000000LL),
+    Decimal(10000000000000LL),
+    Decimal(100000000000000LL),
+    Decimal(1000000000000000LL),
+    Decimal(10000000000000000LL),
+    Decimal(100000000000000000LL),
+    Decimal(1000000000000000000LL),
+    Decimal(0LL, 10000000000000000000ULL),
+    Decimal(5LL, 7766279631452241920ULL),
+    Decimal(54LL, 3875820019684212736ULL),
+    Decimal(542LL, 1864712049423024128ULL),
+    Decimal(5421LL, 200376420520689664ULL),
+    Decimal(54210LL, 2003764205206896640ULL),
+    Decimal(542101LL, 1590897978359414784ULL),
+    Decimal(5421010LL, 15908979783594147840ULL),
+    Decimal(54210108LL, 11515845246265065472ULL),
+    Decimal(542101086LL, 4477988020393345024ULL),
+    Decimal(5421010862LL, 7886392056514347008ULL),
+    Decimal(54210108624LL, 5076944270305263616ULL),
+    Decimal(542101086242LL, 13875954555633532928ULL),
+    Decimal(5421010862427LL, 9632337040368467968ULL),
+    Decimal(54210108624275LL, 4089650035136921600ULL),
+    Decimal(542101086242752LL, 4003012203950112768ULL),
+    Decimal(5421010862427522LL, 3136633892082024448ULL),
+    Decimal(54210108624275221LL, 12919594847110692864ULL),
+    Decimal(542101086242752217LL, 68739955140067328ULL),
+    Decimal(5421010862427522170LL, 687399551400673280ULL)};
+
+static inline void ShiftAndAdd(std::string_view input, uint128_t& out) {
+  for (size_t pos = 0; pos < input.size();) {
+    const size_t group_size = std::min(kInt64DecimalDigits, input.size() - 
pos);
+    const uint64_t multiple = kUInt64PowersOfTen[group_size];
+    uint64_t value = 0;
+
+    std::from_chars(input.data() + pos, input.data() + pos + group_size, 
value);
+
+    out = out * multiple + value;
+    pos += group_size;
+  }
+}
+
+static void AdjustIntegerStringWithScale(std::string* str, int32_t scale) {
+  if (scale == 0) {
+    return;
+  }
+  assert(str != nullptr);

Review Comment:
   Added.



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to