| 855 | |
| 856 | template <typename F1, typename F2, typename F3> |
| 857 | Status ConvertMapHelper(F1 resetRow, F2 addPairToRow, F3 stealRow, |
| 858 | const ChunkedArray& data, PyArrayObject* py_keys, |
| 859 | PyArrayObject* py_items, |
| 860 | // needed for null checks in items |
| 861 | const std::vector<std::shared_ptr<Array>> item_arrays, |
| 862 | PyObject** out_values) { |
| 863 | OwnedRef key_value; |
| 864 | OwnedRef item_value; |
| 865 | |
| 866 | int64_t chunk_offset = 0; |
| 867 | for (int c = 0; c < data.num_chunks(); ++c) { |
| 868 | const auto& arr = checked_cast<const MapArray&>(*data.chunk(c)); |
| 869 | const bool has_nulls = data.null_count() > 0; |
| 870 | |
| 871 | // Make a list of key/item pairs for each row in array |
| 872 | for (int64_t i = 0; i < arr.length(); ++i) { |
| 873 | if (has_nulls && arr.IsNull(i)) { |
| 874 | Py_INCREF(Py_None); |
| 875 | *out_values = Py_None; |
| 876 | } else { |
| 877 | int64_t entry_offset = arr.value_offset(i); |
| 878 | int64_t num_pairs = arr.value_offset(i + 1) - entry_offset; |
| 879 | |
| 880 | // Build the new list object for the row of Python pairs |
| 881 | RETURN_NOT_OK(resetRow(num_pairs)); |
| 882 | |
| 883 | // Add each key/item pair in the row |
| 884 | for (int64_t j = 0; j < num_pairs; ++j) { |
| 885 | // Get key value, key is non-nullable for a valid row |
| 886 | auto ptr_key = reinterpret_cast<const char*>( |
| 887 | PyArray_GETPTR1(py_keys, chunk_offset + entry_offset + j)); |
| 888 | key_value.reset(PyArray_GETITEM(py_keys, ptr_key)); |
| 889 | RETURN_IF_PYERROR(); |
| 890 | |
| 891 | if (item_arrays[c]->IsNull(entry_offset + j)) { |
| 892 | // Translate the Null to a None |
| 893 | Py_INCREF(Py_None); |
| 894 | item_value.reset(Py_None); |
| 895 | } else { |
| 896 | // Get valid value from item array |
| 897 | auto ptr_item = reinterpret_cast<const char*>( |
| 898 | PyArray_GETPTR1(py_items, chunk_offset + entry_offset + j)); |
| 899 | item_value.reset(PyArray_GETITEM(py_items, ptr_item)); |
| 900 | RETURN_IF_PYERROR(); |
| 901 | } |
| 902 | |
| 903 | // Add the key/item pair to the row |
| 904 | RETURN_NOT_OK(addPairToRow(j, key_value, item_value)); |
| 905 | } |
| 906 | |
| 907 | // Pass ownership to the resulting array |
| 908 | *out_values = stealRow(); |
| 909 | } |
| 910 | ++out_values; |
| 911 | } |
| 912 | RETURN_IF_PYERROR(); |
| 913 | |
| 914 | chunk_offset += arr.values()->length(); |
no test coverage detected