Skip to content

Read Iceberg v3 variant columns from Avro data files - #130078

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/avro-read-iceberg-variant
Nov 8, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/avro-read-iceberg-variant

Conversation

@groeneai

@groeneai groeneai commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #129998
Related: #130028

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Iceberg v3 variant columns in Avro data files can now be read: an Avro record of two bytes fields, metadata and value, is decoded with the variant binary encoding when it is read as Dynamic or JSON, as for Parquet. Closes #129998.

Description

Iceberg v3 stores a variant in Avro as a record of two bytes fields, metadata and value. Iceberg asks the Avro format for Dynamic, but AvroDeserializer::createDeserializeFn had no Dynamic/JSON case for a record, so any Iceberg table with Avro data files and a variant column failed with Type Dynamic is not compatible with Avro record (the Impala tables iceberg_v3_variant_avro and iceberg_v3_variant_mixed from the issue). It never worked: the same error on 26.8, 26.9 and 26.10.

The Avro reader now decodes such a record with the Parquet variant decoder, which this PR splits into a per-value Parquet::VariantValueDecoder; decodeVariantColumn keeps its null handling and its behaviour. The record is matched by its structure (exactly two bytes fields named metadata and value, in either order) and only when Dynamic, JSON or Nullable(JSON) is requested, because avro-cpp drops the variant logical type. A [null, record] union is handled directly, so NULL is kept with input_format_null_as_default = 0 and for Nullable(JSON), as in Parquet. Schema inference is unchanged. VariantEncoding.cpp is also built when Avro is enabled without Parquet.

Validation: 05568_avro_read_variant covers optional and required records, swapped fields, JSON/Nullable(JSON), nesting in Array/Map/Tuple, and malformed or non-variant records. It fails on master, passes 50/50 with random settings, and removing either hook reddens it. The issue's tables give the values expected in #130028's 05707_iceberg_impala_v3_variant. The Parquet variant tests and the Avro tests that run locally pass unchanged. The ORC counterpart is #130031.


Workflow [PR]
Sync PR [sync-upstream/pr/130078]

Version info

  • Merged into: 26.11.1.1690-master (included in 26.11 and later)

Iceberg v3 stores a `variant` in Avro as a record of two `bytes` fields,
`metadata` and `value`, in the variant binary encoding. Iceberg requests
`Dynamic` for it, but `AvroDeserializer::createDeserializeFn` had no
`Dynamic`/`JSON` case for a record, so every such table failed with
"Type Dynamic is not compatible with Avro record" (ClickHouse#129998).

The per-value part of `Parquet::decodeVariantColumn` becomes
`Parquet::VariantValueDecoder`, built once per column; the Parquet-only
null policy (a leaf null or an empty blob from `input_format_null_as_default`
is a missing value) stays in `decodeVariantColumn`, whose behaviour is
unchanged. Avro carries its NULL in the union branch, so `decode` requires
two encoded blobs and an empty `bytes` field is malformed.

The Avro reader uses the decoder for a record of exactly two `bytes` fields
named `metadata` and `value` (either order) when the requested type is
`Dynamic`, `JSON` or `Nullable(JSON)`. avro-cpp drops unknown logical types,
so the record is matched by structure plus the requested type, like an
explicit `Dynamic`/`JSON` request in the Parquet reader. Two hooks: the record
case (required, nested, one-branch unions) and the `[null, record]` union,
which is handled before the generic branches so that NULL is kept with
`input_format_null_as_default = 0` (Dynamic stores NULL) and with
`Nullable(JSON)` (the generic branch would turn a variant null into `{}`).
Schema inference is unchanged: without the logical type, inferring `Dynamic`
would change every user record named `{metadata, value}`.

`VariantEncoding.cpp` lives in the Parquet object library, which is built
only with `USE_PARQUET`, while Avro and Iceberg need only `USE_AVRO`; it
includes nothing from Arrow/Parquet, so it is also added to `dbms` when Avro
is enabled without Parquet.

Closes ClickHouse#129998

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review: my own cold review plus an independent model (engine: codex; 1 finding). No blocker or major finding, so the tree is unchanged by review.

# Sev Finding Verdict Evidence / action
1 💡 Errors raised while reading Avro still say "Parquet variant" (VariantEncoding.cpp:84, :766) AGREE, noted, not changed The messages name the Parquet variant binary encoding, which Iceberg also uses for Avro, and they are shared with the Parquet reader. Rewording them would change the existing Parquet references (05228, 05299, 05307).
2 💡 The [null, record] union hook does not resolve a symbolic (named, reused) record leaf AGREE, noted, not changed With the default input_format_null_as_default = 1 the symbolic path still reaches the record hook. Iceberg names each variant record r<field-id>, so no writer reuses it.
3 💡 A malformed value inside an object can leave partial subcolumn state if the format skips the row AGREE, noted, not changed Only AvroConfluent with input_format_allow_errors_num skips rows (plain Avro aborts the read); the Parquet decoder behaves the same. No reproducer.

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-slot-8:20261108-050200

@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. SELECT * FROM icebergLocal('iceberg_v3/iceberg_v3_variant_avro') on the Impala data fails every time on master with Code: 44 Type Dynamic is not compatible with Avro record; so does the new test.
b Root cause explained? Yes. Iceberg requests Dynamic for a variant; AvroDeserializer::createDeserializeFn had no Dynamic/JSON case for a record, so the {metadata: bytes, value: bytes} record reached the generic "not compatible" throw.
c Fix matches root cause? Yes. The Avro reader decodes that record with the existing Parquet variant decoder, at the record case and the [null, record] union; no guard or widened check.
d Test intent preserved / new tests added? Yes. New 05568_avro_read_variant (Dynamic, input_format_null_as_default = 0, required record, swapped fields, JSON, Nullable(JSON), Array/Map/Tuple nesting, 4 error arms). Existing Parquet variant tests unchanged.
e Both directions demonstrated? Yes. Master fails, the fix passes; 5 mutation arms (each hook, the column name, the shape check, the empty-blob rule) each redden the test. 50/50 with random settings.
f Fix is general across code paths? Yes. One deserializer serves Avro, AvroConfluent, Kafka and Iceberg/S3/File; nested positions go through the existing recursion; Parquet uses the same decoder; Avro builds without Parquet compile it. ORC is #130031.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Dynamic, JSON, Nullable(JSON); both union orders; field order; nesting; empty blobs rejected; string fields and other 2-field records keep the old error; Tuple and Variant targets unchanged.
h Backward compatible? (maintainer-approved exception only) Yes. Only inputs that fail on master change; no setting, format or default change; schema inference unchanged.
i Invariants and contracts preserved? Yes. One value per decode call (the Nullable(JSON) null map is pushed once), Parquet's null policy unchanged in decodeVariantColumn, Avro NULL only from the union branch, decoder state per column and immutable.

Session id: cron:clickhouse-impl-slot-5:20261108-040300

@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author

cc @scanhex12 @Avogar, could you review this and add the can be tested label so CI runs? The Avro reader now decodes the Iceberg v3 variant record (two bytes fields, metadata and value) into Dynamic/JSON with the Parquet variant decoder, which this PR splits into a per-value Parquet::VariantValueDecoder.

@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8350cdb]

