Skip to content

ARROW-241: Checking list-types in schema support raises an error #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 28, 2024
Merged
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
5 changes: 4 additions & 1 deletion bindings/python/pymongoarrow/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ def _normalize_typeid(typeid, field_name):
fields.append((sub_field_name, _normalize_typeid(sub_typeid, sub_field_name)))
return struct(fields)
if isinstance(typeid, list):
return list_(_normalize_typeid(type(typeid[0]), "0"))
if len(typeid) != 1:
msg = f"list field in schema must contain exactly one element, not {len(typeid)}"
raise ValueError(msg)
return list_(_normalize_typeid(typeid[0], "0"))
if _is_typeid_supported(typeid):
normalizer = _TYPE_NORMALIZER_FACTORY[typeid]
return normalizer(typeid)
Expand Down
20 changes: 20 additions & 0 deletions bindings/python/test/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from datetime import datetime
from unittest import TestCase

import pytest
from bson import Binary, Code, Decimal128, Int64, ObjectId
from pyarrow import Table, field, float64, int64, list_, struct, timestamp
from pyarrow import schema as ArrowSchema
Expand Down Expand Up @@ -94,3 +95,22 @@ def test_list_of_list_projection(self):
}
)
self.assertEqual(schema._get_projection(), {"_id": True, "list": {"a": True, "b": True}})

def test_py_list_projection(self):
schema = Schema(
{"_id": ObjectId, "list": [(struct([field("a", int64()), field("b", float64())]))]}
)

self.assertEqual(schema._get_projection(), {"_id": True, "list": {"a": True, "b": True}})

def test_py_list_with_multiple_fields_raises(self):
with pytest.raises(
ValueError, match="list field in schema must contain exactly one element, not 2"
):
_ = Schema({"_id": ObjectId, "list": [([field("a", int64()), field("b", float64())])]})

def test_py_empty_list_raises(self):
with pytest.raises(
ValueError, match="list field in schema must contain exactly one element, not 0"
):
_ = Schema({"_id": ObjectId, "list": []})
Loading