You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@arrow.apache.org by "spenczar (via GitHub)" <gi...@apache.org> on 2023/08/30 19:55:47 UTC

[GitHub] [arrow] spenczar opened a new issue, #37476: pyarrow.array silently rewrites dictionary types to use signed integers for indexes

spenczar opened a new issue, #37476:
URL: https://github.com/apache/arrow/issues/37476

   ### Describe the bug, including details regarding any error messages, version, and platform.
   
   If you define a dictionary type to use an unsigned integer for its index type, and explicitly pass it to pyarrow.array, it'll get ignored and you'll find yourself with a signed index instead.
   
   ```py
   import pyarrow as pa
   dictionary_type = pa.dictionary(pa.uint64(), pa.string())
   print(pa.array(['x'], dictionary_type).type)
   # dictionary<values=string, indices=int64, ordered=0>
   ```
   
   Similarly for `uint32`:
   ```py
   import pyarrow as pa
   dictionary_type = pa.dictionary(pa.uint32(), pa.string())
   print(pa.array(['x'], dictionary_type).type)
   # dictionary<values=string, indices=int32, ordered=0>
   ```
   
   ### Component(s)
   
   Python


-- 
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: issues-unsubscribe@arrow.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Python] pyarrow.array silently rewrites dictionary types to use signed integers for indexes [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou closed issue #37476: [Python] pyarrow.array silently rewrites dictionary types to use signed integers for indexes
URL: https://github.com/apache/arrow/issues/37476


-- 
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: issues-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Python] pyarrow.array silently rewrites dictionary types to use signed integers for indexes [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on issue #37476:
URL: https://github.com/apache/arrow/issues/37476#issuecomment-5107241727

   Issue resolved by pull request 50475
   https://github.com/apache/arrow/pull/50475


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow] jorisvandenbossche commented on issue #37476: [Python] pyarrow.array silently rewrites dictionary types to use signed integers for indexes

Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on issue #37476:
URL: https://github.com/apache/arrow/issues/37476#issuecomment-1700486940

   Thanks for the report. I think the problem is that in the python->arrow conversion (`python_to_arrow.cc`), it is using a DictionaryBuilder under the hood, which is created with:
   
   https://github.com/apache/arrow/blob/9b6be29f431705ce1f85cc218c66d4d03698f06b/cpp/src/arrow/builder.cc#L312-L320
   
   This is passing `exact_index_type = False`, and that essentially means that it will use an adaptive int builder (that starts with the bitwidth size you specified, but can still grow eg from int32 to int64 if needed). 
   
   Maybe one way to fix the signed vs unsigned change is to let it use a AdaptiveIntBuilder vs AdaptiveUIntBuilder, depending on the signedness of the original index type. That would preserve the signedness, but keep the ability to let the bitwidth grow if necessary to convert the data to a dictionary type.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Python] pyarrow.array silently rewrites dictionary types to use signed integers for indexes [arrow]

Posted by "mkzung (via GitHub)" <gi...@apache.org>.
mkzung commented on issue #37476:
URL: https://github.com/apache/arrow/issues/37476#issuecomment-4945708067

   Ran into this from the narwhals side: its `Categorical` maps to `dictionary(uint32, string)`, so building one from Python values fails there. I opened #50474 before I spotted this issue, and I've closed that as a duplicate of this one.
   
   One thing that might be worth adding here: it isn't only the signedness that gets dropped. The index type is really treated as a *starting bit width*, so even a signed type stops being honoured once the cardinality outgrows it:
   
   ```py
   >>> import pyarrow as pa, string, itertools
   >>> vals = ["".join(p) for p in itertools.islice(itertools.product(string.ascii_lowercase, repeat=2), 200)]
   >>> pa.array(["a", "b", "a"], type=pa.dictionary(pa.int8(), pa.string())).type.index_type
   DataType(int8)
   >>> pa.array(vals, type=pa.dictionary(pa.int8(), pa.string())).type.index_type
   DataType(int16)
   ```
   
   That falls out of the same place @jorisvandenbossche pointed at: `MakeDictionaryBuilder` passes `exact_index_type=false`, and `DictionaryBuilderCase::CreateFor()` reduces the requested type to `index_type->byte_width()` and hands that to `AdaptiveIntBuilder` as a starting width. So the signedness is lost and the width is only a hint.
   
   I'd like to try the fix along the lines Joris suggested (an adaptive *unsigned* index builder for unsigned index types, keeping the width adaptive). Two things I hit while reading around it:
   
   - `AdaptiveUIntBuilder`'s constructors don't take the `alignment` argument that `AdaptiveIntBuilder`'s do, so `DictionaryBuilderBase<AdaptiveUIntBuilder, T>` doesn't compile as it stands.
   - `DictionaryConverter` in `util/converter.h` hardcodes `DictionaryBuilder<ValueType>` and `checked_cast`s to it, and that trait is shared with the R binding, so it looks like it needs an index-builder template parameter rather than a local change.
   
   Happy to pick this up if nobody else is on it.
   
   Versions: pyarrow 23.0.1, Python 3.10, macOS arm64.
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Python] pyarrow.array silently rewrites dictionary types to use signed integers for indexes [arrow]

Posted by "mkzung (via GitHub)" <gi...@apache.org>.
mkzung commented on issue #37476:
URL: https://github.com/apache/arrow/issues/37476#issuecomment-4945708141

   take


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Python] pyarrow.array silently rewrites dictionary types to use signed integers for indexes [arrow]

Posted by "mkzung (via GitHub)" <gi...@apache.org>.
mkzung commented on issue #37476:
URL: https://github.com/apache/arrow/issues/37476#issuecomment-4947058017

   I had a go at this and landed somewhere different from what I wrote above.
   
   The AdaptiveUIntBuilder route does not work. It makes `MakeDictionaryBuilder` return a different builder class, and `util/converter.h`, `json/from_string.cc` and the R binding all cast the result to `DictionaryBuilder<T>`. `TestDictionaryUnifier.ChunkedArrayNestedDict`, which builds a `dictionary(uint32(), utf8())`, dies on a `checked_pointer_cast` DCHECK as soon as you try it.
   
   What works is keeping one builder class and preserving the requested signedness where the index type is reported. Indices are non-negative and the signed and unsigned types of a given width have the same layout, so it is value-preserving and free, and the width stays adaptive. Nothing outside `builder_dict.h` and `builder.cc` has to change, so R is fixed rather than broken.
   
   One consequence: `dictionary(uint64(), ...)` then really does produce uint64 indices, and `arrow_to_pandas.cc` refuses to convert those, so `to_pandas()` starts raising where it used to work by accident on int32 indices. uint8, uint16 and uint32 are unaffected, and take, filter, cast, IPC, Parquet, the compute kernels and Acero all keep working.
   
   PR shortly.
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org