zhjwpku commented on code in PR #492:
URL: https://github.com/apache/iceberg-cpp/pull/492#discussion_r2678635571


##########
src/iceberg/test/table_scan_test.cc:
##########
@@ -0,0 +1,1183 @@
+/*
+ * 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/table_scan.h"
+
+#include <chrono>
+#include <format>
+#include <memory>
+#include <optional>
+#include <string>
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "iceberg/arrow/arrow_file_io.h"
+#include "iceberg/avro/avro_register.h"
+#include "iceberg/expression/expressions.h"
+#include "iceberg/manifest/manifest_entry.h"
+#include "iceberg/manifest/manifest_list.h"
+#include "iceberg/manifest/manifest_reader.h"
+#include "iceberg/manifest/manifest_writer.h"
+#include "iceberg/partition_spec.h"
+#include "iceberg/schema.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/test/matchers.h"
+#include "iceberg/transform.h"
+#include "iceberg/type.h"
+#include "iceberg/util/timepoint.h"
+
+namespace iceberg {
+
+class TableScanTest : public testing::TestWithParam<int> {
+ protected:
+  void SetUp() override {
+    avro::RegisterAll();
+
+    file_io_ = arrow::MakeMockFileIO();
+    schema_ = std::make_shared<Schema>(std::vector<SchemaField>{
+        SchemaField::MakeRequired(/*field_id=*/1, "id", int32()),
+        SchemaField::MakeRequired(/*field_id=*/2, "data", string())});
+    unpartitioned_spec_ = PartitionSpec::Unpartitioned();
+
+    ICEBERG_UNWRAP_OR_FAIL(
+        partitioned_spec_,
+        PartitionSpec::Make(
+            /*spec_id=*/1, {PartitionField(/*source_id=*/2, /*field_id=*/1000,
+                                           "data_bucket_16_2", 
Transform::Bucket(16))}));
+
+    MakeTableMetadata();
+  }
+
+  void MakeTableMetadata() {
+    constexpr int64_t kSnapshotId = 1000L;
+    constexpr int64_t kSequenceNumber = 1L;
+    constexpr int64_t kTimestampMs = 1609459200000L;  // 2021-01-01 00:00:00 
UTC
+
+    // Create a snapshot
+    ICEBERG_UNWRAP_OR_FAIL(auto timestamp_ms, 
TimePointMsFromUnixMs(kTimestampMs));
+    auto snapshot = std::make_shared<Snapshot>(Snapshot{
+        .snapshot_id = kSnapshotId,
+        .parent_snapshot_id = std::nullopt,
+        .sequence_number = kSequenceNumber,
+        .timestamp_ms = timestamp_ms,
+        .manifest_list =
+            "/tmp/metadata/snap-1000-1-manifest-list.avro",  // Use filesystem 
path
+        .summary = {},
+        .schema_id = schema_->schema_id()});
+
+    table_metadata_ = std::make_shared<TableMetadata>(
+        TableMetadata{.format_version = 2,
+                      .table_uuid = "test-table-uuid",
+                      .location = "/tmp/table",  // Use filesystem path
+                      .last_sequence_number = kSequenceNumber,
+                      .last_updated_ms = timestamp_ms,
+                      .last_column_id = 2,
+                      .schemas = {schema_},
+                      .current_schema_id = schema_->schema_id(),
+                      .partition_specs = {partitioned_spec_, 
unpartitioned_spec_},
+                      .default_spec_id = partitioned_spec_->spec_id(),
+                      .last_partition_id = 1000,
+                      .properties = {},
+                      .current_snapshot_id = kSnapshotId,
+                      .snapshots = {snapshot},
+                      .snapshot_log = {SnapshotLogEntry{.timestamp_ms = 
timestamp_ms,
+                                                        .snapshot_id = 
kSnapshotId}},
+                      .metadata_log = {},
+                      .sort_orders = {},
+                      .default_sort_order_id = 0,
+                      .refs = {{"main", 
std::make_shared<SnapshotRef>(SnapshotRef{
+                                            .snapshot_id = kSnapshotId,
+                                            .retention = 
SnapshotRef::Branch{}})}}});
+  }
+
+  std::shared_ptr<DataFile> MakePositionDeleteFile(
+      const std::string& path, const PartitionValues& partition, int32_t 
spec_id,
+      std::optional<std::string> referenced_file = std::nullopt) {
+    return std::make_shared<DataFile>(DataFile{
+        .content = DataFile::Content::kPositionDeletes,
+        .file_path = path,
+        .file_format = FileFormatType::kParquet,
+        .partition = partition,
+        .record_count = 1,
+        .file_size_in_bytes = 10,
+        .referenced_data_file = referenced_file,
+        .partition_spec_id = spec_id,
+    });
+  }
+
+  std::shared_ptr<DataFile> MakeEqualityDeleteFile(const std::string& path,
+                                                   const PartitionValues& 
partition,
+                                                   int32_t spec_id,
+                                                   std::vector<int> 
equality_ids = {1}) {
+    return std::make_shared<DataFile>(DataFile{
+        .content = DataFile::Content::kEqualityDeletes,
+        .file_path = path,
+        .file_format = FileFormatType::kParquet,
+        .partition = partition,
+        .record_count = 1,
+        .file_size_in_bytes = 10,
+        .equality_ids = std::move(equality_ids),
+        .partition_spec_id = spec_id,
+    });
+  }
+
+  std::string MakeManifestPath() {
+    static int counter = 0;
+    return std::format("manifest-{}-{}.avro", counter++,
+                       
std::chrono::system_clock::now().time_since_epoch().count());
+  }
+
+  std::shared_ptr<DataFile> MakeDataFile(const std::string& path,
+                                         const PartitionValues& partition,
+                                         int32_t spec_id, int64_t record_count 
= 1) {
+    return std::make_shared<DataFile>(DataFile{
+        .file_path = path,
+        .file_format = FileFormatType::kParquet,
+        .partition = partition,
+        .record_count = record_count,
+        .file_size_in_bytes = 10,
+        .sort_order_id = 0,
+        .partition_spec_id = spec_id,
+    });
+  }
+
+  ManifestEntry MakeEntry(ManifestStatus status, int64_t snapshot_id,
+                          int64_t sequence_number, std::shared_ptr<DataFile> 
file) {
+    return ManifestEntry{
+        .status = status,
+        .snapshot_id = snapshot_id,
+        .sequence_number = sequence_number,
+        .file_sequence_number = sequence_number,
+        .data_file = std::move(file),
+    };
+  }
+
+  ManifestFile WriteDataManifest(int format_version, int64_t snapshot_id,
+                                 std::vector<ManifestEntry> entries,
+                                 std::shared_ptr<PartitionSpec> spec) {
+    const std::string manifest_path = MakeManifestPath();
+
+    Result<std::unique_ptr<ManifestWriter>> writer_result =
+        NotSupported("Format version: {}", format_version);
+
+    if (format_version == 1) {
+      writer_result = ManifestWriter::MakeV1Writer(snapshot_id, manifest_path, 
file_io_,

Review Comment:
   nit: use ManifestWriter::MakeWriter



##########
src/iceberg/test/table_scan_test.cc:
##########
@@ -0,0 +1,1183 @@
+/*
+ * 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/table_scan.h"
+
+#include <chrono>
+#include <format>
+#include <memory>
+#include <optional>
+#include <string>
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "iceberg/arrow/arrow_file_io.h"
+#include "iceberg/avro/avro_register.h"
+#include "iceberg/expression/expressions.h"
+#include "iceberg/manifest/manifest_entry.h"
+#include "iceberg/manifest/manifest_list.h"
+#include "iceberg/manifest/manifest_reader.h"
+#include "iceberg/manifest/manifest_writer.h"
+#include "iceberg/partition_spec.h"
+#include "iceberg/schema.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/test/matchers.h"
+#include "iceberg/transform.h"
+#include "iceberg/type.h"
+#include "iceberg/util/timepoint.h"
+
+namespace iceberg {
+
+class TableScanTest : public testing::TestWithParam<int> {
+ protected:
+  void SetUp() override {
+    avro::RegisterAll();
+
+    file_io_ = arrow::MakeMockFileIO();
+    schema_ = std::make_shared<Schema>(std::vector<SchemaField>{
+        SchemaField::MakeRequired(/*field_id=*/1, "id", int32()),
+        SchemaField::MakeRequired(/*field_id=*/2, "data", string())});
+    unpartitioned_spec_ = PartitionSpec::Unpartitioned();
+
+    ICEBERG_UNWRAP_OR_FAIL(
+        partitioned_spec_,
+        PartitionSpec::Make(
+            /*spec_id=*/1, {PartitionField(/*source_id=*/2, /*field_id=*/1000,
+                                           "data_bucket_16_2", 
Transform::Bucket(16))}));
+
+    MakeTableMetadata();
+  }
+
+  void MakeTableMetadata() {
+    constexpr int64_t kSnapshotId = 1000L;
+    constexpr int64_t kSequenceNumber = 1L;
+    constexpr int64_t kTimestampMs = 1609459200000L;  // 2021-01-01 00:00:00 
UTC
+
+    // Create a snapshot
+    ICEBERG_UNWRAP_OR_FAIL(auto timestamp_ms, 
TimePointMsFromUnixMs(kTimestampMs));
+    auto snapshot = std::make_shared<Snapshot>(Snapshot{
+        .snapshot_id = kSnapshotId,
+        .parent_snapshot_id = std::nullopt,
+        .sequence_number = kSequenceNumber,
+        .timestamp_ms = timestamp_ms,
+        .manifest_list =
+            "/tmp/metadata/snap-1000-1-manifest-list.avro",  // Use filesystem 
path
+        .summary = {},
+        .schema_id = schema_->schema_id()});
+
+    table_metadata_ = std::make_shared<TableMetadata>(
+        TableMetadata{.format_version = 2,
+                      .table_uuid = "test-table-uuid",
+                      .location = "/tmp/table",  // Use filesystem path
+                      .last_sequence_number = kSequenceNumber,
+                      .last_updated_ms = timestamp_ms,
+                      .last_column_id = 2,
+                      .schemas = {schema_},
+                      .current_schema_id = schema_->schema_id(),
+                      .partition_specs = {partitioned_spec_, 
unpartitioned_spec_},
+                      .default_spec_id = partitioned_spec_->spec_id(),
+                      .last_partition_id = 1000,
+                      .properties = {},
+                      .current_snapshot_id = kSnapshotId,
+                      .snapshots = {snapshot},
+                      .snapshot_log = {SnapshotLogEntry{.timestamp_ms = 
timestamp_ms,
+                                                        .snapshot_id = 
kSnapshotId}},
+                      .metadata_log = {},
+                      .sort_orders = {},
+                      .default_sort_order_id = 0,
+                      .refs = {{"main", 
std::make_shared<SnapshotRef>(SnapshotRef{
+                                            .snapshot_id = kSnapshotId,
+                                            .retention = 
SnapshotRef::Branch{}})}}});
+  }
+
+  std::shared_ptr<DataFile> MakePositionDeleteFile(
+      const std::string& path, const PartitionValues& partition, int32_t 
spec_id,
+      std::optional<std::string> referenced_file = std::nullopt) {
+    return std::make_shared<DataFile>(DataFile{
+        .content = DataFile::Content::kPositionDeletes,
+        .file_path = path,
+        .file_format = FileFormatType::kParquet,
+        .partition = partition,
+        .record_count = 1,
+        .file_size_in_bytes = 10,
+        .referenced_data_file = referenced_file,
+        .partition_spec_id = spec_id,
+    });
+  }
+
+  std::shared_ptr<DataFile> MakeEqualityDeleteFile(const std::string& path,
+                                                   const PartitionValues& 
partition,
+                                                   int32_t spec_id,
+                                                   std::vector<int> 
equality_ids = {1}) {
+    return std::make_shared<DataFile>(DataFile{
+        .content = DataFile::Content::kEqualityDeletes,
+        .file_path = path,
+        .file_format = FileFormatType::kParquet,
+        .partition = partition,
+        .record_count = 1,
+        .file_size_in_bytes = 10,
+        .equality_ids = std::move(equality_ids),
+        .partition_spec_id = spec_id,
+    });
+  }
+
+  std::string MakeManifestPath() {
+    static int counter = 0;
+    return std::format("manifest-{}-{}.avro", counter++,
+                       
std::chrono::system_clock::now().time_since_epoch().count());
+  }
+
+  std::shared_ptr<DataFile> MakeDataFile(const std::string& path,
+                                         const PartitionValues& partition,
+                                         int32_t spec_id, int64_t record_count 
= 1) {
+    return std::make_shared<DataFile>(DataFile{
+        .file_path = path,
+        .file_format = FileFormatType::kParquet,
+        .partition = partition,
+        .record_count = record_count,
+        .file_size_in_bytes = 10,
+        .sort_order_id = 0,
+        .partition_spec_id = spec_id,
+    });
+  }
+
+  ManifestEntry MakeEntry(ManifestStatus status, int64_t snapshot_id,
+                          int64_t sequence_number, std::shared_ptr<DataFile> 
file) {
+    return ManifestEntry{
+        .status = status,
+        .snapshot_id = snapshot_id,
+        .sequence_number = sequence_number,
+        .file_sequence_number = sequence_number,
+        .data_file = std::move(file),
+    };
+  }
+
+  ManifestFile WriteDataManifest(int format_version, int64_t snapshot_id,
+                                 std::vector<ManifestEntry> entries,
+                                 std::shared_ptr<PartitionSpec> spec) {
+    const std::string manifest_path = MakeManifestPath();
+
+    Result<std::unique_ptr<ManifestWriter>> writer_result =
+        NotSupported("Format version: {}", format_version);
+
+    if (format_version == 1) {
+      writer_result = ManifestWriter::MakeV1Writer(snapshot_id, manifest_path, 
file_io_,
+                                                   spec, schema_);
+    } else if (format_version == 2) {
+      writer_result = ManifestWriter::MakeV2Writer(snapshot_id, manifest_path, 
file_io_,
+                                                   spec, schema_, 
ManifestContent::kData);
+    } else if (format_version == 3) {
+      writer_result =
+          ManifestWriter::MakeV3Writer(snapshot_id, /*first_row_id=*/0L, 
manifest_path,
+                                       file_io_, spec, schema_, 
ManifestContent::kData);
+    }
+
+    EXPECT_THAT(writer_result, IsOk());
+    auto writer = std::move(writer_result.value());
+
+    for (const auto& entry : entries) {
+      EXPECT_THAT(writer->WriteEntry(entry), IsOk());
+    }
+
+    EXPECT_THAT(writer->Close(), IsOk());
+    auto manifest_result = writer->ToManifestFile();
+    EXPECT_THAT(manifest_result, IsOk());
+    return std::move(manifest_result.value());
+  }
+
+  ManifestFile WriteDeleteManifest(int format_version, int64_t snapshot_id,
+                                   std::vector<ManifestEntry> entries,
+                                   std::shared_ptr<PartitionSpec> spec) {
+    const std::string manifest_path = MakeManifestPath();
+
+    Result<std::unique_ptr<ManifestWriter>> writer_result =
+        NotSupported("Format version: {}", format_version);
+
+    if (format_version == 2) {

Review Comment:
   ditto



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