-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EXP: GH-44084: [C++] Improve merge step in chunked sorting
- Loading branch information
Showing
8 changed files
with
345 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include "arrow/compute/kernels/chunked_internal.h" | ||
|
||
#include <algorithm> | ||
|
||
#include "arrow/util/logging.h" | ||
|
||
namespace arrow::compute::internal { | ||
|
||
using ::arrow::internal::TypedChunkLocation; | ||
|
||
std::vector<const Array*> GetArrayPointers(const ArrayVector& arrays) { | ||
std::vector<const Array*> pointers(arrays.size()); | ||
std::transform(arrays.begin(), arrays.end(), pointers.begin(), | ||
[&](const std::shared_ptr<Array>& array) { return array.get(); }); | ||
return pointers; | ||
} | ||
|
||
ChunkedIndexMapper::ChunkedIndexMapper(util::span<const Array* const> chunks, | ||
uint64_t* indices_begin, uint64_t* indices_end) | ||
: resolver_(chunks), | ||
chunks_(chunks), | ||
indices_begin_(indices_begin), | ||
indices_end_(indices_end) {} | ||
|
||
Result<std::pair<CompressedChunkLocation*, CompressedChunkLocation*>> | ||
ChunkedIndexMapper::LogicalToPhysical() { | ||
// Check that indices would fall in bounds for CompressedChunkLocation | ||
if (ARROW_PREDICT_FALSE(static_cast<int64_t>(chunks_.size()) > | ||
CompressedChunkLocation::kMaxChunkIndex + 1)) { | ||
return Status::NotImplemented("Chunked array has more than ", | ||
CompressedChunkLocation::kMaxChunkIndex + 1, " chunks"); | ||
} | ||
for (const Array* chunk : chunks_) { | ||
if (ARROW_PREDICT_FALSE(chunk->length() > | ||
CompressedChunkLocation::kMaxIndexInChunk + 1)) { | ||
return Status::NotImplemented("Individual chunk in chunked array has more than ", | ||
CompressedChunkLocation::kMaxIndexInChunk + 1, | ||
" elements"); | ||
} | ||
} | ||
|
||
constexpr int64_t kMaxBatchSize = 512; | ||
std::array<TypedChunkLocation<uint64_t>, kMaxBatchSize> batch; | ||
|
||
const int64_t num_indices = static_cast<int64_t>(indices_end_ - indices_begin_); | ||
CompressedChunkLocation* physical_begin = | ||
reinterpret_cast<CompressedChunkLocation*>(indices_begin_); | ||
DCHECK_EQ(physical_begin + num_indices, | ||
reinterpret_cast<CompressedChunkLocation*>(indices_end_)); | ||
|
||
for (int64_t i = 0; i < num_indices; i += kMaxBatchSize) { | ||
const int64_t batch_size = std::min(kMaxBatchSize, num_indices - i); | ||
[[maybe_unused]] bool ok = | ||
resolver_.ResolveMany(batch_size, indices_begin_ + i, batch.data()); | ||
DCHECK(ok) << "ResolveMany unexpectedly failed (invalid logical index?)"; | ||
for (int64_t j = 0; j < batch_size; ++j) { | ||
const auto loc = batch[j]; | ||
physical_begin[i + j] = CompressedChunkLocation{ | ||
static_cast<uint32_t>(loc.chunk_index), loc.index_in_chunk}; | ||
} | ||
} | ||
|
||
return std::pair{physical_begin, physical_begin + num_indices}; | ||
} | ||
|
||
Status ChunkedIndexMapper::PhysicalToLogical() { | ||
std::vector<int64_t> chunk_offsets(chunks_.size()); | ||
{ | ||
int64_t offset = 0; | ||
for (int64_t i = 0; i < static_cast<int64_t>(chunks_.size()); ++i) { | ||
chunk_offsets[i] = offset; | ||
offset += chunks_[i]->length(); | ||
} | ||
} | ||
|
||
const int64_t num_indices = static_cast<int64_t>(indices_end_ - indices_begin_); | ||
CompressedChunkLocation* physical_begin = | ||
reinterpret_cast<CompressedChunkLocation*>(indices_begin_); | ||
for (int64_t i = 0; i < num_indices; ++i) { | ||
const auto loc = physical_begin[i]; | ||
DCHECK_LT(loc.chunk_index, chunk_offsets.size()); | ||
DCHECK_LT(static_cast<int64_t>(loc.index_in_chunk), | ||
chunks_[loc.chunk_index]->length()); | ||
indices_begin_[i] = | ||
chunk_offsets[loc.chunk_index] + static_cast<int64_t>(loc.index_in_chunk); | ||
} | ||
|
||
return Status::OK(); | ||
} | ||
|
||
} // namespace arrow::compute::internal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.