MCPcopy Create free account
hub / github.com/apache/arrow / SortInternal

Method SortInternal

cpp/src/arrow/compute/kernels/vector_sort.cc:80–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

78 private:
79 template <typename Type>
80 Status SortInternal() {
81 using ArrayType = typename TypeTraits<Type>::ArrayType;
82 ArraySortOptions options(order_, null_placement_);
83 const auto num_chunks = static_cast<int>(physical_chunks_.size());
84 if (num_chunks == 0) {
85 *output_ = {indices_end_, indices_end_, indices_end_, indices_end_};
86 return Status::OK();
87 }
88 const int64_t num_indices = static_cast<int64_t>(indices_end_ - indices_begin_);
89 const auto arrays = GetArrayPointers(physical_chunks_);
90
91 // Sort each chunk independently and merge to sorted indices.
92 // This is a serial implementation.
93 std::vector<NullPartitionResult> sorted(num_chunks);
94
95 // First sort all individual chunks
96 int64_t begin_offset = 0;
97 int64_t end_offset = 0;
98 int64_t null_count = 0;
99 for (int i = 0; i < num_chunks; ++i) {
100 const auto array = checked_cast<const ArrayType*>(arrays[i]);
101 end_offset += array->length();
102 null_count += array->null_count();
103 ARROW_ASSIGN_OR_RAISE(sorted[i], array_sorter_(indices_begin_ + begin_offset,
104 indices_begin_ + end_offset, *array,
105 begin_offset, options, ctx_));
106 begin_offset = end_offset;
107 }
108 DCHECK_EQ(end_offset, num_indices);
109
110 // Then merge them by pairs, recursively
111 if (sorted.size() > 1) {
112 ChunkedIndexMapper chunked_mapper(arrays, indices_begin_, indices_end_);
113 ARROW_ASSIGN_OR_RAISE(auto chunked_indices_pair,
114 chunked_mapper.LogicalToPhysical());
115 auto [chunked_indices_begin, chunked_indices_end] = chunked_indices_pair;
116
117 std::vector<ChunkedNullPartitionResult> chunk_sorted(num_chunks);
118 for (int i = 0; i < num_chunks; ++i) {
119 chunk_sorted[i] = sorted[i].TranslateTo(indices_begin_, chunked_indices_begin);
120 }
121
122 auto merge_nulls = [&](CompressedChunkLocation* nulls_begin,
123 CompressedChunkLocation* nulls_middle,
124 CompressedChunkLocation* nulls_end,
125 CompressedChunkLocation* temp_indices, int64_t null_count) {
126 if (has_null_like_values<typename ArrayType::TypeClass>()) {
127 PartitionNullsOnly<StablePartitioner>(nulls_begin, nulls_end, arrays,
128 null_count, null_placement_);
129 }
130 };
131 auto merge_non_nulls =
132 [&](CompressedChunkLocation* range_begin, CompressedChunkLocation* range_middle,
133 CompressedChunkLocation* range_end, CompressedChunkLocation* temp_indices) {
134 MergeNonNulls<ArrayType>(range_begin, range_middle, range_end, arrays,
135 temp_indices);
136 };
137

Callers

nothing calls this directly

Calls 15

GetArrayPointersFunction · 0.85
TranslateToMethod · 0.80
overall_endMethod · 0.80
overall_beginMethod · 0.80
resizeMethod · 0.80
PhysicalToLogicalMethod · 0.80
OKFunction · 0.50
ARROW_ASSIGN_OR_RAISEFunction · 0.50
sizeMethod · 0.45
lengthMethod · 0.45
null_countMethod · 0.45
InitMethod · 0.45

Tested by

no test coverage detected