Skip to content

Commit 9f4e216

Browse files
committed
feat(avro): support zstd compression level configuration
1 parent 623db38 commit 9f4e216

8 files changed

Lines changed: 55 additions & 10 deletions

src/paimon/format/avro/avro_file_batch_reader_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ namespace paimon::avro::test {
3939
class AvroFileBatchReaderTest : public ::testing::Test, public ::testing::WithParamInterface<bool> {
4040
public:
4141
void SetUp() override {
42-
ASSERT_OK_AND_ASSIGN(file_format_, FileFormatFactory::Get("avro", {}));
42+
ASSERT_OK_AND_ASSIGN(file_format_,
43+
FileFormatFactory::Get("avro", {{Options::FILE_FORMAT, "avro"}}));
4344
fs_ = std::make_shared<LocalFileSystem>();
4445
dir_ = ::paimon::test::UniqueTestDirectory::Create();
4546
ASSERT_TRUE(dir_);

src/paimon/format/avro/avro_file_format_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ namespace paimon::avro::test {
4545
class AvroFileFormatTest : public testing::Test, public ::testing::WithParamInterface<std::string> {
4646
public:
4747
void SetUp() override {
48-
ASSERT_OK_AND_ASSIGN(file_format_, FileFormatFactory::Get("avro", {}));
48+
ASSERT_OK_AND_ASSIGN(file_format_,
49+
FileFormatFactory::Get("avro", {{Options::FILE_FORMAT, "avro"}}));
4950
fs_ = std::make_shared<LocalFileSystem>();
5051
dir_ = ::paimon::test::UniqueTestDirectory::Create();
5152
ASSERT_TRUE(dir_);

src/paimon/format/avro/avro_format_writer.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ AvroFormatWriter::AvroFormatWriter(std::unique_ptr<::avro::DataFileWriterBase>&&
5353

5454
Result<std::unique_ptr<AvroFormatWriter>> AvroFormatWriter::Create(
5555
std::unique_ptr<AvroOutputStreamImpl> out, const std::shared_ptr<arrow::Schema>& schema,
56-
const ::avro::Codec codec) {
56+
const ::avro::Codec codec, std::optional<int> compression_level) {
5757
try {
5858
PAIMON_ASSIGN_OR_RAISE(::avro::ValidSchema avro_schema,
5959
AvroSchemaConverter::ArrowSchemaToAvroSchema(schema));
6060
AvroOutputStreamImpl* avro_output_stream = out.get();
61-
auto writer = std::make_unique<::avro::DataFileWriterBase>(std::move(out), avro_schema,
62-
DEFAULT_SYNC_INTERVAL, codec);
61+
auto writer = std::make_unique<::avro::DataFileWriterBase>(
62+
std::move(out), avro_schema, DEFAULT_SYNC_INTERVAL, codec, ::avro::Metadata(),
63+
compression_level);
6364
auto data_type = arrow::struct_(schema->fields());
6465
return std::unique_ptr<AvroFormatWriter>(
6566
new AvroFormatWriter(std::move(writer), avro_schema, data_type, avro_output_stream));

src/paimon/format/avro/avro_format_writer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <cstddef>
2020
#include <cstdint>
2121
#include <memory>
22+
#include <optional>
2223

2324
#include "arrow/api.h"
2425
#include "avro/DataFile.hh"
@@ -49,7 +50,7 @@ class AvroFormatWriter : public FormatWriter {
4950
public:
5051
static Result<std::unique_ptr<AvroFormatWriter>> Create(
5152
std::unique_ptr<AvroOutputStreamImpl> out, const std::shared_ptr<arrow::Schema>& schema,
52-
const ::avro::Codec codec);
53+
const ::avro::Codec codec, std::optional<int> compression_level = std::nullopt);
5354

5455
Status AddBatch(ArrowArray* batch) override;
5556

src/paimon/format/avro/avro_format_writer_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ class AvroFormatWriterTest : public ::testing::Test {
6868
int32_t batch_size) {
6969
::ArrowSchema c_schema;
7070
EXPECT_TRUE(arrow::ExportSchema(*schema, &c_schema).ok());
71-
EXPECT_OK_AND_ASSIGN(auto file_format, FileFormatFactory::Get("avro", {}));
71+
EXPECT_OK_AND_ASSIGN(auto file_format,
72+
FileFormatFactory::Get("avro", {{Options::FILE_FORMAT, "avro"}}));
7273
EXPECT_OK_AND_ASSIGN(auto writer_builder,
7374
file_format->CreateWriterBuilder(&c_schema, batch_size));
7475
EXPECT_OK_AND_ASSIGN(std::shared_ptr<FormatWriter> writer,

src/paimon/format/avro/avro_writer_builder.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "avro/DataFile.hh"
2626
#include "avro/Stream.hh"
2727
#include "paimon/common/utils/string_utils.h"
28+
#include "paimon/core/core_options.h"
2829
#include "paimon/format/avro/avro_format_writer.h"
2930
#include "paimon/format/avro/avro_output_stream_impl.h"
3031
#include "paimon/format/writer_builder.h"
@@ -58,7 +59,10 @@ class AvroWriterBuilder : public WriterBuilder {
5859
auto output_stream = std::make_unique<AvroOutputStreamImpl>(out, BUFFER_SIZE, pool_);
5960
PAIMON_ASSIGN_OR_RAISE(::avro::Codec codec,
6061
ToAvroCompressionKind(StringUtils::ToLowerCase(compression)));
61-
return AvroFormatWriter::Create(std::move(output_stream), schema_, codec);
62+
PAIMON_ASSIGN_OR_RAISE(std::optional<int> compression_zstd_level,
63+
GetAvroCompressionZstdLevel(codec));
64+
return AvroFormatWriter::Create(std::move(output_stream), schema_, codec,
65+
compression_zstd_level);
6266
}
6367

6468
private:
@@ -77,6 +81,14 @@ class AvroWriterBuilder : public WriterBuilder {
7781
return Status::Invalid("unknown compression " + file_compression);
7882
}
7983
}
84+
Result<std::optional<int>> GetAvroCompressionZstdLevel(const ::avro::Codec& codec) {
85+
std::optional<int> compression_zstd_level;
86+
if (codec == ::avro::Codec::ZSTD_CODEC) {
87+
PAIMON_ASSIGN_OR_RAISE(CoreOptions core_options, CoreOptions::FromMap(options_));
88+
compression_zstd_level = core_options.GetFileCompressionZstdLevel();
89+
}
90+
return compression_zstd_level;
91+
}
8092

8193
std::shared_ptr<MemoryPool> pool_;
8294
std::shared_ptr<arrow::Schema> schema_;

src/paimon/format/avro/avro_writer_builder_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,32 @@ TEST(ToAvroCompressionKindTest, HandlesInvalidCompression) {
5151
TEST(ToAvroCompressionKindTest, HandlesEmptyString) {
5252
ASSERT_NOK(AvroWriterBuilder::ToAvroCompressionKind(""));
5353
}
54+
55+
TEST(ToAvroCompressionKindTest, GetAvroCompressionZstdLevel) {
56+
AvroWriterBuilder builder(nullptr, -1, {{Options::FILE_FORMAT, "avro"}});
57+
ASSERT_OK_AND_ASSIGN(std::optional<int> zstd_level,
58+
builder.GetAvroCompressionZstdLevel(::avro::Codec::ZSTD_CODEC));
59+
ASSERT_TRUE(zstd_level.has_value());
60+
ASSERT_EQ(zstd_level.value(), 1);
61+
62+
ASSERT_OK_AND_ASSIGN(std::optional<int> compression_level1,
63+
builder.GetAvroCompressionZstdLevel(::avro::Codec::SNAPPY_CODEC));
64+
ASSERT_FALSE(compression_level1.has_value());
65+
66+
ASSERT_OK_AND_ASSIGN(std::optional<int> compression_level2,
67+
builder.GetAvroCompressionZstdLevel(::avro::Codec::DEFLATE_CODEC));
68+
ASSERT_FALSE(compression_level2.has_value());
69+
70+
ASSERT_OK_AND_ASSIGN(std::optional<int> compression_level3,
71+
builder.GetAvroCompressionZstdLevel(::avro::Codec::NULL_CODEC));
72+
ASSERT_FALSE(compression_level3.has_value());
73+
74+
AvroWriterBuilder builder2(
75+
nullptr, -1, {{Options::FILE_FORMAT, "avro"}, {Options::FILE_COMPRESSION_ZSTD_LEVEL, "3"}});
76+
ASSERT_OK_AND_ASSIGN(std::optional<int> zstd_level2,
77+
builder2.GetAvroCompressionZstdLevel(::avro::Codec::ZSTD_CODEC));
78+
ASSERT_TRUE(zstd_level2.has_value());
79+
ASSERT_EQ(zstd_level2.value(), 3);
80+
}
81+
5482
} // namespace paimon::avro::test

third_party/versions.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ PAIMON_GTEST_PKG_NAME=gtest-${PAIMON_GTEST_BUILD_VERSION}.tar.gz
5555
PAIMON_ARROW_BUILD_VERSION=17.0.0
5656
PAIMON_ARROW_BUILD_SHA256_CHECKSUM=9d280d8042e7cf526f8c28d170d93bfab65e50f94569f6a790982a878d8d898d
5757
PAIMON_ARROW_PKG_NAME=apache-arrow-${PAIMON_ARROW_BUILD_VERSION}.tar.gz
58-
PAIMON_AVRO_BUILD_VERSION=54b332161524086dcb6cde8afe097097eed7f3ee
59-
PAIMON_AVRO_BUILD_SHA256_CHECKSUM=00febd590b1e328d3a97b67a6d29a1d0243e0e41bb2b1582ec580d37698d1fe2
58+
PAIMON_AVRO_BUILD_VERSION=c499eefb48aa2db906c7bca14a047223806f36db
59+
PAIMON_AVRO_BUILD_SHA256_CHECKSUM=9771f1dcfe3c01aff7ff670e873e66d3406362f71941821d482de65f3d32d780
6060
PAIMON_AVRO_PKG_NAME=avro-${PAIMON_AVRO_BUILD_VERSION}.tar.gz
6161
PAIMON_FMT_BUILD_VERSION=11.2.0
6262
PAIMON_FMT_BUILD_SHA256_CHECKSUM=bc23066d87ab3168f27cef3e97d545fa63314f5c79df5ea444d41d56f962c6af

0 commit comments

Comments
 (0)