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

Method SortInternal

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

Source from the content-addressed store, hash-verified

673 }
674
675 Status SortInternal() {
676 // Sort each batch independently and merge to sorted indices.
677 const int64_t num_batches = static_cast<int64_t>(batches_.size());
678 if (num_batches == 0) {
679 return Status::OK();
680 }
681 std::vector<NullPartitionResult> sorted(num_batches);
682
683 // First sort all individual batches
684 int64_t begin_offset = 0;
685 int64_t end_offset = 0;
686 int64_t null_count = 0;
687 for (int64_t i = 0; i < num_batches; ++i) {
688 const auto& batch = *batches_[i];
689 end_offset += batch.num_rows();
690 RadixRecordBatchSorter sorter(indices_begin_ + begin_offset,
691 indices_begin_ + end_offset, batch, options_);
692 ARROW_ASSIGN_OR_RAISE(sorted[i], sorter.Sort(begin_offset));
693 DCHECK_EQ(sorted[i].overall_begin(), indices_begin_ + begin_offset);
694 DCHECK_EQ(sorted[i].overall_end(), indices_begin_ + end_offset);
695 DCHECK_EQ(sorted[i].non_null_count() + sorted[i].null_count(), batch.num_rows());
696 begin_offset = end_offset;
697 // XXX this is an upper bound on the true null count
698 null_count += sorted[i].null_count();
699 }
700 DCHECK_EQ(end_offset, indices_end_ - indices_begin_);
701
702 // Then merge them by pairs, recursively
703 if (sorted.size() > 1) {
704 ChunkedIndexMapper chunked_mapper(batches_, indices_begin_, indices_end_);
705 ARROW_ASSIGN_OR_RAISE(auto chunked_indices_pair,
706 chunked_mapper.LogicalToPhysical());
707 auto [chunked_indices_begin, chunked_indices_end] = chunked_indices_pair;
708
709 std::vector<ChunkedNullPartitionResult> chunk_sorted(num_batches);
710 for (int64_t i = 0; i < num_batches; ++i) {
711 chunk_sorted[i] = sorted[i].TranslateTo(indices_begin_, chunked_indices_begin);
712 }
713
714 struct Visitor {
715 TableSorter* sorter;
716 std::vector<ChunkedNullPartitionResult>* chunk_sorted;
717 int64_t null_count;
718
719#define VISIT(TYPE) \
720 Status Visit(const TYPE& type) { \
721 return sorter->MergeInternal<TYPE>(chunk_sorted, null_count); \
722 }
723
724 VISIT_SORTABLE_PHYSICAL_TYPES(VISIT)
725 VISIT(NullType)
726#undef VISIT
727
728 Status Visit(const DataType& type) {
729 return Status::NotImplemented("Unsupported type for sorting: ",
730 type.ToString());
731 }
732 };

Callers

nothing calls this directly

Calls 12

VisitTypeInlineFunction · 0.85
overall_beginMethod · 0.80
overall_endMethod · 0.80
non_null_countMethod · 0.80
TranslateToMethod · 0.80
PhysicalToLogicalMethod · 0.80
OKFunction · 0.50
ARROW_ASSIGN_OR_RAISEFunction · 0.50
sizeMethod · 0.45
num_rowsMethod · 0.45
SortMethod · 0.45
null_countMethod · 0.45

Tested by

no test coverage detected