zjw1111 commented on code in PR #224:
URL: https://github.com/apache/paimon-cpp/pull/224#discussion_r3879630838


##########
src/paimon/core/realtime/primary_key_realtime_store_test.cpp:
##########
@@ -0,0 +1,451 @@
+/*
+ * 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 "paimon/core/realtime/primary_key_realtime_store.h"
+
+#include <cstddef>
+#include <cstdint>
+#include <memory>
+#include <optional>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "arrow/api.h"
+#include "arrow/c/bridge.h"
+#include "arrow/ipc/json_simple.h"
+#include "paimon/common/table/special_fields.h"
+#include "paimon/common/types/data_field.h"
+#include "paimon/common/utils/arrow/status_utils.h"
+#include "paimon/common/utils/checked_cast.h"
+#include "paimon/macros.h"
+#include "paimon/memory/memory_pool.h"
+#include "paimon/realtime/arrow_realtime_store_factory.h"
+#include "paimon/testing/utils/testharness.h"
+
+namespace paimon::test {
+namespace {
+
+std::shared_ptr<arrow::Field> FieldWithId(const std::string& name,
+                                          const 
std::shared_ptr<arrow::DataType>& type,
+                                          int32_t field_id, bool nullable = 
true) {
+    return DataField::ConvertDataFieldToArrowField(
+               DataField(field_id, arrow::field(name, type, nullable)))
+        ->WithNullable(nullable);
+}
+
+std::shared_ptr<arrow::Schema> TransportSchema() {
+    return arrow::schema(
+        
{DataField::ConvertDataFieldToArrowField(SpecialFields::ValueKind())->WithNullable(false),
+         
DataField::ConvertDataFieldToArrowField(SpecialFields::SequenceNumber())
+             ->WithNullable(false),
+         
DataField::ConvertDataFieldToArrowField(SpecialFields::RealtimeOffset()),
+         DataField::ConvertDataFieldToArrowField(DataField(0, 
arrow::field("id", arrow::int64()))),
+         DataField::ConvertDataFieldToArrowField(
+             DataField(1, arrow::field("value", arrow::utf8())))});
+}
+
+std::shared_ptr<arrow::Schema> NestedTransportSchema() {
+    return arrow::schema(
+        
{DataField::ConvertDataFieldToArrowField(SpecialFields::ValueKind())->WithNullable(false),
+         
DataField::ConvertDataFieldToArrowField(SpecialFields::SequenceNumber())
+             ->WithNullable(false),
+         
DataField::ConvertDataFieldToArrowField(SpecialFields::RealtimeOffset()),
+         DataField::ConvertDataFieldToArrowField(DataField(0, 
arrow::field("id", arrow::int64()))),
+         DataField::ConvertDataFieldToArrowField(DataField(
+             1,
+             arrow::field("value",
+                          arrow::struct_({arrow::field("name", arrow::utf8()),
+                                          arrow::field("items", 
arrow::list(arrow::int32()))}))))});
+}
+
+std::unique_ptr<RecordBatch> MakeBatch(const std::string& json) {
+    std::shared_ptr<arrow::Array> array =
+        
arrow::ipc::internal::json::ArrayFromJSON(arrow::struct_(TransportSchema()->fields()),
 json)
+            .ValueOrDie();
+    auto c_array = std::make_unique<ArrowArray>();
+    EXPECT_TRUE(arrow::ExportArray(*array, c_array.get()).ok());
+    return RecordBatchBuilder(c_array.get()).Finish().value();
+}
+
+std::unique_ptr<RecordBatch> MakeSlicedBatch(const 
std::shared_ptr<arrow::Schema>& schema,
+                                             const std::string& json, int64_t 
offset,
+                                             int64_t length) {
+    std::shared_ptr<arrow::Array> array =
+        
arrow::ipc::internal::json::ArrayFromJSON(arrow::struct_(schema->fields()), 
json)
+            .ValueOrDie()
+            ->Slice(offset, length);
+    auto c_array = std::make_unique<ArrowArray>();
+    EXPECT_TRUE(arrow::ExportArray(*array, c_array.get()).ok());
+    return RecordBatchBuilder(c_array.get()).Finish().value();
+}
+
+void AssertOffsetsZero(const ArrowArray* array) {
+    ASSERT_NE(nullptr, array);
+    ASSERT_EQ(0, array->offset);
+    for (int64_t child = 0; child < array->n_children; ++child) {
+        AssertOffsetsZero(array->children[child]);
+    }
+    if (array->dictionary) {
+        AssertOffsetsZero(array->dictionary);
+    }
+}
+
+Result<std::string> ReadJson(const std::vector<std::unique_ptr<BatchReader>>& 
readers) {
+    std::vector<std::shared_ptr<arrow::Array>> batches;
+    for (const std::unique_ptr<BatchReader>& reader : readers) {
+        while (true) {
+            PAIMON_ASSIGN_OR_RAISE(BatchReader::ReadBatch batch, 
reader->NextBatch());
+            if (BatchReader::IsEofBatch(batch)) {
+                break;
+            }
+            PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(
+                std::shared_ptr<arrow::Array> array,
+                arrow::ImportArray(batch.first.get(), batch.second.get()));
+            batches.push_back(std::move(array));
+        }
+    }
+    PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr<arrow::Array> result,
+                                      arrow::Concatenate(batches));
+    return result->ToString();
+}
+
+class TestingMemoryPool final : public MemoryPool {

Review Comment:
   Thanks for adding the lifetime coverage here. Could you evaluate whether two 
test-only wrappers can be removed without reducing that coverage?
   
   - This `TestingMemoryPool` only forwards to `GetMemoryPool()` and is used to 
observe lifetime through a `weak_ptr`. It seems the test could instead use 
`std::shared_ptr<MemoryPool> pool = GetMemoryPool()` together with 
`std::weak_ptr<MemoryPool>`.
   - `ReadViewCheckingBatchReader` in `test/inte/realtime_write_inte_test.cpp` 
checks the same read-view lifetime on `NextBatch()`, while `TestPkRead` already 
asserts that the tracked view remains alive after the context is destroyed and 
immediately before reading, then expires after the reader is closed.
   
   Would it be possible to remove these two wrappers while keeping the existing 
explicit lifetime assertions? I am only suggesting a test-scaffolding cleanup; 
the pool and read-view lifetime coverage itself should remain.



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

Reply via email to