Skip to content

Commit 4bb4dd5

Browse files
committed
fix(mergetree): use memory pool for arrow operations
1 parent 2aeaca6 commit 4bb4dd5

7 files changed

Lines changed: 31 additions & 13 deletions

src/paimon/core/io/key_value_in_memory_record_reader.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "paimon/common/data/columnar/columnar_row_ref.h"
3030
#include "paimon/common/types/row_kind.h"
3131
#include "paimon/common/utils/arrow/arrow_utils.h"
32+
#include "paimon/common/utils/arrow/mem_utils.h"
3233
#include "paimon/common/utils/arrow/status_utils.h"
3334
#include "paimon/common/utils/fields_comparator.h"
3435
#include "paimon/status.h"
@@ -62,6 +63,7 @@ KeyValueInMemoryRecordReader::KeyValueInMemoryRecordReader(
6263
user_defined_sequence_fields_(user_defined_sequence_fields),
6364
sequence_fields_ascending_(sequence_fields_ascending),
6465
pool_(pool),
66+
arrow_pool_(GetArrowPool(pool)),
6567
value_struct_array_(struct_array),
6668
row_kinds_(row_kinds),
6769
key_comparator_(key_comparator) {
@@ -119,9 +121,10 @@ KeyValueInMemoryRecordReader::SortBatch() const {
119121
}
120122
auto sort_options =
121123
arrow::compute::SortOptions(sort_keys, arrow::compute::NullPlacement::AtStart);
122-
PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(
123-
std::shared_ptr<arrow::Array> sorted_indices,
124-
arrow::compute::SortIndices(arrow::Datum(value_struct_array_), sort_options));
124+
arrow::compute::ExecContext exec_context(arrow_pool_.get());
125+
PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr<arrow::Array> sorted_indices,
126+
arrow::compute::SortIndices(arrow::Datum(value_struct_array_),
127+
sort_options, &exec_context));
125128
auto typed_indices =
126129
arrow::internal::checked_pointer_cast<arrow::NumericArray<arrow::UInt64Type>>(
127130
sorted_indices);

src/paimon/core/io/key_value_in_memory_record_reader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class KeyValueInMemoryRecordReader : public KeyValueRecordReader {
8383
std::vector<std::string> user_defined_sequence_fields_;
8484
bool sequence_fields_ascending_ = true;
8585
std::shared_ptr<MemoryPool> pool_;
86+
std::unique_ptr<arrow::MemoryPool> arrow_pool_;
8687
std::shared_ptr<arrow::StructArray> value_struct_array_;
8788
std::vector<RecordBatch::RowKind> row_kinds_;
8889
std::shared_ptr<FieldsComparator> key_comparator_;

src/paimon/core/mergetree/external_sort_buffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Result<int64_t> ExternalSortBuffer::SpillToDisk(
168168
std::unique_ptr<SpillWriter> spill_writer,
169169
SpillWriter::Create(options_.GetFileSystem(), write_schema_, spill_channel_enumerator_,
170170
spill_channel_manager_, spill_compress_options.compress,
171-
spill_compress_options.zstd_level));
171+
spill_compress_options.zstd_level, pool_));
172172
auto cleanup_guard = ScopeGuard([&]() {
173173
[[maybe_unused]] auto status =
174174
spill_channel_manager_->DeleteChannel(spill_writer->GetChannelId());

src/paimon/core/mergetree/spill_reader.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ Status SpillReader::Open(const FileIOChannel::ID& channel_id) {
5353
uint64_t file_len = file_status->GetLen();
5454
arrow_input_stream_adapter_ =
5555
std::make_shared<ArrowInputStreamAdapter>(in_stream_, arrow_pool_, file_len);
56+
auto ipc_read_options = arrow::ipc::IpcReadOptions::Defaults();
57+
ipc_read_options.memory_pool = arrow_pool_.get();
5658
PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(
57-
arrow_reader_, arrow::ipc::RecordBatchFileReader::Open(arrow_input_stream_adapter_));
59+
arrow_reader_,
60+
arrow::ipc::RecordBatchFileReader::Open(arrow_input_stream_adapter_, ipc_read_options));
5861
num_record_batches_ = arrow_reader_->num_record_batches();
5962
current_batch_index_ = 0;
6063
return Status::OK();

src/paimon/core/mergetree/spill_reader_writer_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ class SpillReaderWriterTest : public ::testing::TestWithParam<std::string> {
6464

6565
Result<std::unique_ptr<SpillWriter>> CreateSpillWriter() const {
6666
return SpillWriter::Create(file_system_, write_schema_, channel_enumerator_,
67-
spill_channel_manager_, GetParam(), /*compression_level=*/1);
67+
spill_channel_manager_, GetParam(), /*compression_level=*/1,
68+
pool_);
6869
}
6970

7071
FileIOChannel::ID WriteSpillFile(

src/paimon/core/mergetree/spill_writer.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "paimon/common/utils/arrow/arrow_output_stream_adapter.h"
2020
#include "paimon/common/utils/arrow/arrow_utils.h"
21+
#include "paimon/common/utils/arrow/mem_utils.h"
2122
#include "paimon/common/utils/arrow/status_utils.h"
2223
#include "paimon/common/utils/scope_guard.h"
2324
#include "paimon/core/mergetree/spill_channel_manager.h"
@@ -28,28 +29,33 @@ SpillWriter::SpillWriter(const std::shared_ptr<FileSystem>& fs,
2829
const std::shared_ptr<arrow::Schema>& schema,
2930
const std::shared_ptr<FileIOChannel::Enumerator>& channel_enumerator,
3031
const std::shared_ptr<SpillChannelManager>& spill_channel_manager,
31-
const std::string& compression, int32_t compression_level)
32+
const std::string& compression, int32_t compression_level,
33+
const std::shared_ptr<MemoryPool>& pool)
3234
: fs_(fs),
3335
schema_(schema),
3436
channel_enumerator_(channel_enumerator),
3537
spill_channel_manager_(spill_channel_manager),
3638
compression_(compression),
37-
compression_level_(compression_level) {}
39+
compression_level_(compression_level),
40+
arrow_pool_(GetArrowPool(pool)) {}
3841

3942
Result<std::unique_ptr<SpillWriter>> SpillWriter::Create(
4043
const std::shared_ptr<FileSystem>& fs, const std::shared_ptr<arrow::Schema>& schema,
4144
const std::shared_ptr<FileIOChannel::Enumerator>& channel_enumerator,
4245
const std::shared_ptr<SpillChannelManager>& spill_channel_manager,
43-
const std::string& compression, int32_t compression_level) {
44-
std::unique_ptr<SpillWriter> writer(new SpillWriter(
45-
fs, schema, channel_enumerator, spill_channel_manager, compression, compression_level));
46+
const std::string& compression, int32_t compression_level,
47+
const std::shared_ptr<MemoryPool>& pool) {
48+
std::unique_ptr<SpillWriter> writer(new SpillWriter(fs, schema, channel_enumerator,
49+
spill_channel_manager, compression,
50+
compression_level, pool));
4651
PAIMON_RETURN_NOT_OK(writer->Open());
4752
return writer;
4853
}
4954

5055
Status SpillWriter::Open() {
5156
channel_id_ = channel_enumerator_->Next();
5257
auto ipc_write_options = arrow::ipc::IpcWriteOptions::Defaults();
58+
ipc_write_options.memory_pool = arrow_pool_.get();
5359
auto cleanup_guard = ScopeGuard([&]() {
5460
arrow_writer_.reset();
5561
arrow_output_stream_adapter_.reset();

src/paimon/core/mergetree/spill_writer.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Schema;
3333
namespace paimon {
3434

3535
class ArrowOutputStreamAdapter;
36+
class MemoryPool;
3637
class SpillChannelManager;
3738

3839
class SpillWriter {
@@ -41,7 +42,8 @@ class SpillWriter {
4142
const std::shared_ptr<FileSystem>& fs, const std::shared_ptr<arrow::Schema>& schema,
4243
const std::shared_ptr<FileIOChannel::Enumerator>& channel_enumerator,
4344
const std::shared_ptr<SpillChannelManager>& spill_channel_manager,
44-
const std::string& compression, int32_t compression_level);
45+
const std::string& compression, int32_t compression_level,
46+
const std::shared_ptr<MemoryPool>& pool);
4547

4648
SpillWriter(const SpillWriter&) = delete;
4749
SpillWriter& operator=(const SpillWriter&) = delete;
@@ -55,7 +57,8 @@ class SpillWriter {
5557
SpillWriter(const std::shared_ptr<FileSystem>& fs, const std::shared_ptr<arrow::Schema>& schema,
5658
const std::shared_ptr<FileIOChannel::Enumerator>& channel_enumerator,
5759
const std::shared_ptr<SpillChannelManager>& spill_channel_manager,
58-
const std::string& compression, int32_t compression_level);
60+
const std::string& compression, int32_t compression_level,
61+
const std::shared_ptr<MemoryPool>& pool);
5962

6063
Status Open();
6164

@@ -67,6 +70,7 @@ class SpillWriter {
6770
int32_t compression_level_;
6871
std::shared_ptr<OutputStream> out_stream_;
6972
std::shared_ptr<ArrowOutputStreamAdapter> arrow_output_stream_adapter_;
73+
std::unique_ptr<arrow::MemoryPool> arrow_pool_;
7074
std::shared_ptr<arrow::ipc::RecordBatchWriter> arrow_writer_;
7175
FileIOChannel::ID channel_id_;
7276
bool closed_ = false;

0 commit comments

Comments
 (0)