Skip to content

Commit

Permalink
Relocate random Float16 function
Browse files Browse the repository at this point in the history
  • Loading branch information
benibus committed Nov 14, 2023
1 parent 5eb90d7 commit cb17f56
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
23 changes: 7 additions & 16 deletions cpp/src/parquet/arrow/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "arrow/util/decimal.h"
#include "arrow/util/float16.h"
#include "parquet/column_reader.h"
#include "parquet/test_util.h"

namespace parquet {

Expand Down Expand Up @@ -66,26 +67,15 @@ struct Decimal256WithPrecisionAndScale {
static constexpr int32_t scale = PRECISION - 1;
};

inline void RandomHalfFloatValues(int64_t n, uint32_t seed,
::arrow::util::Float16 min_value,
::arrow::util::Float16 max_value,
std::vector<uint16_t>* out) {
std::vector<float> values;
::arrow::random_real(n, seed, static_cast<float>(min_value),
static_cast<float>(max_value), &values);
out->resize(values.size());
std::transform(values.begin(), values.end(), out->begin(),
[](float f) { return ::arrow::util::Float16(f).bits(); });
}

template <class ArrowType>
::arrow::enable_if_floating_point<ArrowType, Status> NonNullArray(
size_t size, std::shared_ptr<Array>* out) {
using c_type = typename ArrowType::c_type;
std::vector<c_type> values;
if constexpr (::arrow::is_half_float_type<ArrowType>::value) {
RandomHalfFloatValues(size, 0, ::arrow::util::Float16(0.0f),
::arrow::util::Float16(1.0f), &values);
values.resize(size);
test::random_float16_numbers(static_cast<int>(size), 0, ::arrow::util::Float16(0.0f),
::arrow::util::Float16(1.0f), values.data());
} else {
::arrow::random_real(size, 0, static_cast<c_type>(0), static_cast<c_type>(1),
&values);
Expand Down Expand Up @@ -221,8 +211,9 @@ ::arrow::enable_if_floating_point<ArrowType, Status> NullableArray(
using c_type = typename ArrowType::c_type;
std::vector<c_type> values;
if constexpr (::arrow::is_half_float_type<ArrowType>::value) {
RandomHalfFloatValues(size, seed, ::arrow::util::Float16(-1e4f),
::arrow::util::Float16(1e4f), &values);
values.resize(size);
test::random_float16_numbers(static_cast<int>(size), 0, ::arrow::util::Float16(-1e4f),
::arrow::util::Float16(1e4f), values.data());
} else {
::arrow::random_real(size, seed, static_cast<c_type>(-1e10),
static_cast<c_type>(1e10), &values);
Expand Down
10 changes: 10 additions & 0 deletions cpp/src/parquet/test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ void random_Int96_numbers(int n, uint32_t seed, int32_t min_value, int32_t max_v
}
}

void random_float16_numbers(int n, uint32_t seed, ::arrow::util::Float16 min_value,
::arrow::util::Float16 max_value, uint16_t* out) {
std::vector<float> values(n);
random_numbers(n, seed, static_cast<float>(min_value), static_cast<float>(max_value),
values.data());
for (int i = 0; i < n; ++i) {
out[i] = ::arrow::util::Float16(values[i]).bits();
}
}

void random_fixed_byte_array(int n, uint32_t seed, uint8_t* buf, int len, FLBA* out) {
std::default_random_engine gen(seed);
std::uniform_int_distribution<int> d(0, 255);
Expand Down
4 changes: 4 additions & 0 deletions cpp/src/parquet/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "arrow/io/memory.h"
#include "arrow/testing/util.h"
#include "arrow/util/float16.h"

#include "parquet/column_page.h"
#include "parquet/column_reader.h"
Expand Down Expand Up @@ -148,6 +149,9 @@ inline void random_numbers(int n, uint32_t seed, double min_value, double max_va
void random_Int96_numbers(int n, uint32_t seed, int32_t min_value, int32_t max_value,
Int96* out);

void random_float16_numbers(int n, uint32_t seed, ::arrow::util::Float16 min_value,
::arrow::util::Float16 max_value, uint16_t* out);

void random_fixed_byte_array(int n, uint32_t seed, uint8_t* buf, int len, FLBA* out);

void random_byte_array(int n, uint32_t seed, uint8_t* buf, ByteArray* out, int min_size,
Expand Down

0 comments on commit cb17f56

Please sign in to comment.