Repository navigation
Support dynamic type in parquet reader - #117106
Conversation
|
Workflow [PR], commit [6c7d596] Summary: ❌
AI ReviewSummaryThis PR teaches the Parquet reader to decode unshredded Findings❌ Blockers
Tests
Final Verdict
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes462 object files changed (+194.68 KiB total), 1 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units3257 translation units recompiled, 16989 s compile time in total, 3256 of them have a recent master baseline. |
c79bd86 to
adf5b5a
Compare
Avogar
left a comment
There was a problem hiding this comment.
I looked briefly at the implementation. Let's discuss the overall design before I go into details.
1. PR description and docs. First, let's update the PR description and the changelog entry. Right now it's unclear to a reviewer or a future reader what this feature is about. We should state what exactly we add support for here: reading the new Parquet Variant type (let's add a link to its spec as well), how we read it and how we map it to a ClickHouse Dynamic type, and the type mappings. Let's also describe clearly what is supported and what is not, including that we support only reading for now: writing a Dynamic column to Parquet throws UNKNOWN_TYPE, so such a file can't be written back. And please add documentation, we have inline documentation in the code in the ParquetV3BlockInputFormat.cpp file. A link to the Parquet Variant spec in the code where we decode it would help too, so future readers know where to look.
2. Dynamic vs JSON. If I understand correctly, Parquet Variant was added to store semi-structured JSON data. So people might ask why not use JSON instead of Dynamic here. The problem is that our JSON at top-level is always an object, while Parquet Variant can contain any value at top-level. So Dynamic is the right default type to use here, as it can contain any value. We should state it in the PR description and in the docs. But I think users will ask for reading Parquet Variant directly into JSON, so let's also support decoding into a JSON column: we can just throw if some root value isn't an object and say in the exception message to use Dynamic instead.
3. Shredded variants. A shredded group falls through to processSubtreeTuple and the user gets Tuple(metadata, value, typed_value) with raw blobs, which looks very strange to me. This is also why 04930_parquet_variant_shredded fails in all stateless jobs. Let's support shredding, it seems to be one of the main reasons people use Variant in Parquet. If it has to be a follow-up, then let's reject it here with NOT_IMPLEMENTED instead of producing a Tuple.
4. Type mapping. For objects let's use JSON, not Map(String, Dynamic): it's more natural for an unstructured object value than a Map with Dynamic values. Dynamic can hold JSON without a problem. Nested objects will flatten into dot paths. For arrays let's match what JSON does: [1, "s"] -> Array(Dynamic), an array of objects -> Array(JSON). The scalar mapping looks fine.
5. Drop Field. Right now DecodedValue uses a Field, let's try to avoid it as much as possible: fields are very inefficient and can lose the original data types (for example dates/datetimes). Let's deserialize into the column directly. For Dynamic we should use addNewVariant, look up the discriminator, getVariantByGlobalDiscriminator, insert, push the local discriminator and offset, and insertValueIntoSharedVariant if addNewVariant returned false. You can look at how we insert into Dynamic/JSON during JSON data parsing in JSONExtractTree.
6. How do we detect a Variant column? According to the spec a Variant column is a group annotated with the VARIANT logical type, and we already read this flag as logicalType.__isset.VARIANT. It also carries specification_version that we should check. But we detect it by structure instead: any group with 2 unannotated BYTE_ARRAY children named metadata and value. So an ordinary struct with such fields will silently change from Tuple(metadata String, value String) to Dynamic. Let's use the logical type. Old Spark files don't have it, but they have org.apache.spark.sql.parquet.row.metadata with the field typed as "variant" (04928_variant_spark.parquet has exactly that), so we can use it instead of guessing. If we still want the structural fallback for some writer, let's do it behind a setting.
One of the bugs Claude found that I think is worth noting here: readUnsigned does static_cast<UInt32>(data[pos + i]) and data[pos + i] is char, which is signed on x86 and unsigned on ARM. So any byte >= 0x80 sign-extends, and for example an object with more than 127 bytes of values and one-byte offsets reads offset 150 as 4294967190 and fails with Malformed Parquet variant. All test files are under 128 bytes, so we need a new test for it.
I'll review the code in detail once we agree on these.
e63a7ae to
8c8b120
Compare
|
Let's do support for Dynamic type right now. The comments about JSON type and shredded parquet are reasonable, but let me add them in another PR. 5 and 6 are fixed |
Initialize `role` in `SchemaConverter::processSubtreeDynamic` to satisfy `cppcoreguidelines-init-variables`, and describe the new `VARIANT` support in the Parquet format documentation to pass the feature docs check. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117106&sha=202738e6d60d5802a22cdc83a66524a425baaf82&name_0=PR PR: #117106 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`PODArray<char8_t>::push_back` was instantiated with `int`, which `clang-tidy` rejects with `clang-diagnostic-implicit-int-conversion`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117106&sha=e3f7c4700f9ece4e65e2cb0913b4403622edfaf5&name_0=PR&name_1=Build%20%28arm_tidy%2C%201%2F4%29 PR: #117106 Co-Authored-By: Claude Opus 5.5 <[email protected]>
| const DataTypePtr type = getDataTypesCache().getType(type_name); | ||
| auto value_column = type->createColumn(); | ||
| decodeValueIntoColumn(data, pos, context, depth, *value_column); | ||
| const ColumnPtr casted = castColumn({std::move(value_column), type, ""}, typed_type); |
There was a problem hiding this comment.
This path bypasses the normal JSON typed-path coercion rules and applies a generic SQL CAST instead. castColumn does not behave like JSON(a T) insertion: it uses regular cast semantics (for example, narrowing numerics can wrap/truncate) and it never honors settings such as type_json_skip_invalid_typed_paths.
So a Parquet VARIANT object read as JSON(a UInt8) can silently materialize a different value than the same payload inserted through the regular JSON parser. Please route typed-path materialization through the same JSONExtractTree/typed-path conversion helpers that JSON uses, rather than castColumn on a one-row column.
`05299_parquet_variant_read_as_json`: add the output of the `Nullable(JSON)` section added in e3f7c47. `04931_parquet_variant_max_dynamic_types`: decimals of the same scale and a different precision have the same string form, so the 68 values give 39 distinct strings, one per scale. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117106&sha=60c03e5d88d441be92f259fe177b93cc54d96ce5&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29 PR: #117106 Co-Authored-By: Claude Opus 5.5 <[email protected]>
`master` gained an independent reader for unshredded Parquet `VARIANT` columns into `Dynamic` (ClickHouse#117106). Both readers are kept: - `processSubtreeVariant` of this branch handles groups annotated with the `VARIANT` logical type, including shredded ones. - `processSubtreeDynamic` from `master` runs after it, so it now only handles unannotated groups recognized by structure (as written by Spark 4.0, controlled by `input_format_parquet_detect_variant_by_structure`). It is adapted to the `depth` parameter of `processSubtree` of this branch. - `VariantEncoding.h` holds both the encoding constants of this branch and the `decodeVariantColumn` declaration from `master`. - `StorageFile` / `StorageURL` use `ReadFromFormatInfo::getFormatFilter` from `master` and still pass `format_filter_input_header`. - `FormatFilterInfo::hasFilter` takes `top_k_filter` from `master`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Previously this branch inferred `JSON` when `input_format_parquet_enable_json_parsing = 1`. `master` (ClickHouse#117106) reads a variant as `Dynamic`, because a top-level variant value is not necessarily an object. Use the same rule, so that both readers (annotated and structure-detected variants) agree. Type-hint metadata written by ClickHouse still restores `JSON` for files written from `JSON` columns, and `JSON` can be requested explicitly. Tests: - `04848_parquet_variant_foreign_wrapper_names`, `04402_parquet_variant_malformed_wrapper_value_schema`: adapted to the inferred `Dynamic` (it is not allowed in `ORDER BY`). - `04401_parquet_variant_schema_cache_json_nullable` is removed: it checked that the schema cache distinguishes `schema_inference_make_json_columns_nullable` for an inferred `VARIANT` `JSON` column, which is no longer inferred. Co-Authored-By: Claude Opus 5.5 <[email protected]>
After the merge, a group annotated with the `VARIANT` logical type is read by `processSubtreeVariant` of this branch, not by `processSubtreeDynamic` from `master`, so the behaviour pinned by the tests of ClickHouse#117106 changed: - `05306_parquet_variant_unsupported_version`: a specification version other than 1 was silently decoded as version 1. `processSubtreeVariant` now throws `NOT_IMPLEMENTED` with the same message as `master` (or skips the column in schema inference with `input_format_parquet_skip_columns_with_unsupported_types_in_schema_inference`). - `05298_parquet_variant_json_duplicate_path`: the object {"a.b": 1, "a": {"b": 2}} was rejected with `INCORRECT_DATA` by `master`'s reader, because the path `a.b` occurs twice once objects are flattened. This branch escapes the dot in the key (`a%2Eb`), so both values are kept. This is intended, and the test now pins the lossless result. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@groeneai is this correct?
|
|
Yes, all four points hold. The suggested entry needs two precisions. I checked against master (
Entry with these precisions:
|
|
The schema cache key fix from point 4 is in #125408. |
Edited by the nightly changelog CI job following .claude/skills/edit-changelog/SKILL.md. Changelog-revert: 125455 117198 Revert "Iceberg variant type" Changelog-deleted-entry: 117198 [Improvement] [raw] * Support iceberg variant type. Based on parquet variant reader - #117106. [#117198](#117198) ([Konstantin Vedernikov](https://github.com/scanhex12)).
Antalya 26.8: Backport of ClickHouse#117106 support dynamic type in parquet reader
…arquet reader Prerequisite for the Iceberg `variant` type backport: without the Parquet variant reader, Spark-written variant columns are read as `Tuple(value String, metadata String)` instead of `Dynamic`. The new `input_format_parquet_detect_variant_by_structure` setting is recorded in the `26.8.34.20001.altinityantalya` settings history block. Only the variant fixtures are added to the exclude list of `00900_long_parquet_load_2`. Related: ClickHouse#117106 Related: #2677 (comment) Signed-off-by: Andrey Zvonov <[email protected]>
Support reading columns stored in the Parquet
VARIANTencoding (semi-structured data, written for example by Spark 4.0).Detection. A column is read as a variant if it is a group annotated with the
VARIANTlogical type with specification version 1. Spark 4.0 writes variant columns without this annotation, so an unannotated group with exactly ametadataand avaluefield of typeBYTE_ARRAYis also recognized as a variant. Previously such a group was read asTuple(value String, metadata String), so this structural detection is controlled by the new settinginput_format_parquet_detect_variant_by_structure(enabled by default, guarded bycompatibility). Explicitly requestingDynamicorJSONfor the column reads it as a variant regardless of the setting.Type mapping. The column is read as
Dynamic, notJSON, because a top-level variant value is not necessarily an object. Values are decoded directly into the columns, withoutField, and keep their own types:int32->Int32,decimal8->Decimal(18, S),timestamp->DateTime64(6, 'UTC'),timestamp_ntz->DateTime64(6),binary/string->String,uuid->UUID, etc.), and the variantnullisNULL;JSON, with nested objects flattened into dot-separated paths;Array(JSON), other arrays asArray(Dynamic).Reading as
JSON. A variant column can also be read asJSONby requesting that type explicitly. Then every value must be an object, otherwise an exception suggests usingDynamic. Typed paths andSKIP/SKIP REGEXPof the requestedJSONtype are respected.Not supported yet:
typed_valuefield): reading such a column throwsNOT_IMPLEMENTEDinstead of silently losing the values stored intyped_value; the other columns of the file stay readable, and schema inference can skip the column withinput_format_parquet_skip_columns_with_unsupported_types_in_schema_inference;VARIANTspecification versions other than 1 (NOT_IMPLEMENTED);DynamicorJSONcolumns to Parquet asVARIANT(UNKNOWN_TYPE), so such a file can't be written back.Malformed variant data (truncated blobs, out-of-range offsets, a null in only one of
metadata/value) is reported asINCORRECT_DATA, and nesting deeper thanmax_parser_depthasTOO_DEEP_RECURSION.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Support reading the Parquet
VARIANTtype (unshredded, as written for example by Spark 4.0) into theDynamictype, with objects read asJSON. A variant column can also be read directly asJSONif all its values are objects. Shredded variants are not supported yet.Workflow [PR]
Sync PR [sync-upstream/pr/117106]
🤖 Generated with Claude Code
Version info
26.10.1.1950-master(included in26.10and later)