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

yiguolei pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.2 by this push:
     new 5ed458fc6d6 branch-4.2: [fix](be) Prepare Parquet null maps before 
logical conversion (#70438)
5ed458fc6d6 is described below

commit 5ed458fc6d69186ab41c92c97d95f59edeb7c9d6
Author: Socrates <[email protected]>
AuthorDate: Fri Dec 4 16:03:04 2026 +0800

    branch-4.2: [fix](be) Prepare Parquet null maps before logical conversion 
(#70438)
    
    ### What problem does this PR solve?
    
    Issue Number: None
    
    Related PR: #69448 (backport to branch-4.2)
    
    PR Type: Bugfix
    
    Problem Summary:
    
    Backport the merged Parquet conversion fix from #69448. When a
    mixed-schema Parquet scan infers BIGINT and then reads an invalid string
    such as "xx", the logical converter writes a failed-cast NULL before the
    destination null map has allocated entries, which can crash the BE.
    
    Copy the appended source null-map slice into the destination before
    logical conversion. Preserve the existing destination prefix and source
    NULLs, allow the converter to mark additional failed casts, and retain
    copy-on-write isolation. Non-nullable conversion errors stay unchanged.
    
    ### Details
    
    - **Frequency:** Triggered by nullable Parquet schema conversions that
    produce NULL, including invalid strings and numeric overflow.
    - **Root cause:** The destination null map was aligned after logical
    conversion, while converters can write NULL flags during conversion.
    - **Why existing tests missed it:** The conversion boundary lacked
    coverage for failed nullable casts into empty destinations, append
    batches and shared column aliases.
    - **Fix:** Move the existing null-map preparation before the converter
    call; no new production conditional, protocol or storage-format changes.
    - **Other paths and branches:** Includes both BYTE_ARRAY and
    FIXED_LEN_BYTE_ARRAY unit coverage. The existing S3 TVF suite compares
    complete ordered File Scanner V1/V2 results over a BIGINT seed plus 4096
    STRING rows, using 1024-row batches and non-strict casts. Session
    settings are restored. This PR targets branch-4.2.
    
    The backport preserves branch-local tests and inserts only the three new
    source unit tests at their available insertion point. The production and
    S3 regression changes match the merged source PR.
    
    ### Testing
    
    Passed locally on branch-4.2: clang-format 16.0.6 checks for both
    changed C++ files, `git diff --cached --check`, Groovy syntax/AST
    conversion, and checks that the new unit-test block matches the merged
    source PR and all merge conflicts are resolved.
    
    Full Groovy semantic compilation was blocked by the host Java 25 /
    cached Groovy 4.0.19 combination (unsupported class-file major version
    69); the syntax/AST conversion check passed. No Doris compilation, BE
    unit-test execution or runtime regression was performed. CI test
    pipelines are deferred at the author's request. No submodules were
    initialized for this work.
    
    ### Notes for Reviewers
    
    This is a backport of master commit
    `1bb35373ee076af701e9070d472f7ee47073b565` from #69448. Keep this PR in
    draft while pipeline testing is deferred. The commit includes a CI skip
    directive, and no test-triggering comments were posted.
    
    ### Release note
    
    Fix a BE crash when Parquet schema conversion produces NULL for an
    invalid value.
    
    ### Check List (For Author)
    
    - Test:
        - [ ] Regression test executed
        - [ ] Unit Test executed
    - [x] Manual/static checks: clang-format 16, whitespace, Groovy
    syntax/AST conversion, source-patch and conflict checks.
    - Behavior changed:
        - [ ] No.
    - [x] Yes. Failed nullable conversions produce NULL without accessing an
    unallocated null map.
    - Does this need documentation?
        - [x] No.
        - [ ] Yes.
    - Pre-submission status: Partially verified: compilation, UT, sanitizer
    and runtime regression validation deferred.
    
    ### Check List (For Reviewer who merge this PR)
    
    - [ ] Confirm the release note
    - [ ] Confirm test cases
    - [ ] Confirm document
    - [ ] Add branch pick label
    
    ### AI Disclosure
    
    - Tool: Codex (GPT-6)
    - Generated: Backport conflict resolution and this description.
    - Human review: Pending.
---
 be/src/format/parquet/parquet_column_convert.h     |  10 +-
 .../format/parquet/parquet_column_convert_test.cpp | 114 +++++++++++++++++++++
 .../external_table_p0/tvf/test_s3_tvf.groovy       |  42 ++++++++
 3 files changed, 162 insertions(+), 4 deletions(-)

diff --git a/be/src/format/parquet/parquet_column_convert.h 
b/be/src/format/parquet/parquet_column_convert.h
index 6c70d787b31..5c2d1e9928c 100644
--- a/be/src/format/parquet/parquet_column_convert.h
+++ b/be/src/format/parquet/parquet_column_convert.h
@@ -290,14 +290,16 @@ public:
                        get_appended_null_map_start(src_physical_col, 
src_new_rows));
 
         dst_logical_col = IColumn::mutate(std::move(dst_logical_col));
-        const size_t dst_old_rows = 
get_mutable_inner_column_size(dst_logical_col);
         const size_t dst_old_null_map_size =
                 get_null_map_size_or_inner_column_size(dst_logical_col);
+        const size_t rows_to_convert = 
get_mutable_inner_column_size(src_logical_column);
+        // Logical converters append all source rows and may mark failed casts 
as NULL (e.g.
+        // "xx" -> BIGINT). Copy source nulls first so those writes have 
allocated entries and
+        // are not overwritten after conversion. Keep any existing destination 
prefix intact.
+        align_null_map(src_logical_column, dst_logical_col, 
dst_old_null_map_size, rows_to_convert,
+                       get_appended_null_map_start(src_logical_column, 
rows_to_convert));
         auto converted_column = dst_logical_col->assert_mutable();
         RETURN_IF_ERROR(_logical_converter->convert(src_logical_column, 
converted_column));
-        const size_t dst_new_rows = 
get_mutable_inner_column_size(dst_logical_col) - dst_old_rows;
-        align_null_map(src_logical_column, dst_logical_col, 
dst_old_null_map_size, dst_new_rows,
-                       get_appended_null_map_start(src_logical_column, 
dst_new_rows));
         return Status::OK();
     }
 
diff --git a/be/test/format/parquet/parquet_column_convert_test.cpp 
b/be/test/format/parquet/parquet_column_convert_test.cpp
index 49758c28172..e3da0154a5c 100644
--- a/be/test/format/parquet/parquet_column_convert_test.cpp
+++ b/be/test/format/parquet/parquet_column_convert_test.cpp
@@ -32,6 +32,120 @@
 
 namespace doris::parquet {
 
+// Exercise both a direct BYTE_ARRAY source and a physical-to-logical 
temporary for
+// FIXED_LEN_BYTE_ARRAY. Both must prepare destination nulls before string 
casts.
+TEST(ParquetColumnConvertTest, StringCastFailuresPreserveNullsAcrossBatches) {
+    for (const auto physical_type :
+         {tparquet::Type::BYTE_ARRAY, tparquet::Type::FIXED_LEN_BYTE_ARRAY}) {
+        for (const bool nullable_source : {false, true}) {
+            SCOPED_TRACE(testing::Message() << "physical_type=" << 
physical_type
+                                            << ", nullable_source=" << 
nullable_source);
+            FieldSchema field;
+            field.parquet_schema.__set_type(physical_type);
+            field.parquet_schema.__set_type_length(2);
+            field.data_type =
+                    DataTypeFactory::instance().create_data_type(TYPE_STRING, 
nullable_source);
+            const auto dst_type = 
DataTypeFactory::instance().create_data_type(TYPE_BIGINT, true);
+            auto converter = PhysicalToLogicalConverter::get_converter(&field, 
field.data_type,
+                                                                       
dst_type, nullptr);
+            ASSERT_TRUE(converter->support()) << converter->get_error_msg();
+            ColumnPtr dst = dst_type->create_column();
+            const std::vector<std::string> values {"xx", "05", "07", "09"};
+            const std::vector<uint8_t> expected_nulls {1, 0, 0, 0};
+            for (size_t batch = 0; batch < 2; ++batch) {
+                // Retain an alias to ensure preparing the null map respects 
COW and does not
+                // modify an existing batch through another owner.
+                ColumnPtr dst_alias = dst;
+                MutableColumnPtr nested;
+                if (physical_type == tparquet::Type::BYTE_ARRAY) {
+                    nested = ColumnString::create();
+                } else {
+                    nested = ColumnFixedLengthObject::create(2);
+                }
+                auto nulls = ColumnUInt8::create();
+                for (size_t i = 0; i < values.size(); ++i) {
+                    nested->insert_data(values[i].data(), values[i].size());
+                    nulls->insert_value(i == 2);
+                }
+                ColumnPtr src;
+                if (nullable_source) {
+                    src = ColumnNullable::create(std::move(nested), 
std::move(nulls));
+                } else {
+                    src = std::move(nested);
+                }
+                const ColumnPtr src_alias = src;
+                ASSERT_TRUE(converter->convert(src, field.data_type, dst_type, 
dst, false).ok());
+                const auto& nullable = assert_cast<const 
ColumnNullable&>(*dst);
+                const auto& numbers = assert_cast<const 
ColumnInt64&>(nullable.get_nested_column());
+                ASSERT_EQ(nullable.get_null_map_data().size(), (batch + 1) * 
values.size());
+                ASSERT_EQ(numbers.size(), nullable.get_null_map_data().size());
+                for (size_t i = 0; i < numbers.size(); ++i) {
+                    const size_t value_index = i % values.size();
+                    EXPECT_EQ(nullable.get_null_map_data()[i],
+                              expected_nulls[value_index] || (nullable_source 
&& value_index == 2));
+                    if (!nullable.get_null_map_data()[i]) {
+                        EXPECT_EQ(numbers.get_data()[i], 
std::stoi(values[value_index]));
+                    }
+                }
+                EXPECT_EQ(dst_alias->size(), batch * values.size());
+                const auto& original = assert_cast<const 
ColumnNullable&>(*dst_alias);
+                EXPECT_EQ(original.get_nested_column().size(), batch * 
values.size());
+                if (nullable_source) {
+                    const auto& source = assert_cast<const 
ColumnNullable&>(*src_alias);
+                    EXPECT_EQ(source.get_null_map_data()[0], 0);
+                    EXPECT_EQ(source.get_null_map_data()[2], 1);
+                }
+            }
+        }
+    }
+}
+
+TEST(ParquetColumnConvertTest, InvalidStringToNonNullableBigIntReturnsError) {
+    FieldSchema field;
+    field.parquet_schema.__set_type(tparquet::Type::BYTE_ARRAY);
+    field.data_type = 
DataTypeFactory::instance().create_data_type(TYPE_STRING, false);
+    const auto dst_type = 
DataTypeFactory::instance().create_data_type(TYPE_BIGINT, false);
+    auto converter =
+            PhysicalToLogicalConverter::get_converter(&field, field.data_type, 
dst_type, nullptr);
+    ASSERT_TRUE(converter->support()) << converter->get_error_msg();
+    auto strings = ColumnString::create();
+    strings->insert_data("xx", 2);
+    ColumnPtr src = std::move(strings);
+    ColumnPtr dst = dst_type->create_column();
+    auto status = converter->convert(src, field.data_type, dst_type, dst, 
false);
+    EXPECT_FALSE(status.ok());
+    EXPECT_NE(status.to_string().find("Failed to cast"), std::string::npos);
+}
+
+TEST(ParquetColumnConvertTest, NumericOverflowMarksNullableDestination) {
+    FieldSchema field;
+    field.parquet_schema.__set_type(tparquet::Type::INT64);
+    field.data_type = 
DataTypeFactory::instance().create_data_type(TYPE_BIGINT, true);
+    const auto dst_type = 
DataTypeFactory::instance().create_data_type(TYPE_FLOAT, true);
+    auto converter =
+            PhysicalToLogicalConverter::get_converter(&field, field.data_type, 
dst_type, nullptr);
+    ASSERT_TRUE(converter->support()) << converter->get_error_msg();
+    auto values = ColumnInt64::create();
+    values->insert_value(1LL << 40);
+    values->insert_value(7);
+    values->insert_value(9);
+    auto nulls = ColumnUInt8::create();
+    nulls->insert_value(0);
+    nulls->insert_value(0);
+    nulls->insert_value(1);
+    ColumnPtr src = ColumnNullable::create(std::move(values), 
std::move(nulls));
+    ColumnPtr dst = dst_type->create_column();
+    ASSERT_TRUE(converter->convert(src, field.data_type, dst_type, dst, 
false).ok());
+    const auto& nullable = assert_cast<const ColumnNullable&>(*dst);
+    ASSERT_EQ(nullable.get_null_map_data().size(), 3);
+    ASSERT_EQ(nullable.get_nested_column().size(), 3);
+    EXPECT_EQ(nullable.get_null_map_data()[0], 1);
+    EXPECT_EQ(nullable.get_null_map_data()[1], 0);
+    EXPECT_EQ(nullable.get_null_map_data()[2], 1);
+    EXPECT_FLOAT_EQ(assert_cast<const 
ColumnFloat32&>(nullable.get_nested_column()).get_data()[1],
+                    7.0F);
+}
+
 TEST(ParquetColumnConvertTest, RejectInvalidUuidPhysicalWidth) {
     FieldSchema field;
     field.parquet_schema.__set_type(tparquet::Type::FIXED_LEN_BYTE_ARRAY);
diff --git a/regression-test/suites/external_table_p0/tvf/test_s3_tvf.groovy 
b/regression-test/suites/external_table_p0/tvf/test_s3_tvf.groovy
index df5085f8f2a..e377fc2e3c7 100644
--- a/regression-test/suites/external_table_p0/tvf/test_s3_tvf.groovy
+++ b/regression-test/suites/external_table_p0/tvf/test_s3_tvf.groovy
@@ -169,4 +169,46 @@ suite("test_s3_tvf", "p0,external,external_docker") {
     } finally {
     }
 
+    // Exercise schema conversion with both readers, including failed casts, 
source NULLs and
+    // multiple batches. The BIGINT seed must be listed before the STRING file 
for TVF inference.
+    String castPrefix = 
"s3://${getS3BucketName()}/regression/parquet_invalid_string_cast/${UUID.randomUUID()}"
+    String castStorageProperties = """
+        "s3.endpoint" = "${s3_endpoint}",
+        "s3.region" = "${region}",
+        "s3.access_key" = "${ak}",
+        "s3.secret_key" = "${sk}"
+    """
+    sql """SELECT CAST(1 AS BIGINT) AS id, CAST('seed' AS STRING) AS name
+           INTO OUTFILE "${castPrefix}/00_seed/part_" FORMAT AS parquet
+           PROPERTIES (${castStorageProperties})"""
+    sql """SELECT CAST(CASE number % 4
+                          WHEN 0 THEN 'xx'
+                          WHEN 1 THEN '5'
+                          WHEN 2 THEN NULL
+                          ELSE '9' END AS STRING) AS id,
+                  CAST(number AS STRING) AS name
+           FROM numbers("number" = "4096")
+           INTO OUTFILE "${castPrefix}/01_strings/part_" FORMAT AS parquet
+           PROPERTIES (${castStorageProperties})"""
+
+    def previousCastSettings = sql "SELECT @@enable_file_scanner_v2, 
@@batch_size, @@enable_strict_cast"
+    try {
+        sql "SET batch_size=1024"
+        sql "SET enable_strict_cast=false"
+        // Materialize every value; COUNT(*) could use footer statistics and 
skip conversion.
+        // Compare the actual reader outputs rather than hardcoding expected 
rows in Groovy.
+        def castResults = [false, true].collect { scannerV2 ->
+            sql "SET enable_file_scanner_v2=${scannerV2}"
+            sql """SELECT id, name FROM S3(
+                       "uri" = "${castPrefix}/{00_seed,01_strings}/*.parquet",
+                       "format" = "parquet",
+                       ${castStorageProperties}) ORDER BY name"""
+        }
+        assertEquals(castResults[0], castResults[1],
+                "V1 and V2 must preserve the same values and NULLs across 
mixed-schema batches")
+    } finally {
+        sql "SET enable_file_scanner_v2=${previousCastSettings[0][0]}"
+        sql "SET batch_size=${previousCastSettings[0][1]}"
+        sql "SET enable_strict_cast=${previousCastSettings[0][2]}"
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to