Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ If you experience network issues when downloading dependencies, you can customiz
- `ICEBERG_UTF8PROC_URL`: utf8proc tarball URL
- `ICEBERG_NLOHMANN_JSON_URL`: nlohmann-json tarball URL
- `ICEBERG_SPDLOG_URL`: spdlog tarball URL
- `ICEBERG_ZSTD_URL`: zstd tarball URL
- `ICEBERG_CPR_URL`: cpr tarball URL

Example:
Expand Down
83 changes: 77 additions & 6 deletions cmake_modules/IcebergThirdpartyToolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -902,16 +902,86 @@ endfunction()

# ----------------------------------------------------------------------
# Zstd
#

function(resolve_zstd_dependency)
find_package(zstd CONFIG)
if(zstd_FOUND)
prepare_fetchcontent()

set(ZSTD_BUILD_PROGRAMS OFF)
set(ZSTD_BUILD_TESTS OFF)
set(ZSTD_BUILD_SHARED OFF)
set(ZSTD_BUILD_STATIC ON)
set(ZSTD_MULTITHREAD_SUPPORT OFF)

set(ICEBERG_ZSTD_BUILD_VERSION "1.5.7")
set(ICEBERG_ZSTD_BUILD_SHA256_CHECKSUM
"eb33e51f49a15e023950cd7825ca74a4a2b43db8354825ac24fc1b7ee09e6fa3")

if(DEFINED ENV{ICEBERG_ZSTD_URL})
set(ZSTD_URL "$ENV{ICEBERG_ZSTD_URL}")
else()
set(ZSTD_URL
"https://github.com/facebook/zstd/releases/download/v${ICEBERG_ZSTD_BUILD_VERSION}/zstd-${ICEBERG_ZSTD_BUILD_VERSION}.tar.gz"
)
endif()

fetchcontent_declare(zstd
${FC_DECLARE_COMMON_OPTIONS}
URL ${ZSTD_URL}
URL_HASH "SHA256=${ICEBERG_ZSTD_BUILD_SHA256_CHECKSUM}"
SOURCE_SUBDIR
build/cmake
FIND_PACKAGE_ARGS
NAMES
zstd
CONFIG)
fetchcontent_makeavailable(zstd)

if(zstd_SOURCE_DIR)
set(ZSTD_VENDORED TRUE)
set_target_properties(libzstd_static PROPERTIES OUTPUT_NAME "iceberg_vendored_zstd"
POSITION_INDEPENDENT_CODE ON)
install(TARGETS libzstd_static
EXPORT iceberg_targets
RUNTIME DESTINATION "${ICEBERG_INSTALL_BINDIR}"
ARCHIVE DESTINATION "${ICEBERG_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${ICEBERG_INSTALL_LIBDIR}")
set(ICEBERG_ZSTD_STATIC_TARGET libzstd_static)
set(ICEBERG_ZSTD_SHARED_TARGET libzstd_static)
else()
set(ZSTD_VENDORED FALSE)
list(APPEND ICEBERG_SYSTEM_DEPENDENCIES zstd)
message(STATUS "Found zstd, version: ${zstd_VERSION}")
set(ICEBERG_SYSTEM_DEPENDENCIES
${ICEBERG_SYSTEM_DEPENDENCIES}
PARENT_SCOPE)
# zstd exports libzstd_static and libzstd_shared; vcpkg may only provide the
# unified zstd::libzstd target.
if(TARGET zstd::libzstd_static)
set(ICEBERG_ZSTD_STATIC_TARGET zstd::libzstd_static)
elseif(TARGET zstd::libzstd_shared)
set(ICEBERG_ZSTD_STATIC_TARGET zstd::libzstd_shared)
else()
set(ICEBERG_ZSTD_STATIC_TARGET zstd::libzstd)
endif()
if(TARGET zstd::libzstd_shared)
set(ICEBERG_ZSTD_SHARED_TARGET zstd::libzstd_shared)
elseif(TARGET zstd::libzstd_static)
set(ICEBERG_ZSTD_SHARED_TARGET zstd::libzstd_static)
else()
set(ICEBERG_ZSTD_SHARED_TARGET zstd::libzstd)
endif()
endif()

