@@ -33,11 +33,12 @@ namespace arrow::util {
3333using ::arrow::internal::checked_cast;
3434
3535bool IsFixedWidthLike (const ArraySpan& source, bool force_null_count,
36- bool exclude_dictionary) {
37- return IsFixedWidthLike (source, force_null_count,
38- [exclude_dictionary](const DataType& type) {
39- return !exclude_dictionary || type.id () != Type::DICTIONARY ;
40- });
36+ bool exclude_bool_and_dictionary) {
37+ return IsFixedWidthLike (
38+ source, force_null_count, [exclude_bool_and_dictionary](const DataType& type) {
39+ return !exclude_bool_and_dictionary ||
40+ (type.id () != Type::DICTIONARY && type.id () != Type::BOOL );
41+ });
4142}
4243
4344static int64_t FixedWidthInBytesFallback (const FixedSizeListType& fixed_size_list_type) {
@@ -73,16 +74,37 @@ int64_t FixedWidthInBytes(const DataType& type) {
7374 return -1 ;
7475}
7576
77+ static int64_t FixedWidthInBitsFallback (const FixedSizeListType& fixed_size_list_type) {
78+ auto * fsl = &fixed_size_list_type;
79+ int64_t list_size = fsl->list_size ();
80+ for (auto type = fsl->value_type ().get ();;) {
81+ auto type_id = type->id ();
82+ if (type_id == Type::FIXED_SIZE_LIST ) {
83+ fsl = checked_cast<const FixedSizeListType*>(type);
84+ list_size *= fsl->list_size ();
85+ type = fsl->value_type ().get ();
86+ continue ;
87+ }
88+ if (is_fixed_width (type_id)) {
89+ const int64_t flat_bit_width = list_size * type->bit_width ();
90+ DCHECK_GE (flat_bit_width, 0 );
91+ return flat_bit_width;
92+ }
93+ break ;
94+ }
95+ return -1 ;
96+ }
97+
7698int64_t FixedWidthInBits (const DataType& type) {
7799 auto type_id = type.id ();
78100 if (is_fixed_width (type_id)) {
79101 return type.bit_width ();
80102 }
81- const int64_t byte_width = FixedWidthInBytes (type);
82- if ( ARROW_PREDICT_FALSE (byte_width < 0 )) {
83- return - 1 ;
103+ if (type_id == Type:: FIXED_SIZE_LIST ) {
104+ auto & fsl = ::arrow::internal::checked_cast< const FixedSizeListType&>(type);
105+ return FixedWidthInBitsFallback (fsl) ;
84106 }
85- return byte_width * 8 ;
107+ return - 1 ;
86108}
87109
88110namespace internal {
@@ -121,9 +143,6 @@ Status PreallocateFixedWidthArrayData(::arrow::compute::KernelContext* ctx,
121143 if (type->id () == Type::FIXED_SIZE_LIST ) {
122144 auto & fsl_type = checked_cast<const FixedSizeListType&>(*type);
123145 auto & value_type = fsl_type.value_type ();
124- if (ARROW_PREDICT_FALSE (value_type->id () == Type::BOOL )) {
125- return Status::Invalid (" PreallocateFixedWidthArrayData: Invalid type: " , fsl_type);
126- }
127146 if (ARROW_PREDICT_FALSE (value_type->id () == Type::DICTIONARY )) {
128147 return Status::NotImplemented (
129148 " PreallocateFixedWidthArrayData: DICTIONARY type allocation: " , *type);
@@ -146,16 +165,13 @@ Status PreallocateFixedWidthArrayData(::arrow::compute::KernelContext* ctx,
146165
147166} // namespace internal
148167
149- // / \pre same as OffsetPointerOfFixedWidthValues
150- // / \pre source.type->id() != Type::BOOL
151- static const uint8_t * OffsetPointerOfFixedWidthValuesFallback (const ArraySpan& source) {
168+ std::pair<int , const uint8_t *> OffsetPointerOfFixedBitWidthValues (
169+ const ArraySpan& source) {
152170 using OffsetAndListSize = std::pair<int64_t , int64_t >;
153171 auto get_offset = [](auto pair) { return pair.first ; };
154172 auto get_list_size = [](auto pair) { return pair.second ; };
155173 ::arrow::internal::SmallVector<OffsetAndListSize, 1 > stack;
156174
157- DCHECK_NE (source.type ->id (), Type::BOOL );
158-
159175 int64_t list_size = 1 ;
160176 auto * array = &source;
161177 while (array->type ->id () == Type::FIXED_SIZE_LIST ) {
@@ -166,31 +182,25 @@ static const uint8_t* OffsetPointerOfFixedWidthValuesFallback(const ArraySpan& s
166182 // Now that innermost values were reached, pop the stack and calculate the offset
167183 // in bytes of the innermost values buffer by considering the offset at each
168184 // level of nesting.
169- DCHECK (array-> type -> id () != Type:: BOOL && is_fixed_width (*array->type ));
185+ DCHECK (is_fixed_width (*array->type ));
170186 DCHECK (array == &source || !array->MayHaveNulls ())
171187 << " OffsetPointerOfFixedWidthValues: array is expected to be flat or have no "
172188 " nulls in the arrays nested by FIXED_SIZE_LIST." ;
173- int64_t value_width = array->type ->byte_width ();
174- int64_t offset_in_bytes = array->offset * value_width ;
189+ int64_t value_width_in_bits = array->type ->bit_width ();
190+ int64_t offset_in_bits = array->offset * value_width_in_bits ;
175191 for (auto it = stack.rbegin (); it != stack.rend (); ++it) {
176- value_width *= get_list_size (*it);
177- offset_in_bytes += get_offset (*it) * value_width ;
192+ value_width_in_bits *= get_list_size (*it);
193+ offset_in_bits += get_offset (*it) * value_width_in_bits ;
178194 }
179- return value_width < 0 ? nullptr : array->GetValues <uint8_t >(1 , offset_in_bytes);
195+ DCHECK_GE (value_width_in_bits, 0 );
196+ const auto * values_ptr = array->GetValues <uint8_t >(1 , 0 );
197+ return {static_cast <int >(offset_in_bits % 8 ), values_ptr + (offset_in_bits / 8 )};
180198}
181199
182- const uint8_t * OffsetPointerOfFixedWidthValues (const ArraySpan& source) {
183- auto type_id = source.type ->id ();
184- if (is_fixed_width (type_id)) {
185- if (ARROW_PREDICT_FALSE (type_id == Type::BOOL )) {
186- // BOOL arrays are bit-packed, thus a byte-aligned pointer cannot be produced in the
187- // general case. Returning something for BOOL arrays that happen to byte-align
188- // because offset=0 would create too much confusion.
189- return nullptr ;
190- }
191- return source.GetValues <uint8_t >(1 , 0 ) + source.offset * source.type ->byte_width ();
192- }
193- return OffsetPointerOfFixedWidthValuesFallback (source);
200+ const uint8_t * OffsetPointerOfFixedByteWidthValues (const ArraySpan& source) {
201+ DCHECK (IsFixedWidthLike (source, /* force_null_count=*/ false ,
202+ [](const DataType& type) { return type.id () != Type::BOOL ; }));
203+ return OffsetPointerOfFixedBitWidthValues (source).second ;
194204}
195205
196206// / \brief Get the mutable pointer to the fixed-width values of an array
@@ -203,24 +213,20 @@ const uint8_t* OffsetPointerOfFixedWidthValues(const ArraySpan& source) {
203213// / \return The mutable pointer to the fixed-width byte blocks of the array. If
204214// / pre-conditions are not satisfied, the return values is undefined.
205215uint8_t * MutableFixedWidthValuesPointer (ArrayData* mutable_array) {
206- auto type_id = mutable_array->type ->id ();
207- if (type_id == Type::FIXED_SIZE_LIST ) {
208- auto * array = mutable_array;
209- do {
210- DCHECK_EQ (array->offset , 0 );
211- DCHECK_EQ (array->child_data .size (), 1 ) << array->type ->ToString (true ) << " part of "
212- << mutable_array->type ->ToString (true );
213- array = array->child_data [0 ].get ();
214- } while (array->type ->id () == Type::FIXED_SIZE_LIST );
216+ auto * array = mutable_array;
217+ auto type_id = array->type ->id ();
218+ while (type_id == Type::FIXED_SIZE_LIST ) {
215219 DCHECK_EQ (array->offset , 0 );
216- DCHECK (array->type ->id () != Type::BOOL && is_fixed_width (*array->type ));
217- return array->GetMutableValues <uint8_t >(1 , 0 );
220+ DCHECK_EQ (array->child_data .size (), 1 ) << array->type ->ToString (true ) << " part of "
221+ << mutable_array->type ->ToString (true );
222+ array = array->child_data [0 ].get ();
223+ type_id = array->type ->id ();
218224 }
219225 DCHECK_EQ (mutable_array->offset , 0 );
220226 // BOOL is allowed here only because the offset is expected to be 0,
221227 // so the byte-aligned pointer also points to the first *bit* of the buffer.
222228 DCHECK (is_fixed_width (type_id));
223- return mutable_array ->GetMutableValues <uint8_t >(1 , 0 );
229+ return array ->GetMutableValues <uint8_t >(1 , 0 );
224230}
225231
226232} // namespace arrow::util
0 commit comments