You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "ArnavBalyan (via GitHub)" <gi...@apache.org> on 2026/05/02 15:35:51 UTC

[PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

ArnavBalyan opened a new pull request, #49909:
URL: https://github.com/apache/arrow/pull/49909

   ### Rationale for this change
      - Parquet spec requires that `REQUIRED` columns can't encode nulls (max_definition_level = 0 always has null_count = 0).
      - When this is violated by a writer (eg. parquet-mr with disabled validating record consumer), the reader fails with "Unexpected end of stream" during decode.
      - Ensure that this issue can be detected eagerly by the reader, and show correct error. We should consider mandating the validating consumer in upstream to ensure it cannot accidentally write such cases.
      - Closes #47662.
   
   ### What changes are included in this PR?
    - Eagerly detect corrupt file and surface relevant error. 
   
   ### Are these changes tested?
    - Yes
   
   ### Are there any user-facing changes?
    - Yes


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "ArnavBalyan (via GitHub)" <gi...@apache.org>.
ArnavBalyan commented on code in PR #49909:
URL: https://github.com/apache/arrow/pull/49909#discussion_r3189160017


##########
cpp/src/parquet/metadata.cc:
##########
@@ -307,6 +307,19 @@ class ColumnChunkMetaData::ColumnChunkMetaDataImpl {
     possible_encoded_stats_ = nullptr;
     possible_geo_stats_ = nullptr;
     InitKeyValueMetadata();
+
+    // Per the Parquet specification, a column with max_definition_level == 0
+    // is required at every level and therefore cannot contain null values.
+    // Reject inconsistent metadata from writers violating this invariant.
+    if (descr_->max_definition_level() == 0 && column_metadata_->__isset.statistics &&
+        column_metadata_->statistics.__isset.null_count &&
+        column_metadata_->statistics.null_count > 0) {
+      std::stringstream ss;
+      ss << "Malformed Parquet file: column '" << descr_->path()->ToDotString()
+         << "' has max_definition_level == 0 but statistics report null_count="
+         << column_metadata_->statistics.null_count;
+      throw ParquetException(ss.str());

Review Comment:
   Looks like some legacy versions can get it through with disabled validating record consumer



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "ArnavBalyan (via GitHub)" <gi...@apache.org>.
ArnavBalyan commented on code in PR #49909:
URL: https://github.com/apache/arrow/pull/49909#discussion_r3189168462


##########
cpp/src/parquet/metadata.cc:
##########
@@ -307,6 +307,19 @@ class ColumnChunkMetaData::ColumnChunkMetaDataImpl {
     possible_encoded_stats_ = nullptr;
     possible_geo_stats_ = nullptr;
     InitKeyValueMetadata();
+
+    // Per the Parquet specification, a column with max_definition_level == 0
+    // is required at every level and therefore cannot contain null values.
+    // Reject inconsistent metadata from writers violating this invariant.
+    if (descr_->max_definition_level() == 0 && column_metadata_->__isset.statistics &&
+        column_metadata_->statistics.__isset.null_count &&
+        column_metadata_->statistics.null_count > 0) {
+      std::stringstream ss;
+      ss << "Malformed Parquet file: column '" << descr_->path()->ToDotString()
+         << "' has max_definition_level == 0 but statistics report null_count="
+         << column_metadata_->statistics.null_count;
+      throw ParquetException(ss.str());

Review Comment:
   > unset the null_count field when building the statistics
   Yeah agreed, thanks for sharing, updated to maintain parity with java



##########
cpp/src/parquet/metadata.cc:
##########
@@ -307,6 +307,19 @@ class ColumnChunkMetaData::ColumnChunkMetaDataImpl {
     possible_encoded_stats_ = nullptr;
     possible_geo_stats_ = nullptr;
     InitKeyValueMetadata();
+
+    // Per the Parquet specification, a column with max_definition_level == 0
+    // is required at every level and therefore cannot contain null values.
+    // Reject inconsistent metadata from writers violating this invariant.
+    if (descr_->max_definition_level() == 0 && column_metadata_->__isset.statistics &&
+        column_metadata_->statistics.__isset.null_count &&
+        column_metadata_->statistics.null_count > 0) {
+      std::stringstream ss;
+      ss << "Malformed Parquet file: column '" << descr_->path()->ToDotString()
+         << "' has max_definition_level == 0 but statistics report null_count="
+         << column_metadata_->statistics.null_count;
+      throw ParquetException(ss.str());

Review Comment:
   > unset the null_count field when building the statistics
   
   Yeah agreed, thanks for sharing, updated to maintain parity with java



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #49909:
URL: https://github.com/apache/arrow/pull/49909#issuecomment-4378387746

   @wgtmac Could you please take a look?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "ArnavBalyan (via GitHub)" <gi...@apache.org>.
ArnavBalyan commented on PR #49909:
URL: https://github.com/apache/arrow/pull/49909#issuecomment-4364170772

   cc @pitrou could you ptal thanks!


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "ArnavBalyan (via GitHub)" <gi...@apache.org>.
ArnavBalyan commented on PR #49909:
URL: https://github.com/apache/arrow/pull/49909#issuecomment-4378500588

   updated thanks


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #49909:
URL: https://github.com/apache/arrow/pull/49909#discussion_r3207472080


##########
cpp/src/parquet/metadata.cc:
##########
@@ -307,6 +307,20 @@ class ColumnChunkMetaData::ColumnChunkMetaDataImpl {
     possible_encoded_stats_ = nullptr;
     possible_geo_stats_ = nullptr;
     InitKeyValueMetadata();
+
+    // A column with max_definition_level == 0 cannot contain null values, so
+    // a non-zero null_count in its statistics is contradictory. Drop the bad
+    // value so the file remains readable.
+    if (descr_->max_definition_level() == 0 && column_metadata_->__isset.statistics &&
+        column_metadata_->statistics.__isset.null_count &&
+        column_metadata_->statistics.null_count > 0) {
+      if (column_metadata_ != &decrypted_metadata_) {

Review Comment:
   Do we really need this check? A copy is alway performed in an unencrypted case. Is it better to remove them?



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #49909:
URL: https://github.com/apache/arrow/pull/49909#discussion_r3189088269


##########
cpp/src/parquet/metadata.cc:
##########
@@ -307,6 +307,19 @@ class ColumnChunkMetaData::ColumnChunkMetaDataImpl {
     possible_encoded_stats_ = nullptr;
     possible_geo_stats_ = nullptr;
     InitKeyValueMetadata();
+
+    // Per the Parquet specification, a column with max_definition_level == 0
+    // is required at every level and therefore cannot contain null values.
+    // Reject inconsistent metadata from writers violating this invariant.
+    if (descr_->max_definition_level() == 0 && column_metadata_->__isset.statistics &&
+        column_metadata_->statistics.__isset.null_count &&
+        column_metadata_->statistics.null_count > 0) {
+      std::stringstream ss;
+      ss << "Malformed Parquet file: column '" << descr_->path()->ToDotString()
+         << "' has max_definition_level == 0 but statistics report null_count="
+         << column_metadata_->statistics.null_count;
+      throw ParquetException(ss.str());

Review Comment:
   Instead of throwing an exception here, is it better to unset the `null_count` field when building the statistics?
   
   Just FYI that I recently reviewed https://github.com/apache/parquet-java/pull/3458 which has dealt with a similar case. From downstream users' standpoint, it is a bad experience if the Parquet file cannot be read simply because of malformed statistics.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #49909:
URL: https://github.com/apache/arrow/pull/49909#issuecomment-4364148918

   :warning: GitHub issue #47662 **has been automatically assigned in GitHub** to PR creator.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "ArnavBalyan (via GitHub)" <gi...@apache.org>.
ArnavBalyan commented on code in PR #49909:
URL: https://github.com/apache/arrow/pull/49909#discussion_r3187791355


##########
cpp/src/parquet/metadata_test.cc:
##########
@@ -427,6 +427,38 @@ TEST(Metadata, TestReadPageIndex) {
   }
 }
 
+// Regression test: a column with max_definition_level == 0 cannot encode
+// nulls, so any positive null_count in its statistics indicates a malformed
+// file. The reader should reject it when the metadata is loaded.
+TEST(Metadata, RejectsRequiredColumnWithNonZeroNullCount) {
+  schema::NodeVector fields;
+  fields.push_back(schema::Int32("required_col", Repetition::REQUIRED));
+  auto schema_node = std::static_pointer_cast<schema::GroupNode>(
+      schema::GroupNode::Make("schema", Repetition::REQUIRED, fields));
+
+  SchemaDescriptor schema_descr;
+  schema_descr.Init(schema_node);
+
+  format::ColumnChunk column_chunk;
+  format::ColumnMetaData& column_metadata = column_chunk.meta_data;
+  column_chunk.__isset.meta_data = true;
+
+  column_metadata.type = format::Type::INT32;
+  column_metadata.codec = format::CompressionCodec::UNCOMPRESSED;
+  column_metadata.num_values = 1000;
+  column_metadata.total_uncompressed_size = 4000;
+  column_metadata.total_compressed_size = 4000;
+  column_metadata.data_page_offset = 4;
+  column_metadata.path_in_schema.push_back("required_col");
+
+  column_metadata.statistics.null_count = 105;
+  column_metadata.statistics.__isset.null_count = true;
+  column_metadata.__isset.statistics = true;
+
+  EXPECT_THROW(ColumnChunkMetaData::Make(&column_chunk, schema_descr.Column(0)),
+               ParquetException);

Review Comment:
   updated



##########
cpp/src/parquet/metadata.cc:
##########
@@ -307,6 +307,20 @@ class ColumnChunkMetaData::ColumnChunkMetaDataImpl {
     possible_encoded_stats_ = nullptr;
     possible_geo_stats_ = nullptr;
     InitKeyValueMetadata();
+
+    // Per the Parquet spec, a column with max_definition_level == 0 cannot
+    // have nulls, so null_count must be 0. Reject inconsistent metadata
+    // from writers that skip ValidatingRecordConsumer checks for missing

Review Comment:
   updated



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #49909:
URL: https://github.com/apache/arrow/pull/49909#discussion_r3187684258


##########
cpp/src/parquet/metadata_test.cc:
##########
@@ -427,6 +427,38 @@ TEST(Metadata, TestReadPageIndex) {
   }
 }
 
+// Regression test: a column with max_definition_level == 0 cannot encode
+// nulls, so any positive null_count in its statistics indicates a malformed
+// file. The reader should reject it when the metadata is loaded.
+TEST(Metadata, RejectsRequiredColumnWithNonZeroNullCount) {
+  schema::NodeVector fields;
+  fields.push_back(schema::Int32("required_col", Repetition::REQUIRED));
+  auto schema_node = std::static_pointer_cast<schema::GroupNode>(
+      schema::GroupNode::Make("schema", Repetition::REQUIRED, fields));
+
+  SchemaDescriptor schema_descr;
+  schema_descr.Init(schema_node);
+
+  format::ColumnChunk column_chunk;
+  format::ColumnMetaData& column_metadata = column_chunk.meta_data;
+  column_chunk.__isset.meta_data = true;
+
+  column_metadata.type = format::Type::INT32;
+  column_metadata.codec = format::CompressionCodec::UNCOMPRESSED;
+  column_metadata.num_values = 1000;
+  column_metadata.total_uncompressed_size = 4000;
+  column_metadata.total_compressed_size = 4000;
+  column_metadata.data_page_offset = 4;
+  column_metadata.path_in_schema.push_back("required_col");
+
+  column_metadata.statistics.null_count = 105;
+  column_metadata.statistics.__isset.null_count = true;
+  column_metadata.__isset.statistics = true;
+
+  EXPECT_THROW(ColumnChunkMetaData::Make(&column_chunk, schema_descr.Column(0)),
+               ParquetException);

Review Comment:
   Can we please test something about the exception message? You can use the `EXPECT_THROW_THAT` macro.



##########
cpp/src/parquet/metadata.cc:
##########
@@ -307,6 +307,20 @@ class ColumnChunkMetaData::ColumnChunkMetaDataImpl {
     possible_encoded_stats_ = nullptr;
     possible_geo_stats_ = nullptr;
     InitKeyValueMetadata();
+
+    // Per the Parquet spec, a column with max_definition_level == 0 cannot
+    // have nulls, so null_count must be 0. Reject inconsistent metadata
+    // from writers that skip ValidatingRecordConsumer checks for missing

Review Comment:
   Readers of the Parquet C++ code will not know anything about `ValidatingRecordConsumer`, can we make this comment more generic?



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #49909:
URL: https://github.com/apache/arrow/pull/49909#discussion_r3217836336


##########
cpp/src/parquet/metadata.cc:
##########
@@ -307,6 +307,18 @@ class ColumnChunkMetaData::ColumnChunkMetaDataImpl {
     possible_encoded_stats_ = nullptr;
     possible_geo_stats_ = nullptr;
     InitKeyValueMetadata();
+
+    // A column with max_definition_level == 0 cannot contain null values, so
+    // a non-zero null_count in its statistics is contradictory. Drop the bad
+    // value so the file remains readable.
+    if (descr_->max_definition_level() == 0 && column_metadata_->__isset.statistics &&
+        column_metadata_->statistics.__isset.null_count &&
+        column_metadata_->statistics.null_count > 0) {
+      decrypted_metadata_ = *column_metadata_;
+      column_metadata_ = &decrypted_metadata_;
+      decrypted_metadata_.statistics.null_count = 0;
+      decrypted_metadata_.statistics.__isset.null_count = false;

Review Comment:
   ```suggestion
         column_metadata_->statistics.null_count = 0;
         column_metadata_->statistics.__isset.null_count = false;
   ```
   
   My previous comment was to totally remove lines with `decrypted_metadata_` because it should be in sync with `column_metadata_` automatically if exists.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "ArnavBalyan (via GitHub)" <gi...@apache.org>.
ArnavBalyan commented on PR #49909:
URL: https://github.com/apache/arrow/pull/49909#issuecomment-4404112299

   cc @pitrou @wgtmac do you think anything else should be addressed in this PR


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on code in PR #49909:
URL: https://github.com/apache/arrow/pull/49909#discussion_r3189106523


##########
cpp/src/parquet/metadata.cc:
##########
@@ -307,6 +307,19 @@ class ColumnChunkMetaData::ColumnChunkMetaDataImpl {
     possible_encoded_stats_ = nullptr;
     possible_geo_stats_ = nullptr;
     InitKeyValueMetadata();
+
+    // Per the Parquet specification, a column with max_definition_level == 0
+    // is required at every level and therefore cannot contain null values.
+    // Reject inconsistent metadata from writers violating this invariant.
+    if (descr_->max_definition_level() == 0 && column_metadata_->__isset.statistics &&
+        column_metadata_->statistics.__isset.null_count &&
+        column_metadata_->statistics.null_count > 0) {
+      std::stringstream ss;
+      ss << "Malformed Parquet file: column '" << descr_->path()->ToDotString()
+         << "' has max_definition_level == 0 but statistics report null_count="
+         << column_metadata_->statistics.null_count;
+      throw ParquetException(ss.str());

Review Comment:
   Well, is it possible to get such a malformed file using the regular Parquet writing APIs?



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-47662: [C++][Parquet] Reject metadata with null_count on required column [arrow]

Posted by "ArnavBalyan (via GitHub)" <gi...@apache.org>.
ArnavBalyan commented on code in PR #49909:
URL: https://github.com/apache/arrow/pull/49909#discussion_r3215036052


##########
cpp/src/parquet/metadata.cc:
##########
@@ -307,6 +307,20 @@ class ColumnChunkMetaData::ColumnChunkMetaDataImpl {
     possible_encoded_stats_ = nullptr;
     possible_geo_stats_ = nullptr;
     InitKeyValueMetadata();
+
+    // A column with max_definition_level == 0 cannot contain null values, so
+    // a non-zero null_count in its statistics is contradictory. Drop the bad
+    // value so the file remains readable.
+    if (descr_->max_definition_level() == 0 && column_metadata_->__isset.statistics &&
+        column_metadata_->statistics.__isset.null_count &&
+        column_metadata_->statistics.null_count > 0) {
+      if (column_metadata_ != &decrypted_metadata_) {

Review Comment:
   updated thanks



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org