@@ -623,6 +623,9 @@ class TableMetadataBuilder::Impl {
623623 Status RemovePartitionSpecs (const std::vector<int32_t >& spec_ids);
624624 Status SetStatistics (std::shared_ptr<StatisticsFile> statistics_file);
625625 Status RemoveStatistics (int64_t snapshot_id);
626+ Status SetPartitionStatistics (
627+ std::shared_ptr<PartitionStatisticsFile> partition_statistics_file);
628+ Status RemovePartitionStatistics (int64_t snapshot_id);
626629
627630 Result<std::unique_ptr<TableMetadata>> Build ();
628631
@@ -1208,6 +1211,41 @@ Status TableMetadataBuilder::Impl::RemoveStatistics(int64_t snapshot_id) {
12081211 return {};
12091212}
12101213
1214+ Status TableMetadataBuilder::Impl::SetPartitionStatistics (
1215+ std::shared_ptr<PartitionStatisticsFile> partition_statistics_file) {
1216+ ICEBERG_PRECHECK (partition_statistics_file != nullptr ,
1217+ " Cannot set null partition statistics file" );
1218+
1219+ // Find and replace existing partition statistics for the same snapshot_id, or add new
1220+ // one
1221+ auto it = std::ranges::find_if (
1222+ metadata_.partition_statistics ,
1223+ [snapshot_id = partition_statistics_file->snapshot_id ](const auto & stat) {
1224+ return stat && stat->snapshot_id == snapshot_id;
1225+ });
1226+
1227+ if (it != metadata_.partition_statistics .end ()) {
1228+ *it = partition_statistics_file;
1229+ } else {
1230+ metadata_.partition_statistics .push_back (partition_statistics_file);
1231+ }
1232+
1233+ changes_.push_back (std::make_unique<table::SetPartitionStatistics>(
1234+ std::move (partition_statistics_file)));
1235+ return {};
1236+ }
1237+
1238+ Status TableMetadataBuilder::Impl::RemovePartitionStatistics (int64_t snapshot_id) {
1239+ auto removed_count =
1240+ std::erase_if (metadata_.partition_statistics , [snapshot_id](const auto & stat) {
1241+ return stat && stat->snapshot_id == snapshot_id;
1242+ });
1243+ if (removed_count != 0 ) {
1244+ changes_.push_back (std::make_unique<table::RemovePartitionStatistics>(snapshot_id));
1245+ }
1246+ return {};
1247+ }
1248+
12111249std::unordered_set<int64_t > TableMetadataBuilder::Impl::IntermediateSnapshotIdSet (
12121250 int64_t current_snapshot_id) const {
12131251 std::unordered_set<int64_t > added_snapshot_ids;
@@ -1636,12 +1674,15 @@ TableMetadataBuilder& TableMetadataBuilder::RemoveStatistics(int64_t snapshot_id
16361674
16371675TableMetadataBuilder& TableMetadataBuilder::SetPartitionStatistics (
16381676 const std::shared_ptr<PartitionStatisticsFile>& partition_statistics_file) {
1639- throw IcebergError (std::format (" {} not implemented" , __FUNCTION__));
1677+ ICEBERG_BUILDER_RETURN_IF_ERROR (
1678+ impl_->SetPartitionStatistics (partition_statistics_file));
1679+ return *this ;
16401680}
16411681
16421682TableMetadataBuilder& TableMetadataBuilder::RemovePartitionStatistics (
16431683 int64_t snapshot_id) {
1644- throw IcebergError (std::format (" {} not implemented" , __FUNCTION__));
1684+ ICEBERG_BUILDER_RETURN_IF_ERROR (impl_->RemovePartitionStatistics (snapshot_id));
1685+ return *this ;
16451686}
16461687
16471688TableMetadataBuilder& TableMetadataBuilder::SetProperties (
0 commit comments