Repository navigation
Conversation
|
|
|
|
…ding when the dense array would overflow Converting a string/binary dictionary array to a NumPy object array, or to pandas as the child of a nested type, decodes the dictionary into a dense array first. When the decoded data exceeds the 32-bit offset limit this fails although the object array has no such limit. Predict the overflow from the dictionary offsets and the indices, and only then convert the dictionary values to Python objects once and reference them per index instead of decoding. All other conversions are unchanged. Drafted by Claude Code (an AI agent) and reviewed & approved by pearu.
320d429 to
7ceca05
Compare
|
I find it very weird to vary the output type depending on the input size. Why can't we generate a CATEGORICAL without converting to dense first? |
Agreed, the size gate is a premature optimization. I'll reconsider the approach: convert string/binary dictionaries from the dictionary whenever an object array is requested, with no dense intermediate, and drop the overflow predictor. I'll update the PR once I have the implementation and measurements. On categorical: the two paths this PR touches produce NumPy arrays ( |
| // 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++) { |
There was a problem hiding this comment.
🤖 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.
| 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(); |
There was a problem hiding this comment.
🤖 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):
| const int64_t dictionary_length = arr.dictionary()->length(); | |
| const int64_t dictionary_length = arr.dictionary()->length(); | |
| RETURN_NOT_OK(CheckIndexBounds(*arr.indices()->data(), dictionary_length)); |
| 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; |
There was a problem hiding this comment.
🤖 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:
| 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; | |
| case Type::DICTIONARY: | |
| *output_type = | |
| options.decode_dictionaries ? PandasWriter::OBJECT : PandasWriter::CATEGORICAL; | |
| break; |
| // 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)) { |
There was a problem hiding this comment.
🤖 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:
| !DecodingWouldOverflow(*arr)) { | |
| !is_binary_like( | |
| checked_cast<const DictionaryType&>(*arr->type()).value_type()->id())) { |
Rationale for this change
Converting a
DictionaryArraywithstring/binaryvalues to a NumPy object array (to_numpy,np.asarray), or to pandas as the child of a nested type, decodes the dictionary into a dense array first. When the decoded data exceeds the 32-bit offset limit this fails withTake operation overflowed binary array capacity(a segfault or garbage before #50841), although the result, a NumPy object array, has no such limit.What changes are included in this PR?
DecodingWouldOverflowpredicts from the dictionary offsets and the indices whether decoding a chunk would exceed the limit thetakekernel enforces. Two cheap bounds settle the common case without reading the rows.ObjectWriterVisitor::Visit(const DictionaryType&)converts each dictionary value to a Python object once and references it per index (thededuplicate_objectsidea, without the memo table since the dictionary already holds the unique values).Not covered: map keys and items decode before reaching this check (
ConvertMap), so a map with an overflowing dictionary child still raises.Are these changes tested?
Yes:
test_dictionary_to_numpy_without_decoding(plus null index, null dictionary value andbinaryvalues) andtest_list_of_dictionary_without_decoding(the nested path, also with several chunks). The dense form in the tests is 2.1 GB, built from 16385 references to one 128 KiB value, so the tests themselves need under 1 MB.Are there any user-facing changes?
Conversions that raised now succeed. In that regime, repeated values in the result refer to one Python object, as
to_pandas(deduplicate_objects=True)already does.AI usage disclosure
Developed with AI assistance (Claude Code): the approach was worked out and the implementation, tests and benchmarks were generated under my direction and reviewed line by line by me. I own and can debug these changes.