Copilot commented on code in PR #801:
URL: https://github.com/apache/iceberg-cpp/pull/801#discussion_r3635050836


##########
src/iceberg/test/metadata_table_test_base.h:
##########
@@ -0,0 +1,161 @@
+/*
+ * 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.
+ */
+
+/// \file metadata_table_test_base.h
+/// Shared test base for all metadata table tests.
+///
+/// Provides common helpers (FinishAndImport, MakeTestSnapshots,
+/// MakeTableWithSnapshots) and the MockFileIO + MockCatalog fixture that
+/// every metadata table test needs.
+
+#pragma once
+
+#include <memory>
+#include <unordered_map>
+#include <utility>
+#include <vector>
+
+#include <arrow/array.h>
+#include <arrow/c/bridge.h>
+#include <arrow/record_batch.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "iceberg/schema.h"
+#include "iceberg/schema_field.h"
+#include "iceberg/schema_internal.h"
+#include "iceberg/snapshot.h"
+#include "iceberg/table.h"
+#include "iceberg/table_identifier.h"
+#include "iceberg/table_metadata.h"
+#include "iceberg/test/matchers.h"
+#include "iceberg/test/mock_catalog.h"
+#include "iceberg/test/mock_io.h"
+#include "iceberg/type.h"
+#include "iceberg/util/timepoint.h"
+
+namespace iceberg {
+
+/// \brief Base class for all metadata table tests.
+///
+/// Provides MockFileIO and MockCatalog instances plus helpers shared across
+/// metadata table tests (SnapshotsTable, HistoryTable, RefsTable, ...).
+class MetadataTableTestBase : public ::testing::Test {
+ protected:
+  void SetUp() override {
+    io_ = std::make_shared<MockFileIO>();
+    catalog_ = std::make_shared<MockCatalog>();
+
+    auto schema = std::make_shared<Schema>(
+        std::vector<SchemaField>{SchemaField::MakeRequired(1, "id", int64()),
+                                 SchemaField::MakeOptional(2, "name", 
string())},
+        1);
+    metadata_ = std::make_shared<TableMetadata>(
+        TableMetadata{.format_version = 2, .schemas = {schema}, 
.current_schema_id = 1});
+
+    TableIdentifier source_ident{.ns = Namespace{.levels = {"db"}},
+                                 .name = "source_table"};
+    ICEBERG_UNWRAP_OR_FAIL(table_, Table::Make(source_ident, metadata_,
+                                               "s3://bucket/meta.json", io_, 
catalog_));
+  }
+
+  /// \brief Import a Scan()-produced ArrowArray into an Arrow RecordBatch.
+  static std::shared_ptr<::arrow::RecordBatch> FinishAndImport(ArrowArray 
array,
+                                                               const Schema& 
schema) {
+    ArrowSchema c_schema{};
+    EXPECT_THAT(ToArrowSchema(schema, &c_schema), IsOk());
+    auto arrow_schema = ::arrow::ImportSchema(&c_schema).ValueOrDie();
+
+    // ImportRecordBatch takes ownership of the array and releases it.
+    return ::arrow::ImportRecordBatch(&array, arrow_schema).ValueOrDie();
+  }

Review Comment:
   FinishAndImport takes ArrowArray by value, but ImportRecordBatch takes 
ownership of (and mutates/releases) the ArrowArray passed by pointer. Passing a 
copy here means the caller's ArrowArray is left unchanged (still pointing at 
released buffers), which is easy to misuse and can lead to confusing UAF/leak 
patterns in tests. Prefer taking ArrowArray&& so ImportRecordBatch operates on 
the caller-owned instance.



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