| 773 | // |
| 774 | template <typename ArrowType> |
| 775 | enable_if_t<!is_null_type<ArrowType>::value> MergeNonNulls( |
| 776 | std::span<CompressedChunkLocation> range, size_t middle, |
| 777 | CompressedChunkLocation* temp_indices) { |
| 778 | auto& comparator = comparator_; |
| 779 | const auto& first_sort_key = sort_keys_[0]; |
| 780 | |
| 781 | auto left = range.subspan(0, middle); |
| 782 | auto right = range.subspan(middle); |
| 783 | std::merge(left.data(), left.data() + left.size(), right.data(), |
| 784 | right.data() + right.size(), temp_indices, |
| 785 | [&](CompressedChunkLocation left, CompressedChunkLocation right) { |
| 786 | // Both values are never null nor NaN. |
| 787 | const auto left_loc = ChunkLocation{left}; |
| 788 | const auto right_loc = ChunkLocation{right}; |
| 789 | auto chunk_left = first_sort_key.GetChunk(left_loc); |
| 790 | auto chunk_right = first_sort_key.GetChunk(right_loc); |
| 791 | DCHECK(!chunk_left.IsNull()); |
| 792 | DCHECK(!chunk_right.IsNull()); |
| 793 | const auto value_left = chunk_left.Value<ArrowType>(); |
| 794 | const auto value_right = chunk_right.Value<ArrowType>(); |
| 795 | if (value_left == value_right) { |
| 796 | // If the left value equals to the right value, |
| 797 | // we need to compare the second and following |
| 798 | // sort keys. |
| 799 | return comparator.Compare(left_loc, right_loc, 1); |
| 800 | } else { |
| 801 | auto compared = value_left < value_right; |
| 802 | if (first_sort_key.order == SortOrder::Ascending) { |
| 803 | return compared; |
| 804 | } else { |
| 805 | return !compared; |
| 806 | } |
| 807 | } |
| 808 | }); |
| 809 | |
| 810 | // Copy back temp area into main buffer |
| 811 | std::copy(temp_indices, temp_indices + range.size(), range.data()); |
| 812 | } |
| 813 | |
| 814 | template <typename ArrowType> |
| 815 | enable_if_null<ArrowType> MergeNonNulls(std::span<CompressedChunkLocation> range, |