Skip to content

Commit 5098e07

Browse files
committed
feat(spill): add SpillWriter, SpillReader and SpillChannelManager for spill-to-disk support
1 parent 3b444c7 commit 5098e07

18 files changed

Lines changed: 1282 additions & 35 deletions

src/paimon/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ set(PAIMON_COMMON_SRCS
146146
common/utils/string_utils.cpp)
147147

148148
set(PAIMON_CORE_SRCS
149+
core/disk/file_io_channel.cpp
149150
core/disk/io_manager.cpp
151+
core/mergetree/spill_reader.cpp
152+
core/mergetree/spill_writer.cpp
150153
core/append/append_only_writer.cpp
151154
core/append/bucketed_append_compact_manager.cpp
152155
core/casting/binary_to_string_cast_executor.cpp
@@ -605,6 +608,8 @@ if(PAIMON_BUILD_TESTS)
605608
core/mergetree/merge_tree_writer_test.cpp
606609
core/mergetree/write_buffer_test.cpp
607610
core/mergetree/sorted_run_test.cpp
611+
core/mergetree/spill_channel_manager_test.cpp
612+
core/mergetree/spill_reader_writer_test.cpp
608613
core/migrate/file_meta_utils_test.cpp
609614
core/operation/metrics/compaction_metrics_test.cpp
610615
core/operation/data_evolution_file_store_scan_test.cpp

src/paimon/common/utils/arrow/arrow_utils.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
#include "arrow/array/array_base.h"
2020
#include "arrow/array/array_nested.h"
21+
#include "arrow/util/compression.h"
2122
#include "paimon/common/utils/arrow/status_utils.h"
23+
#include "paimon/common/utils/string_utils.h"
2224

2325
namespace paimon {
2426
Result<std::shared_ptr<arrow::Schema>> ArrowUtils::DataTypeToSchema(
@@ -161,4 +163,14 @@ Result<std::shared_ptr<arrow::StructArray>> ArrowUtils::RemoveFieldFromStructArr
161163
return array;
162164
}
163165

166+
Result<arrow::Compression::type> ArrowUtils::GetCompressionType(const std::string& compression) {
167+
std::string normalized = StringUtils::ToLowerCase(compression);
168+
if (normalized.empty() || normalized == "none") {
169+
normalized = "uncompressed";
170+
}
171+
PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(arrow::Compression::type compression_type,
172+
arrow::util::Codec::GetCompressionType(normalized));
173+
return compression_type;
174+
}
175+
164176
} // namespace paimon

src/paimon/common/utils/arrow/arrow_utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <vector>
2020

2121
#include "arrow/api.h"
22+
#include "arrow/util/type_fwd.h"
2223
#include "fmt/format.h"
2324
#include "paimon/result.h"
2425

@@ -49,6 +50,10 @@ class PAIMON_EXPORT ArrowUtils {
4950
static bool EqualsIgnoreNullable(const std::shared_ptr<arrow::DataType>& type,
5051
const std::shared_ptr<arrow::DataType>& other_type);
5152

53+
/// Normalize and resolve a compression string to an Arrow compression type.
54+
/// Handles "none" and empty string by mapping them to "uncompressed".
55+
static Result<arrow::Compression::type> GetCompressionType(const std::string& compression);
56+
5257
private:
5358
static Status InnerCheckNullabilityMatch(const std::shared_ptr<arrow::Field>& field,
5459
const std::shared_ptr<arrow::Array>& data);

src/paimon/common/utils/arrow/arrow_utils_test.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,43 @@ TEST(ArrowUtilsTest, TestEqualsIgnoreNullable) {
445445
ASSERT_TRUE(ArrowUtils::EqualsIgnoreNullable(struct_type1, struct_type2));
446446
}
447447
}
448+
449+
TEST(ArrowUtilsTest, TestGetCompressionType) {
450+
{
451+
ASSERT_OK_AND_ASSIGN(auto type, ArrowUtils::GetCompressionType(""));
452+
ASSERT_EQ(type, arrow::Compression::UNCOMPRESSED);
453+
}
454+
{
455+
ASSERT_OK_AND_ASSIGN(auto type, ArrowUtils::GetCompressionType("none"));
456+
ASSERT_EQ(type, arrow::Compression::UNCOMPRESSED);
457+
}
458+
{
459+
ASSERT_OK_AND_ASSIGN(auto type, ArrowUtils::GetCompressionType("uncompressed"));
460+
ASSERT_EQ(type, arrow::Compression::UNCOMPRESSED);
461+
}
462+
{
463+
ASSERT_OK_AND_ASSIGN(auto type, ArrowUtils::GetCompressionType("zstd"));
464+
ASSERT_EQ(type, arrow::Compression::ZSTD);
465+
}
466+
{
467+
ASSERT_OK_AND_ASSIGN(auto type, ArrowUtils::GetCompressionType("ZSTD"));
468+
ASSERT_EQ(type, arrow::Compression::ZSTD);
469+
}
470+
{
471+
ASSERT_OK_AND_ASSIGN(auto type, ArrowUtils::GetCompressionType("lz4"));
472+
ASSERT_EQ(type, arrow::Compression::LZ4_FRAME);
473+
}
474+
{
475+
ASSERT_OK_AND_ASSIGN(auto type, ArrowUtils::GetCompressionType("snappy"));
476+
ASSERT_EQ(type, arrow::Compression::SNAPPY);
477+
}
478+
{
479+
ASSERT_OK_AND_ASSIGN(auto type, ArrowUtils::GetCompressionType("gzip"));
480+
ASSERT_EQ(type, arrow::Compression::GZIP);
481+
}
482+
{
483+
ASSERT_NOK(ArrowUtils::GetCompressionType("invalid_codec"));
484+
}
485+
}
486+
448487
} // namespace paimon::test
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2026-present Alibaba Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <memory>
20+
#include <mutex>
21+
#include <random>
22+
#include <string>
23+
24+
#include "paimon/common/utils/path_util.h"
25+
#include "paimon/common/utils/uuid.h"
26+
#include "paimon/core/disk/file_io_channel.h"
27+
#include "paimon/fs/local/local_file_system.h"
28+
#include "paimon/result.h"
29+
30+
namespace paimon {
31+
class FileChannelManager {
32+
public:
33+
static Result<std::unique_ptr<FileChannelManager>> Create(const std::string& tmp_dir,
34+
const std::string& prefix) {
35+
std::string uuid;
36+
if (!UUID::Generate(&uuid)) {
37+
return Status::Invalid("Failed to generate UUID for FileChannelManager.");
38+
}
39+
std::string spill_dir = PathUtil::JoinPath(tmp_dir, "paimon-" + prefix + "-" + uuid);
40+
41+
auto fs = std::make_shared<LocalFileSystem>();
42+
PAIMON_RETURN_NOT_OK(fs->Mkdirs(spill_dir));
43+
44+
std::random_device rd;
45+
std::mt19937 random(rd());
46+
47+
return std::unique_ptr<FileChannelManager>(
48+
new FileChannelManager(spill_dir, std::move(random), fs));
49+
}
50+
51+
~FileChannelManager() {
52+
if (!spill_dir_.empty() && fs_ != nullptr) {
53+
[[maybe_unused]] auto status = fs_->Delete(spill_dir_, /*recursive=*/true);
54+
}
55+
}
56+
57+
FileChannelManager(const FileChannelManager&) = delete;
58+
FileChannelManager& operator=(const FileChannelManager&) = delete;
59+
60+
FileIOChannel::ID CreateChannel() {
61+
std::lock_guard<std::mutex> lock(mutex_);
62+
return FileIOChannel::ID(spill_dir_, &random_);
63+
}
64+
65+
FileIOChannel::ID CreateChannel(const std::string& prefix) {
66+
std::lock_guard<std::mutex> lock(mutex_);
67+
return FileIOChannel::ID(spill_dir_, prefix, &random_);
68+
}
69+
70+
std::shared_ptr<FileIOChannel::Enumerator> CreateChannelEnumerator() {
71+
std::lock_guard<std::mutex> lock(mutex_);
72+
return std::make_shared<FileIOChannel::Enumerator>(spill_dir_, &random_);
73+
}
74+
75+
const std::string& GetSpillDir() const {
76+
return spill_dir_;
77+
}
78+
79+
private:
80+
FileChannelManager(const std::string& spill_dir, std::mt19937&& random,
81+
const std::shared_ptr<FileSystem>& fs)
82+
: spill_dir_(spill_dir), random_(std::move(random)), fs_(fs) {}
83+
std::string spill_dir_;
84+
std::mutex mutex_;
85+
std::mt19937 random_;
86+
std::shared_ptr<FileSystem> fs_;
87+
};
88+
89+
} // namespace paimon
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2026-present Alibaba Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "paimon/core/disk/file_io_channel.h"
18+
19+
#include <iomanip>
20+
#include <sstream>
21+
#include <utility>
22+
23+
#include "paimon/common/utils/path_util.h"
24+
25+
namespace paimon {
26+
std::string FileIOChannel::GenerateRandomHexString(std::mt19937* random) {
27+
std::uniform_int_distribution<int32_t> dist(0, 255);
28+
std::ostringstream hex_stream;
29+
hex_stream << std::hex << std::setfill('0');
30+
for (int32_t i = 0; i < kRandomBytesLength; ++i) {
31+
hex_stream << std::setw(2) << dist(*random);
32+
}
33+
return hex_stream.str();
34+
}
35+
36+
FileIOChannel::ID::ID(const std::string& path) : path_(path) {}
37+
38+
FileIOChannel::ID::ID(const std::string& base_path, std::mt19937* random)
39+
: path_(PathUtil::JoinPath(base_path, GenerateRandomHexString(random) + ".channel")) {}
40+
41+
FileIOChannel::ID::ID(const std::string& base_path, const std::string& prefix, std::mt19937* random)
42+
: path_(PathUtil::JoinPath(base_path,
43+
prefix + "-" + GenerateRandomHexString(random) + ".channel")) {}
44+
45+
const std::string& FileIOChannel::ID::GetPath() const {
46+
return path_;
47+
}
48+
49+
bool FileIOChannel::ID::operator==(const ID& other) const {
50+
return path_ == other.path_;
51+
}
52+
53+
bool FileIOChannel::ID::operator!=(const ID& other) const {
54+
return !(*this == other);
55+
}
56+
57+
size_t FileIOChannel::ID::Hash::operator()(const ID& id) const {
58+
return std::hash<std::string>{}(id.path_);
59+
}
60+
61+
FileIOChannel::Enumerator::Enumerator(const std::string& base_path, std::mt19937* random)
62+
: path_(base_path), name_prefix_(GenerateRandomHexString(random)) {}
63+
64+
FileIOChannel::ID FileIOChannel::Enumerator::Next() {
65+
std::ostringstream filename;
66+
filename << name_prefix_ << "." << std::setfill('0') << std::setw(6) << (local_counter_++)
67+
<< ".channel";
68+
69+
std::string full_path = PathUtil::JoinPath(path_, filename.str());
70+
return ID(full_path);
71+
}
72+
73+
} // namespace paimon
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2026-present Alibaba Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <cstdint>
20+
#include <memory>
21+
#include <random>
22+
#include <string>
23+
24+
#include "paimon/visibility.h"
25+
26+
namespace paimon {
27+
class PAIMON_EXPORT FileIOChannel {
28+
public:
29+
class PAIMON_EXPORT ID {
30+
public:
31+
ID() = default;
32+
33+
explicit ID(const std::string& path);
34+
35+
ID(const std::string& base_path, std::mt19937* random);
36+
37+
ID(const std::string& base_path, const std::string& prefix, std::mt19937* random);
38+
39+
const std::string& GetPath() const;
40+
41+
bool operator==(const ID& other) const;
42+
43+
bool operator!=(const ID& other) const;
44+
45+
struct Hash {
46+
size_t operator()(const ID& id) const;
47+
};
48+
49+
private:
50+
std::string path_;
51+
};
52+
53+
private:
54+
static constexpr int32_t kRandomBytesLength = 16;
55+
static std::string GenerateRandomHexString(std::mt19937* random);
56+
57+
public:
58+
class PAIMON_EXPORT Enumerator {
59+
public:
60+
Enumerator(const std::string& base_path, std::mt19937* random);
61+
62+
ID Next();
63+
64+
private:
65+
std::string path_;
66+
std::string name_prefix_;
67+
uint64_t local_counter_{0};
68+
};
69+
};
70+
71+
} // namespace paimon

