Skip to content

Commit 6390d93

Browse files
committed
Add variant blog
1 parent 2cf30d7 commit 6390d93

2 files changed

Lines changed: 263 additions & 0 deletions

File tree

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
---
2+
title: "The Evolution of Semi-Structured Data: Introducing Variant in Apache Parquet"
3+
date: 2026-02-14
4+
description: "Native Variant Type in Apache Parquet"
5+
author: "[Aihua Xu](https://github.com/aihuaxu)"
6+
categories: ["features"]
7+
---
8+
9+
## Introduction
10+
11+
The Apache Parquet community is excited to announce the addition of the **Variant type**—a feature that brings native support for semi-structured data to Parquet, significantly improving efficiency compared to less efficient formats such as JSON. This marks a significant addition to Parquet, demonstrating how the format continues to evolve to meet modern data engineering needs.
12+
13+
While Apache Parquet has long been the standard for structured data where each value has a fixed and known type, handling heterogeneous, nested data often required a compromise: either store it as a costly-to-parse JSON string or flatten it into a rigid schema. The introduction of the Variant logical type provides a native, high-performance solution for semi-structured data that is already seeing rapid uptake across the ecosystem.
14+
15+
---
16+
17+
## What is Variant?
18+
19+
**Variant** is a self-describing data type designed to efficiently store and process semi-structured data—JSON-like documents with arbitrary and evolving schemas.
20+
21+
---
22+
23+
## Why Variant?
24+
25+
Unlike traditional approaches that store JSON as text strings and require full parsing to access any field, making queries slow and resource-intensive, Variant solves this by storing data in a **structured binary format** that enables direct field access through offset-based navigation. Query engines can jump directly to nested fields without deserializing the entire document, dramatically improving performance.
26+
27+
Unlike similar binary encodings such as BSON, Variant is optimized for the common case where multiple values share a similar structure: It avoids redundantly storing repeated field names and standardizes the best practice of **"shredded storage"** for pre-extracting structured subsets.
28+
29+
### Key Benefits
30+
31+
- **Type-Preserving Storage:** Original data types are maintained in their native formats—data types (integers, strings, booleans, timestamps, etc.) are preserved, unlike JSON which has a limited type system with no native support for types like timestamps or integers.
32+
33+
- **Efficient Encoding:** The binary format uses field name deduplication to minimize storage overhead compared to JSON strings or BSON encoding.
34+
35+
- **Fast Query Performance:** Direct offset-based field access provides performance improvement over JSON string parsing. Optional shredding of frequently accessed fields into typed columns further enhances query pruning and predicate pushdown.
36+
37+
- **Schema Flexibility:** No predefined schema is required, allowing documents with different structures to coexist in the same column. This enables seamless schema evolution while maintaining full queryability across all schema variations, while still taking advantage of common structures when present.
38+
39+
---
40+
41+
## Overview of Variant Type in Parquet
42+
43+
Parquet introduced the [Variant logical type](https://github.com/apache/parquet-format/blob/master/LogicalTypes.md#variant) in [August 2025](https://github.com/apache/parquet-format/pull/509).
44+
45+
### Variant Encoding
46+
47+
In Parquet, Variant is represented as a logical type and stored physically as a struct with two binary fields. The encoding is [designed](https://github.com/apache/parquet-format/blob/master/VariantEncoding.md) so engines can efficiently navigate nested structures and extract only the fields they need, rather than parsing the entire binary blob.
48+
49+
```parquet
50+
optional group event_data (VARIANT(1)) {
51+
required binary metadata;
52+
required binary value;
53+
}
54+
```
55+
56+
- **`metadata`:** Encodes type information and shared dictionaries (for example, field-name dictionaries for objects). This avoids repeatedly storing the same strings and enables efficient navigation.
57+
- **`value`:** Encodes the actual data in a compact binary form, supporting primitive values as well as arrays and objects.
58+
59+
#### Example
60+
61+
A web access event can be stored in a single Variant column while preserving the original data types:
62+
63+
```json
64+
{
65+
"user_id": 12345,
66+
"events": [
67+
{"type": "login", "timestamp": "2026-01-15T10:30:00Z"},
68+
{"type": "purchase", "timestamp": "2026-01-15T11:45:00Z", "amount": 99.99}
69+
]
70+
}
71+
```
72+
73+
Compared with storing the same payload as a JSON string, Variant retains type information (for example, timestamp values are stored as integers rather than being stored as strings), which improves correctness, enables more efficient querying and requires fewer bytes to store.
74+
75+
Just as importantly, Variant supports **schema variability**: records with different shapes can coexist in the same column without requiring schema migrations. For example, the following record can be stored alongside the event record above:
76+
77+
```json
78+
{
79+
"user_id": 12345,
80+
"error": "auth_failure"
81+
}
82+
```
83+
84+
---
85+
86+
## Shredding Encoding
87+
88+
To enhance query performance and storage efficiency, Variant data can be **shredded** by extracting frequently accessed fields into separate, strongly-typed columns, as described in the [detailed shredding specification](https://github.com/apache/parquet-format/blob/master/VariantShredding.md). For each shredded field:
89+
90+
- If the field **matches the expected schema**, its value is written to the strongly typed field.
91+
- If the field **does not match**, the original representation is written as Variant-encoded binary field and the corresponding strongly typed field is left NULL.
92+
93+
![Shredding Variant Visualization](/blog/variant/variant_shredding.png)
94+
95+
The query engine decides which fields to shred based on access patterns and workload characteristics. Once shredded, the standard Parquet columnar optimizations (encoding, compression, statistics) are used for the typed columns.
96+
97+
### Implementation Considerations
98+
99+
- **Schema Inference:** Engines can infer the shredding schema from sample data by selecting the most frequently occurring type for each field. For example, if `event.id` is predominantly an integer, the engine shreds it to an INT64 column.
100+
101+
- **Type Promotion:** To maximize shredding coverage, engines can promote types within the same type family. For example, if integer values vary in size (INT8, INT32, INT64), selecting INT64 as the shredded type ensures all integer values can be shredded rather than falling back to the unshredded representation.
102+
103+
- **Metadata Control:** To control metadata overhead, engines may limit the number of shredded fields, since each field contributes statistics (min/max values, null counts) to the file footer and column stats.
104+
105+
- **Explicit Shredding Schema:** When read patterns are known in advance, engines can specify an explicit shredding schema at write time, ensuring that frequently accessed fields are shredded for optimal query performance.
106+
107+
### Performance Characteristics
108+
109+
- **Selective field access:** When queries access only the shredded fields, only those columns are read from Parquet, skipping the rest, benefiting from column pruning and predicate pushdown.
110+
111+
- **Full Variant reconstruction:** When queries require access to the complete Variant object, there is a performance overhead as the engine must reconstruct the Variant by merging data from the shredded typed fields and the base Variant column.
112+
113+
### Examples of Shredded Parquet Schemas
114+
115+
If a field's value matches the shredded type, it is stored in the typed column `typed_value`. If a field's value has a different type, it remains in the `value` binary column using standard Variant encoding.
116+
117+
#### Example 1: Shredding to String Type
118+
119+
The Variant values are shredded to string type.
120+
121+
```parquet
122+
optional group SIMPLE_DATA (VARIANT(1)) = 1 {
123+
required binary metadata; # variant metadata
124+
optional binary value; # non-shredded value
125+
optional binary typed_value (STRING); # the shredded value
126+
}
127+
```
128+
129+
**Encoding Table:**
130+
131+
| Variant Value | `value` | `typed_value` |
132+
|---------------|---------|---------------|
133+
| `"Jim"` | `null` | `"Jim"` |
134+
| `100` | `100` | `null` |
135+
| `{"name": "Jim"}` | `{"name": "Jim"}` | `null` |
136+
137+
---
138+
139+
#### Example 2: Shredding to Object with Typed Fields
140+
141+
The Variant values are shredded to an object with `user_id` field of integer type and `type` field of string type.
142+
143+
```parquet
144+
optional group EVENT_DATA (VARIANT(1)) = 1 {
145+
required binary metadata; # variant metadata
146+
optional binary value; # non-shredded value
147+
optional group typed_value {
148+
required group user_id { # user_id field
149+
optional binary value; # non-shredded value
150+
optional int32 typed_value; # the shredded value
151+
}
152+
required group type { # type field
153+
optional binary value; # non-shredded value
154+
optional binary typed_value (STRING); # the shredded value
155+
}
156+
}
157+
}
158+
```
159+
160+
**Encoding Table:**
161+
162+
| Variant Value | `value` | `typed_value` | `.user_id.value` | `.user_id.typed_value` | `.type.value` | `.type.typed_value` |
163+
|-------------------------------------|------------------|---------------|------------------|------------------------|---------------|---------------------|
164+
| `{"user_id": 100, "type": "login"}` | `null` | | `null` | `100` | `null` | `"login"` |
165+
| `100` | `100` | `null` | | | | |
166+
| `{"user_id": "Jim"}` | `null` | | `"Jim"` | `null` | `null` | `null` |
167+
| `{"user_id": 200, "amount": 99}` | `{"amount": 99}` | | `null` | `200` | `null` | `null` |
168+
169+
---
170+
171+
## Ecosystem Adoption: A Success Story
172+
173+
One of the most remarkable aspects of Variant's addition to Parquet is the rapid and widespread ecosystem adoption, demonstrating the strength of collaboration within the Apache Parquet community.
174+
175+
Variant support has been implemented across multiple Parquet libraries including **Java**, **Arrow C++**, **Rust**, and **Go**. For the most current implementation status across all languages and platforms, refer to the [official Parquet documentation](https://github.com/apache/parquet-format).
176+
177+
Major query engines have also integrated Variant support, including **DuckDB**, **[Apache Spark](https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.types.VariantType.html)**, and **[Snowflake](https://docs.snowflake.com/en/sql-reference/data-types-semistructured)**. This cross-ecosystem adoption highlights both the value of the Variant type and the Parquet community's commitment to evolving the format to meet modern data challenges.
178+
179+
---
180+
181+
## Real-World Use Cases
182+
183+
### Event Stream Analytics
184+
185+
Event streaming applications often handle events with evolving schemas, where different event types contain varying fields. Variant provides a flexible solution for storing heterogeneous event data without requiring schema migrations.
186+
187+
**Example: User Activity Events**
188+
189+
```sql
190+
-- Create table with Variant column
191+
CREATE TABLE event_stream (
192+
event_id INTEGER,
193+
event_data VARIANT
194+
);
195+
196+
-- Insert events with different schemas
197+
INSERT INTO event_stream VALUES
198+
(1, PARSE_JSON('{"user": {"id": 100, "country": "US"}, "actions": ["login", "view_dashboard"]}')),
199+
(2, PARSE_JSON('{"user": {"id": 101, "country": "UK", "premium": true}, "actions": ["login", "upgrade"]}')),
200+
(3, PARSE_JSON('{"user": {"id": 102, "country": "CA"}, "session_duration": 3600}'));
201+
202+
-- Query events with path notation - handles different schemas gracefully
203+
SELECT
204+
event_id,
205+
event_data:user.id::INTEGER as user_id,
206+
event_data:user.country::STRING as country,
207+
event_data:user.premium::BOOLEAN as is_premium
208+
FROM event_stream;
209+
```
210+
211+
---
212+
213+
### IoT Sensor Data
214+
215+
IoT deployments often involve diverse sensor types, each producing data with unique structures. Traditional approaches require either separate tables per sensor type or complex union schemas, or inefficient JSON / BSON encoding. Variant enables unified storage while maintaining type safety.
216+
217+
**Example: Multi-Sensor Data Pipeline**
218+
219+
```sql
220+
-- Create unified sensor table
221+
CREATE TABLE sensor_readings (
222+
reading_id INTEGER,
223+
timestamp TIMESTAMP,
224+
sensor_data VARIANT
225+
);
226+
227+
-- Insert data from different sensor types
228+
INSERT INTO sensor_readings VALUES
229+
(1, '2026-01-28 10:00:00',
230+
PARSE_JSON('{"sensor_id": "T001", "temp": 72.5, "unit": "F", "battery": 95}')),
231+
(2, '2026-01-28 10:00:05',
232+
PARSE_JSON('{"sensor_id": "M001", "motion_detected": true, "confidence": 0.95, "zone": "entrance"}')),
233+
(3, '2026-01-28 10:00:10',
234+
PARSE_JSON('{"sensor_id": "C001", "image_url": "s3://bucket/img_001.jpg", "objects_detected": ["person", "vehicle"]}'));
235+
236+
-- Query temperature sensors only
237+
SELECT
238+
reading_id,
239+
sensor_data:sensor_id::STRING as sensor_id,
240+
sensor_data:temp::FLOAT as temperature,
241+
sensor_data:unit::STRING as unit,
242+
sensor_data:battery::INTEGER as battery_level
243+
FROM sensor_readings
244+
WHERE sensor_data:sensor_id LIKE 'T%';
245+
```
246+
247+
---
248+
249+
## Conclusion
250+
251+
The addition of Variant to Apache Parquet represents a significant milestone in the format's evolution. By standardizing Variant as a logical type within Apache Parquet, the format now provides efficient storage for semi-structured data, enables meaningful statistics collection, and ensures cross-engine interoperability.
252+
253+
The well-documented specification has catalyzed broad ecosystem adoption, with multiple reference implementations now available across languages. This cross-language support ensures that Variant can be seamlessly integrated into diverse data processing environments, from analytical databases to streaming platforms, making it a universal solution for handling evolving schemas in modern data architectures.
254+
255+
---
256+
257+
## Resources
258+
259+
- **Apache Parquet Format Specification:** https://github.com/apache/parquet-format
260+
- **Variant Type Specification:** [Variant Logical Type](https://github.com/apache/parquet-format/blob/master/LogicalTypes.md#variant)
261+
- **Variant Encoding Specification:** [Variant Binary Encoding](https://github.com/apache/parquet-format/blob/master/VariantEncoding.md)
262+
- **Variant Shredding Specification:** [Variant Shredding](https://github.com/apache/parquet-format/blob/master/VariantShredding.md)
263+
- **Community Discussions:** dev@parquet.apache.org
87.3 KB
Loading

0 commit comments

Comments
 (0)