@@ -612,14 +612,16 @@ class TableMetadataBuilder::Impl {
612612 Status SetCurrentSchema (int32_t schema_id);
613613 Status RemoveSchemas (const std::unordered_set<int32_t >& schema_ids);
614614 Result<int32_t > AddSchema (const Schema& schema, int32_t new_last_column_id);
615- void SetLocation (std::string_view location);
615+ Status SetLocation (std::string_view location);
616616 Status AddSnapshot (std::shared_ptr<Snapshot> snapshot);
617617 Status SetBranchSnapshot (int64_t snapshot_id, const std::string& branch);
618618 Status SetBranchSnapshot (std::shared_ptr<Snapshot> snapshot, const std::string& branch);
619619 Status SetRef (const std::string& name, std::shared_ptr<SnapshotRef> ref);
620620 Status RemoveRef (const std::string& name);
621621 Status RemoveSnapshots (const std::vector<int64_t >& snapshot_ids);
622622 Status RemovePartitionSpecs (const std::vector<int32_t >& spec_ids);
623+ Status SetStatistics (const std::shared_ptr<StatisticsFile>& statistics_file);
624+ Status RemoveStatistics (int64_t snapshot_id);
623625
624626 Result<std::unique_ptr<TableMetadata>> Build ();
625627
@@ -1032,12 +1034,13 @@ Result<int32_t> TableMetadataBuilder::Impl::AddSchema(const Schema& schema,
10321034 return new_schema_id;
10331035}
10341036
1035- void TableMetadataBuilder::Impl::SetLocation (std::string_view location) {
1037+ Status TableMetadataBuilder::Impl::SetLocation (std::string_view location) {
10361038 if (location == metadata_.location ) {
1037- return ;
1039+ return {} ;
10381040 }
10391041 metadata_.location = std::string (location);
10401042 changes_.push_back (std::make_unique<table::SetLocation>(std::string (location)));
1043+ return {};
10411044}
10421045
10431046Status TableMetadataBuilder::Impl::AddSnapshot (std::shared_ptr<Snapshot> snapshot) {
@@ -1173,6 +1176,45 @@ Status TableMetadataBuilder::Impl::SetRef(const std::string& name,
11731176 return {};
11741177}
11751178
1179+ Status TableMetadataBuilder::Impl::SetStatistics (
1180+ const std::shared_ptr<StatisticsFile>& statistics_file) {
1181+ ICEBERG_CHECK (statistics_file != nullptr , " Cannot set null statistics file" );
1182+
1183+ // Find and replace existing statistics for the same snapshot_id, or add new one
1184+ auto it = std::ranges::find_if (
1185+ metadata_.statistics ,
1186+ [snapshot_id = statistics_file->snapshot_id ](const auto & stat) {
1187+ return stat && stat->snapshot_id == snapshot_id;
1188+ });
1189+
1190+ if (it != metadata_.statistics .end ()) {
1191+ *it = statistics_file;
1192+ } else {
1193+ metadata_.statistics .push_back (statistics_file);
1194+ }
1195+
1196+ changes_.push_back (std::make_unique<table::SetStatistics>(statistics_file));
1197+ return {};
1198+ }
1199+
1200+ Status TableMetadataBuilder::Impl::RemoveStatistics (int64_t snapshot_id) {
1201+ auto it = std::ranges::find_if (metadata_.statistics , [snapshot_id](const auto & stat) {
1202+ return stat && stat->snapshot_id == snapshot_id;
1203+ });
1204+
1205+ if (it == metadata_.statistics .end ()) {
1206+ return {};
1207+ }
1208+
1209+ // Remove statistics for the given snapshot_id
1210+ std::erase_if (metadata_.statistics , [snapshot_id](const auto & stat) {
1211+ return stat && stat->snapshot_id == snapshot_id;
1212+ });
1213+
1214+ changes_.push_back (std::make_unique<table::RemoveStatistics>(snapshot_id));
1215+ return {};
1216+ }
1217+
11761218std::unordered_set<int64_t > TableMetadataBuilder::Impl::IntermediateSnapshotIdSet (
11771219 int64_t current_snapshot_id) const {
11781220 std::unordered_set<int64_t > added_snapshot_ids;
@@ -1590,11 +1632,13 @@ TableMetadataBuilder& TableMetadataBuilder::SuppressHistoricalSnapshots() {
15901632
15911633TableMetadataBuilder& TableMetadataBuilder::SetStatistics (
15921634 const std::shared_ptr<StatisticsFile>& statistics_file) {
1593- throw IcebergError (std::format (" {} not implemented" , __FUNCTION__));
1635+ impl_->SetStatistics (statistics_file);
1636+ return *this ;
15941637}
15951638
15961639TableMetadataBuilder& TableMetadataBuilder::RemoveStatistics (int64_t snapshot_id) {
1597- throw IcebergError (std::format (" {} not implemented" , __FUNCTION__));
1640+ impl_->RemoveStatistics (snapshot_id);
1641+ return *this ;
15981642}
15991643
16001644TableMetadataBuilder& TableMetadataBuilder::SetPartitionStatistics (
0 commit comments