src/paimon/core/disk/io_manager.cpp

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
#include "paimon/disk/io_manager.h"
17-
18-
#include "paimon/common/utils/path_util.h"
19-
#include "paimon/common/utils/uuid.h"
16+
#include "paimon/core/disk/io_manager_impl.h"
2017

2118
namespace paimon {
22-
class IOManagerImpl : public IOManager {
23-
public:
24-
explicit IOManagerImpl(const std::string& tmp_dir) : tmp_dir_(tmp_dir) {}
25-
26-
const std::string& GetTempDir() const override {
27-
return tmp_dir_;
28-
}
29-
30-
Result<std::string> GenerateTempFilePath(const std::string& prefix) const override {
31-
std::string uuid;
32-
if (!UUID::Generate(&uuid)) {
33-
return Status::Invalid("generate uuid for io manager tmp path failed.");
34-
}
35-
return PathUtil::JoinPath(tmp_dir_, prefix + "-" + uuid + std::string(kSuffix));
36-
}
37-
38-
private:
39-
static constexpr char kSuffix[] = ".channel";
40-
std::string tmp_dir_;
41-
};
42-
4319
std::unique_ptr<IOManager> IOManager::Create(const std::string& tmp_dir) {
4420
return std::make_unique<IOManagerImpl>(tmp_dir);
4521
}

0 commit comments

Comments
 (0)