You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "syahra712 (via GitHub)" <gi...@apache.org> on 2026/10/16 08:42:53 UTC

[PR] Preserve null maps when projecting a map of structs [iceberg-python]

syahra712 opened a new pull request, #4118:
URL: https://github.com/apache/iceberg-python/pull/4118

   # Rationale for this change
   
   Closes #4114.
   
   When `ArrowProjectionVisitor.map()` rebuilds a map whose values are structs, it calls `pa.MapArray.from_arrays(offsets, keys, values)`. That drops the validity bitmap of the original array, so null maps come back as empty maps (`{}`) after projection. This is the map counterpart of the list issue handled in #4065.
   
   The fix keeps the validity and offsets buffers of the original map array and only swaps in the projected child array, so nulls (and slicing offsets) are preserved.
   
   ## Are these changes tested?
   
   Yes. Added `test__to_requested_schema_null_map_of_structs` in `tests/io/test_pyarrow.py`, covering a map with struct values, a map with primitive values, and a sliced batch. The test fails on `main` (null rows become `[]`) and passes with the fix.
   
   ## Are there any user-facing changes?
   
   Null map columns with struct values are now read back as null instead of an empty map.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Preserve null maps when projecting a map of structs [iceberg-python]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko commented on code in PR #4118:
URL: https://github.com/apache/iceberg-python/pull/4118#discussion_r4306875236


##########
pyiceberg/io/pyarrow.py:
##########
@@ -2120,7 +2120,16 @@ def map(
             )
             if isinstance(value_result, pa.StructArray):
                 # Arrow does not allow reordering of fields, therefore we have to copy the array :(
-                return pa.MapArray.from_arrays(map_array.offsets, key_result, value_result, arrow_field)
+                projected = pa.MapArray.from_arrays(map_array.offsets, key_result, value_result, arrow_field)
+                # Keep the validity and offsets buffers of the original array, otherwise null maps become empty maps
+                return pa.Array.from_buffers(
+                    projected.type,
+                    len(map_array),
+                    map_array.buffers()[:2],
+                    map_array.null_count,
+                    map_array.offset,
+                    [projected.values],
+                )

Review Comment:
   How about using `StructArray`, and then we can pass iin the `arrow_field` as the type:
   ```suggestion
                   entries = pa.StructArray.from_arrays(
                       [key_result, value_result], fields=[arrow_field.key_field, arrow_field.item_field]
                   )
                   # Keep the validity and offsets buffers of the original array, otherwise null maps become empty maps
                   return pa.Array.from_buffers(
                       arrow_field,
                       len(map_array),
                       map_array.buffers()[:2],
                       map_array.null_count,
                       map_array.offset,
                       [entries],
                   )
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Preserve null maps when projecting a map of structs [iceberg-python]

Posted by "syahra712 (via GitHub)" <gi...@apache.org>.
syahra712 commented on code in PR #4118:
URL: https://github.com/apache/iceberg-python/pull/4118#discussion_r4306943443


##########
pyiceberg/io/pyarrow.py:
##########
@@ -2120,7 +2120,16 @@ def map(
             )
             if isinstance(value_result, pa.StructArray):
                 # Arrow does not allow reordering of fields, therefore we have to copy the array :(
-                return pa.MapArray.from_arrays(map_array.offsets, key_result, value_result, arrow_field)
+                projected = pa.MapArray.from_arrays(map_array.offsets, key_result, value_result, arrow_field)
+                # Keep the validity and offsets buffers of the original array, otherwise null maps become empty maps
+                return pa.Array.from_buffers(
+                    projected.type,
+                    len(map_array),
+                    map_array.buffers()[:2],
+                    map_array.null_count,
+                    map_array.offset,
+                    [projected.values],
+                )

Review Comment:
   Thanks, that's cleaner. It skips building the intermediate MapArray. Applied in 59f8d82, and the map tests still pass.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Preserve null maps when projecting a map of structs [iceberg-python]

Posted by "Fokko (via GitHub)" <gi...@apache.org>.
Fokko merged PR #4118:
URL: https://github.com/apache/iceberg-python/pull/4118


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org