← Back to Pull requests

GH-49704: [C++] Read and write DictionaryEncoding.custom_metadata for dictionary value types #53994

Open
rustyconover opened this pull request with 1338 lines diff across 20 files
View on GitHub
rustyconover rustyconover commented on Oct 16, 2026 16:33 UTC
Author

[!NOTE]

Depends on #54959, which holds the format change, split out at review request. This PR is stacked on it: please review the C++ implementation commits after the format commit. Once #54959 merges, this will be rebased onto main.

Rationale for this change


In IPC a dictionary-encoded column is a single Field whose type is the value type. The only metadata slot was Field.custom_metadata, so dictionary<values=extension<X>> and extension<X, storage=dictionary<...>> serialized identically. Arrow C++ reads both as the latter, so pyarrow cannot read back its own stream of pa.dictionary(pa.int32(), pa.uuid()): Invalid storage type for UuidType: dictionary<...>.

#54959 adds an optional custom_metadata to DictionaryEncoding for the dictionary value type (format version 1.6). This PR is the C++ reference implementation. Companion PRs:

  • Go, the second reference implementation: apache/arrow-go#1381

  • Rust, which reads the new member and maps it into Field metadata until apache/arrow-rs#7982 is fixed: apache/arrow-rs#11622

  • nanoarrow, which maps the member losslessly to ArrowSchema.dictionary->metadata: apache/arrow-nanoarrow#972
  • What changes are included in this PR?


  • Generated code: Schema_generated.h regenerated with flatc 24.3.6, limited to the new member.

  • C++: the IPC writer and reader, and the integration JSON reader and writer ("dictionary": {..., "metadata": [...]}).

  • - The writer puts a dictionary value extension type's ARROW:extension: keys in DictionaryEncoding.custom_metadata.
    - The reader applies them to the value type before wrapping it in the dictionary type, then applies Field.custom_metadata as 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.
  • Archery: a new dictionary_extension case, 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.
  • Not included: the Ruby flatbuffers bindings (ruby/red-arrow-format/.../dictionary_encoding.rb) were not regenerated, because no Ruby toolchain was available. They need rake flat_buffers:generate.

  • Follow-ups:

  • - A Parquet ARROW:schema round-trip test.

    Are these changes tested?


    Yes.
  • C++ IPC tests:

  • - 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.
  • Also: JSON integration tests, C Data metadata round trips, metadata-aware equality/promotion and reserved-key conflict tests. Metadata containing NUL bytes is covered in IPC, JSON and C Data. The original implementation also includes archery C++ IPC/C Data coverage and a pyarrow regression test.

  • Follow-up validation: 826 C++ tests passed across the full type, IPC read/write, C Data bridge and JSON integration suites. clang-format 18.1.8 and cpplint 1.6.1 passed.

  • Cross-implementation, run manually: C++ ⇄ Go passes with the companion arrow-go PR, and arrow-rs reads the C++ and Go output in every format. The follow-up was additionally checked against arrow-go commit 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:
  • C++ and pyarrow now round-trip a dictionary whose value type is an extension type (e.g. pa.dictionary(pa.int32(), pa.uuid())) through IPC.

  • The C++/pyarrow IPC output for such columns changes: the extension keys move from Field.custom_metadata to DictionaryEncoding.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).
  • Extension types whose storage type is a dictionary are serialized as before.

  • There is deliberately no fallback for data written by existing writers. A field-level arrow.uuid on a dictionary still fails to read, as it does today.
  • Was AI used for this PR?


    PR code and description written by:
  • Human

  • AI
  • Latest follow-up reviewed before submission by:

  • Human

  • AI

  • Not reviewed
  • GitHub Issue: #49704

    Initial implementation generated with Claude Code; metadata-preservation follow-up prepared with Codex.

    Diff: 1338 lines (+1283 / -55) across 20 files Reviewer: raulcd Review duration: 64766m
    All Vouch review confidence checks passed
    Residual risk 0.07 = Change Risk (0.79) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.79 · 1338 lines diff across 20 files
    0.79
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.94 · 5 substantive comment(s)
    0.94
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    90%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.400
    Mass File Touch mass_file_touch
    0.080
    Real PR Conversation Timeline
    7 items
    github-actions[bot] github-actions[bot] commented on Oct 16, 2026 16:34 UTC
    Bot

    :warning: GitHub issue #49704 has been automatically assigned in GitHub to PR creator.

    raulcd raulcd commented on code on Oct 19, 2026 09:12 UTC
    Code Review
    cpp/src/arrow/ipc/metadata_internal.cc:1
    @@ -900,35 +953,21 @@ Status FieldFromFlatbuffer(const flatbuf::Field* field, FieldPosition field_pos,
         auto int_data = encoding->indexType();
         CHECK_FLATBUFFERS_NOT_NULL(int_data, "DictionaryEncoding.indexType");
         RETURN_NOT_OK(IntFromFlatbuffer(int_data, &index_type));
    +    // The dictionary value type may be an extension type, whose metadata is
    +    // found in DictionaryEncoding.custom_metadata (GH-49704)
    +    if (encoding->custom_metadata() != nullptr) {
    +      ARROW_ASSIGN_OR_RAISE(auto value_metadata,
    +                            internal::GetKeyValueMetadata(encoding->custom_metadata()));
    +      RETURN_NOT_OK(MaybeDeserializeExtensionType(value_metadata.get(), &type));

    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.

    rustyconover rustyconover commented on code on Oct 21, 2026 17:53 UTC
    Code Review
    cpp/src/arrow/ipc/metadata_internal.cc:1
    @@ -900,35 +953,21 @@ Status FieldFromFlatbuffer(const flatbuf::Field* field, FieldPosition field_pos,
         auto int_data = encoding->indexType();
         CHECK_FLATBUFFERS_NOT_NULL(int_data, "DictionaryEncoding.indexType");
         RETURN_NOT_OK(IntFromFlatbuffer(int_data, &index_type));
    +    // The dictionary value type may be an extension type, whose metadata is
    +    // found in DictionaryEncoding.custom_metadata (GH-49704)
    +    if (encoding->custom_metadata() != nullptr) {
    +      ARROW_ASSIGN_OR_RAISE(auto value_metadata,
    +                            internal::GetKeyValueMetadata(encoding->custom_metadata()));
    +      RETURN_NOT_OK(MaybeDeserializeExtensionType(value_metadata.get(), &type));

    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

    pitrou pitrou commented on Oct 22, 2026 15:22 UTC
    Contributor

    @rustyconover Can you split the Format changes into a separate PR to make them easier to review? Thanks!

    rustyconover rustyconover commented on Oct 22, 2026 16:10 UTC
    Author

    @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

    rustyconover rustyconover commented on Oct 22, 2026 17:12 UTC
    Author

    @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.

    Reviewers
    Assignees
    rustyconover rustyconover
    Labels
    risk: low confidence: 90.6% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 78.5% (HIGH) · Higher = larger blast radius
    78.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 90.6% · Lower = rushed or shallow review
    90.6%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 7.4% · Safe to merge
    7.4%
    Repository