From 5f83850b301c8d498534fd9ae2e021ed4c7575ed Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Fri, 31 Mar 2017 09:53:32 -0400 Subject: [PATCH 1/2] Ensure that passing chunk_size=0 when writing Parquet file does not enter infinite loop Change-Id: I16a9d71002d1160d4cb795e2c4b27c3e6b9a4039 --- python/pyarrow/_parquet.pyx | 6 ++++++ python/pyarrow/tests/test_parquet.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/python/pyarrow/_parquet.pyx b/python/pyarrow/_parquet.pyx index 8e67da9f75a6..659e87530fb3 100644 --- a/python/pyarrow/_parquet.pyx +++ b/python/pyarrow/_parquet.pyx @@ -468,6 +468,9 @@ cdef ParquetCompression compression_from_name(object name): return ParquetCompression_UNCOMPRESSED +cdef int MIN_ROW_GROUP_SIZE = 1000 + + cdef class ParquetWriter: cdef: shared_ptr[WriterProperties] properties @@ -540,8 +543,11 @@ cdef class ParquetWriter: if row_group_size is None: row_group_size = ctable.num_rows() + elif row_group_size == 0: + row_group_size = MIN_ROW_GROUP_SIZE cdef int c_row_group_size = row_group_size + with nogil: check_status(WriteTable(deref(ctable), self.allocator, self.sink, c_row_group_size, diff --git a/python/pyarrow/tests/test_parquet.py b/python/pyarrow/tests/test_parquet.py index fc32b9fac8b9..e507035ef6e0 100644 --- a/python/pyarrow/tests/test_parquet.py +++ b/python/pyarrow/tests/test_parquet.py @@ -365,6 +365,20 @@ def test_multithreaded_read(): assert table1.equals(table2) +@parquet +def test_min_chunksize(): + data = pd.DataFrame([np.arange(4)], columns=['A', 'B', 'C', 'D']) + table = pa.Table.from_pandas(data.reset_index()) + + buf = io.BytesIO() + pq.write_table(table, buf, chunk_size=0) + + buf.seek(0) + result = pq.read_table(buf) + + assert result.equals(table) + + @parquet def test_pass_separate_metadata(): # ARROW-471 From f9387035516dd0a168b591084910c6fb3fa4f918 Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Sat, 1 Apr 2017 00:25:19 -0400 Subject: [PATCH 2/2] Raise if row group size is 0, use default if -1 Change-Id: I650a13ebcf93f4738ddfc825a2e110f41adee138 --- python/pyarrow/_parquet.pyx | 7 ++----- python/pyarrow/parquet.py | 2 +- python/pyarrow/tests/test_parquet.py | 5 ++++- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/python/pyarrow/_parquet.pyx b/python/pyarrow/_parquet.pyx index 659e87530fb3..c4cbd28e85da 100644 --- a/python/pyarrow/_parquet.pyx +++ b/python/pyarrow/_parquet.pyx @@ -468,9 +468,6 @@ cdef ParquetCompression compression_from_name(object name): return ParquetCompression_UNCOMPRESSED -cdef int MIN_ROW_GROUP_SIZE = 1000 - - cdef class ParquetWriter: cdef: shared_ptr[WriterProperties] properties @@ -541,10 +538,10 @@ cdef class ParquetWriter: def write_table(self, Table table, row_group_size=None): cdef CTable* ctable = table.table - if row_group_size is None: + if row_group_size is None or row_group_size == -1: row_group_size = ctable.num_rows() elif row_group_size == 0: - row_group_size = MIN_ROW_GROUP_SIZE + raise ValueError('Row group size cannot be 0') cdef int c_row_group_size = row_group_size diff --git a/python/pyarrow/parquet.py b/python/pyarrow/parquet.py index fa96f9569801..2985316f35f0 100644 --- a/python/pyarrow/parquet.py +++ b/python/pyarrow/parquet.py @@ -187,7 +187,7 @@ def write_table(table, sink, chunk_size=None, version='1.0', ---------- table : pyarrow.Table sink: string or pyarrow.io.NativeFile - chunk_size : int + chunk_size : int, default None The maximum number of rows in each Parquet RowGroup. As a default, we will write a single RowGroup per file. version : {"1.0", "2.0"}, default "1.0" diff --git a/python/pyarrow/tests/test_parquet.py b/python/pyarrow/tests/test_parquet.py index e507035ef6e0..b8b2800259ca 100644 --- a/python/pyarrow/tests/test_parquet.py +++ b/python/pyarrow/tests/test_parquet.py @@ -371,13 +371,16 @@ def test_min_chunksize(): table = pa.Table.from_pandas(data.reset_index()) buf = io.BytesIO() - pq.write_table(table, buf, chunk_size=0) + pq.write_table(table, buf, chunk_size=-1) buf.seek(0) result = pq.read_table(buf) assert result.equals(table) + with pytest.raises(ValueError): + pq.write_table(table, buf, chunk_size=0) + @parquet def test_pass_separate_metadata():