Skip to content

Commit 9cca92c

Browse files
committed
feat: adopt partition summary
1 parent 7f7f85b commit 9cca92c

19 files changed

Lines changed: 239 additions & 51 deletions

src/iceberg/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ set(ICEBERG_SOURCES
3939
name_mapping.cc
4040
partition_field.cc
4141
partition_spec.cc
42+
partition_summary_internal.cc
4243
row/arrow_array_wrapper.cc
4344
row/manifest_wrapper.cc
4445
schema.cc

src/iceberg/expression/literal.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,17 @@ bool Literal::IsAboveMax() const { return std::holds_alternative<AboveMax>(value
504504

505505
bool Literal::IsNull() const { return std::holds_alternative<std::monostate>(value_); }
506506

507+
bool Literal::IsNan() const {
508+
if (type_->type_id() == TypeId::kFloat) {
509+
auto val = std::get<float>(value_);
510+
return std::isnan(val);
511+
} else if (type_->type_id() == TypeId::kDouble) {
512+
auto val = std::get<double>(value_);
513+
return std::isnan(val);
514+
}
515+
return false;
516+
}
517+
507518
// LiteralCaster implementation
508519

509520
Result<Literal> LiteralCaster::CastTo(const Literal& literal,

src/iceberg/expression/literal.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,14 @@ class ICEBERG_EXPORT Literal : public util::Formattable {
150150
/// \return true if this literal represents a BelowMin value, false otherwise
151151
bool IsBelowMin() const;
152152

153-
/// Check if this literal is null.
153+
/// \brief Check if this literal is null.
154154
/// \return true if this literal is null, false otherwise
155155
bool IsNull() const;
156156

157+
/// \brief Check if this literal is NaN (Not a Number).
158+
/// \return true if this literal is NaN, false otherwise
159+
bool IsNan() const;
160+
157161
std::string ToString() const override;
158162

159163
private:

src/iceberg/manifest_adapter.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "iceberg/arrow/nanoarrow_status_internal.h"
2727
#include "iceberg/manifest_entry.h"
2828
#include "iceberg/manifest_list.h"
29+
#include "iceberg/partition_summary_internal.h"
2930
#include "iceberg/result.h"
3031
#include "iceberg/schema.h"
3132
#include "iceberg/schema_internal.h"
@@ -248,6 +249,7 @@ Status ManifestEntryAdapter::AppendDataFile(
248249
ArrowArray* array, const std::shared_ptr<StructType>& data_file_type,
249250
const DataFile& file) {
250251
auto fields = data_file_type->fields();
252+
ICEBERG_RETURN_UNEXPECTED(partition_summary_->Update(file.partition));
251253
for (size_t i = 0; i < fields.size(); i++) {
252254
const auto& field = fields[i];
253255
auto child_array = array->children[i];

src/iceberg/manifest_adapter.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <memory>
2626
#include <optional>
2727
#include <unordered_map>
28-
#include <unordered_set>
2928
#include <vector>
3029

3130
#include "iceberg/arrow_c_data.h"
@@ -95,6 +94,14 @@ class ICEBERG_EXPORT ManifestEntryAdapter : public ManifestAdapter {
9594
std::shared_ptr<Schema> current_schema_;
9695
std::shared_ptr<Schema> manifest_schema_;
9796
const ManifestContent content_;
97+
int32_t add_files_count_{0};
98+
int32_t existing_files_count_{0};
99+
int32_t delete_files_count_{0};
100+
int64_t add_rows_count_{0L};
101+
int64_t existing_rows_count_{0L};
102+
int64_t delete_rows_count_{0L};
103+
std::optional<int64_t> min_data_sequence_number_{std::nullopt};
104+
std::unique_ptr<PartitionSummary> partition_summary_;
98105
};
99106

100107
/// \brief Adapter for appending a list of `ManifestFile`s to an `ArrowArray`.

src/iceberg/manifest_entry.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,6 @@ ICEBERG_EXPORT constexpr Result<ManifestStatus> ManifestStatusFromInt(
5757
}
5858
}
5959

60-
enum class ManifestContent {
61-
kData = 0,
62-
kDeletes = 1,
63-
};
64-
65-
ICEBERG_EXPORT constexpr std::string_view ToString(ManifestContent content) noexcept;
66-
ICEBERG_EXPORT constexpr Result<ManifestContent> ManifestContentFromString(
67-
std::string_view str) noexcept;
68-
6960
/// \brief DataFile carries data file path, partition tuple, metrics, ...
7061
struct ICEBERG_EXPORT DataFile {
7162
/// \brief Content of a data file
@@ -315,6 +306,11 @@ struct ICEBERG_EXPORT ManifestEntry {
315306
inline static const SchemaField kFileSequenceNumber =
316307
SchemaField::MakeOptional(4, "file_sequence_number", iceberg::int64());
317308

309+
/// \brief Check if this manifest entry is deleted.
310+
constexpr bool IsAlive() const {
311+
return status == ManifestStatus::kAdded || status == ManifestStatus::kExisting;
312+
}
313+
318314
bool operator==(const ManifestEntry& other) const;
319315

320316
static std::shared_ptr<StructType> TypeFromPartitionType(

src/iceberg/manifest_list.h

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <optional>
2626
#include <string>
2727
#include <string_view>
28-
#include <utility>
2928

3029
#include "iceberg/iceberg_export.h"
3130
#include "iceberg/partition_spec.h"
@@ -72,17 +71,17 @@ struct ICEBERG_EXPORT PartitionFieldSummary {
7271
static const StructType& Type();
7372
};
7473

74+
/// \brief The type of files tracked by the manifest, either data or delete files; 0 for
75+
/// all v1 manifests
76+
enum class ManifestContent {
77+
/// The manifest content is data.
78+
kData = 0,
79+
/// The manifest content is deletes.
80+
kDeletes = 1,
81+
};
82+
7583
/// \brief Entry in a manifest list.
7684
struct ICEBERG_EXPORT ManifestFile {
77-
/// \brief The type of files tracked by the manifest, either data or delete files; 0 for
78-
/// all v1 manifests
79-
enum class Content {
80-
/// The manifest content is data.
81-
kData = 0,
82-
/// The manifest content is deletes.
83-
kDeletes = 1,
84-
};
85-
8685
/// Field id: 500
8786
/// Location of the manifest file
8887
std::string manifest_path;
@@ -96,7 +95,7 @@ struct ICEBERG_EXPORT ManifestFile {
9695
/// Field id: 517
9796
/// The type of files tracked by the manifest, either data or delete files; 0 for all v1
9897
/// manifests
99-
Content content = Content::kData;
98+
ManifestContent content = ManifestContent::kData;
10099
/// Field id: 515
101100
/// The sequence number when the manifest was added to the table; use 0 when reading v1
102101
/// manifest lists
@@ -218,21 +217,20 @@ struct ICEBERG_EXPORT ManifestList {
218217
};
219218

220219
/// \brief Get the relative manifest content type name
221-
ICEBERG_EXPORT constexpr std::string_view ToString(ManifestFile::Content type) noexcept {
220+
ICEBERG_EXPORT constexpr std::string_view ToString(ManifestContent type) noexcept {
222221
switch (type) {
223-
case ManifestFile::Content::kData:
222+
case ManifestContent::kData:
224223
return "data";
225-
case ManifestFile::Content::kDeletes:
224+
case ManifestContent::kDeletes:
226225
return "deletes";
227226
}
228-
std::unreachable();
229227
}
230228

231229
/// \brief Get the relative manifest content type from name
232-
ICEBERG_EXPORT constexpr Result<ManifestFile::Content> ManifestFileContentFromString(
230+
ICEBERG_EXPORT constexpr Result<ManifestContent> ManifestContentFromString(
233231
std::string_view str) noexcept {
234-
if (str == "data") return ManifestFile::Content::kData;
235-
if (str == "deletes") return ManifestFile::Content::kDeletes;
232+
if (str == "data") return ManifestContent::kData;
233+
if (str == "deletes") return ManifestContent::kDeletes;
236234
return InvalidArgument("Invalid manifest content type: {}", str);
237235
}
238236

src/iceberg/manifest_reader_internal.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ Result<std::vector<ManifestFile>> ParseManifestList(ArrowSchema* schema,
237237
break;
238238
case ManifestFileField::kContent:
239239
PARSE_PRIMITIVE_FIELD(manifest_files[row_idx].content, view_of_column,
240-
ManifestFile::Content);
240+
ManifestContent);
241241
break;
242242
case ManifestFileField::kSequenceNumber:
243243
PARSE_PRIMITIVE_FIELD(manifest_files[row_idx].sequence_number, view_of_column,

src/iceberg/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ iceberg_sources = files(
6161
'name_mapping.cc',
6262
'partition_field.cc',
6363
'partition_spec.cc',
64+
'partition_summary_internal.cc',
6465
'row/arrow_array_wrapper.cc',
6566
'row/manifest_wrapper.cc',
6667
'schema.cc',
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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/partition_summary_internal.h"
21+
22+
#include <memory>
23+
24+
#include "iceberg/expression/literal.h"
25+
#include "iceberg/manifest_list.h"
26+
#include "iceberg/result.h"
27+
28+
namespace iceberg {
29+
30+
Status PartitionFieldStats::Update(const Literal& value) {
31+
if (type_->type_id() != value.type()->type_id()) {
32+
return InvalidArgument("value is not compatible with type");
33+
}
34+
35+
if (value.IsNull()) {
36+
contains_null_ = true;
37+
return {};
38+
}
39+
40+
if (value.IsNan()) {
41+
contains_nan_ = true;
42+
return {};
43+
}
44+
45+
if (!lower_bound_ || value < *lower_bound_) {
46+
lower_bound_ = value;
47+
}
48+
if (!upper_bound_ || value > *upper_bound_) {
49+
upper_bound_ = value;
50+
}
51+
return {};
52+
}
53+
54+
PartitionFieldSummary PartitionFieldStats::Finish() const {
55+
PartitionFieldSummary summary;
56+
summary.contains_null = contains_null_;
57+
summary.contains_nan = contains_nan_;
58+
if (lower_bound_) {
59+
summary.lower_bound = lower_bound_->Serialize().value();
60+
}
61+
if (upper_bound_) {
62+
summary.upper_bound = upper_bound_->Serialize().value();
63+
}
64+
return summary;
65+
}
66+
67+
Status PartitionSummary::Update(const std::vector<Literal>& partition) {
68+
if (partition.size() != field_stats_.size()) {
69+
return InvalidArgument("partition size does not match field stats size");
70+
}
71+
72+
for (size_t i = 0; i < partition.size(); i++) {
73+
ICEBERG_RETURN_UNEXPECTED(field_stats_[i].Update(partition[i]));
74+
}
75+
return {};
76+
}
77+
78+
std::vector<PartitionFieldSummary> PartitionSummary::Summaries() const {
79+
std::vector<PartitionFieldSummary> summaries;
80+
for (const auto& field_stat : field_stats_) {
81+
summaries.push_back(field_stat.Finish());
82+
}
83+
return summaries;
84+
}
85+
86+
Result<std::unique_ptr<PartitionSummary>> PartitionSummary::Make(
87+
const StructType& partition_type) {
88+
std::vector<PartitionFieldStats> field_stats;
89+
for (const auto& field : partition_type.fields()) {
90+
field_stats.emplace_back(field.type());
91+
}
92+
return std::unique_ptr<PartitionSummary>(new PartitionSummary(std::move(field_stats)));
93+
}
94+
95+
} // namespace iceberg

0 commit comments

Comments
 (0)