Skip to content

Commit 2b0fd8f

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 2b0fd8f

File tree

6 files changed

+300
-0
lines changed

6 files changed

+300
-0
lines changed

src/iceberg/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ set(ICEBERG_SOURCES
6262
partition_field.cc
6363
partition_spec.cc
6464
partition_summary.cc
65+
puffin/file_metadata.cc
6566
row/arrow_array_wrapper.cc
6667
row/manifest_wrapper.cc
6768
row/partition_values.cc
@@ -166,6 +167,7 @@ add_subdirectory(catalog)
166167
add_subdirectory(data)
167168
add_subdirectory(expression)
168169
add_subdirectory(manifest)
170+
add_subdirectory(puffin)
169171
add_subdirectory(row)
170172
add_subdirectory(update)
171173
add_subdirectory(util)

src/iceberg/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ iceberg_sources = files(
8080
'partition_field.cc',
8181
'partition_spec.cc',
8282
'partition_summary.cc',
83+
'puffin/file_metadata.cc',
8384
'row/arrow_array_wrapper.cc',
8485
'row/manifest_wrapper.cc',
8586
'row/partition_values.cc',
@@ -221,6 +222,7 @@ install_headers(
221222
subdir('catalog')
222223
subdir('expression')
223224
subdir('manifest')
225+
subdir('puffin')
224226
subdir('row')
225227
subdir('update')
226228
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: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
#include <utility>
24+
25+
#include "iceberg/util/formatter_internal.h"
26+
27+
namespace iceberg::puffin {
28+
29+
namespace {
30+
constexpr std::string_view kLz4CodecName = "lz4";
31+
constexpr std::string_view kZstdCodecName = "zstd";
32+
} // namespace
33+
34+
std::string_view CodecName(PuffinCompressionCodec codec) {
35+
switch (codec) {
36+
case PuffinCompressionCodec::kNone:
37+
return "";
38+
case PuffinCompressionCodec::kLz4:
39+
return kLz4CodecName;
40+
case PuffinCompressionCodec::kZstd:
41+
return kZstdCodecName;
42+
}
43+
std::unreachable();
44+
}
45+
46+
Result<PuffinCompressionCodec> PuffinCompressionCodecFromName(
47+
std::string_view codec_name) {
48+
if (codec_name.empty()) {
49+
return PuffinCompressionCodec::kNone;
50+
}
51+
if (codec_name == kLz4CodecName) {
52+
return PuffinCompressionCodec::kLz4;
53+
}
54+
if (codec_name == kZstdCodecName) {
55+
return PuffinCompressionCodec::kZstd;
56+
}
57+
return InvalidArgument("Unknown codec name: {}", codec_name);
58+
}
59+
60+
std::string ToString(PuffinCompressionCodec codec) {
61+
return std::string(CodecName(codec));
62+
}
63+
64+
std::string ToString(const Blob& blob) {
65+
std::string repr = "Blob[";
66+
std::format_to(std::back_inserter(repr), "type='{}',inputFields={},", blob.type,
67+
blob.input_fields);
68+
std::format_to(std::back_inserter(repr), "snapshotId={},sequenceNumber={},",
69+
blob.snapshot_id, blob.sequence_number);
70+
std::format_to(std::back_inserter(repr), "dataSize={}", blob.data.size());
71+
if (blob.requested_compression.has_value()) {
72+
std::format_to(std::back_inserter(repr), ",requestedCompression={}",
73+
ToString(*blob.requested_compression));
74+
}
75+
if (!blob.properties.empty()) {
76+
std::format_to(std::back_inserter(repr), ",properties={}", blob.properties);
77+
}
78+
std::format_to(std::back_inserter(repr), "]");
79+
return repr;
80+
}
81+
82+
std::string ToString(const BlobMetadata& blob_metadata) {
83+
std::string repr = "BlobMetadata[";
84+
std::format_to(std::back_inserter(repr), "type='{}',inputFields={},",
85+
blob_metadata.type, blob_metadata.input_fields);
86+
std::format_to(std::back_inserter(repr), "snapshotId={},sequenceNumber={},",
87+
blob_metadata.snapshot_id, blob_metadata.sequence_number);
88+
std::format_to(std::back_inserter(repr), "offset={},length={}", blob_metadata.offset,
89+
blob_metadata.length);
90+
if (!blob_metadata.compression_codec.empty()) {
91+
std::format_to(std::back_inserter(repr), ",compressionCodec='{}'",
92+
blob_metadata.compression_codec);
93+
}
94+
if (!blob_metadata.properties.empty()) {
95+
std::format_to(std::back_inserter(repr), ",properties={}", blob_metadata.properties);
96+
}
97+
std::format_to(std::back_inserter(repr), "]");
98+
return repr;
99+
}
100+
101+
std::string ToString(const FileMetadata& file_metadata) {
102+
std::string repr = "FileMetadata[";
103+
std::format_to(std::back_inserter(repr), "blobs=[");
104+
for (size_t i = 0; i < file_metadata.blobs.size(); ++i) {
105+
if (i > 0) {
106+
std::format_to(std::back_inserter(repr), ",");
107+
}
108+
std::format_to(std::back_inserter(repr), "{}", ToString(file_metadata.blobs[i]));
109+
}
110+
std::format_to(std::back_inserter(repr), "]");
111+
if (!file_metadata.properties.empty()) {
112+
std::format_to(std::back_inserter(repr), ",properties={}", file_metadata.properties);
113+
}
114+
std::format_to(std::back_inserter(repr), "]");
115+
return repr;
116+
}
117+
118+
} // namespace iceberg::puffin

src/iceberg/puffin/file_metadata.h

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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.
24+
25+
#include <cstdint>
26+
#include <optional>
27+
#include <string>
28+
#include <string_view>
29+
#include <unordered_map>
30+
#include <vector>
31+
32+
#include "iceberg/iceberg_export.h"
33+
#include "iceberg/result.h"
34+
35+
namespace iceberg::puffin {
36+
37+
/// \brief Compression codecs supported by Puffin files.
38+
enum class PuffinCompressionCodec {
39+
/// \brief No compression
40+
kNone,
41+
/// \brief LZ4 single compression frame with content size present
42+
kLz4,
43+
/// \brief Zstandard single compression frame with content size present
44+
kZstd,
45+
};
46+
47+
/// \brief Get the codec name for a compression codec.
48+
ICEBERG_EXPORT std::string_view CodecName(PuffinCompressionCodec codec);
49+
50+
/// \brief Get the compression codec from a codec name.
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+
/// \brief Standard blob types defined by the Iceberg specification.
58+
struct StandardBlobTypes {
59+
/// \brief A serialized form of a "compact" Theta sketch produced by the
60+
/// Apache DataSketches library.
61+
static constexpr std::string_view kApacheDatasketchesThetaV1 =
62+
"apache-datasketches-theta-v1";
63+
64+
/// \brief A serialized deletion vector according to the Iceberg spec.
65+
static constexpr std::string_view kDeletionVectorV1 = "deletion-vector-v1";
66+
};
67+
68+
/// \brief Standard file-level properties for Puffin files.
69+
struct StandardPuffinProperties {
70+
/// \brief Human-readable identification of the application writing the file,
71+
/// along with its version.
72+
static constexpr std::string_view kCreatedBy = "created-by";
73+
};
74+
75+
/// \brief A blob in a Puffin file.
76+
struct ICEBERG_EXPORT Blob {
77+
/// \brief Type of the blob. See StandardBlobTypes for known types.
78+
std::string type;
79+
/// \brief List of field IDs the blob was computed for.
80+
std::vector<int32_t> input_fields;
81+
/// \brief ID of the Iceberg table's snapshot the blob was computed from.
82+
int64_t snapshot_id;
83+
/// \brief Sequence number of the Iceberg table's snapshot the blob was computed from.
84+
int64_t sequence_number;
85+
/// \brief The uncompressed blob data.
86+
std::vector<uint8_t> data;
87+
/// \brief Requested compression codec. If not set, the writer's default will be used.
88+
std::optional<PuffinCompressionCodec> requested_compression;
89+
/// \brief Additional properties of the blob.
90+
std::unordered_map<std::string, std::string> properties;
91+
92+
/// \brief Compare two Blobs for equality.
93+
friend bool operator==(const Blob& lhs, const Blob& rhs) = default;
94+
};
95+
96+
/// \brief Returns a string representation of a Blob.
97+
ICEBERG_EXPORT std::string ToString(const Blob& blob);
98+
99+
/// \brief Metadata about a blob stored in a Puffin file.
100+
struct ICEBERG_EXPORT BlobMetadata {
101+
/// \brief Type of the blob. See StandardBlobTypes for known types.
102+
std::string type;
103+
/// \brief List of field IDs the blob was computed for.
104+
std::vector<int32_t> input_fields;
105+
/// \brief ID of the Iceberg table's snapshot the blob was computed from.
106+
int64_t snapshot_id;
107+
/// \brief Sequence number of the Iceberg table's snapshot the blob was computed from.
108+
int64_t sequence_number;
109+
/// \brief Offset in the file where the blob data starts.
110+
int64_t offset;
111+
/// \brief Length of the blob data in the file.
112+
int64_t length;
113+
/// \brief Compression codec name (e.g. "lz4", "zstd"), or empty if not compressed.
114+
std::string compression_codec;
115+
/// \brief Additional properties of the blob.
116+
std::unordered_map<std::string, std::string> properties;
117+
118+
/// \brief Compare two BlobMetadatas for equality.
119+
friend bool operator==(const BlobMetadata& lhs, const BlobMetadata& rhs) = default;
120+
};
121+
122+
/// \brief Returns a string representation of a BlobMetadata.
123+
ICEBERG_EXPORT std::string ToString(const BlobMetadata& blob_metadata);
124+
125+
/// \brief Metadata about a Puffin file.
126+
struct ICEBERG_EXPORT FileMetadata {
127+
/// \brief List of blob metadata for all blobs in the file.
128+
std::vector<BlobMetadata> blobs;
129+
/// \brief File-level properties.
130+
std::unordered_map<std::string, std::string> properties;
131+
132+
/// \brief Compare two FileMetadatas for equality.
133+
friend bool operator==(const FileMetadata& lhs, const FileMetadata& rhs) = default;
134+
};
135+
136+
/// \brief Returns a string representation of a FileMetadata.
137+
ICEBERG_EXPORT std::string ToString(const FileMetadata& file_metadata);
138+
139+
} // 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'],
20+
subdir: 'iceberg/puffin',
21+
)

0 commit comments

Comments
 (0)