wgtmac commented on code in PR #511:
URL: https://github.com/apache/iceberg-cpp/pull/511#discussion_r2697636165


##########
src/iceberg/update/update_statistics.h:
##########
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <cstdint>
+#include <memory>
+#include <unordered_map>
+#include <vector>
+
+#include "iceberg/iceberg_export.h"
+#include "iceberg/result.h"
+#include "iceberg/type_fwd.h"
+#include "iceberg/update/pending_update.h"
+
+/// \file iceberg/update/update_statistics.h
+/// \brief Updates table statistics.
+
+namespace iceberg {
+
+/// \brief Updates table statistics.
+class ICEBERG_EXPORT UpdateStatistics : public PendingUpdate {
+ public:
+  static Result<std::shared_ptr<UpdateStatistics>> Make(
+      std::shared_ptr<Transaction> transaction);
+
+  ~UpdateStatistics() override;
+
+  /// \brief Set statistics file for a snapshot.
+  ///
+  /// Associates a statistics file with a snapshot ID. If statistics already 
exist
+  /// for this snapshot, they will be replaced.
+  ///
+  /// \param statistics_file The statistics file to set
+  /// \return Reference to this UpdateStatistics for chaining
+  UpdateStatistics& SetStatistics(const std::shared_ptr<StatisticsFile>& 
statistics_file);
+
+  /// \brief Remove statistics for a snapshot.
+  ///
+  /// Marks the statistics for the given snapshot ID for removal.
+  ///
+  /// \param snapshot_id The snapshot ID whose statistics to remove
+  /// \return Reference to this UpdateStatistics for chaining
+  UpdateStatistics& RemoveStatistics(int64_t snapshot_id);
+
+  Kind kind() const final { return Kind::kUpdateStatistics; }
+
+  struct ApplyResult {
+    std::unordered_map<int64_t, std::shared_ptr<StatisticsFile>> to_set;

Review Comment:
   Is it cheaper to use `std::vector<std::pair<int64_t, 
std::shared_ptr<StatisticsFile>>>`?



##########
src/iceberg/table_metadata.cc:
##########
@@ -1173,6 +1177,45 @@ Status TableMetadataBuilder::Impl::SetRef(const 
std::string& name,
   return {};
 }
 
+Status TableMetadataBuilder::Impl::SetStatistics(
+    const std::shared_ptr<StatisticsFile>& statistics_file) {

Review Comment:
   ```suggestion
       std::shared_ptr<StatisticsFile> statistics_file) {
   ```



##########
src/iceberg/table_metadata.cc:
##########
@@ -1173,6 +1177,45 @@ Status TableMetadataBuilder::Impl::SetRef(const 
std::string& name,
   return {};
 }
 
+Status TableMetadataBuilder::Impl::SetStatistics(
+    const std::shared_ptr<StatisticsFile>& statistics_file) {
+  ICEBERG_CHECK(statistics_file != nullptr, "Cannot set null statistics file");
+
+  // Find and replace existing statistics for the same snapshot_id, or add new 
one
+  auto it = std::ranges::find_if(
+      metadata_.statistics,
+      [snapshot_id = statistics_file->snapshot_id](const auto& stat) {
+        return stat && stat->snapshot_id == snapshot_id;
+      });
+
+  if (it != metadata_.statistics.end()) {
+    *it = statistics_file;
+  } else {
+    metadata_.statistics.push_back(statistics_file);
+  }
+
+  changes_.push_back(std::make_unique<table::SetStatistics>(statistics_file));
+  return {};
+}
+
+Status TableMetadataBuilder::Impl::RemoveStatistics(int64_t snapshot_id) {
+  auto it = std::ranges::find_if(metadata_.statistics, [snapshot_id](const 
auto& stat) {
+    return stat && stat->snapshot_id == snapshot_id;
+  });
+
+  if (it == metadata_.statistics.end()) {
+    return {};
+  }
+
+  // Remove statistics for the given snapshot_id
+  std::erase_if(metadata_.statistics, [snapshot_id](const auto& stat) {

Review Comment:
   Can we combine find and erase in a single call?



##########
src/iceberg/table_metadata.cc:
##########
@@ -1173,6 +1177,45 @@ Status TableMetadataBuilder::Impl::SetRef(const 
std::string& name,
   return {};
 }
 
+Status TableMetadataBuilder::Impl::SetStatistics(
+    const std::shared_ptr<StatisticsFile>& statistics_file) {
+  ICEBERG_CHECK(statistics_file != nullptr, "Cannot set null statistics file");
+
+  // Find and replace existing statistics for the same snapshot_id, or add new 
one
+  auto it = std::ranges::find_if(
+      metadata_.statistics,
+      [snapshot_id = statistics_file->snapshot_id](const auto& stat) {
+        return stat && stat->snapshot_id == snapshot_id;
+      });
+
+  if (it != metadata_.statistics.end()) {
+    *it = statistics_file;
+  } else {
+    metadata_.statistics.push_back(statistics_file);
+  }
+
+  changes_.push_back(std::make_unique<table::SetStatistics>(statistics_file));

Review Comment:
   Use std::move



##########
src/iceberg/update/update_statistics.cc:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/update/update_statistics.h"
+
+#include <cstdint>
+#include <memory>
+#include <unordered_map>
+#include <vector>
+
+#include "iceberg/result.h"
+#include "iceberg/statistics_file.h"
+#include "iceberg/transaction.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+Result<std::shared_ptr<UpdateStatistics>> UpdateStatistics::Make(
+    std::shared_ptr<Transaction> transaction) {
+  ICEBERG_PRECHECK(transaction != nullptr,
+                   "Cannot create UpdateStatistics without a transaction");
+  return std::shared_ptr<UpdateStatistics>(new 
UpdateStatistics(std::move(transaction)));
+}
+
+UpdateStatistics::UpdateStatistics(std::shared_ptr<Transaction> transaction)
+    : PendingUpdate(std::move(transaction)) {}
+
+UpdateStatistics::~UpdateStatistics() = default;
+
+UpdateStatistics& UpdateStatistics::SetStatistics(
+    const std::shared_ptr<StatisticsFile>& statistics_file) {
+  ICEBERG_BUILDER_CHECK(statistics_file != nullptr, "Statistics file cannot be 
null");
+  statistics_to_set_[statistics_file->snapshot_id] = statistics_file;

Review Comment:
   ```suggestion
       std::shared_ptr<StatisticsFile> statistics_file) {
     ICEBERG_BUILDER_CHECK(statistics_file != nullptr, "Statistics file cannot 
be null");
     statistics_to_set_[statistics_file->snapshot_id] = 
std::move(statistics_file);
   ```



##########
src/iceberg/table_metadata.cc:
##########
@@ -1173,6 +1177,45 @@ Status TableMetadataBuilder::Impl::SetRef(const 
std::string& name,
   return {};
 }
 
+Status TableMetadataBuilder::Impl::SetStatistics(
+    const std::shared_ptr<StatisticsFile>& statistics_file) {
+  ICEBERG_CHECK(statistics_file != nullptr, "Cannot set null statistics file");
+
+  // Find and replace existing statistics for the same snapshot_id, or add new 
one
+  auto it = std::ranges::find_if(
+      metadata_.statistics,
+      [snapshot_id = statistics_file->snapshot_id](const auto& stat) {
+        return stat && stat->snapshot_id == snapshot_id;
+      });
+
+  if (it != metadata_.statistics.end()) {
+    *it = statistics_file;
+  } else {
+    metadata_.statistics.push_back(statistics_file);
+  }
+
+  changes_.push_back(std::make_unique<table::SetStatistics>(statistics_file));
+  return {};
+}
+
+Status TableMetadataBuilder::Impl::RemoveStatistics(int64_t snapshot_id) {
+  auto it = std::ranges::find_if(metadata_.statistics, [snapshot_id](const 
auto& stat) {
+    return stat && stat->snapshot_id == snapshot_id;
+  });
+
+  if (it == metadata_.statistics.end()) {
+    return {};
+  }
+
+  // Remove statistics for the given snapshot_id
+  std::erase_if(metadata_.statistics, [snapshot_id](const auto& stat) {

Review Comment:
   Or you can just call something like `metadata_.statistics.erase(it)` to 
avoid a 2nd iteration



##########
src/iceberg/table_metadata.cc:
##########
@@ -1173,6 +1177,45 @@ Status TableMetadataBuilder::Impl::SetRef(const 
std::string& name,
   return {};
 }
 
+Status TableMetadataBuilder::Impl::SetStatistics(
+    const std::shared_ptr<StatisticsFile>& statistics_file) {
+  ICEBERG_CHECK(statistics_file != nullptr, "Cannot set null statistics file");

Review Comment:
   ```suggestion
     ICEBERG_PRECHECK(statistics_file != nullptr, "Cannot set null statistics 
file");
   ```
   
   Note that PRECHECK returns InvalidArgument while CHECK returns 
ValidationFailed.



##########
src/iceberg/json_internal.cc:
##########
@@ -1399,6 +1401,18 @@ nlohmann::json ToJson(const TableUpdate& update) {
       json[kLocation] = u.location();
       break;
     }
+    case TableUpdate::Kind::kSetStatistics: {

Review Comment:
   Please also add `FromJson` equivalents and their test cases.



##########
src/iceberg/table_metadata.cc:
##########
@@ -1032,12 +1035,13 @@ Result<int32_t> 
TableMetadataBuilder::Impl::AddSchema(const Schema& schema,
   return new_schema_id;
 }
 
-void TableMetadataBuilder::Impl::SetLocation(std::string_view location) {
+Status TableMetadataBuilder::Impl::SetLocation(std::string_view location) {

Review Comment:
   Why changing this? We don't need to return status if it never has any error.



##########
src/iceberg/transaction.cc:
##########
@@ -183,6 +185,17 @@ Status Transaction::Apply(PendingUpdate& update) {
         
metadata_builder_->RemoveSchemas(std::move(result.schema_ids_to_remove));
       }
     } break;
+    case PendingUpdate::Kind::kUpdateStatistics: {
+      auto& update_statistics = 
internal::checked_cast<UpdateStatistics&>(update);
+      ICEBERG_ASSIGN_OR_RAISE(auto result, update_statistics.Apply());
+      // Apply statistics changes to the metadata builder
+      for (const auto& [snapshot_id, stat_file] : result.to_set) {
+        metadata_builder_->SetStatistics(stat_file);
+      }

Review Comment:
   ```suggestion
         for (auto&& [_, stat_file] : result.to_set) {
           metadata_builder_->SetStatistics(std::move(stat_file));
         }
   ```



##########
src/iceberg/table_update.cc:
##########
@@ -446,4 +447,50 @@ std::unique_ptr<TableUpdate> SetLocation::Clone() const {
   return std::make_unique<SetLocation>(location_);
 }
 
+// SetStatistics
+
+int64_t SetStatistics::snapshot_id() const { return 
statistics_file_->snapshot_id; }
+
+void SetStatistics::ApplyTo(TableMetadataBuilder& builder) const {
+  builder.SetStatistics(statistics_file_);
+}
+
+void SetStatistics::GenerateRequirements(TableUpdateContext& context) const {
+  // SetStatistics doesn't generate any requirements
+}
+
+bool SetStatistics::Equals(const TableUpdate& other) const {
+  if (other.kind() != Kind::kSetStatistics) {
+    return false;
+  }
+  const auto& other_set = static_cast<const SetStatistics&>(other);
+  return *statistics_file_ == *other_set.statistics_file_;

Review Comment:
   nit: check null just in case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to