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

raulcd pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 87eb47ece82 GH-53500: [Python][Parquet] Prevent crashes when comparing 
metadata with None (#55168)
87eb47ece82 is described below

commit 87eb47ece82003145ffff6b6b9b64321af996649
Author: nleerollergh <[email protected]>
AuthorDate: Mon Nov 9 02:32:38 2026 -0800

    GH-53500: [Python][Parquet] Prevent crashes when comparing metadata with 
None (#55168)
    
    ### Rationale for this change
    Five Parquet metadata/schema `equals()` wrappers (`Statistics`, 
`ColumnChunkMetaData`, `RowGroupMetaData`, `ParquetSchema`, `ColumnSchema`) 
accept `None` for their typed `other` argument and then dereference a native 
pointer on it, which can crash the interpreter. This follows the existing 
`FileMetaData.equals()` pattern, which already declares `other not None`.
    
    ### What changes are included in this PR?
    - Require non-null typed arguments in the five affected Cython `equals()` 
methods. `__eq__` already catches the resulting `TypeError` and returns 
`NotImplemented`, so `obj == None` is `False` and `obj != None` is `True`.
    - Add a regression test covering `==`, `!=` and direct `.equals(None)` for 
all five wrappers.
    
    ### Are these changes tested?
    Yes. I added a regression test and ran it locally: it passes with the fix 
and segfaults without it (details in the comment below). `flake8` and 
`cython-lint` pass on the changed files.
    
    See comment for detailed testing evidence.
    
    ### Are there any user-facing changes?
    `obj.equals(None)` now raises `TypeError` instead of risking a crash. `obj 
== None` returns `False`.
    
    **This PR contains a "Critical Fix".** It fixes a bug that causes a crash 
(category c): calling `equals(None)`, or comparing with `==`/`!=` against 
`None`, on `Statistics`, `ColumnChunkMetaData`, `RowGroupMetaData`, 
`ParquetSchema` or `ColumnSchema` could dereference a null native pointer and 
crash the Python interpreter instead of raising an error.
    
    ### Was AI used for this PR?
    
    Yes, I wrote the initial implementation (the `not None` change to the five 
`equals()` methods, following the existing `FileMetaData.equals()` pattern) and 
a first version of the regression test. I then used AI (Claude Code) to review 
and refine it before then structuring the commit message. The AI completed the 
test (added the `!=` and `.equals(None)` `TypeError` assertions and renamed a 
variable that shadowed a builtin) and drafted this PR description.
    
    **PR code and description written by:**
    
    - [x] Human
    - [x] AI
    
    **Reviewed before submission by:**
    
    - [x] Human
    - [x] AI
    - [ ] Not reviewed
    
    * GitHub Issue: #53500
    
    Authored-by: Nick Lee-Roller <[email protected]>
    Signed-off-by: Raúl Cumplido <[email protected]>
---
 python/pyarrow/_parquet.pyx                   | 10 +++++-----
 python/pyarrow/tests/parquet/test_metadata.py | 21 +++++++++++++++++++++
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/python/pyarrow/_parquet.pyx b/python/pyarrow/_parquet.pyx
index 40c504ef3b0..afe6ee99450 100644
--- a/python/pyarrow/_parquet.pyx
+++ b/python/pyarrow/_parquet.pyx
@@ -108,7 +108,7 @@ cdef class Statistics(_Weakrefable):
         except TypeError:
             return NotImplemented
 
-    def equals(self, Statistics other):
+    def equals(self, Statistics other not None):
         """
         Return whether the two column statistics objects are equal.
 
@@ -521,7 +521,7 @@ cdef class ColumnChunkMetaData(_Weakrefable):
         except TypeError:
             return NotImplemented
 
-    def equals(self, ColumnChunkMetaData other):
+    def equals(self, ColumnChunkMetaData other not None):
         """
         Return whether the two column chunk metadata objects are equal.
 
@@ -919,7 +919,7 @@ cdef class RowGroupMetaData(_Weakrefable):
         except TypeError:
             return NotImplemented
 
-    def equals(self, RowGroupMetaData other):
+    def equals(self, RowGroupMetaData other not None):
         """
         Return whether the two row group metadata objects are equal.
 
@@ -1295,7 +1295,7 @@ cdef class ParquetSchema(_Weakrefable):
         except TypeError:
             return NotImplemented
 
-    def equals(self, ParquetSchema other):
+    def equals(self, ParquetSchema other not None):
         """
         Return whether the two schemas are equal.
 
@@ -1350,7 +1350,7 @@ cdef class ColumnSchema(_Weakrefable):
     def __reduce__(self):
         return ColumnSchema, (self.parent, self.index)
 
-    def equals(self, ColumnSchema other):
+    def equals(self, ColumnSchema other not None):
         """
         Return whether the two column schemas are equal.
 
diff --git a/python/pyarrow/tests/parquet/test_metadata.py 
b/python/pyarrow/tests/parquet/test_metadata.py
index 9eee70b125e..817566125fe 100644
--- a/python/pyarrow/tests/parquet/test_metadata.py
+++ b/python/pyarrow/tests/parquet/test_metadata.py
@@ -724,6 +724,27 @@ def test_metadata_equals():
         original_metadata.equals(None)
 
 
+def test_parquet_metadata_equals_none(tempdir):
+    table = pa.table({"a": [1, 2, 3]})
+    path = tempdir / "metadata_equals_none.parquet"
+    pq.write_table(table, path)
+
+    metadata = pq.read_metadata(path)
+    row_group = metadata.row_group(0)
+    column_chunk = row_group.column(0)
+    objects = [
+        column_chunk.statistics,
+        column_chunk,
+        row_group,
+        metadata.schema,
+        metadata.schema[0],
+    ]
+
+    for obj in objects:
+        with pytest.raises(TypeError):
+            obj.equals(None)
+
+
 @pytest.mark.parametrize("t1,t2,expected_error", (
     ({'col1': range(10)}, {'col1': range(10)}, None),
     ({'col1': range(10)}, {'col2': range(10)},

Reply via email to