Skip to content

Commit d85235f

Browse files
committed
reduce code duplication
1 parent 9773710 commit d85235f

1 file changed

Lines changed: 15 additions & 54 deletions

File tree

cpp/src/arrow/compute/kernels/vector_search_sorted.cc

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,11 @@ struct SearchSortedCompare {
256256
}
257257
};
258258

259-
class PlainArrayAccessorBase {
259+
class SearchWindow {
260260
public:
261-
PlainArrayAccessorBase(int64_t array_length, NonNullValuesRange non_null_range)
261+
SearchWindow(int64_t length, NonNullValuesRange non_null_range)
262262
: offset_(non_null_range.offset),
263-
length_(non_null_range.is_identity(array_length) ? array_length
264-
: non_null_range.length) {}
263+
length_(non_null_range.is_identity(length) ? length : non_null_range.length) {}
265264

266265
int64_t length() const { return length_; }
267266

@@ -279,7 +278,7 @@ class PlainArrayAccessorBase {
279278

280279
/// Access logical values from a plain Arrow array.
281280
template <typename ArrowType>
282-
class PlainArrayAccessor : public PlainArrayAccessorBase {
281+
class PlainArrayAccessor : public SearchWindow {
283282
public:
284283
using ArrayType = typename TypeTraits<ArrowType>::ArrayType;
285284
using ValueType = SearchValue<ArrowType>;
@@ -288,7 +287,7 @@ class PlainArrayAccessor : public PlainArrayAccessorBase {
288287
/// to a non-null subrange.
289288
explicit PlainArrayAccessor(const std::shared_ptr<ArrayData>& array_data,
290289
NonNullValuesRange non_null_range = {})
291-
: PlainArrayAccessorBase(array_data->length, non_null_range), array_(array_data) {}
290+
: SearchWindow(array_data->length, non_null_range), array_(array_data) {}
292291

293292
/// Return the logical value at the given position within the search window.
294293
ValueType Value(int64_t index) const {
@@ -406,40 +405,9 @@ class RunEndEncodedValuesAccessor : public RunEndEncodedValuesAccessorBase {
406405
ArrayType values_;
407406
};
408407

409-
class ChunkedArrayAccessorBase {
410-
public:
411-
ChunkedArrayAccessorBase(const ChunkedArray& chunked_array,
412-
NonNullValuesRange non_null_range = {})
413-
: chunked_array_(chunked_array),
414-
resolver_(chunked_array.chunks()),
415-
offset_(non_null_range.offset),
416-
length_(non_null_range.is_identity(chunked_array_.length())
417-
? chunked_array_.length()
418-
: non_null_range.length) {}
419-
420-
int64_t length() const { return length_; }
421-
422-
uint64_t LogicalInsertionIndex(int64_t index) const {
423-
return static_cast<uint64_t>(index);
424-
}
425-
426-
protected:
427-
const ChunkedArray& chunked_array() const { return chunked_array_; }
428-
429-
ChunkLocation ResolveIndex(int64_t index) const {
430-
return resolver_.Resolve(offset_ + index);
431-
}
432-
433-
private:
434-
const ChunkedArray& chunked_array_;
435-
ChunkResolver resolver_;
436-
int64_t offset_ = 0;
437-
int64_t length_;
438-
};
439-
440408
/// Access logical values from a chunked Arrow array without combining chunks.
441409
template <typename ArrowType>
442-
class ChunkedArrayAccessor : public ChunkedArrayAccessorBase {
410+
class ChunkedArrayAccessor : public SearchWindow {
443411
public:
444412
using ArrayType = typename TypeTraits<ArrowType>::ArrayType;
445413
using ValueType = SearchValue<ArrowType>;
@@ -448,7 +416,9 @@ class ChunkedArrayAccessor : public ChunkedArrayAccessorBase {
448416
/// without concatenating the input, optionally restricted to a subrange.
449417
explicit ChunkedArrayAccessor(const ChunkedArray& chunked_array,
450418
NonNullValuesRange non_null_range = {})
451-
: ChunkedArrayAccessorBase(chunked_array, non_null_range) {
419+
: SearchWindow(chunked_array.length(), non_null_range),
420+
chunked_array_(chunked_array),
421+
resolver_(chunked_array.chunks()) {
452422
chunks_.reserve(static_cast<size_t>(chunked_array.num_chunks()));
453423
for (const auto& chunk : chunked_array.chunks()) {
454424
DCHECK_NE(chunk->type_id(), Type::RUN_END_ENCODED);
@@ -459,13 +429,15 @@ class ChunkedArrayAccessor : public ChunkedArrayAccessorBase {
459429
/// Resolve a logical index within the search window to its chunk-local
460430
/// storage and return that value.
461431
ValueType Value(int64_t index) const {
462-
const auto location = ResolveIndex(index);
463-
DCHECK_LT(location.chunk_index, chunked_array().num_chunks());
432+
const auto location = resolver_.Resolve(physical_offset() + index);
433+
DCHECK_LT(location.chunk_index, chunked_array_.num_chunks());
464434
return GetViewType<ArrowType>::LogicalValue(
465435
chunks_[location.chunk_index].GetView(location.index_in_chunk));
466436
}
467437

468438
private:
439+
const ChunkedArray& chunked_array_;
440+
ChunkResolver resolver_;
469441
std::vector<ArrayType> chunks_;
470442
};
471443

@@ -1003,17 +975,6 @@ Result<Datum> ComputeInsertionIndices(const ValuesAccessor& sorted_values,
1003975
return Datum(std::move(out));
1004976
}
1005977

1006-
// Main entry point for search_sorted over a single array of sorted values and scalar or
1007-
// array needles. The accessor already has the non-null range folded in so this is a
1008-
// straight pass-through to ComputeInsertionIndices.
1009-
template <typename ArrowType, typename ValuesAccessor>
1010-
Result<Datum> SearchWithAccessor(const ValuesAccessor& values_accessor,
1011-
const Datum& needles, SearchSortedOptions::Side side,
1012-
uint64_t insertion_offset, ExecContext* ctx) {
1013-
return ComputeInsertionIndices<ArrowType>(values_accessor, needles, side,
1014-
insertion_offset, ctx);
1015-
}
1016-
1017978
/// Normalize a single ArrayData values input to the correct accessor type and
1018979
/// invoke the supplied visitor with that accessor.
1019980
template <typename ArrowType, typename Visitor>
@@ -1178,15 +1139,15 @@ class SearchSortedMetaFunction : public MetaFunction {
11781139
return VisitValuesAccessor<ArrowType>(
11791140
*values.chunked_array(), non_null_values_range,
11801141
[&](const auto& values_accessor) {
1181-
return SearchWithAccessor<ArrowType>(
1142+
return ComputeInsertionIndices<ArrowType>(
11821143
values_accessor, needles, side,
11831144
static_cast<uint64_t>(non_null_values_range.offset), ctx);
11841145
});
11851146
}
11861147

11871148
return VisitValuesAccessor<ArrowType>(
11881149
values.array(), non_null_values_range, [&](const auto& values_accessor) {
1189-
return SearchWithAccessor<ArrowType>(
1150+
return ComputeInsertionIndices<ArrowType>(
11901151
values_accessor, needles, side,
11911152
static_cast<uint64_t>(non_null_values_range.offset), ctx);
11921153
});

0 commit comments

Comments
 (0)