-
Notifications
You must be signed in to change notification settings - Fork 4k
GH-48864: [C++] Support customizing more Zstd parameters #48865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HuaHuaY
wants to merge
5
commits into
apache:main
Choose a base branch
from
HuaHuaY:fix_issue_48864
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -16,12 +16,15 @@ | |
| // under the License. | ||
|
|
||
| #include <algorithm> | ||
| #include <concepts> | ||
| #include <cstdint> | ||
| #include <cstring> | ||
| #include <memory> | ||
| #include <ostream> | ||
| #include <random> | ||
| #include <span> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
@@ -446,36 +449,17 @@ TEST(TestCodecMisc, SpecifyCompressionLevel) { | |
| } | ||
| } | ||
|
|
||
| TEST(TestCodecMisc, SpecifyCodecOptionsGZip) { | ||
| // for now only GZIP & Brotli codec options supported, since it has specific parameters | ||
| // to be customized, other codecs could directly go with CodecOptions, could add more | ||
| // specific codec options if needed. | ||
| struct CombinationOption { | ||
| int level; | ||
| GZipFormat format; | ||
| int window_bits; | ||
| bool expect_success; | ||
| }; | ||
| constexpr CombinationOption combinations[] = {{2, GZipFormat::ZLIB, 12, true}, | ||
| {9, GZipFormat::GZIP, 9, true}, | ||
| {9, GZipFormat::GZIP, 20, false}, | ||
| {5, GZipFormat::DEFLATE, -12, false}, | ||
| {-992, GZipFormat::GZIP, 15, false}}; | ||
|
|
||
| template <std::derived_from<arrow::util::CodecOptions> T> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neat, a concept 😀 |
||
| void CheckSpecifyCodecOptions(Compression::type compression, | ||
| std::span<const std::pair<T, bool>> options) { | ||
| std::vector<uint8_t> data = MakeRandomData(2000); | ||
| for (const auto& combination : combinations) { | ||
| const auto compression = Compression::GZIP; | ||
| for (const auto& [codec_option, expect_success] : options) { | ||
| if (!Codec::IsAvailable(compression)) { | ||
| // Support for this codec hasn't been built | ||
| continue; | ||
| } | ||
| auto codec_options = arrow::util::GZipCodecOptions(); | ||
| codec_options.compression_level = combination.level; | ||
| codec_options.gzip_format = combination.format; | ||
| codec_options.window_bits = combination.window_bits; | ||
| const auto expect_success = combination.expect_success; | ||
| auto result1 = Codec::Create(compression, codec_options); | ||
| auto result2 = Codec::Create(compression, codec_options); | ||
| auto result1 = Codec::Create(compression, codec_option); | ||
| auto result2 = Codec::Create(compression, codec_option); | ||
| ASSERT_EQ(expect_success, result1.ok()); | ||
| ASSERT_EQ(expect_success, result2.ok()); | ||
| if (expect_success) { | ||
|
|
@@ -484,37 +468,169 @@ TEST(TestCodecMisc, SpecifyCodecOptionsGZip) { | |
| } | ||
| } | ||
|
|
||
| TEST(TestCodecMisc, SpecifyCodecOptionsGZip) { | ||
| auto make_option = [](int compression_level, GZipFormat format, | ||
| std::optional<int> window_bits) { | ||
| arrow::util::GZipCodecOptions option; | ||
| option.compression_level = compression_level; | ||
| option.gzip_format = format; | ||
| option.window_bits = window_bits; | ||
| return option; | ||
| }; | ||
| const std::pair<arrow::util::GZipCodecOptions, bool> options[]{ | ||
| {make_option(5, GZipFormat::GZIP, 15), true}, | ||
| {make_option(9, GZipFormat::ZLIB, 12), true}, | ||
| {make_option(-1, GZipFormat::DEFLATE, 10), true}, | ||
| {make_option(10, GZipFormat::GZIP, 25), false}, | ||
| {make_option(-992, GZipFormat::GZIP, 15), false}, | ||
| }; | ||
| CheckSpecifyCodecOptions<arrow::util::GZipCodecOptions>(Compression::GZIP, options); | ||
| } | ||
|
|
||
| TEST(TestCodecMisc, SpecifyCodecOptionsBrotli) { | ||
| // for now only GZIP & Brotli codec options supported, since it has specific parameters | ||
| // to be customized, other codecs could directly go with CodecOptions, could add more | ||
| // specific codec options if needed. | ||
| struct CombinationOption { | ||
| int level; | ||
| int window_bits; | ||
| bool expect_success; | ||
| auto make_option = [](int compression_level, std::optional<int> window_bits) { | ||
| arrow::util::BrotliCodecOptions option; | ||
| option.compression_level = compression_level; | ||
| option.window_bits = window_bits; | ||
| return option; | ||
| }; | ||
| constexpr CombinationOption combinations[] = { | ||
| {8, 22, true}, {11, 10, true}, {1, 24, true}, {5, -12, false}, {-992, 25, false}}; | ||
| const std::pair<arrow::util::BrotliCodecOptions, bool> options[]{ | ||
| {make_option(8, 22), true}, {make_option(11, 10), true}, | ||
| {make_option(1, 24), true}, {make_option(5, -12), false}, | ||
| {make_option(-992, 25), false}, | ||
| }; | ||
| CheckSpecifyCodecOptions<arrow::util::BrotliCodecOptions>(Compression::BROTLI, options); | ||
| } | ||
|
|
||
| std::vector<uint8_t> data = MakeRandomData(2000); | ||
| for (const auto& combination : combinations) { | ||
| const auto compression = Compression::BROTLI; | ||
| if (!Codec::IsAvailable(compression)) { | ||
| // Support for this codec hasn't been built | ||
| continue; | ||
| TEST(TestCodecMisc, SpecifyCodecOptionsZstd) { | ||
| auto make_option = [](int compression_level, | ||
| std::vector<std::pair<int, int>> compression_context_params, | ||
| std::vector<std::pair<int, int>> decompression_context_params) { | ||
| arrow::util::ZstdCodecOptions option; | ||
| option.compression_level = compression_level; | ||
| option.compression_context_params = std::move(compression_context_params); | ||
| option.decompression_context_params = std::move(decompression_context_params); | ||
| return option; | ||
| }; | ||
| constexpr int ZSTD_c_windowLog = 101; | ||
| const std::pair<arrow::util::ZstdCodecOptions, bool> options[]{ | ||
| {make_option(2, {}, {}), true}, | ||
| {make_option(9, {}, {}), true}, | ||
| {make_option(15, {}, {}), true}, | ||
| {make_option(-992, {}, {}), true}, | ||
| {make_option(3, {{ZSTD_c_windowLog, 23}}, {}), true}, | ||
| {make_option(3, {{ZSTD_c_windowLog, 28}}, {}), true}}; | ||
| CheckSpecifyCodecOptions<arrow::util::ZstdCodecOptions>(Compression::ZSTD, options); | ||
| } | ||
|
|
||
| TEST(TestCodecMisc, ZstdLargerWindowLog) { | ||
| constexpr int ZSTD_c_windowLog = 101; | ||
|
|
||
| arrow::util::ZstdCodecOptions option1; | ||
| arrow::util::ZstdCodecOptions option2; | ||
| option2.compression_context_params = {{ZSTD_c_windowLog, 28}}; | ||
|
|
||
| std::vector<uint8_t> data = MakeRandomData(4 * 1024 * 1024); | ||
| data.reserve(data.size() * 2); | ||
| data.insert(data.end(), data.begin(), data.end()); | ||
|
|
||
| auto compress = [&data](const arrow::util::ZstdCodecOptions& codecOption) | ||
| -> Result<std::vector<uint8_t>> { | ||
| ARROW_ASSIGN_OR_RAISE(auto codec, Codec::Create(Compression::ZSTD, codecOption)); | ||
| auto max_compressed_len = codec->MaxCompressedLen(data.size(), data.data()); | ||
| std::vector<uint8_t> compressed(max_compressed_len); | ||
|
|
||
| ARROW_ASSIGN_OR_RAISE( | ||
| auto actual_size, | ||
| codec->Compress(data.size(), data.data(), max_compressed_len, compressed.data())); | ||
| compressed.resize(actual_size); | ||
| return compressed; | ||
| }; | ||
|
|
||
| ASSERT_OK_AND_ASSIGN(auto compressed1, compress(option1)); | ||
| ASSERT_OK_AND_ASSIGN(auto compressed2, compress(option2)); | ||
| ASSERT_GT(compressed1.size(), compressed2.size()); | ||
| } | ||
|
|
||
| TEST(TestCodecMisc, ZstdStreamLargerWindowLog) { | ||
| constexpr int ZSTD_c_windowLog = 101; | ||
| constexpr int ZSTD_d_windowLogMax = 100; | ||
|
|
||
| arrow::util::ZstdCodecOptions option1; | ||
| arrow::util::ZstdCodecOptions option2; | ||
| option2.compression_context_params = {{ZSTD_c_windowLog, 28}}; | ||
| option2.decompression_context_params = {{ZSTD_d_windowLogMax, 28}}; | ||
|
|
||
| std::vector<uint8_t> data = MakeRandomData(4 * 1024 * 1024); | ||
| data.reserve(data.size() * 2); | ||
| data.insert(data.end(), data.begin(), data.end()); | ||
|
|
||
| ASSERT_OK_AND_ASSIGN(auto codec1, Codec::Create(Compression::ZSTD, option1)); | ||
| ASSERT_OK_AND_ASSIGN(auto codec2, Codec::Create(Compression::ZSTD, option2)); | ||
|
|
||
| auto compress = [&data](Codec& codec) -> Result<std::vector<uint8_t>> { | ||
| auto max_compressed_len = codec.MaxCompressedLen(data.size(), data.data()); | ||
| std::vector<uint8_t> compressed(max_compressed_len); | ||
|
|
||
| int64_t bytes_written = 0; | ||
| int64_t bytes_read = 0; | ||
| ARROW_ASSIGN_OR_RAISE(auto compressor, codec.MakeCompressor()); | ||
| while (bytes_read < static_cast<int64_t>(data.size())) { | ||
| ARROW_ASSIGN_OR_RAISE( | ||
| auto result, | ||
| compressor->Compress(data.size() - bytes_read, data.data() + bytes_read, | ||
| max_compressed_len - bytes_written, | ||
| compressed.data() + bytes_written)); | ||
| bytes_written += result.bytes_written; | ||
| bytes_read += result.bytes_read; | ||
| } | ||
| auto codec_options = arrow::util::BrotliCodecOptions(); | ||
| codec_options.compression_level = combination.level; | ||
| codec_options.window_bits = combination.window_bits; | ||
| const auto expect_success = combination.expect_success; | ||
| auto result1 = Codec::Create(compression, codec_options); | ||
| auto result2 = Codec::Create(compression, codec_options); | ||
| ASSERT_EQ(expect_success, result1.ok()); | ||
| ASSERT_EQ(expect_success, result2.ok()); | ||
| if (expect_success) { | ||
| CheckCodecRoundtrip(*result1, *result2, data); | ||
| while (true) { | ||
| ARROW_ASSIGN_OR_RAISE(auto result, | ||
| compressor->End(max_compressed_len - bytes_written, | ||
| compressed.data() + bytes_written)); | ||
| bytes_written += result.bytes_written; | ||
| if (!result.should_retry) { | ||
| break; | ||
| } | ||
| } | ||
| compressed.resize(bytes_written); | ||
| return compressed; | ||
| }; | ||
|
|
||
| ASSERT_OK_AND_ASSIGN(auto compressed1, compress(*codec1)); | ||
| ASSERT_OK_AND_ASSIGN(auto compressed2, compress(*codec2)); | ||
| ASSERT_GT(compressed1.size(), compressed2.size()); | ||
|
|
||
| ASSERT_OK_AND_ASSIGN(auto decompressor1, codec1->MakeDecompressor()); | ||
| ASSERT_OK_AND_ASSIGN(auto decompressor2, codec2->MakeDecompressor()); | ||
|
|
||
| std::vector<uint8_t> decompressed(data.size()); | ||
| // Using a windowLog greater than ZSTD_WINDOWLOG_LIMIT_DEFAULT(1 << 27) at compression | ||
| // stage requires explicitly allowing such size at streaming decompression stage. | ||
| auto ret = decompressor1->Decompress(compressed2.size(), compressed2.data(), | ||
| decompressed.size(), decompressed.data()); | ||
| ASSERT_NOT_OK(ret); | ||
| ASSERT_EQ(ret.status().message(), | ||
| "ZSTD decompress failed: Frame requires too much memory for decoding"); | ||
|
|
||
| int64_t bytes_written = 0; | ||
| int64_t bytes_read = 0; | ||
| while (true) { | ||
| ASSERT_OK_AND_ASSIGN(auto result, | ||
| decompressor2->Decompress(compressed2.size() - bytes_read, | ||
| compressed2.data() + bytes_read, | ||
| decompressed.size() - bytes_written, | ||
| decompressed.data() + bytes_written)); | ||
| bytes_read += result.bytes_read; | ||
| bytes_written += result.bytes_written; | ||
| if (!result.need_more_output) { | ||
| break; | ||
| } | ||
| } | ||
| ASSERT_TRUE(decompressor2->IsFinished()); | ||
| ASSERT_EQ(bytes_read, compressed2.size()); | ||
| ASSERT_EQ(bytes_written, data.size()); | ||
| ASSERT_EQ(decompressed, data); | ||
| } | ||
|
|
||
| TEST_P(CodecTest, MinMaxCompressionLevel) { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.