Summary: ✅


AI Review

Summary

PR #130078 reads Iceberg v3 variant records from Avro into Dynamic, JSON, and Nullable(JSON) using the extracted Parquet::VariantValueDecoder. Review of head 8350cdb7efbfc0ceef44eac03631c1a20ee172ac found one confirmed correctness blocker in error recovery. The new 05568_avro_read_variant test passes with an exact reference match on the PR's arm_binary artifact, but a separate reproduction shows values from a rejected message leaking into the next accepted row. This concern was already acknowledged and left unchanged in the author's discussion; it is retained here without a duplicate inline comment.

Findings

❌ Blockers

  • src/Processors/Formats/Impl/AvroRowInputFormat.cpp:310: Skipping a malformed variant message must discard every value decoded from that message. [dismissed by author -- https://github.com/Read Iceberg v3 variant columns from Avro data files #130078#issuecomment-6717680067] The concern remains because the current PR binary demonstrably returns a rejected message's value in a later row.

    With AvroConfluent, target v JSON, and input_format_allow_errors_num = 1, decode a message with metadata 11020001026162 and value 020200010002030C010C. This appends a = 1, then raises INCORRECT_DATA for the truncated b value. Follow it with a valid message containing metadata 1101000163 and value 02010000020C03, encoding {"c":3}. Using separate input-buffer chunks (reproduced with max_read_buffer_size = 24 and storage_file_read_method = 'read'), the query succeeds and returns {"a":1,"c":3}, rather than {"c":3}.

    decodeObjectIntoJSON mutates subcolumns before appending the shared-data offset that determines ColumnObject::size. On the exception, the outer size is unchanged, so the size-based rollback in IRowInputFormat::read does nothing. The next successful object retains the stale a value. This is silent data contamination. The same decoder is reached through Dynamic object values and nested Avro targets; their outer-size rollback does not restore partial object state either. Plain Avro and the Parquet batch reader abort on this error, whereas AvroConfluent resumes using the mutated columns.

    Restore a recursive column checkpoint when per-value decoding throws, or decode into a temporary column and append only after success. Keep propagating the original exception after cleanup.

Tests
  • ⚠️ Add a focused recovery regression: a malformed object followed by a valid object in the same output block, with error skipping enabled. Assert that only the valid message's paths and values survive. The new error tests stop at the exception and cannot detect this contamination.
Final Verdict

Status: ❌ Block. Make per-value decoding exception-safe and add the recovery regression. No new inline findings or thread changes were posted because the remaining issue already appears in the author's adjudication log.

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements can be tested Allows running workflows for external contributors comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). labels Nov 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8350cdb7e with master 7b5f2bf32 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

13 translation units recompiled, 78 s compile time in total, 13 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good.

@alexey-milovidov alexey-milovidov self-assigned this Nov 8, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 8, 2026
Merged via the queue into ClickHouse:master with commit 2f038ad Nov 8, 2026
374 of 375 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Iceberg v3 variant in Avro data files is not supported (Type Dynamic is not compatible with Avro record)

3 participants