diff --git a/cpp/src/parquet/arrow/arrow_statistics_test.cc b/cpp/src/parquet/arrow/arrow_statistics_test.cc index a6817f1cbc2..c023ea9e0f7 100644 --- a/cpp/src/parquet/arrow/arrow_statistics_test.cc +++ b/cpp/src/parquet/arrow/arrow_statistics_test.cc @@ -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> must include null and empty diff --git a/cpp/src/parquet/arrow/index_test.cc b/cpp/src/parquet/arrow/index_test.cc index b12265cf4ee..01677f2d413 100644 --- a/cpp/src/parquet/arrow/index_test.cc +++ b/cpp/src/parquet/arrow/index_test.cc @@ -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) { @@ -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); diff --git a/cpp/src/parquet/bloom_filter_writer.cc b/cpp/src/parquet/bloom_filter_writer.cc index a52ca25163b..112e82a6c80 100644 --- a/cpp/src/parquet/bloom_filter_writer.cc +++ b/cpp/src/parquet/bloom_filter_writer.cc @@ -133,9 +133,27 @@ void TypedBloomFilterWriter::Update(const ::arrow::Array& values) *bloom_filter_, ::arrow::internal::checked_cast(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::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(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; diff --git a/cpp/src/parquet/column_writer.cc b/cpp/src/parquet/column_writer.cc index e08009dd5c1..7e0c4ac72e0 100644 --- a/cpp/src/parquet/column_writer.cc +++ b/cpp/src/parquet/column_writer.cc @@ -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::value); + 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) { @@ -2517,6 +2522,17 @@ Status TypedColumnWriterImpl::WriteArrowDense( ARROW_UNSUPPORTED(); } + return WriteArrowBinary(def_levels, rep_levels, num_levels, array, ctx); +} + +template +Status TypedColumnWriterImpl::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::value) +{ int64_t value_offset = 0; auto WriteChunk = [&](int64_t offset, int64_t batch_size, bool check_page) { int64_t batch_num_values = 0; @@ -2564,33 +2580,6 @@ Status TypedColumnWriterImpl::WriteArrowDense( return Status::OK(); } -// ---------------------------------------------------------------------- -// Write Arrow to FIXED_LEN_BYTE_ARRAY - -template -struct SerializeFunctor< - ParquetType, ArrowType, - ::arrow::enable_if_t<::arrow::is_fixed_size_binary_type::value && - !::arrow::is_decimal_type::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 @@ -2702,12 +2691,19 @@ Status TypedColumnWriterImpl::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(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); + } default: break; } diff --git a/cpp/src/parquet/encoder.cc b/cpp/src/parquet/encoder.cc index 30341bbe25e..67b417418f5 100644 --- a/cpp/src/parquet/encoder.cc +++ b/cpp/src/parquet/encoder.cc @@ -307,6 +307,9 @@ void AssertFixedSizeBinary(const ::arrow::Array& values, int type_length) { template <> inline void PlainEncoder::Put(const ::arrow::Array& values) { AssertFixedSizeBinary(values, descr_->type_length()); + if (values.length() == 0) { + return; + } const auto& data = checked_cast(values); if (data.null_count() == 0) { @@ -979,6 +982,9 @@ class ByteStreamSplitEncoder : public ByteStreamSplitEncoderBase(values); if (data.null_count() == 0) { // no nulls, just buffer the data diff --git a/cpp/src/parquet/statistics.cc b/cpp/src/parquet/statistics.cc index 71222db79f1..ccc9d548e05 100644 --- a/cpp/src/parquet/statistics.cc +++ b/cpp/src/parquet/statistics.cc @@ -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 GetMinMax(const ::arrow::Array& values) const override; + private: + template + std::pair 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, @@ -570,11 +579,6 @@ class TypedComparatorImpl return {min, max}; } - std::pair GetMinMax(const ::arrow::Array& values) const override { - ParquetException::NYI(values.type()->ToString()); - } - - private: int type_length_; }; @@ -601,9 +605,7 @@ TypedComparatorImpl::GetMinMax(const int32_t* va } template -std::pair GetMinMaxBinaryHelper( - const TypedComparatorImpl& comparator, - const ::arrow::Array& values) { +std::pair GetMinMaxBinaryHelper(const ::arrow::Array& values) { using Helper = CompareHelper; ByteArray min = Helper::DefaultMin(); @@ -632,16 +634,26 @@ std::pair GetMinMaxBinaryHelper( return {min, max}; } -template <> -std::pair TypedComparatorImpl::GetMinMax( - const ::arrow::Array& values) const { - return GetMinMaxBinaryHelper(*this, values); -} - -template <> -std::pair TypedComparatorImpl::GetMinMax( - const ::arrow::Array& values) const { - return GetMinMaxBinaryHelper(*this, values); +template +auto TypedComparatorImpl::GetMinMax(const ::arrow::Array& values) const + -> std::pair { + if constexpr (std::is_same_v) { + 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(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) { + return GetMinMaxBinaryHelper(values); + } else { + ParquetException::NYI(values.type()->ToString()); + } } LogicalType::Type::type LogicalTypeId(const ColumnDescriptor* descr) { diff --git a/cpp/src/parquet/statistics_test.cc b/cpp/src/parquet/statistics_test.cc index af9d96a0ade..826f7e60695 100644 --- a/cpp/src/parquet/statistics_test.cc +++ b/cpp/src/parquet/statistics_test.cc @@ -234,12 +234,15 @@ TEST(Comparison, SignedLittleEndianFLBA12TimestampMinMax) { std::vector 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) { @@ -1695,6 +1698,15 @@ void TestFloatStatistics::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(&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