Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cpp/src/parquet/arrow/arrow_statistics_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,15 @@ INSTANTIATE_TEST_SUITE_P(
/*expected_null_count=*/5,
/*expected_value_count=*/2,
/*expected_min=*/"z",
/*expected_max=*/"z"}));
/*expected_max=*/"z"},
StatisticsTestParam{
/*table=*/Table::Make(
::arrow::schema({::arrow::field("a", ::arrow::fixed_size_binary(2))}),
{ArrayFromJSON(::arrow::fixed_size_binary(2), R"(["bb", null, "aa"])")}),
/*expected_null_count=*/1,
/*expected_value_count=*/2,
/*expected_min=*/"aa",
/*expected_max=*/"bb"}));

TEST(StatisticsTest, FixedWidthLeafUnderListStructNullCount) {
// Null counts for leaves under list<struct<...>> must include null and empty
Expand Down
8 changes: 7 additions & 1 deletion cpp/src/parquet/arrow/index_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ TEST_F(ParquetBloomFilterRoundTripTest, SimpleRoundTrip) {
test->template VerifyBloomFilterAcrossRowGroups<::arrow::StringViewType>(
col, kNumRowGroups, kRowGroupRowCount, kNumBFColumns, /*col_idx=*/1);
}});
test_cases.push_back(
{::arrow::fixed_size_binary(1), use_dict,
[&](auto* test, auto& col, auto& counts) {
test->template VerifyBloomFilterAcrossRowGroups<::arrow::FixedSizeBinaryType>(
col, kNumRowGroups, kRowGroupRowCount, kNumBFColumns, /*col_idx=*/1);
}});
}

for (const auto& test_case : test_cases) {
Expand All @@ -593,7 +599,7 @@ TEST_F(ParquetBloomFilterRoundTripTest, SimpleRoundTrip) {
} else {
// Both write and verify use the same non-dictionary schema
table = ::arrow::TableFromJSON(schema, json_contents);
WriteFile(writer_properties, ::arrow::TableFromJSON(schema, json_contents));
WriteFile(writer_properties, table);
}

ReadBloomFilters(kNumRowGroups);
Expand Down
22 changes: 20 additions & 2 deletions cpp/src/parquet/bloom_filter_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,27 @@ void TypedBloomFilterWriter<ByteArrayType>::Update(const ::arrow::Array& values)
*bloom_filter_,
::arrow::internal::checked_cast<const ::arrow::LargeBinaryArray&>(values));
} else {
ParquetException::NYI("Bloom filter is not supported for this Arrow type: " +
values.type()->ToString());
throw ParquetException(
"BYTE_ARRAY Bloom filter expects a variable-length binary or string array, got ",
values.type()->ToString());
}
}

template <>
void TypedBloomFilterWriter<FLBAType>::Update(const ::arrow::Array& values) {
ARROW_DCHECK(bloom_filter_ != nullptr);
if (values.type_id() != ::arrow::Type::FIXED_SIZE_BINARY) {
throw ParquetException(
"FIXED_LEN_BYTE_ARRAY Bloom filter expects a fixed-size binary array, got ",
values.type()->ToString());
}
const auto& array =
::arrow::internal::checked_cast<const ::arrow::FixedSizeBinaryArray&>(values);
if (array.byte_width() != descr_->type_length()) {
throw ParquetException("Unexpected fixed-size binary width: ", array.byte_width(),
", expected ", descr_->type_length());
}
UpdateBinaryBloomFilter(*bloom_filter_, array);
}

template class TypedBloomFilterWriter<BooleanType>;
Expand Down
52 changes: 24 additions & 28 deletions cpp/src/parquet/column_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,11 @@ class TypedColumnWriterImpl : public ColumnWriterImpl,
int64_t num_levels, const ::arrow::Array& array,
ArrowWriteContext* ctx, bool maybe_parent_nulls);

Status WriteArrowBinary(const int16_t* def_levels, const int16_t* rep_levels,
int64_t num_levels, const ::arrow::Array& array,
ArrowWriteContext* ctx)
requires(::arrow::internal::IsOneOf<ParquetType, ByteArrayType, FLBAType>::value);
Comment thread
HuaHuaY marked this conversation as resolved.
Resolved

Status WriteArrowZeroCopy(const int16_t* def_levels, const int16_t* rep_levels,
int64_t num_levels, const ::arrow::Array& array,
ArrowWriteContext* ctx, bool maybe_parent_nulls) {
Expand Down Expand Up @@ -2517,6 +2522,17 @@ Status TypedColumnWriterImpl<ByteArrayType>::WriteArrowDense(
ARROW_UNSUPPORTED();
}

return WriteArrowBinary(def_levels, rep_levels, num_levels, array, ctx);
}

