Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/pyarrow/_parquet.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ cdef extern from "parquet/api/schema.h" namespace "parquet::schema" nogil:

cdef cppclass ColumnPath:
c_string ToDotString()
vector[c_string] ToDotVector()


cdef extern from "parquet/api/schema.h" namespace "parquet" nogil:
Expand Down
29 changes: 24 additions & 5 deletions python/pyarrow/_parquet.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,11 @@ cdef class ParquetReader:
object source
CMemoryPool* allocator
unique_ptr[FileReader] reader
column_idx_map
FileMetaData _metadata

cdef public:
_column_idx_map

def __cinit__(self, MemoryPool memory_pool=None):
self.allocator = maybe_unbox_memory_pool(memory_pool)
self._metadata = None
Expand All @@ -624,6 +626,23 @@ cdef class ParquetReader:
check_status(OpenFile(rd_handle, self.allocator, properties,
c_metadata, &self.reader))

property column_paths:

def __get__(self):
cdef:
FileMetaData container = self.metadata
const CFileMetaData* metadata = container._metadata
vector[c_string] path
int i = 0

paths = []
for i in range(0, metadata.num_columns()):
path = (metadata.schema().Column(i)
.path().get().ToDotVector())
paths.append([frombytes(x) for x in path])

return paths

@property
def metadata(self):
cdef:
Expand Down Expand Up @@ -729,14 +748,14 @@ cdef class ParquetReader:
const CFileMetaData* metadata = container._metadata
int i = 0

if self.column_idx_map is None:
self.column_idx_map = {}
if self._column_idx_map is None:
self._column_idx_map = {}
for i in range(0, metadata.num_columns()):
col_bytes = tobytes(metadata.schema().Column(i)
.path().get().ToDotString())
self.column_idx_map[col_bytes] = i
self._column_idx_map[col_bytes] = i

return self.column_idx_map[tobytes(column_name)]
return self._column_idx_map[tobytes(column_name)]

def read_column(self, int column_index):
cdef:
Expand Down
41 changes: 36 additions & 5 deletions python/pyarrow/parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.

from collections import defaultdict
import os
import inspect
import json
Expand Down Expand Up @@ -54,6 +55,24 @@ def __init__(self, source, metadata=None, common_metadata=None):
self.reader = ParquetReader()
self.reader.open(source, metadata=metadata)
self.common_metadata = common_metadata
self._nested_paths_by_prefix = self._build_nested_paths()

def _build_nested_paths(self):
paths = self.reader.column_paths

result = defaultdict(list)

def _visit_piece(i, key, rest):
result[key].append(i)

if len(rest) > 0:
nested_key = '.'.join((key, rest[0]))
_visit_piece(i, nested_key, rest[1:])

for i, path in enumerate(paths):
_visit_piece(i, path[0], path[1:])

return result

@property
def metadata(self):
Expand All @@ -75,7 +94,9 @@ def read_row_group(self, i, columns=None, nthreads=1,
Parameters
----------
columns: list
If not None, only these columns will be read from the row group.
If not None, only these columns will be read from the row group. A
column name may be a prefix of a nested field, e.g. 'a' will select
'a.b', 'a.c', and 'a.d.e'
nthreads : int, default 1
Number of columns to read in parallel. If > 1, requires that the
underlying file source is threadsafe
Expand All @@ -100,7 +121,9 @@ def read(self, columns=None, nthreads=1, use_pandas_metadata=False):
Parameters
----------
columns: list
If not None, only these columns will be read from the file.
If not None, only these columns will be read from the file. A
column name may be a prefix of a nested field, e.g. 'a' will select
'a.b', 'a.c', and 'a.d.e'
nthreads : int, default 1
Number of columns to read in parallel. If > 1, requires that the
underlying file source is threadsafe
Expand Down Expand Up @@ -143,7 +166,11 @@ def _get_column_indices(self, column_names, use_pandas_metadata=False):
if column_names is None:
return None

indices = list(map(self.reader.column_name_idx, column_names))
indices = []

for name in column_names:
if name in self._nested_paths_by_prefix:
indices.extend(self._nested_paths_by_prefix[name])

if use_pandas_metadata:
file_keyvalues = self.metadata.metadata
Expand Down Expand Up @@ -837,7 +864,9 @@ def read_table(source, columns=None, nthreads=1, metadata=None,
name or directory name. For passing Python file objects or byte
buffers, see pyarrow.io.PythonFileInterface or pyarrow.io.BufferReader.
columns: list
If not None, only these columns will be read from the file.
If not None, only these columns will be read from the file. A column
name may be a prefix of a nested field, e.g. 'a' will select 'a.b',
'a.c', and 'a.d.e'
nthreads : int, default 1
Number of columns to read in parallel. Requires that the underlying
file source is threadsafe
Expand Down Expand Up @@ -875,7 +904,9 @@ def read_pandas(source, columns=None, nthreads=1, metadata=None):
name. For passing Python file objects or byte buffers,
see pyarrow.io.PythonFileInterface or pyarrow.io.BufferReader.
columns: list
If not None, only these columns will be read from the file.
If not None, only these columns will be read from the file. A column
name may be a prefix of a nested field, e.g. 'a' will select 'a.b',
'a.c', and 'a.d.e'
nthreads : int, default 1
Number of columns to read in parallel. Requires that the underlying
file source is threadsafe
Expand Down
22 changes: 22 additions & 0 deletions python/pyarrow/tests/test_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,28 @@ def test_index_column_name_duplicate(tmpdir):
tm.assert_frame_equal(result_df, dfx)


@parquet
def test_parquet_nested_convenience(tmpdir):
# ARROW-1684
import pyarrow.parquet as pq

df = pd.DataFrame({
'a': [[1, 2, 3], None, [4, 5], []],
'b': [[1.], None, None, [6., 7.]],
})

path = str(tmpdir / 'nested_convenience.parquet')

table = pa.Table.from_pandas(df, preserve_index=False)
_write_table(table, path)

read = pq.read_table(path, columns=['a'])
tm.assert_frame_equal(read.to_pandas(), df[['a']])

read = pq.read_table(path, columns=['a', 'b'])
tm.assert_frame_equal(read.to_pandas(), df)


@parquet
def test_backwards_compatible_index_naming():
expected_string = b"""\
Expand Down