Skip to content

Commit 657c4fa

Browse files
authored
GH-41596: [C++] fixed_width_internal.h: Simplify docstring and support bit-sized types (BOOL) (#41597)
### Rationale for this change Post-merge feedback from #41297. ### What changes are included in this PR? - Supporting `BOOL` as both a top-level and nested in FSL types - Removing the long example from the docstring of `IsFixedWidthLike` These changes don't affect users because this header was added recently and not released. ### Are these changes tested? Yes, by existing and new test cases. * GitHub Issue: #41596 Authored-by: Felipe Oliveira Carvalho <[email protected]> Signed-off-by: Felipe Oliveira Carvalho <[email protected]>
1 parent e411e0e commit 657c4fa

6 files changed

Lines changed: 212 additions & 208 deletions

File tree

‎cpp/src/arrow/compute/kernels/vector_selection_filter_internal.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class PrimitiveFilterImpl {
164164
values_is_valid_(values.buffers[0].data),
165165
// No offset applied for boolean because it's a bitmap
166166
values_data_(kIsBoolean ? values.buffers[1].data
167-
: util::OffsetPointerOfFixedWidthValues(values)),
167+
: util::OffsetPointerOfFixedByteWidthValues(values)),
168168
values_null_count_(values.null_count),
169169
values_offset_(values.offset),
170170
values_length_(values.length),
@@ -470,7 +470,7 @@ Status PrimitiveFilterExec(KernelContext* ctx, const ExecSpan& batch, ExecResult
470470
// validity bitmap.
471471
const bool allocate_validity = values.null_count != 0 || !filter_null_count_is_zero;
472472

473-
DCHECK(util::IsFixedWidthLike(values, /*force_null_count=*/false));
473+
DCHECK(util::IsFixedWidthLike(values));
474474
const int64_t bit_width = util::FixedWidthInBits(*values.type);
475475
RETURN_NOT_OK(util::internal::PreallocateFixedWidthArrayData(
476476
ctx, output_length, /*source=*/values, allocate_validity, out_arr));

‎cpp/src/arrow/compute/kernels/vector_selection_internal.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ Status FSLFilterExec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out)
898898
// PrimitiveFilterExec for a fixed-size list array.
899899
if (util::IsFixedWidthLike(values,
900900
/*force_null_count=*/true,
901-
/*exclude_dictionary=*/true)) {
901+
/*exclude_bool_and_dictionary=*/true)) {
902902
const auto byte_width = util::FixedWidthInBytes(*values.type);
903903
// 0 is a valid byte width for FixedSizeList, but PrimitiveFilterExec
904904
// might not handle it correctly.
@@ -971,7 +971,7 @@ Status FSLTakeExec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
971971
// PrimitiveTakeExec for a fixed-size list array.
972972
if (util::IsFixedWidthLike(values,
973973
/*force_null_count=*/true,
974-
/*exclude_dictionary=*/true)) {
974+
/*exclude_bool_and_dictionary=*/true)) {
975975
const auto byte_width = util::FixedWidthInBytes(*values.type);
976976
// Additionally, PrimitiveTakeExec is only implemented for specific byte widths.
977977
// TODO(GH-41301): Extend PrimitiveTakeExec for any fixed-width type.

‎cpp/src/arrow/compute/kernels/vector_selection_take_internal.cc‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ struct PrimitiveTakeImpl {
347347
static void Exec(const ArraySpan& values, const ArraySpan& indices,
348348
ArrayData* out_arr) {
349349
DCHECK_EQ(util::FixedWidthInBytes(*values.type), kValueWidth);
350-
const auto* values_data = util::OffsetPointerOfFixedWidthValues(values);
350+
const auto* values_data = util::OffsetPointerOfFixedByteWidthValues(values);
351351
const uint8_t* values_is_valid = values.buffers[0].data;
352352
auto values_offset = values.offset;
353353

@@ -588,8 +588,7 @@ Status PrimitiveTakeExec(KernelContext* ctx, const ExecSpan& batch, ExecResult*
588588

589589
ArrayData* out_arr = out->array_data().get();
590590

591-
DCHECK(util::IsFixedWidthLike(values, /*force_null_count=*/false,
592-
/*exclude_dictionary=*/true));
591+
DCHECK(util::IsFixedWidthLike(values));
593592
const int64_t bit_width = util::FixedWidthInBits(*values.type);
594593

595594
// TODO: When neither values nor indices contain nulls, we can skip

‎cpp/src/arrow/util/fixed_width_internal.cc‎

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ namespace arrow::util {
3333
using ::arrow::internal::checked_cast;
3434

3535
bool 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

4344
static 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+
7698
int64_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

88110
namespace 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.
205215
uint8_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

Comments
 (0)