pitrou commented on code in PR #43957:
URL: https://github.com/apache/arrow/pull/43957#discussion_r1763368294


##########
cpp/src/arrow/testing/random.cc:
##########
@@ -343,8 +343,72 @@ struct DecimalGenerator {
   }
 };
 
+template <typename DecimalType>
+struct SmallDecimalGenerator {
+  using DecimalBuilderType = typename TypeTraits<DecimalType>::BuilderType;
+  using DecimalValue = typename DecimalBuilderType::ValueType;
+  using IntegerType = typename DecimalValue::ValueType;
+  using IntArrowType = typename CTypeTraits<IntegerType>::ArrowType;
+
+  std::shared_ptr<DataType> type_;
+  RandomArrayGenerator* rng_;
+
+  static IntegerType MaxDecimalInteger(int32_t digits) {
+    return static_cast<IntegerType>(std::ceil(std::pow(10.0, digits))) - 1;
+  }
+
+  std::shared_ptr<Array> MakeRandomArray(int64_t size, double null_probability,
+                                         int64_t alignment, MemoryPool* 
memory_pool) {
+    static constexpr int32_t kMaxDigitsInInteger =
+        std::is_same_v<DecimalType, Decimal32Type> ? 9 : 18;
+    static_assert(
+        kMaxDigitsInInteger >= DecimalType::kByteWidth,
+        "inconsistent decimal metadata: kMaxPrecision doesn't match 
kByteWidth");
+
+    const auto& decimal_type = checked_cast<const DecimalType&>(*type_);
+
+    auto digits_to_generate = decimal_type.precision();
+    auto values = checked_pointer_cast<typename 
TypeTraits<IntArrowType>::ArrayType>(
+        rng_->Numeric<IntArrowType>(size, -1 * 
MaxDecimalInteger(digits_to_generate),
+                                    MaxDecimalInteger(digits_to_generate),
+                                    null_probability, alignment, memory_pool));
+

Review Comment:
   Note that we could just reuse the buffers, or perhaps even return 
`values.View(decimal_type)`.



-- 
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]

Reply via email to