zhf999 commented on code in PR #243:
URL: https://github.com/apache/paimon-cpp/pull/243#discussion_r3860105376


##########
test/inte/scan_and_read_inte_test.cpp:
##########
@@ -744,6 +744,47 @@ TEST_P(ScanAndReadInteTest, 
TestWithPKWithDvBatchScanSnapshot6WithPredicate) {
     ASSERT_TRUE(expected->Equals(read_result)) << read_result->ToString();
 }
 
+TEST_P(ScanAndReadInteTest, 
TestWithPKWithDvBatchScanSnapshot6WithLateMaterializing) {
+    auto file_format = FileFormat();
+    std::string table_path = paimon::test::GetDataDir() + file_format +
+                             
"/pk_table_scan_and_read_dv.db/pk_table_scan_and_read_dv/";
+    ScanContextBuilder scan_context_builder(table_path);
+    scan_context_builder.AddOption(Options::SCAN_SNAPSHOT_ID, "6");
+
+    std::string literal_str = "Alice";
+    auto not_equal = PredicateBuilder::NotEqual(
+        /*field_index=*/0, /*field_name=*/"f0", FieldType::STRING,
+        Literal(FieldType::STRING, literal_str.data(), literal_str.size()));
+    auto greater_than = PredicateBuilder::GreaterThan(/*field_index=*/3, 
/*field_name=*/"f3",
+                                                      FieldType::DOUBLE, 
Literal(18.0));
+    ASSERT_OK_AND_ASSIGN(auto predicate, PredicateBuilder::And({not_equal, 
greater_than}));
+    scan_context_builder.SetPredicate(predicate);
+    ASSERT_OK_AND_ASSIGN(auto scan_context, 
FinishScanContext(scan_context_builder));
+    ASSERT_OK_AND_ASSIGN(auto table_scan, 
TableScan::Create(std::move(scan_context)));
+
+    ReadContextBuilder read_context_builder(table_path);
+    AddReadOptionsForPrefetch(&read_context_builder);
+    read_context_builder.SetPredicate(predicate)
+        .EnablePredicateFilter(true)
+        .EnableLateMaterializing(true);

Review Comment:
   Done.



##########
src/paimon/common/reader/late_materializing_file_batch_reader_test.cpp:
##########
@@ -0,0 +1,658 @@
+/*
+ * 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/common/reader/late_materializing_file_batch_reader.h"
+
+#include <algorithm>
+#include <cstdint>
+#include <limits>
+#include <memory>
+#include <optional>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "arrow/api.h"
+#include "arrow/array/builder_nested.h"
+#include "arrow/c/bridge.h"
+#include "gtest/gtest.h"
+#include "paimon/common/reader/late_materializing_reader_builder.h"
+#include "paimon/common/reader/prefetch_file_batch_reader_impl.h"
+#include "paimon/common/reader/reader_utils.h"
+#include "paimon/common/utils/arrow/status_utils.h"
+#include "paimon/common/utils/checked_cast.h"
+#include "paimon/common/utils/read_ahead_cache.h"
+#include "paimon/executor.h"
+#include "paimon/format/reader_builder.h"
+#include "paimon/memory/memory_pool.h"
+#include "paimon/predicate/literal.h"
+#include "paimon/predicate/predicate.h"
+#include "paimon/predicate/predicate_builder.h"
+#include "paimon/reader/prefetch_file_batch_reader.h"
+#include "paimon/status.h"
+#include "paimon/testing/mock/mock_file_batch_reader.h"
+#include "paimon/testing/mock/mock_file_system.h"
+#include "paimon/testing/mock/mock_format_reader_builder.h"
+#include "paimon/testing/utils/testharness.h"
+#include "paimon/utils/roaring_bitmap32.h"
+
+namespace paimon::test {
+
+class LateMaterializingFileBatchReaderTest : public ::testing::Test {
+ public:
+    void SetUp() override {
+        k_field_ = arrow::field("k", arrow::int64());
+        v_field_ = arrow::field("v", arrow::utf8());
+        full_fields_ = {k_field_, v_field_};
+        full_type_ = arrow::struct_(full_fields_);
+    }
+
+    // Build a struct array with column k (int64, values = ks) and column v 
(utf8, "v_<index>").
+    std::shared_ptr<arrow::Array> BuildData(const std::vector<int64_t>& ks) {
+        arrow::StructBuilder builder(
+            full_type_, arrow::default_memory_pool(),
+            {std::make_shared<arrow::Int64Builder>(), 
std::make_shared<arrow::StringBuilder>()});
+        auto* k_builder = 
checked_cast<arrow::Int64Builder*>(builder.field_builder(0));
+        auto* v_builder = 
checked_cast<arrow::StringBuilder*>(builder.field_builder(1));
+        for (size_t i = 0; i < ks.size(); ++i) {
+            EXPECT_TRUE(builder.Append().ok());
+            EXPECT_TRUE(k_builder->Append(ks[i]).ok());
+            EXPECT_TRUE(v_builder->Append("v_" + std::to_string(i)).ok());
+        }
+        std::shared_ptr<arrow::Array> array;
+        EXPECT_TRUE(builder.Finish(&array).ok());
+        return array;
+    }
+
+    struct Row {
+        int64_t k;
+        std::string v;
+        uint64_t file_row;
+    };
+
+    // Drive the reader through NextBatchWithBitmap to EOF, decoding the 
full-schema output rows.
+    Result<std::vector<Row>> Collect(LateMaterializingFileBatchReader* reader) 
{
+        std::vector<Row> rows;
+        while (true) {
+            PAIMON_ASSIGN_OR_RAISE(BatchReader::ReadBatchWithBitmap 
batch_with_bitmap,
+                                   reader->NextBatchWithBitmap());
+            if (BatchReader::IsEofBatch(batch_with_bitmap)) {
+                break;
+            }
+            auto& [batch, bitmap] = batch_with_bitmap;
+            auto& [c_array, c_schema] = batch;
+            PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr<arrow::Array> 
array,
+                                              
arrow::ImportArray(c_array.get(), c_schema.get()));
+            auto struct_array = 
arrow::internal::checked_pointer_cast<arrow::StructArray>(array);
+            EXPECT_EQ(bitmap.Cardinality(), 
static_cast<int32_t>(struct_array->length()));
+            auto k_array = 
arrow::internal::checked_pointer_cast<arrow::Int64Array>(
+                struct_array->GetFieldByName("k"));
+            if (!k_array) {
+                return Status::Invalid("output batch missing k column");
+            }
+            // v is only present when it belongs to the read schema (payload 
projection).
+            auto v_array = 
arrow::internal::checked_pointer_cast<arrow::StringArray>(
+                struct_array->GetFieldByName("v"));
+            for (int64_t i = 0; i < struct_array->length(); ++i) {
+                PAIMON_ASSIGN_OR_RAISE(uint64_t file_row,
+                                       
reader->GetPreviousBatchFileRowId(static_cast<uint64_t>(i)));
+                rows.push_back(Row{k_array->Value(i),
+                                   v_array ? v_array->GetString(i) : 
std::string(), file_row});
+            }
+        }
+        return rows;
+    }
+
+    Status SetReadSchema(LateMaterializingFileBatchReader* reader,
+                         const std::shared_ptr<arrow::Schema>& schema,
+                         const std::shared_ptr<Predicate>& predicate,
+                         const std::optional<RoaringBitmap32>& selection) {
+        ::ArrowSchema c_schema;
+        PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow::ExportSchema(*schema, 
&c_schema));
+        return reader->SetReadSchema(&c_schema, predicate, selection);
+    }
+
+    // Collect all output rows as a single concatenated struct array (for 
schema/nested checks).
+    Result<std::shared_ptr<arrow::StructArray>> CollectStruct(FileBatchReader* 
reader) {
+        arrow::ArrayVector chunks;

Review Comment:
   Done.



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