This is an automated email from the ASF dual-hosted git repository.

lucasfang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/paimon-cpp.git


The following commit(s) were added to refs/heads/main by this push:
     new 9520864c fix(orc): validate string offsets before deep copying (#591)
9520864c is described below

commit 9520864cb8b222a671eda24cf6c56b48231d63e5
Author: lszskye <[email protected]>
AuthorDate: Mon Nov 23 17:25:10 2026 +0800

    fix(orc): validate string offsets before deep copying (#591)
---
 src/paimon/format/orc/orc_adapter.cpp      |  5 +-
 src/paimon/format/orc/orc_adapter_test.cpp | 20 +++++++
 test/inte/write_and_read_inte_test.cpp     | 87 ++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+), 2 deletions(-)

diff --git a/src/paimon/format/orc/orc_adapter.cpp 
b/src/paimon/format/orc/orc_adapter.cpp
index 67652d53..3cad1c59 100644
--- a/src/paimon/format/orc/orc_adapter.cpp
+++ b/src/paimon/format/orc/orc_adapter.cpp
@@ -778,10 +778,11 @@ Result<std::shared_ptr<arrow::ArrayBuilder>> 
MakeOrcBackedBinaryBuilder(
     } else {
         // condition3: disable dict batch in orc, while blob is also invalid, 
degrade to deep copy
         // version
-        PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<arrow::Buffer> data,
-                               DeepCopyDataBufferForString(typed_batch, pool));
+        // Validate 32-bit offsets before allocating or copying the decoded 
string data.
         PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<arrow::Buffer> offsets,
                                PrepareOffsetsBufferForString(typed_batch, 
pool));
+        PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<arrow::Buffer> data,
+                               DeepCopyDataBufferForString(typed_batch, pool));
         auto builder = std::make_shared<UnPooledBinaryBuilder>(type, pool);
         builder->SetData(data);
         builder->SetOffsets(offsets);
diff --git a/src/paimon/format/orc/orc_adapter_test.cpp 
b/src/paimon/format/orc/orc_adapter_test.cpp
index 620707c7..d876aebf 100644
--- a/src/paimon/format/orc/orc_adapter_test.cpp
+++ b/src/paimon/format/orc/orc_adapter_test.cpp
@@ -495,6 +495,26 @@ TEST_P(OrcAdapterTest, TestDeepCopyWithString) {
     ASSERT_TRUE(src_array->Equals(converted_array));
 }
 