template <typename ParquetType>
Status TypedColumnWriterImpl<ParquetType>::WriteArrowBinary(const int16_t* def_levels,
const int16_t* rep_levels,
int64_t num_levels,
const ::arrow::Array& array,
ArrowWriteContext* ctx)
requires(::arrow::internal::IsOneOf<ParquetType, ByteArrayType, FLBAType>::value)
{
Comment thread
HuaHuaY marked this conversation as resolved.
Resolved
int64_t value_offset = 0;
auto WriteChunk = [&](int64_t offset, int64_t batch_size, bool check_page) {
int64_t batch_num_values = 0;
Expand Down Expand Up @@ -2564,33 +2580,6 @@ Status TypedColumnWriterImpl<ByteArrayType>::WriteArrowDense(
return Status::OK();
}

// ----------------------------------------------------------------------
// Write Arrow to FIXED_LEN_BYTE_ARRAY

template <typename ParquetType, typename ArrowType>
struct SerializeFunctor<
ParquetType, ArrowType,
::arrow::enable_if_t<::arrow::is_fixed_size_binary_type<ArrowType>::value &&
!::arrow::is_decimal_type<ArrowType>::value>> {
Status Serialize(const ::arrow::FixedSizeBinaryArray& array, ArrowWriteContext*,
FLBA* out) {
if (array.null_count() == 0) {
// no nulls, just dump the data
// todo(advancedxy): use a writeBatch to avoid this step
for (int64_t i = 0; i < array.length(); i++) {
out[i] = FixedLenByteArray(array.GetValue(i));
}
} else {
for (int64_t i = 0; i < array.length(); i++) {
if (array.IsValid(i)) {
out[i] = FixedLenByteArray(array.GetValue(i));
}
}
}
return Status::OK();
}
};

// ----------------------------------------------------------------------
// Write Arrow to Decimal128

Expand Down Expand Up @@ -2702,12 +2691,19 @@ Status TypedColumnWriterImpl<FLBAType>::WriteArrowDense(
const int16_t* def_levels, const int16_t* rep_levels, int64_t num_levels,
const ::arrow::Array& array, ArrowWriteContext* ctx, bool maybe_parent_nulls) {
switch (array.type()->id()) {
WRITE_SERIALIZE_CASE(FIXED_SIZE_BINARY)
WRITE_SERIALIZE_CASE(DECIMAL32)
WRITE_SERIALIZE_CASE(DECIMAL64)
WRITE_SERIALIZE_CASE(DECIMAL128)
WRITE_SERIALIZE_CASE(DECIMAL256)
WRITE_SERIALIZE_CASE(HALF_FLOAT)
case ::arrow::Type::FIXED_SIZE_BINARY: {
if (const auto& binary = checked_cast<const ::arrow::FixedSizeBinaryArray&>(array);
binary.byte_width() != descr_->type_length()) {
return Status::Invalid("Unexpected fixed-size binary width: ",
binary.byte_width(), ", expected ", descr_->type_length());
}
return WriteArrowBinary(def_levels, rep_levels, num_levels, array, ctx);
Comment thread
wgtmac marked this conversation as resolved.
Resolved
}
default:
break;
}
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/parquet/encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ void AssertFixedSizeBinary(const ::arrow::Array& values, int type_length) {
template <>
inline void PlainEncoder<FLBAType>::Put(const ::arrow::Array& values) {
AssertFixedSizeBinary(values, descr_->type_length());
if (values.length() == 0) {
return;
}
const auto& data = checked_cast<const ::arrow::FixedSizeBinaryArray&>(values);

if (data.null_count() == 0) {
Expand Down Expand Up @@ -979,6 +982,9 @@ class ByteStreamSplitEncoder<FLBAType> : public ByteStreamSplitEncoderBase<FLBAT

void Put(const ::arrow::Array& values) override {
AssertFixedSizeBinary(values, byte_width_);
if (values.length() == 0) {
return;
}
const auto& data = checked_cast<const ::arrow::FixedSizeBinaryArray&>(values);
if (data.null_count() == 0) {
// no nulls, just buffer the data
Expand Down
50 changes: 31 additions & 19 deletions cpp/src/parquet/statistics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -552,14 +552,23 @@ class TypedComparatorImpl
const uint8_t* valid_bits,
int64_t valid_bits_offset) const override {
DCHECK_GT(length, 0);
return GetMinMaxSpacedImpl(length, valid_bits, valid_bits_offset,
[values](int64_t i) { return SafeLoad(values + i); });
}

std::pair<T, T> GetMinMax(const ::arrow::Array& values) const override;

private:
template <typename ValueAt>
std::pair<T, T> GetMinMaxSpacedImpl(int64_t length, const uint8_t* valid_bits,
int64_t valid_bits_offset, ValueAt value_at) const {
T min = Helper::DefaultMin();
T max = Helper::DefaultMax();

::arrow::internal::VisitSetBitRunsVoid(
valid_bits, valid_bits_offset, length, [&](int64_t position, int64_t length) {
for (int64_t i = 0; i < length; i++) {
const auto val = SafeLoad(values + i + position);
const T val = value_at(position + i);
min = Helper::Min(type_length_, min,
Helper::Coalesce(val, Helper::DefaultMin()));
max = Helper::Max(type_length_, max,
Expand All @@ -570,11 +579,6 @@ class TypedComparatorImpl
return {min, max};
}

std::pair<T, T> GetMinMax(const ::arrow::Array& values) const override {
ParquetException::NYI(values.type()->ToString());
}

private:
int type_length_;
};

Expand All @@ -601,9 +605,7 @@ TypedComparatorImpl</*is_signed=*/false, Int32Type>::GetMinMax(const int32_t* va
}

template <bool is_signed>
std::pair<ByteArray, ByteArray> GetMinMaxBinaryHelper(
const TypedComparatorImpl<is_signed, ByteArrayType>& comparator,
const ::arrow::Array& values) {
std::pair<ByteArray, ByteArray> GetMinMaxBinaryHelper(const ::arrow::Array& values) {
using Helper = CompareHelper<ByteArrayType, is_signed>;

ByteArray min = Helper::DefaultMin();
Expand Down Expand Up @@ -632,16 +634,26 @@ std::pair<ByteArray, ByteArray> GetMinMaxBinaryHelper(
return {min, max};
}

template <>
std::pair<ByteArray, ByteArray> TypedComparatorImpl<true, ByteArrayType>::GetMinMax(
const ::arrow::Array& values) const {
return GetMinMaxBinaryHelper<true>(*this, values);
}

template <>
std::pair<ByteArray, ByteArray> TypedComparatorImpl<false, ByteArrayType>::GetMinMax(
const ::arrow::Array& values) const {
return GetMinMaxBinaryHelper<false>(*this, values);
template <bool is_signed, typename DType>
auto TypedComparatorImpl<is_signed, DType>::GetMinMax(const ::arrow::Array& values) const
-> std::pair<T, T> {
if constexpr (std::is_same_v<T, FLBA>) {
if (values.type_id() != ::arrow::Type::FIXED_SIZE_BINARY) {
throw ParquetException("Expected a fixed-size binary array, got ",
values.type()->ToString());
}
const auto& array = checked_cast<const ::arrow::FixedSizeBinaryArray&>(values);
if (array.byte_width() != type_length_) {
throw ParquetException("Unexpected fixed-size binary width: ", array.byte_width(),
", expected ", type_length_);
}
return GetMinMaxSpacedImpl(array.length(), array.null_bitmap_data(), array.offset(),
[&array](int64_t i) { return FLBA{array.GetValue(i)}; });
} else if constexpr (std::is_same_v<T, ByteArray>) {
return GetMinMaxBinaryHelper<is_signed>(values);
} else {
ParquetException::NYI(values.type()->ToString());
}
}

LogicalType::Type::type LogicalTypeId(const ColumnDescriptor* descr) {
Expand Down
24 changes: 18 additions & 6 deletions cpp/src/parquet/statistics_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,15 @@ TEST(Comparison, SignedLittleEndianFLBA12TimestampMinMax) {
std::vector<FLBA> vals = {FLBA(plus_five.data()), FLBA(large_pos.data()),
FLBA(plus_one.data())};

auto min_max = comparator->GetMinMax(vals.data(), vals.size());
// min == plus_one and max == large_pos.
EXPECT_FALSE(comparator->Compare(min_max.first, FLBA(plus_one.data())));
EXPECT_FALSE(comparator->Compare(FLBA(plus_one.data()), min_max.first));
EXPECT_FALSE(comparator->Compare(min_max.second, FLBA(large_pos.data())));
EXPECT_FALSE(comparator->Compare(FLBA(large_pos.data()), min_max.second));
std::shared_ptr<::arrow::Array> array;
::arrow::ArrayFromVector<::arrow::FixedSizeBinaryType, const uint8_t*>(
::arrow::fixed_size_binary(12),
{plus_five.data(), large_pos.data(), plus_one.data()}, &array);
for (const auto& [min, max] :
{comparator->GetMinMax(vals.data(), vals.size()), comparator->GetMinMax(*array)}) {
EXPECT_EQ(std::memcmp(min.ptr, plus_one.data(), 12), 0);
EXPECT_EQ(std::memcmp(max.ptr, large_pos.data(), 12), 0);
}
}

TEST(Comparison, UnsignedFLBA) {
Expand Down Expand Up @@ -1695,6 +1698,15 @@ void TestFloatStatistics<Float16LogicalType>::TestNaNs() {
auto some_nans = prepare_values(some_nans_f16);
auto other_nans = prepare_values(other_nans_f16);

std::shared_ptr<::arrow::Array> array;
::arrow::ArrayFromVector<::arrow::FixedSizeBinaryType, const uint8_t*>(
::arrow::fixed_size_binary(2), {nan_f16.bytes(), max.ptr, min.ptr}, &array);
auto stats = MakeStatistics<FLBAType>(&descr);
stats->Update(*array);
ASSERT_TRUE(stats->HasMinMax());
this->CheckEq(stats->min(), min);
this->CheckEq(stats->max(), max);

uint8_t valid_bitmap = 0x7F; // 0b01111111
// NaNs excluded
uint8_t valid_bitmap_no_nans = 0x6E; // 0b01101110
Expand Down
Loading