Skip to content

feat: Support the canonical Parquet Variant extension type - #26822

Open
sdf-jkl wants to merge 2 commits into
apache:mainfrom
sdf-jkl:codex/variant-extension
Open

sdf-jkl wants to merge 2 commits into
apache:mainfrom
sdf-jkl:codex/variant-extension

Conversation

@sdf-jkl

@sdf-jkl sdf-jkl commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Which issue does this PR close?

Part of #21144. Follows #21291 and #21714.

Rationale for this change

Add arrow.parquet.variant to DataFusion's canonical extension type registry, following the thin wrappers used for JSON and the other canonical types, and map the SQL VARIANT type to it as #21714 did for UUID and JSON.

Arrow 60.2.0 moved VariantType into arrow-schema (apache/arrow-rs#12033), so this needs no new dependencies.

What changes are included in this PR?

Add DFVariant and its canonical registration. The wrapper keeps the storage type and delegates validation and metadata to Arrow's VariantType, which checks the storage fields since apache/arrow-rs#12245. display_name returns VARIANT, like JSON and UUID. Values use ordinary storage-type formatting.

SQL VARIANT maps to unshredded storage, Struct(metadata: BinaryView, value: BinaryView) with both fields non-nullable as the Variant spec requires. This matches what arrow-rs cast_to_variant produces and how Spark keeps Variant values in memory. sqlparser has no VARIANT keyword, so the planner matches it as a custom type. Casts stay storage casts, as for UUID: converting text or shredded input to Variant needs a Variant kernel and is left to a follow-up.

What is the testing strategy for this PR?

sql_extension_types.slt covers CREATE TABLE with a VARIANT column, DESCRIBE, NULL inserts, a Variant value built with with_metadata, and errors for casting text, casting a struct with nullable fields, and type parameters. The value output changes to JSON with the formatter follow-up.

Formatting, Clippy (-p datafusion-common -p datafusion-expr -p datafusion-sql --all-targets --all-features), the datafusion-common, datafusion-expr, and datafusion-sql tests, and the sql_extension_types and information_schema SLTs pass.

Are there any user-facing changes?

Yes. SQL accepts VARIANT as a type, the canonical registry recognizes arrow.parquet.variant, and DFVariant is available through datafusion_common::types.

🤖 Generated with Claude Code

@github-actions github-actions Bot added logical-expr Logical plan and expressions common Related to common crate labels Oct 27, 2026
alamb pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Oct 30, 2026
…et-variant` (apache#12005)

# Which issue does this PR close?

- Closes apache#11952.

# Rationale for this change

Using the `arrow.parquet.variant` extension type currently requires
depending on `parquet-variant-compute`, which pulls in the full `arrow`
crate and `parquet-variant-json`. Schema-only consumers such as
`datafusion-common` and `datafusion-expr` (apache/datafusion#26822) only
need the type itself, which depends on nothing beyond `arrow-schema`.

# What changes are included in this PR?

`VariantType` moves unchanged from
`parquet-variant-compute/src/variant_array.rs` to a new
`parquet-variant/src/extension.rs` and is exported as
`parquet_variant::VariantType`. `parquet-variant-compute` re-exports it,
so `parquet_variant_compute::VariantType` and
`parquet::variant::VariantType` keep working and refer to the same type.

# Are these changes tested?

Yes. Existing uses through the old paths (the `VariantArray` doctests,
`variant_get`, and the `parquet` Variant schema tests) are unchanged and
still pass. A new unit test in `parquet-variant` covers the type on its
own: a struct field accepts the extension and a non-struct field is
rejected with the existing error message.

# Are there any significant user-facing changes?

`VariantType` is now also available from `parquet-variant`. The existing
import path is kept, so this is not a breaking change.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@tschwarzinger

Copy link
Copy Markdown
Contributor

I think apart from moving VariantType to the other package this looks good! After that we can add the display integration.

From what I can gather, the display implementation is mostly done in datafusion-variant. Should we add this code to this PR or do a follow-up?

@sdf-jkl

sdf-jkl commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

From what I can gather, the display implementation is mostly done in datafusion-variant. Should we add this code to this PR or do a follow-up?

I thought about doing it separately to make the review process easier 😅

cetra3 pushed a commit to pydantic/arrow-rs that referenced this pull request Nov 18, 2026
# Which issue does this PR close?

- Closes apache#11938.

# Rationale for this change

`VariantType` accepted any `Struct` as storage, so `try_new`, `validate`
and `Field::try_canonical_extension_type` treated structs like
`Struct("unrelated": Int32)` as Variant. Extension wrappers such as
DataFusion's `DFVariant` (apache/datafusion#26822) should be able to
delegate this check to the Arrow type instead of building a
`VariantArray`.

# What changes are included in this PR?

`VariantType::supports_data_type` now requires a struct with a binary
`metadata` field (or a dictionary or run-end encoded array of binary)
and at least one of a binary `value` field and a `typed_value` field.

These are the top-level checks `VariantArray::try_new` already applied,
and `try_new` now calls this check instead of its own, so everything
`VariantArray` reads still passes. That includes shredded storage
without `value`, and the Parquet reader, which attaches `VariantType` to
every VARIANT column. `try_new` runs it before canonicalizing shredded
types, so a non-Variant struct reports the storage error rather than an
illegal shredded type. Nullability is deliberately not checked, to keep
reads lenient: arrow-rs wrote unshredded `value` as nullable before
apache#10320, and those files must still read. `typed_value` types and nested
shredded groups stay with `VariantArray`. Each failed check reports its
own error, reusing the wording of `VariantArray`'s former per-field
messages with `VariantType` in place of `VariantArray`. The storage
layout is now documented on `VariantType`, and `VariantArray::try_new`
links to it.

# Are these changes tested?

Yes. New unit tests in `variant.rs` cover accepted layouts (each binary
type, dictionary and run-end encoded `metadata`, shredded, and
`typed_value` without `value`) and rejected ones, asserting the error
message for each. `VariantArray::try_new`'s per-field error tests
duplicated these cases, so a single test replaces them, checking that
the storage error is reported before canonicalization's illegal shredded
type error. The other `parquet-variant-compute` and `parquet` Variant
tests, including `variant_integration`, pass unchanged.

# Are there any significant user-facing changes?

Storage types that are not Variant storage now fail
`VariantType::try_new` and `validate`, so
`Field::with_extension_type(VariantType)` panics on them and
`try_with_extension_type` returns an error. Code that tagged placeholder
structs such as `Struct(Fields::empty())` needs a real storage type.
`VariantArray::try_new` now reports invalid storage with `VariantType`'s
errors. They keep the old per-field wording but name `VariantType`, a
non-struct input reports `VariantType only supports StructArray, got
...`, and a struct with neither `value` nor `typed_value` now says
either field is accepted.

# AI usage disclosure

Implementation and tests prepared with Claude Code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sdf-jkl
sdf-jkl force-pushed the codex/variant-extension branch from c2f6aa8 to d92e610 Compare December 1, 2026 22:58
@sdf-jkl sdf-jkl changed the title [Not ready for review] feat: Support the canonical Parquet Variant extension type feat: Support the canonical Parquet Variant extension type Dec 1, 2026
@sdf-jkl
sdf-jkl marked this pull request as ready for review December 1, 2026 23:34
@sdf-jkl

sdf-jkl commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@tschwarzinger ready for another look 😁

@tschwarzinger tschwarzinger left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To me this looks good! Variant has an empty string as metadata so we don't need to keep VariantType around. I think this can be merged as is and any tests can then land with the follow-up that contains the formatter.

From what I can gather this fits the idea "core variant" functionality in datafusion, "udfs" in the external crate.

SQL can't produce a Variant column yet, so there is no SLT.

I think we can produce it with some manual labor. Here is an attempt from an LLM, so I am not 100% sure this is correct. But something along these lines:

#######
# Parquet Variant Extension Type
#######

# Manually construct a Variant struct encoding {"a": 1} and attach arrow.parquet.variant metadata
statement ok
CREATE TABLE test_variant AS
SELECT
    with_metadata(
        named_struct('metadata', X'1101000161', 'value', X'02010000020C01'),
        'ARROW:extension:name', 'arrow.parquet.variant',
        'ARROW:extension:metadata', ''
    ) AS v;

query TTT
DESCRIBE test_variant;
----
v VARIANT NO

query ?T
SELECT v, arrow_metadata(v, 'ARROW:extension:name') FROM test_variant;
----
{metadata: 1101000161, value: 02010000020c01} arrow.parquet.variant

statement ok
DROP TABLE test_variant;

But I think we can do that in the formatting PR so we can have an end 2 end test that will hopefully be superseded by '{"a": 1}' AS VARIANT at some point.

///
/// <https://arrow.apache.org/docs/format/CanonicalExtensions.html#parquet-variant>
#[derive(Debug, Clone)]
pub struct DFVariant {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We could do

pub struct DFVariant {
    inner: VariantType,
    storage_type: DataType,
}

and then self.inner.serialize_metadata() but I think this will just be for the sake of consistency. If Arrow ever tries to update the variant type and adds some metadata there, we can also add it later.

I am fine with both.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think other ext types do that 🤷

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@sdf-jkl sdf-jkl left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added Variant SQL type after the last review

///
/// <https://arrow.apache.org/docs/format/CanonicalExtensions.html#parquet-variant>
#[derive(Debug, Clone)]
pub struct DFVariant {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think other ext types do that 🤷

Field::new("", DataType::Utf8, true).with_extension_type(Json::default()),
)),
// sqlparser has no VARIANT keyword, so it parses as a custom type
SQLDataType::Custom(name, modifiers)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Instead of waiting for a new sqlparser-rs release I think we could add it here right away and use it in SLTs

I will open a PR upstream too

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

named_struct('metadata', X'1101000161', 'value', X'02010000020C01'),
'ARROW:extension:name', 'arrow.parquet.variant',
'ARROW:extension:metadata', ''
) AS v;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@github-actions github-actions Bot added sql SQL Planner sqllogictest SQL Logic Tests (.slt) labels Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to common crate logical-expr Logical plan and expressions sql SQL Planner sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants