-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement basic struct handling (#91)
- Loading branch information
1 parent
06a505f
commit 23e2fd7
Showing
10 changed files
with
368 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
"""Routines to manipulate arrow tables.""" | ||
import pyarrow as pa | ||
|
||
|
||
def _reapply_names_to_type(array: pa.ChunkedArray, names: list[str]) -> (pa.Array, list[str]): | ||
new_arrays = [] | ||
new_schema = [] | ||
|
||
if array.type.num_fields > len(names): | ||
raise ValueError('Insufficient number of names provided to reapply names.') | ||
|
||
remaining_names = names | ||
if pa.types.is_list(array.type): | ||
raise NotImplementedError('Reapplying names to lists not yet supported') | ||
if pa.types.is_map(array.type): | ||
raise NotImplementedError('Reapplying names to maps not yet supported') | ||
if pa.types.is_struct(array.type): | ||
field_num = 0 | ||
while field_num < array.type.num_fields: | ||
field = array.chunks[0].field(field_num) | ||
this_name = remaining_names.pop(0) | ||
|
||
new_array, remaining_names = _reapply_names_to_type(field, remaining_names) | ||
new_arrays.append(new_array) | ||
|
||
new_schema.append(pa.field(this_name, new_array.type)) | ||
|
||
field_num += 1 | ||
|
||
return pa.StructArray.from_arrays(new_arrays, fields=new_schema), remaining_names | ||
if array.type.num_fields != 0: | ||
raise ValueError(f'Unsupported complex type: {array.type}') | ||
return array, remaining_names | ||
|
||
|
||
def reapply_names(table: pa.Table, names: list[str]) -> pa.Table: | ||
"""Apply the provided names to the given table recursively.""" | ||
new_arrays = [] | ||
new_schema = [] | ||
|
||
remaining_names = names | ||
for column in iter(table.columns): | ||
if not remaining_names: | ||
raise ValueError('Insufficient number of names provided to reapply names.') | ||
|
||
this_name = remaining_names.pop(0) | ||
|
||
new_array, remaining_names = _reapply_names_to_type(column, remaining_names) | ||
new_arrays.append(new_array) | ||
|
||
new_schema.append(pa.field(this_name, new_array.type)) | ||
|
||
if remaining_names: | ||
raise ValueError('Too many names provided to reapply names.') | ||
|
||
return pa.Table.from_arrays(new_arrays, schema=pa.schema(new_schema)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from dataclasses import dataclass | ||
|
||
import pyarrow as pa | ||
import pytest | ||
|
||
from src.backends.arrow_tools import reapply_names | ||
|
||
|
||
@dataclass | ||
class TestCase: | ||
name: str | ||
input: pa.Table | ||
names: list[str] | ||
expected: pa.table | ||
fail: bool = False | ||
|
||
|
||
cases: list[TestCase] = [ | ||
TestCase('empty table', pa.Table.from_arrays([]), [], pa.Table.from_arrays([])), | ||
TestCase('too many names', pa.Table.from_arrays([]), ['a', 'b'], pa.Table.from_arrays([]), | ||
fail=True), | ||
TestCase('normal columns', | ||
pa.Table.from_pydict( | ||
{"name": [None, "Joe", "Sarah", None], "age": [99, None, 42, None]}, | ||
schema=pa.schema({"name": pa.string(), "age": pa.int32()}) | ||
), | ||
['renamed_name', 'renamed_age'], | ||
pa.Table.from_pydict( | ||
{"renamed_name": [None, "Joe", "Sarah", None], | ||
"renamed_age": [99, None, 42, None]}, | ||
schema=pa.schema({"renamed_name": pa.string(), "renamed_age": pa.int32()}) | ||
)), | ||
TestCase('too few names', | ||
pa.Table.from_pydict( | ||
{"name": [None, "Joe", "Sarah", None], "age": [99, None, 42, None]}, | ||
schema=pa.schema({"name": pa.string(), "age": pa.int32()}) | ||
), | ||
['renamed_name'], | ||
pa.Table.from_pydict( | ||
{"renamed_name": [None, "Joe", "Sarah", None], | ||
"renamed_age": [99, None, 42, None]}, | ||
schema=pa.schema({"renamed_name": pa.string(), "renamed_age": pa.int32()}) | ||
), | ||
fail=True), | ||
TestCase('struct column', | ||
pa.Table.from_arrays( | ||
[pa.array([{"": 1, "b": "b"}], | ||
type=pa.struct([("", pa.int64()), ("b", pa.string())]))], | ||
names=["r"]), | ||
['r', 'a', 'b'], | ||
pa.Table.from_arrays( | ||
[pa.array([{"a": 1, "b": "b"}], | ||
type=pa.struct([("a", pa.int64()), ("b", pa.string())]))], names=["r"]) | ||
), | ||
# TODO -- Test nested structs. | ||
# TODO -- Test a list. | ||
# TODO -- Test a map. | ||
# TODO -- Test a mixture of complex and simple types. | ||
] | ||
|
||
|
||
class TestArrowTools: | ||
"""Tests the functionality of the arrow tools package.""" | ||
|
||
@pytest.mark.parametrize( | ||
"case", cases, ids=lambda case: case.name | ||
) | ||
def test_reapply_names(self, case): | ||
failed = False | ||
try: | ||
result = reapply_names(case.input, case.names) | ||
except ValueError as _: | ||
result = None | ||
failed = True | ||
if case.fail: | ||
assert failed | ||
else: | ||
assert result == case.expected | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.