MCPcopy Create free account
hub / github.com/apache/arrow / ConvertMapHelper

Function ConvertMapHelper

python/pyarrow/src/arrow/python/arrow_to_pandas.cc:868–929  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

ConvertMapFunction · 0.85

Calls 8

OKFunction · 0.50
num_chunksMethod · 0.45
null_countMethod · 0.45
lengthMethod · 0.45
IsNullMethod · 0.45
value_offsetMethod · 0.45
resetMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected