2020
2121#include < cstddef>
2222#include < cstdint>
23+ #include < cstring>
2324#include < functional>
2425#include < utility>
2526#include < vector>
2930#include " paimon/common/predicate/literal_converter.h"
3031#include " paimon/common/utils/arrow/status_utils.h"
3132#include " paimon/common/utils/checked_cast.h"
33+ #include " paimon/common/utils/math.h"
3234#include " paimon/common/utils/options_utils.h"
3335#include " paimon/fs/file_system.h"
3436#include " paimon/memory/bytes.h"
@@ -106,17 +108,8 @@ BloomFilterFileIndexWriter::BloomFilterFileIndexWriter(
106108Status BloomFilterFileIndexWriter::AddBatch (::ArrowArray* batch) {
107109 PAIMON_ASSIGN_OR_RAISE_FROM_ARROW (std::shared_ptr<arrow::Array> array,
108110 arrow::ImportArray (batch, struct_type_));
109- if (!array || array->type_id () != arrow::Type::STRUCT ) {
110- return Status::Invalid (
111- " invalid batch for BloomFilterFileIndexWriter, expected a struct array" );
112- }
113111 std::shared_ptr<arrow::StructArray> struct_array =
114112 checked_pointer_cast<arrow::StructArray>(array);
115- if (struct_array->num_fields () != 1 ) {
116- return Status::Invalid (
117- " invalid batch for BloomFilterFileIndexWriter, expected a struct array with exactly "
118- " one field" );
119- }
120113 PAIMON_ASSIGN_OR_RAISE (
121114 std::vector<Literal> values,
122115 LiteralConverter::ConvertLiteralsFromArray (*struct_array->field (0 ), /* own_data=*/ false ));
@@ -133,24 +126,19 @@ Result<PAIMON_UNIQUE_PTR<Bytes>> BloomFilterFileIndexWriter::SerializedBytes() c
133126 const int32_t bit_set_length = filter_.GetBitSet ().ByteLength ();
134127 PAIMON_UNIQUE_PTR <Bytes> bytes =
135128 Bytes::AllocateBytes (kHeaderLength + bit_set_length, pool_.get ());
136- const auto num_hash_functions = static_cast <uint32_t >(filter_.GetNumHashFunctions ());
137- bytes->data ()[0 ] = static_cast <char >((num_hash_functions >> 24 ) & 0xff );
138- bytes->data ()[1 ] = static_cast <char >((num_hash_functions >> 16 ) & 0xff );
139- bytes->data ()[2 ] = static_cast <char >((num_hash_functions >> 8 ) & 0xff );
140- bytes->data ()[3 ] = static_cast <char >(num_hash_functions & 0xff );
129+ const int32_t num_hash_functions = ToBigEndian (filter_.GetNumHashFunctions ());
130+ std::memcpy (bytes->data (), &num_hash_functions, sizeof (num_hash_functions));
141131 filter_.GetBitSet ().ToByteArray (kHeaderLength , bit_set_length, bytes->data ());
142132 return bytes;
143133}
144134
145135Result<std::shared_ptr<BloomFilterFileIndexReader>> BloomFilterFileIndexReader::Create (
146136 const std::shared_ptr<arrow::DataType>& arrow_type, const std::shared_ptr<Bytes>& bytes) {
147137 // Compatible with Java's big-endian numHashFunctions header.
148- const char * data = bytes->data ();
149- auto num_hash_functions =
150- static_cast <int32_t >((static_cast <uint32_t >(static_cast <uint8_t >(data[0 ])) << 24 ) |
151- (static_cast <uint32_t >(static_cast <uint8_t >(data[1 ])) << 16 ) |
152- (static_cast <uint32_t >(static_cast <uint8_t >(data[2 ])) << 8 ) |
153- static_cast <uint32_t >(static_cast <uint8_t >(data[3 ])));
138+ int32_t big_endian_num_hash_functions;
139+ std::memcpy (&big_endian_num_hash_functions, bytes->data (),
140+ sizeof (big_endian_num_hash_functions));
141+ const int32_t num_hash_functions = FromBigEndian (big_endian_num_hash_functions);
154142 PAIMON_ASSIGN_OR_RAISE (FastHash::HashFunction hash_function,
155143 FastHash::GetHashFunction (arrow_type));
156144 auto bit_set = std::make_unique<BloomFilter64::BitSet>(bytes, /* offset=*/ sizeof (int32_t ));
0 commit comments