GH-49704: [C++] Read and write DictionaryEncoding.custom_metadata for dictionary value types #53994
:warning: GitHub issue #49704 has been automatically assigned in GitHub to PR creator.
I am wondering whether there would be a way of retaining this value_metadata even if this is a not known extension type. So the reader could retain it and a new writer would write it. I am thinking on the use case of data passing through:app knows extension type -> app does not know extension type -> app knows extension type
Maybe not entirely related and just an implementation detail that can be done as a follow up.
Hi @raulcd,
Do you think we need to make a different IPC reader options flag for that? Where we can do extension type pass through?
Rusty
@rustyconover Can you split the Format changes into a separate PR to make them easier to review? Thanks!
@rustyconover Can you split the Format changes into a separate PR to make them easier to review? Thanks!
Sure I can and I will! thanks @pitrou
@pitrou Done: the format change is now in #54959 (issue #54958). This PR is stacked on it and contains only the C++ implementation, so please review just the second commit.
Note that #54959 was auto-closed by the 3-open-PR limit for contributors without repository access; I've asked on it for it to be reopened.
raulcd
rustyconover
Rationale for this change
In IPC a dictionary-encoded column is a single
Fieldwhose type is the value type. The only metadata slot wasField.custom_metadata, sodictionary<values=extension<X>>andextension<X, storage=dictionary<...>>serialized identically. Arrow C++ reads both as the latter, so pyarrow cannot read back its own stream ofpa.dictionary(pa.int32(), pa.uuid()):Invalid storage type for UuidType: dictionary<...>.#54959 adds an optional
custom_metadatatoDictionaryEncodingfor the dictionary value type (format version 1.6). This PR is the C++ reference implementation. Companion PRs:Fieldmetadata until apache/arrow-rs#7982 is fixed: apache/arrow-rs#11622ArrowSchema.dictionary->metadata: apache/arrow-nanoarrow#972What changes are included in this PR?
Schema_generated.hregenerated with flatc 24.3.6, limited to the new member."dictionary": {..., "metadata": [...]}).- The writer puts a dictionary value extension type's
ARROW:extension:keys inDictionaryEncoding.custom_metadata.- The reader applies them to the value type before wrapping it in the dictionary type, then applies
Field.custom_metadataas before.-
DictionaryType::value_metadata()retains unconsumed metadata, including unregistered extension annotations and arbitrary keys, separately from the enclosing field. IPC and integration JSON write it back to the dictionary encoding. No reader option is needed.- C Data import/export preserves the same metadata on
ArrowSchema.dictionary->metadata.- If the values are a registered extension type, its annotations are authoritative; conflicting or duplicate reserved keys in supplied value metadata are removed without mutating the caller.
- Metadata-aware type/schema equality and fingerprints include value metadata. Dictionary type promotion retains the first type's metadata. Existing constructor and factory overloads remain available.
- Dictionary traversal looks through extension value types to find nested dictionaries. The dictionary fingerprint fallback also now compares actual types when an extension type has no fingerprint.
dictionary_extensioncase, run for C++ only for now.- Go is skipped until the companion arrow-go PR lands, because integration CI builds arrow-go from main. With that PR, C++ ⇄ Go passes over IPC.
- Rust is skipped because arrow-rs cannot represent a dictionary of an extension type (apache/arrow-rs#7982). It reads the case correctly but cannot produce it, and archery has no producer-only skip.
ruby/red-arrow-format/.../dictionary_encoding.rb) were not regenerated, because no Ruby toolchain was available. They needrake flat_buffers:generate.- A Parquet
ARROW:schemaround-trip test.Are these changes tested?
Yes.
- schema and record batch round trips, including nested types and the file footer;
- the flatbuffer layout;
- delta and replacement dictionaries of an extension value type;
- unknown-extension read/write/read round trips, including restoration after registration, extra metadata, nulls, ordered dictionaries and nested dictionaries inside extension values;
- legacy field-level metadata, which behaves as before.
e3ee6e33604767c2d3122b1c2ea4336c3ae96f06: repeated C++ → Go → C++ and Go → C++ → Go IPC file/JSON conversions preserve unknown extension annotations, ordinary metadata (including duplicate keys and NUL bytes), separate parent metadata, nested fields and dictionary data.Are there any user-facing changes?
Yes:
pa.dictionary(pa.int32(), pa.uuid())) through IPC.Field.custom_metadatatoDictionaryEncoding.custom_metadata.- Readers that predate this change, including older C++/pyarrow, read such columns as a plain dictionary of the storage type and lose the extension type.
- That includes types like
arrow.opaque, which older readers used to keep, though with the wrong nesting (as an extension type with dictionary storage).arrow.uuidon a dictionary still fails to read, as it does today.Was AI used for this PR?
PR code and description written by:
Latest follow-up reviewed before submission by:
GitHub Issue: #49704
Initial implementation generated with Claude Code; metadata-preservation follow-up prepared with Codex.