Skip to content
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
6 changes: 6 additions & 0 deletions bindings/python/pymongoarrow/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ cdef class BuilderManager:
# For list children, the nulls are stored in the parent.
key = field.encode('utf-8')
parent_type = self.parent_types.get(key, None)
# Check if the item was in our schema but never seen, and should have a parent.
if parent_type is None and "." in field:
parent_key, _, _ = field.rpartition('.')
self.parent_names[key] = parent_key.encode('utf-8')
parent_type = BSON_TYPE_DOCUMENT
# Add nulls according to parent type.
if parent_type == BSON_TYPE_ARRAY:
continue
if parent_type == BSON_TYPE_DOCUMENT:
Expand Down
43 changes: 43 additions & 0 deletions bindings/python/test/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,49 @@ def test_schema_arrays_of_documents_with_nulls(self):
expected = json.load(fid)
assert df.to_pylist() == expected

def test_schema_arrays_of_documents_orphaned_null(self):
# From https://github.com/mongodb-labs/mongo-arrow/issues/265.
col = self.coll
col.delete_many({})
schema = Schema(
{
"_id": ObjectId,
"test_list_struct": [
{
"field1": {
"sub_field1": pa.string(),
"sub_field2": pa.string(),
}
}
],
}
)

col.insert_one(
{
"_id": ObjectId("000000000000000000000001"),
"test_list_struct": [
{
"field1": {
"sub_field1": "test_data",
}
},
{
"field1": "test_data",
},
],
}
)
df = aggregate_arrow_all(col, schema=schema, pipeline=[])
doc = df.to_pylist()[0]
del doc["_id"]
assert doc == {
"test_list_struct": [
{"field1": {"sub_field1": "test_data", "sub_field2": None}},
{"field1": {"sub_field1": None, "sub_field2": None}},
]
}

def test_auto_schema_nested(self):
# Create table with random data of various types.
_, data = self._create_nested_data()
Expand Down
Loading