rok commented on code in PR #52547:
URL: https://github.com/apache/arrow/pull/52547#discussion_r4774414245


##########
python/pyarrow/src/arrow/python/arrow_to_pandas.cc:
##########
@@ -1380,6 +1439,40 @@ struct ObjectWriterVisitor {
     return ConvertStruct(options, data, out_values);
   }
 
+  // Dictionaries whose decoding would overflow the 32-bit offsets of the dense
+  // array (GH-50842) are not decoded. Instead, the dictionary values are
+  // converted once and referenced for each index.
+  Status Visit(const DictionaryType& type) {
+    for (int c = 0; c < data.num_chunks(); c++) {

Review Comment:
   :robot: Once this path runs unconditionally (without 
`DecodingWouldOverflow`), re-converting the full dictionary on every chunk will 
hurt chunked arrays that share a dictionary across chunks, and eager conversion 
also pays for unreferenced entries in small slices (or fails on an unreferenced 
invalid-UTF8 entry).
   
   Reusing the converted `values` vector when `arr.dictionary().get()` matches 
the previous chunk (or converting dictionary entries lazily on first index hit) 
would address both regressions from GH-50842 directly.



##########
python/pyarrow/src/arrow/python/arrow_to_pandas.cc:
##########
@@ -1380,6 +1439,40 @@ struct ObjectWriterVisitor {
     return ConvertStruct(options, data, out_values);
   }
 
+  // Dictionaries whose decoding would overflow the 32-bit offsets of the dense
+  // array (GH-50842) are not decoded. Instead, the dictionary values are
+  // converted once and referenced for each index.
+  Status Visit(const DictionaryType& type) {
+    for (int c = 0; c < data.num_chunks(); c++) {
+      const auto& arr = checked_cast<const DictionaryArray&>(*data.chunk(c));
+      const int64_t dictionary_length = arr.dictionary()->length();

Review Comment:
   :robot: `DictionaryArray::GetValueIndex()` casts signed index types 
(`int8`/`int16`/`int32`) through unsigned, so a negative index like `int8(-1)` 
maps to `255` and slips past `index < 0 || index >= dictionary_length` whenever 
`dictionary_length > 255`.
   
   Validating upfront with `CheckIndexBounds` (matching `CategoricalWriter`) 
catches negative signed indices properly and fails before converting the 
dictionary (making the per-row bounds check below redundant):
   
   ```suggestion
         const int64_t dictionary_length = arr.dictionary()->length();
         RETURN_NOT_OK(CheckIndexBounds(*arr.indices()->data(), 
dictionary_length));
   ```



##########
python/pyarrow/src/arrow/python/arrow_to_pandas.cc:
##########
@@ -2275,9 +2368,16 @@ static Status GetPandasWriterType(const ChunkedArray& 
data, const PandasOptions&
       }
       *output_type = PandasWriter::OBJECT;
     } break;
-    case Type::DICTIONARY:
-      *output_type = PandasWriter::CATEGORICAL;
-      break;
+    case Type::DICTIONARY: {
+      // A string/binary dictionary that is not decoded because the dense array
+      // would overflow is converted to objects instead, see
+      // ObjectWriterVisitor::Visit(const DictionaryType&)
+      const auto& value_type =
+          checked_cast<const DictionaryType&>(*data.type()).value_type();
+      *output_type = options.decode_dictionaries && 
is_binary_like(value_type->id())
+                         ? PandasWriter::OBJECT
+                         : PandasWriter::CATEGORICAL;
+    } break;

Review Comment:
   :robot: Keep the value-type dispatch decision in one place 
(`ConvertChunkedArrayToPandas`): any dictionary that reaches 
`GetPandasWriterType` with `decode_dictionaries == true` must become `OBJECT` 
anyway (enforced by `ARROW_DCHECK_NE(output_type, PandasWriter::CATEGORICAL)`), 
so we don't need to re-check `is_binary_like` here:
   
   ```suggestion
       case Type::DICTIONARY:
         *output_type =
             options.decode_dictionaries ? PandasWriter::OBJECT : 
PandasWriter::CATEGORICAL;
         break;
   ```



##########
python/pyarrow/src/arrow/python/arrow_to_pandas.cc:
##########
@@ -2595,7 +2695,11 @@ Status ConvertArrayToPandas(const PandasOptions& 
options, std::shared_ptr<Array>
 Status ConvertChunkedArrayToPandas(const PandasOptions& options,
                                    std::shared_ptr<ChunkedArray> arr, 
PyObject* py_ref,
                                    PyObject** out) {
-  if (options.decode_dictionaries && arr->type()->id() == Type::DICTIONARY) {
+  // Decoding a string/binary dictionary can overflow the 32-bit offsets of
+  // the dense array (GH-50842): such arrays are converted to objects without
+  // decoding instead, see ObjectWriterVisitor::Visit(const DictionaryType&)
+  if (options.decode_dictionaries && arr->type()->id() == Type::DICTIONARY &&
+      !DecodingWouldOverflow(*arr)) {

Review Comment:
   :robot: To avoid branching on decoded size (as discussed above), skip 
`DecodeDictionaries` directly for binary-like dictionaries (or 
`is_base_binary_like` to cover large string/binary too) so 
`DecodingWouldOverflow` and `<limits>` can be removed:
   
   ```suggestion
         !is_binary_like(
             checked_cast<const 
DictionaryType&>(*arr->type()).value_type()->id())) {
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to