shangxinli commented on code in PR #774:
URL: https://github.com/apache/iceberg-cpp/pull/774#discussion_r4271875189


##########
src/iceberg/update/rewrite_manifests.cc:
##########
@@ -0,0 +1,484 @@
+/*
+ * 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/rewrite_manifests.h"
+
+#include <algorithm>
+#include <array>
+#include <memory>
+#include <optional>
+#include <ranges>
+#include <span>
+#include <tuple>
+#include <unordered_map>
+#include <utility>
+#include <vector>
+
+#include "iceberg/constants.h"
+#include "iceberg/manifest/manifest_entry.h"
+#include "iceberg/manifest/manifest_list.h"
+#include "iceberg/manifest/manifest_reader.h"
+#include "iceberg/manifest/manifest_util_internal.h"
+#include "iceberg/manifest/manifest_writer.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/table.h"  // IWYU pragma: keep
+#include "iceberg/table_metadata.h"
+#include "iceberg/transaction.h"
+#include "iceberg/util/executor_util_internal.h"
+#include "iceberg/util/macros.h"
+
+namespace iceberg {
+
+namespace {
+
+struct RewriteCandidate {
+  ManifestFile manifest;
+  std::shared_ptr<PartitionSpec> spec;
+};
+
+struct ManifestStream {
+  ManifestFile manifest;
+  std::shared_ptr<PartitionSpec> spec;
+  ManifestEntryStreamPtr entries;
+};
+
+struct RewriteWriter {
+  std::shared_ptr<PartitionSpec> spec;
+  std::unique_ptr<ManifestWriter> writer;
+};
+
+}  // namespace
+
+Result<std::shared_ptr<RewriteManifests>> RewriteManifests::Make(
+    std::string table_name, std::shared_ptr<TransactionContext> ctx) {
+  ICEBERG_PRECHECK(!table_name.empty(), "Table name cannot be empty");
+  ICEBERG_PRECHECK(ctx != nullptr, "Cannot create RewriteManifests without a 
context");
+  return std::shared_ptr<RewriteManifests>(
+      new RewriteManifests(std::move(table_name), std::move(ctx)));
+}
+
+RewriteManifests::RewriteManifests(std::string table_name,
+                                   std::shared_ptr<TransactionContext> ctx)
+    : SnapshotUpdate(std::move(ctx)), table_name_(std::move(table_name)) {}
+
+RewriteManifests& RewriteManifests::ClusterBy(ClusterByFunc func) {
+  ICEBERG_BUILDER_CHECK(static_cast<bool>(func), "Cluster function cannot be 
null");
+  cluster_by_func_ = std::move(func);
+  rewrite_dirty_ = true;
+  return *this;
+}
+
+RewriteManifests& RewriteManifests::RewriteIf(RewritePredicate predicate) {
+  ICEBERG_BUILDER_CHECK(static_cast<bool>(predicate), "Rewrite predicate 
cannot be null");
+  predicate_ = std::move(predicate);
+  rewrite_dirty_ = true;
+  return *this;
+}
+
+RewriteManifests& RewriteManifests::DeleteManifest(const ManifestFile& 
manifest) {
+  deleted_manifests_.insert(manifest);
+  return *this;
+}
+
+RewriteManifests& RewriteManifests::AddManifest(const ManifestFile& manifest) {
+  // Reject added/deleted files unconditionally, matching Java's 
checkArgument. A
+  // missing count is treated as non-zero (has_*_files defaults to true), so 
the
+  // error is reported at the AddManifest call site rather than deferred to 
Apply.
+  ICEBERG_BUILDER_CHECK(!manifest.has_added_files(),
+                        "Cannot add manifest with added files");
+  ICEBERG_BUILDER_CHECK(!manifest.has_deleted_files(),
+                        "Cannot add manifest with deleted files");
+  ICEBERG_BUILDER_CHECK(!manifest.added_snapshot_id.has_value() ||
+                            manifest.added_snapshot_id == kInvalidSnapshotId,
+                        "Snapshot id must be assigned during commit");
+  ICEBERG_BUILDER_CHECK(manifest.sequence_number == kInvalidSequenceNumber,
+                        "Sequence number must be assigned during commit");

Review Comment:
   Java says "Sequence must be assigned during commit" here 
(BaseRewriteManifests.java:141), without "number". The other three checks in 
this block match Java character-for-character, so this one looks like a slip.



##########
src/iceberg/manifest/manifest_util.cc:
##########
@@ -68,4 +96,43 @@ Result<ManifestFile> CopyAppendManifest(
   return writer->ToManifestFile();
 }
 
+Result<ManifestFile> CopyRewriteManifest(
+    const ManifestFile& manifest, const std::shared_ptr<FileIO>& file_io,
+    const std::shared_ptr<Schema>& schema, const 
std::shared_ptr<PartitionSpec>& spec,
+    int64_t snapshot_id, const std::string& output_path, int8_t 
format_version) {
+  ICEBERG_ASSIGN_OR_RAISE(auto source_format_version,
+                          ReadManifestFormatVersion(manifest, file_io));
+  // Require explicit snapshot IDs and preserve them in the copied entries.
+  ICEBERG_ASSIGN_OR_RAISE(auto inheritable_metadata, 
InheritableMetadataFactory::Empty());
+  ICEBERG_ASSIGN_OR_RAISE(
+      auto reader, ManifestReader::Make(
+                       manifest.manifest_path, manifest.manifest_length, 
file_io, schema,
+                       spec, std::move(inheritable_metadata), 
manifest.first_row_id,
+                       /*is_committed=*/false));
+  ICEBERG_ASSIGN_OR_RAISE(auto entries, reader->Entries());
+
+  ICEBERG_ASSIGN_OR_RAISE(
+      auto writer,
+      ManifestWriter::MakeWriter(format_version, snapshot_id, output_path, 
file_io, spec,
+                                 schema, manifest.content, 
manifest.first_row_id));
+  for (auto& entry : entries) {
+    // A rewritten added manifest may only contain existing entries.
+    if (entry.status == ManifestStatus::kAdded) {
+      return ValidationFailed("Cannot add manifest with added files");

Review Comment:
   Java rejects a bad entry status in the copy path with "Invalid manifest 
entry status: ADDED (allowed status: EXISTING)" (ManifestFiles.java:388), not 
the addManifest message. Reusing the addManifest text here drops the 
information about which status was actually found.



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