set(ICEBERG_SYSTEM_DEPENDENCIES
${ICEBERG_SYSTEM_DEPENDENCIES}
PARENT_SCOPE)
set(ZSTD_VENDORED
${ZSTD_VENDORED}
PARENT_SCOPE)
set(ICEBERG_ZSTD_STATIC_TARGET
${ICEBERG_ZSTD_STATIC_TARGET}
PARENT_SCOPE)
set(ICEBERG_ZSTD_SHARED_TARGET
${ICEBERG_ZSTD_SHARED_TARGET}
PARENT_SCOPE)
endfunction()

# ----------------------------------------------------------------------
Expand Down Expand Up @@ -992,9 +1062,10 @@ if(ICEBERG_BUILD_BUNDLE)
resolve_aws_sdk_dependency()
endif()
resolve_avro_dependency()
resolve_zstd_dependency()
endif()

resolve_zstd_dependency()

if(ICEBERG_BUILD_REST)
resolve_cpr_dependency()
endif()
Expand Down
1 change: 1 addition & 0 deletions mkdocs/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ If you experience network issues when downloading dependencies, you can override
| `ICEBERG_CROARING_URL` | CRoaring tarball |
| `ICEBERG_UTF8PROC_URL` | utf8proc tarball |
| `ICEBERG_NLOHMANN_JSON_URL` | nlohmann-json tarball |
| `ICEBERG_ZSTD_URL` | zstd tarball |
| `ICEBERG_CPR_URL` | cpr tarball |

Example:
Expand Down
14 changes: 10 additions & 4 deletions src/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,26 +189,32 @@ list(APPEND
nlohmann_json::nlohmann_json
utf8proc::utf8proc
ZLIB::ZLIB
roaring::roaring)
roaring::roaring
${ICEBERG_ZSTD_STATIC_TARGET})
list(APPEND
ICEBERG_SHARED_BUILD_INTERFACE_LIBS
"$<IF:$<BOOL:${NANOARROW_VENDORED}>,nanoarrow::nanoarrow_static,$<IF:$<TARGET_EXISTS:nanoarrow::nanoarrow_shared>,nanoarrow::nanoarrow_shared,nanoarrow::nanoarrow_static>>"
nlohmann_json::nlohmann_json
utf8proc::utf8proc
ZLIB::ZLIB
roaring::roaring)
roaring::roaring
${ICEBERG_ZSTD_SHARED_TARGET})
list(APPEND
ICEBERG_STATIC_INSTALL_INTERFACE_LIBS
"$<IF:$<BOOL:${NANOARROW_VENDORED}>,iceberg::nanoarrow_static,$<IF:$<TARGET_EXISTS:nanoarrow::nanoarrow_static>,nanoarrow::nanoarrow_static,nanoarrow::nanoarrow_shared>>"
"$<IF:$<BOOL:${NLOHMANN_JSON_VENDORED}>,iceberg::nlohmann_json,$<IF:$<TARGET_EXISTS:nlohmann_json::nlohmann_json>,nlohmann_json::nlohmann_json,nlohmann_json::nlohmann_json>>"
"$<IF:$<BOOL:${UTF8PROC_VENDORED}>,iceberg::utf8proc,utf8proc::utf8proc>"
"$<IF:$<BOOL:${CROARING_VENDORED}>,iceberg::roaring,roaring::roaring>")
"$<IF:$<BOOL:${CROARING_VENDORED}>,iceberg::roaring,roaring::roaring>"
"$<IF:$<BOOL:${ZSTD_VENDORED}>,iceberg::libzstd_static,${ICEBERG_ZSTD_STATIC_TARGET}>"
)
list(APPEND
ICEBERG_SHARED_INSTALL_INTERFACE_LIBS
"$<IF:$<BOOL:${NANOARROW_VENDORED}>,iceberg::nanoarrow_static,$<IF:$<TARGET_EXISTS:nanoarrow::nanoarrow_shared>,nanoarrow::nanoarrow_shared,nanoarrow::nanoarrow_static>>"
"$<IF:$<BOOL:${NLOHMANN_JSON_VENDORED}>,iceberg::nlohmann_json,$<IF:$<TARGET_EXISTS:nlohmann_json::nlohmann_json>,nlohmann_json::nlohmann_json,nlohmann_json::nlohmann_json>>"
"$<IF:$<BOOL:${UTF8PROC_VENDORED}>,iceberg::utf8proc,utf8proc::utf8proc>"
"$<IF:$<BOOL:${CROARING_VENDORED}>,iceberg::roaring,roaring::roaring>")
"$<IF:$<BOOL:${CROARING_VENDORED}>,iceberg::roaring,roaring::roaring>"
"$<IF:$<BOOL:${ZSTD_VENDORED}>,iceberg::libzstd_static,${ICEBERG_ZSTD_SHARED_TARGET}>"
)

if(ICEBERG_SPDLOG)
set(ICEBERG_HAS_SPDLOG ON)
Expand Down
69 changes: 65 additions & 4 deletions src/iceberg/puffin/puffin_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@

#include "iceberg/puffin/puffin_format.h"

#include <zstd.h>

#include <cstddef>
#include <limits>
#include <utility>
#include <vector>

#include "iceberg/util/endian.h"

namespace iceberg::puffin {

namespace {
Expand Down Expand Up @@ -56,8 +61,25 @@ Result<std::vector<std::byte>> Compress(PuffinCompressionCodec codec,
return std::vector<std::byte>(input.begin(), input.end());
case PuffinCompressionCodec::kLz4:
return NotSupported("LZ4 compression is not yet supported");
case PuffinCompressionCodec::kZstd:
return NotSupported("Zstd compression is not yet supported");
case PuffinCompressionCodec::kZstd: {
const size_t input_size = input.size();
if (input_size > std::numeric_limits<int32_t>::max()) {
// Matches Java, which rejects content sizes beyond the int range.
return CompressError("Input size {} is too large to compress", input_size);
}

const size_t max_compressed_size = ZSTD_compressBound(input_size);
std::vector<std::byte> output(max_compressed_size);
// Match Java's default compression level, which is 3.
const size_t compressed_size = ZSTD_compress(
output.data(), output.size(), input.data(), input.size(), ZSTD_CLEVEL_DEFAULT);
if (ZSTD_isError(compressed_size)) {
return CompressError("Failed to compress data: {}",
ZSTD_getErrorName(compressed_size));
}
output.resize(compressed_size);
return output;
}
}
std::unreachable();
}
Expand All @@ -69,8 +91,47 @@ Result<std::vector<std::byte>> Decompress(PuffinCompressionCodec codec,
return std::vector<std::byte>(input.begin(), input.end());
case PuffinCompressionCodec::kLz4:
return NotSupported("LZ4 decompression is not yet supported");
case PuffinCompressionCodec::kZstd:
return NotSupported("Zstd decompression is not yet supported");
case PuffinCompressionCodec::kZstd: {
if (input.size() < sizeof(uint32_t) ||
ReadLittleEndian<uint32_t>(input.data()) != ZSTD_MAGICNUMBER) {
return DecompressError("Compressed data is not a Zstandard frame");
}
const size_t compressed_frame_size =
ZSTD_findFrameCompressedSize(input.data(), input.size());
if (ZSTD_isError(compressed_frame_size)) {
return DecompressError("Invalid Zstandard frame: {}",
ZSTD_getErrorName(compressed_frame_size));
}
if (compressed_frame_size != input.size()) {
return DecompressError("Found {} bytes after the Zstandard frame",
input.size() - compressed_frame_size);
}

const uint64_t frame_size = ZSTD_getFrameContentSize(input.data(), input.size());
Comment thread
proost marked this conversation as resolved.
Resolved
if (frame_size == ZSTD_CONTENTSIZE_ERROR) {
return DecompressError("Content size of the compressed data is invalid");
}
if (frame_size == ZSTD_CONTENTSIZE_UNKNOWN) {
return DecompressError("Content size of the compressed data is unknown");
}
if (frame_size > std::numeric_limits<int32_t>::max()) {
// Matches Java, which rejects content sizes beyond the int range.
return DecompressError("Content size of the compressed data is too large");
}

std::vector<std::byte> output(frame_size);
const size_t decompressed_size =
ZSTD_decompress(output.data(), output.size(), input.data(), input.size());
if (ZSTD_isError(decompressed_size)) {
return DecompressError("Failed to decompress data: {}",
ZSTD_getErrorName(decompressed_size));
}
if (decompressed_size != frame_size) {
return DecompressError("Decompressed size {} does not match the expected size {}",
decompressed_size, frame_size);
}
return output;
}
}
std::unreachable();
}
Expand Down
Loading
Loading