+TEST_F(OrcAdapterTest, TestDeepCopyStringExceedsOffsetLimit) {
+    constexpr uint64_t kRowCount = 2048;
+    std::string value(1024 * 1024, 'x');
+    ::orc::StringVectorBatch batch(kRowCount, *::orc::getDefaultPool());
+    batch.numElements = kRowCount;
+    batch.hasNulls = false;
+    // Model decoded dictionary rows sharing one string, with no owned blob.
+    // Expanding all rows would require 2 GiB, exceeding the 32-bit offset 
limit.
+    for (uint64_t i = 0; i < kRowCount; ++i) {
+        batch.data[i] = value.data();
+        batch.length[i] = value.size();
+    }
+    ASSERT_EQ(batch.blob.size(), 0);
+
+    arrow::ProxyMemoryPool pool(arrow::default_memory_pool());
+    ASSERT_NOK_WITH_MSG(OrcAdapter::AppendBatch(arrow::utf8(), &batch, &pool),
+                        "total length is out-of-bounds");
+    ASSERT_EQ(pool.total_bytes_allocated(), 0);
+}
+
 TEST_P(OrcAdapterTest, TestComplexTypeShallowCopyWithBlob) {
     // when disable_dict, meet condition1 in MakeOrcBackedBinaryBuilder, use 
shallow copy with valid
     // blob
diff --git a/test/inte/write_and_read_inte_test.cpp 
b/test/inte/write_and_read_inte_test.cpp
index c291351c..a9a12b66 100644
--- a/test/inte/write_and_read_inte_test.cpp
+++ b/test/inte/write_and_read_inte_test.cpp
@@ -376,6 +376,93 @@ TEST_P(WriteAndReadInteTest, TestAppendSimple) {
     ASSERT_TRUE(success);
 }
 
+TEST_P(WriteAndReadInteTest, TestOrcDictionaryStringExceedsOffsetLimit) {
+    auto [file_format, file_system] = GetParam();
+    if (file_format != "orc" || file_system != "local") {
+        GTEST_SKIP() << "Requires ORC on the local file system";
+    }
+    constexpr int32_t kRowCount = 2048;
+    constexpr int32_t kSmallBatchSize = 8;
+    const std::string value(1024 * 1024, 'x');
+    arrow::FieldVector fields = {arrow::field("value", arrow::utf8())};
+    std::map<std::string, std::string> options = {
+        {Options::FILE_FORMAT, file_format},
+        {Options::FILE_SYSTEM, file_system},
+        {Options::BUCKET, "-1"},
+        {Options::TARGET_FILE_SIZE, "4 gb"},
+        {"orc.dictionary-key-size-threshold", "1"},
+        {"orc.read.enable-lazy-decoding", "false"},
+    };
+    ASSERT_OK_AND_ASSIGN(auto helper,
+                         TestHelper::Create(test_dir_, arrow::schema(fields), 
/*partition_keys=*/{},
+                                            /*primary_keys=*/{}, options,
+                                            /*is_streaming_mode=*/false));
+
+    // Reuse a small Arrow batch: the ORC dictionary stores just one 1 MiB 
value,
+    // but decoding all 2048 rows in one read batch would require 2 GiB of 
string data.
+    arrow::StringBuilder builder;
+    for (int32_t i = 0; i < kSmallBatchSize; ++i) {
+        ASSERT_TRUE(builder.Append(value).ok());
+    }
+    std::shared_ptr<arrow::Array> values;
+    ASSERT_TRUE(builder.Finish(&values).ok());
+    auto array_result = arrow::StructArray::Make({values}, fields);
+    ASSERT_TRUE(array_result.ok()) << array_result.status().ToString();
+    auto array = array_result.ValueOrDie();
+    std::vector<std::unique_ptr<RecordBatch>> batches;
+    for (int32_t i = 0; i < kRowCount; i += kSmallBatchSize) {
+        ArrowArray c_array;
+        ASSERT_TRUE(arrow::ExportArray(*array, &c_array).ok());
+        RecordBatchBuilder batch_builder(&c_array);
+        ASSERT_OK_AND_ASSIGN(auto batch, batch_builder.SetBucket(0).Finish());
+        batches.push_back(std::move(batch));
+    }
+    ASSERT_OK(helper->WriteAndCommit(std::move(batches), 
/*commit_identifier=*/0,
+                                     
/*expected_commit_messages=*/std::nullopt));
+    ASSERT_OK_AND_ASSIGN(auto splits,
+                         helper->NewScan(StartupMode::LatestFull(), 
/*snapshot_id=*/std::nullopt));
+    ASSERT_EQ(splits.size(), 1);
+
+    auto create_reader = [&](int32_t batch_size) -> 
Result<std::unique_ptr<BatchReader>> {
+        ReadContextBuilder read_context_builder(helper->TablePath());
+        PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<ReadContext> read_context,
+                               read_context_builder.SetOptions(options)
+                                   .AddOption(Options::READ_BATCH_SIZE, 
std::to_string(batch_size))
+                                   .SetReadFieldNames({"value"})
+                                   .Finish());
+        PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<TableRead> table_read,
+                               TableRead::Create(std::move(read_context)));
+        return table_read->CreateReader(splits);
+    };
+    ASSERT_OK_AND_ASSIGN(auto reader, create_reader(kRowCount));
+    ASSERT_NOK_WITH_MSG(reader->NextBatch(), "total length is out-of-bounds");
+    reader.reset();
+
+    // The same committed table remains readable with batches below the offset 
limit.
+    ASSERT_OK_AND_ASSIGN(reader, create_reader(kSmallBatchSize));
+    int64_t rows_read = 0;
+    while (true) {
+        ASSERT_OK_AND_ASSIGN(auto batch, reader->NextBatch());
+        if (BatchReader::IsEofBatch(batch)) {
+            break;
+        }
+        auto import_result = arrow::ImportArray(batch.first.get(), 
batch.second.get());
+        ASSERT_TRUE(import_result.ok()) << import_result.status().ToString();
+        auto result = import_result.ValueOrDie();
+        ASSERT_TRUE(result->ValidateFull().ok());
+        auto struct_array = checked_pointer_cast<arrow::StructArray>(result);
+        auto strings =
+            
checked_pointer_cast<arrow::StringArray>(struct_array->GetFieldByName("value"));
+        ASSERT_EQ(strings->length(), kSmallBatchSize);
+        ASSERT_EQ(strings->null_count(), 0);
+        for (int64_t i = 0; i < strings->length(); ++i) {
+            ASSERT_EQ(strings->GetView(i), value);
+        }
+        rows_read += strings->length();
+    }
+    ASSERT_EQ(rows_read, kRowCount);
+}
+
 TEST_P(WriteAndReadInteTest, TestAppendReadWithNestedPredicateAcrossBatches) {
     auto [file_format, file_system] = GetParam();
     arrow::FieldVector fields = {

Reply via email to