lidavidm commented on a change in pull request #9715:
URL: https://github.com/apache/arrow/pull/9715#discussion_r596086831



##########
File path: cpp/src/arrow/testing/random.cc
##########
@@ -558,5 +584,248 @@ std::shared_ptr<Array> 
RandomArrayGenerator::ArrayOf(std::shared_ptr<DataType> t
   return RandomArrayGeneratorOfImpl{this, type, size, null_probability, 
nullptr}.Finish();
 }
 
+namespace {
+template <typename T>
+typename T::c_type GetMetadata(const KeyValueMetadata* metadata, const 
std::string& key,
+                               typename T::c_type default_value) {
+  if (!metadata) return default_value;
+  const auto index = metadata->FindKey(key);
+  if (index < 0) return default_value;
+  const auto& value = metadata->value(index);
+  typename T::c_type output{};
+  auto type = checked_pointer_cast<T>(TypeTraits<T>::type_singleton());
+  if (!internal::ParseValue(*type, value.data(), value.length(), &output)) {
+    ABORT_NOT_OK(Status::Invalid("Could not parse ", key, " = ", value));
+  }
+  return output;
+}
+
+Result<std::shared_ptr<Array>> GenerateArray(const Field& field, int64_t 
length,
+                                             RandomArrayGenerator* generator) {
+#define GENERATE_INTEGRAL_CASE_VIEW(BASE_TYPE, VIEW_TYPE)                      
          \
+  case VIEW_TYPE::type_id: {                                                   
          \
+    const BASE_TYPE::c_type min_value = GetMetadata<BASE_TYPE>(                
          \
+        field.metadata().get(), "min", 
std::numeric_limits<BASE_TYPE::c_type>::min());   \
+    const BASE_TYPE::c_type max_value = GetMetadata<BASE_TYPE>(                
          \
+        field.metadata().get(), "max", 
std::numeric_limits<BASE_TYPE::c_type>::max());   \
+    return generator->Numeric<BASE_TYPE>(length, min_value, max_value, 
null_probability) \
+        ->View(field.type());                                                  
          \
+  }
+#define GENERATE_INTEGRAL_CASE(ARROW_TYPE) \
+  GENERATE_INTEGRAL_CASE_VIEW(ARROW_TYPE, ARROW_TYPE)
+#define GENERATE_FLOATING_CASE(ARROW_TYPE, GENERATOR_FUNC)                     
         \
+  case ARROW_TYPE::type_id: {                                                  
         \
+    const ARROW_TYPE::c_type min_value = GetMetadata<ARROW_TYPE>(              
         \
+        field.metadata().get(), "min", 
std::numeric_limits<ARROW_TYPE::c_type>::min()); \
+    const ARROW_TYPE::c_type max_value = GetMetadata<ARROW_TYPE>(              
         \
+        field.metadata().get(), "max", 
std::numeric_limits<ARROW_TYPE::c_type>::max()); \
+    const double nan_probability =                                             
         \
+        GetMetadata<DoubleType>(field.metadata().get(), "nan_probability", 0); 
         \
+    return generator->GENERATOR_FUNC(length, min_value, max_value, 
null_probability,    \
+                                     nan_probability);                         
         \
+  }
+
+  const double null_probability =
+      field.nullable()
+          ? GetMetadata<DoubleType>(field.metadata().get(), 
"null_probability", 0.01)
+          : 0.0;
+  switch (field.type()->id()) {
+    case Type::type::NA:

Review comment:
       Yeah, I will add more validation for these things - I also noticed that 
a null_probability outside [0,1] on MSVC leads to runtime errors (which do not 
appear to apply to GCC).




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

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


Reply via email to