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

Function ConvertMapHelper

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

Source from the content-addressed store, hash-verified

855
856template <typename F1, typename F2, typename F3>
857Status 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();

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