Skip to content

Commit e4c7610

Browse files
author
xuan.zhao
committed
feat(puffin): add basic data structures and constants
Add the foundational types for Puffin file format support: - Blob, BlobMetadata, FileMetadata structs - PuffinCompressionCodec enum with codec name conversion - StandardBlobTypes and StandardPuffinProperties constants - ToString functions for all types - 24 unit tests covering all public APIs
1 parent 8bf089f commit e4c7610

File tree

9 files changed

+397
-0
lines changed

9 files changed

+397
-0
lines changed

src/iceberg/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ set(ICEBERG_SOURCES
6262
partition_field.cc
6363
partition_spec.cc
6464
partition_summary.cc
65+
puffin/file_metadata.cc
66+
puffin/puffin_compression_codec.cc
6567
row/arrow_array_wrapper.cc
6668
row/manifest_wrapper.cc
6769
row/partition_values.cc
@@ -166,6 +168,7 @@ add_subdirectory(catalog)
166168
add_subdirectory(data)
167169
add_subdirectory(expression)
168170
add_subdirectory(manifest)
171+
add_subdirectory(puffin)
169172
add_subdirectory(row)
170173
add_subdirectory(update)
171174
add_subdirectory(util)

src/iceberg/meson.build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ iceberg_sources = files(
8080
'partition_field.cc',
8181
'partition_spec.cc',
8282
'partition_summary.cc',
83+
'puffin/file_metadata.cc',
84+
'puffin/puffin_compression_codec.cc',
8385
'row/arrow_array_wrapper.cc',
8486
'row/manifest_wrapper.cc',
8587
'row/partition_values.cc',
@@ -221,6 +223,7 @@ install_headers(
221223
subdir('catalog')
222224
subdir('expression')
223225
subdir('manifest')
226+
subdir('puffin')
224227
subdir('row')
225228
subdir('update')
226229
subdir('util')

src/iceberg/puffin/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
iceberg_install_all_headers(iceberg/puffin)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include "iceberg/puffin/file_metadata.h"
21+
22+
#include <format>
23+
24+
#include "iceberg/util/formatter_internal.h"
25+
26+
namespace iceberg::puffin {
27+
28+
std::string ToString(const Blob& blob) {
29+
std::string repr = "Blob[";
30+
std::format_to(std::back_inserter(repr), "type='{}',inputFields={},", blob.type,
31+
blob.input_fields);
32+
std::format_to(std::back_inserter(repr), "snapshotId={},sequenceNumber={},",
33+
blob.snapshot_id, blob.sequence_number);
34+
std::format_to(std::back_inserter(repr), "dataSize={}", blob.data.size());
35+
if (blob.requested_compression.has_value()) {
36+
std::format_to(std::back_inserter(repr), ",requestedCompression={}",
37+
iceberg::puffin::ToString(*blob.requested_compression));
38+
}
39+
if (!blob.properties.empty()) {
40+
std::format_to(std::back_inserter(repr), ",properties={}", blob.properties);
41+
}
42+
std::format_to(std::back_inserter(repr), "]");
43+
return repr;
44+
}
45+
46+
std::string ToString(const BlobMetadata& blob_metadata) {
47+
std::string repr = "BlobMetadata[";
48+
std::format_to(std::back_inserter(repr), "type='{}',inputFields={},",
49+
blob_metadata.type, blob_metadata.input_fields);
50+
std::format_to(std::back_inserter(repr), "snapshotId={},sequenceNumber={},",
51+
blob_metadata.snapshot_id, blob_metadata.sequence_number);
52+
std::format_to(std::back_inserter(repr), "offset={},length={}", blob_metadata.offset,
53+
blob_metadata.length);
54+
if (!blob_metadata.compression_codec.empty()) {
55+
std::format_to(std::back_inserter(repr), ",compressionCodec='{}'",
56+
blob_metadata.compression_codec);
57+
}
58+
if (!blob_metadata.properties.empty()) {
59+
std::format_to(std::back_inserter(repr), ",properties={}", blob_metadata.properties);
60+
}
61+
std::format_to(std::back_inserter(repr), "]");
62+
return repr;
63+
}
64+
65+
std::string ToString(const FileMetadata& file_metadata) {
66+
std::string repr = "FileMetadata[";
67+
std::format_to(std::back_inserter(repr), "blobs=[");
68+
for (size_t i = 0; i < file_metadata.blobs.size(); ++i) {
69+
if (i > 0) {
70+
std::format_to(std::back_inserter(repr), ",");
71+
}
72+
std::format_to(std::back_inserter(repr), "{}", ToString(file_metadata.blobs[i]));
73+
}
74+
std::format_to(std::back_inserter(repr), "]");
75+
if (!file_metadata.properties.empty()) {
76+
std::format_to(std::back_inserter(repr), ",properties={}", file_metadata.properties);
77+
}
78+
std::format_to(std::back_inserter(repr), "]");
79+
return repr;
80+
}
81+
82+
} // namespace iceberg::puffin

src/iceberg/puffin/file_metadata.h

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#pragma once
21+
22+
/// \file iceberg/puffin/file_metadata.h
23+
/// Data structures for Puffin files: Blob, BlobMetadata, and FileMetadata.
24+
25+
#include <cstdint>
26+
#include <optional>
27+
#include <string>
28+
#include <unordered_map>
29+
#include <vector>
30+
31+
#include "iceberg/iceberg_export.h"
32+
#include "iceberg/puffin/puffin_compression_codec.h"
33+
34+
namespace iceberg::puffin {
35+
36+
/// \brief A blob in a Puffin file.
37+
struct ICEBERG_EXPORT Blob {
38+
/// \brief Type of the blob. See StandardBlobTypes for known types.
39+
std::string type;
40+
/// \brief List of field IDs the blob was computed for.
41+
std::vector<int32_t> input_fields;
42+
/// \brief ID of the Iceberg table's snapshot the blob was computed from.
43+
int64_t snapshot_id;
44+
/// \brief Sequence number of the Iceberg table's snapshot the blob was computed from.
45+
int64_t sequence_number;
46+
/// \brief The uncompressed blob data.
47+
std::vector<uint8_t> data;
48+
/// \brief Requested compression codec. If not set, the writer's default will be used.
49+
std::optional<PuffinCompressionCodec> requested_compression;
50+
/// \brief Additional properties of the blob.
51+
std::unordered_map<std::string, std::string> properties;
52+
53+
/// \brief Compare two Blobs for equality.
54+
friend bool operator==(const Blob& lhs, const Blob& rhs) = default;
55+
};
56+
57+
/// \brief Returns a string representation of a Blob.
58+
ICEBERG_EXPORT std::string ToString(const Blob& blob);
59+
60+
/// \brief Metadata about a blob stored in a Puffin file.
61+
struct ICEBERG_EXPORT BlobMetadata {
62+
/// \brief Type of the blob. See StandardBlobTypes for known types.
63+
std::string type;
64+
/// \brief List of field IDs the blob was computed for.
65+
std::vector<int32_t> input_fields;
66+
/// \brief ID of the Iceberg table's snapshot the blob was computed from.
67+
int64_t snapshot_id;
68+
/// \brief Sequence number of the Iceberg table's snapshot the blob was computed from.
69+
int64_t sequence_number;
70+
/// \brief Offset in the file where the blob data starts.
71+
int64_t offset;
72+
/// \brief Length of the blob data in the file.
73+
int64_t length;
74+
/// \brief Compression codec name (e.g. "lz4", "zstd"), or empty if not compressed.
75+
std::string compression_codec;
76+
/// \brief Additional properties of the blob.
77+
std::unordered_map<std::string, std::string> properties;
78+
79+
/// \brief Compare two BlobMetadatas for equality.
80+
friend bool operator==(const BlobMetadata& lhs, const BlobMetadata& rhs) = default;
81+
};
82+
83+
/// \brief Returns a string representation of a BlobMetadata.
84+
ICEBERG_EXPORT std::string ToString(const BlobMetadata& blob_metadata);
85+
86+
/// \brief Metadata about a Puffin file.
87+
struct ICEBERG_EXPORT FileMetadata {
88+
/// \brief List of blob metadata for all blobs in the file.
89+
std::vector<BlobMetadata> blobs;
90+
/// \brief File-level properties.
91+
std::unordered_map<std::string, std::string> properties;
92+
93+
/// \brief Compare two FileMetadatas for equality.
94+
friend bool operator==(const FileMetadata& lhs, const FileMetadata& rhs) = default;
95+
};
96+
97+
/// \brief Returns a string representation of a FileMetadata.
98+
ICEBERG_EXPORT std::string ToString(const FileMetadata& file_metadata);
99+
100+
} // namespace iceberg::puffin

src/iceberg/puffin/meson.build

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
install_headers(
19+
['file_metadata.h', 'puffin_compression_codec.h', 'types.h'],
20+
subdir: 'iceberg/puffin',
21+
)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include "iceberg/puffin/puffin_compression_codec.h"
21+
22+
#include <format>
23+
#include <utility>
24+
25+
namespace iceberg::puffin {
26+
27+
namespace {
28+
constexpr std::string_view kLz4CodecName = "lz4";
29+
constexpr std::string_view kZstdCodecName = "zstd";
30+
} // namespace
31+
32+
std::string_view CodecName(PuffinCompressionCodec codec) {
33+
switch (codec) {
34+
case PuffinCompressionCodec::kNone:
35+
return "";
36+
case PuffinCompressionCodec::kLz4:
37+
return kLz4CodecName;
38+
case PuffinCompressionCodec::kZstd:
39+
return kZstdCodecName;
40+
}
41+
std::unreachable();
42+
}
43+
44+
Result<PuffinCompressionCodec> PuffinCompressionCodecFromName(
45+
std::string_view codec_name) {
46+
if (codec_name.empty()) {
47+
return PuffinCompressionCodec::kNone;
48+
}
49+
if (codec_name == kLz4CodecName) {
50+
return PuffinCompressionCodec::kLz4;
51+
}
52+
if (codec_name == kZstdCodecName) {
53+
return PuffinCompressionCodec::kZstd;
54+
}
55+
return InvalidArgument("Unknown codec name: {}", codec_name);
56+
}
57+
58+
std::string ToString(PuffinCompressionCodec codec) {
59+
auto name = CodecName(codec);
60+
if (!name.empty()) {
61+
return std::string(name);
62+
}
63+
return "none";
64+
}
65+
66+
} // namespace iceberg::puffin
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#pragma once
21+
22+
/// \file iceberg/puffin/puffin_compression_codec.h
23+
/// Compression codec definitions for Puffin files.
24+
25+
#include <string>
26+
#include <string_view>
27+
28+
#include "iceberg/iceberg_export.h"
29+
#include "iceberg/result.h"
30+
31+
namespace iceberg::puffin {
32+
33+
/// \brief Compression codecs supported by Puffin files.
34+
enum class PuffinCompressionCodec {
35+
/// No compression
36+
kNone,
37+
/// LZ4 single compression frame with content size present
38+
kLz4,
39+
/// Zstandard single compression frame with content size present
40+
kZstd,
41+
};
42+
43+
/// \brief Get the codec name for a compression codec.
44+
///
45+
/// Returns an empty string for kNone.
46+
ICEBERG_EXPORT std::string_view CodecName(PuffinCompressionCodec codec);
47+
48+
/// \brief Get the compression codec from a codec name.
49+
///
50+
/// An empty string is interpreted as kNone.
51+
ICEBERG_EXPORT Result<PuffinCompressionCodec> PuffinCompressionCodecFromName(
52+
std::string_view codec_name);
53+
54+
/// \brief Returns a string representation of a PuffinCompressionCodec.
55+
ICEBERG_EXPORT std::string ToString(PuffinCompressionCodec codec);
56+
57+
} // namespace iceberg::puffin

0 commit comments

Comments
 (0)