Skip to content

Commit

Permalink
Current work on variant updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdblue committed Oct 20, 2024
1 parent 4f20815 commit 8352319
Show file tree
Hide file tree
Showing 2 changed files with 276 additions and 129 deletions.
88 changes: 64 additions & 24 deletions VariantEncoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,31 @@ Another motivation for the representation is that (aside from metadata) each nes
For example, in a Variant containing an Array of Variant values, the representation of an inner Variant value, when paired with the metadata of the full variant, is itself a valid Variant.

This document describes the Variant Binary Encoding scheme.
[VariantShredding.md](VariantShredding.md) describes the details of the Variant shredding scheme.
The [Variant Shredding spec](VariantShredding.md) describes the details of shredding Variant values as typed Parquet columns.

# Variant in Parquet
A Variant value in Parquet is represented by a group with 2 fields, named `value` and `metadata`.
Both fields `value` and `metadata` are of type `binary`, and cannot be `null`.
## Variant in Parquet

# Metadata encoding
A Variant value in Parquet is represented by a group with 2 fields, named `variant_value` and `metadata`.
The Variant group must be annotated with the `VARIANT` logical type.
Both fields `value` and `metadata` are of type `binary`.
The `metadata` field is required and must be a valid Variant metadata, as defined below.
The `variant_value` field is optional.
When present, the `variant_value` field must be a valid Variant value, as defined below.
The `variant_value` field may be null only when parts of the Variant value are shredded according to the Variant Shreedding spec.

This is the expected representation in Parquet:

```
optional group variant_event (VARIANT) {
required binary metadata;
optional binary variant_value;
}
```

There are no restrictions on the repetition of Variant groups (required, optional, or repeated).
The Variant group name is the name of the Variant column.

## Metadata encoding

The encoded metadata always starts with a header byte.
```
Expand Down Expand Up @@ -94,7 +112,7 @@ Each `offset` is a little-endian value of `offset_size` bytes, and represents th
The first `offset` value will always be `0`, and the last `offset` value will always be the total length of `bytes`.
The last part of the metadata is `bytes`, which stores all the string values in the dictionary.

## Metadata encoding grammar
### Metadata encoding grammar

The grammar for encoded metadata is as follows

Expand All @@ -118,7 +136,7 @@ Notes:
- If `sorted_strings` is set to 1, strings in the dictionary must be unique and sorted in lexicographic order. If the value is set to 0, readers may not make any assumptions about string order or uniqueness.


# Value encoding
## Value encoding

The entire encoded Variant value includes the `value_metadata` byte, and then 0 or more bytes for the `val`.
```
Expand All @@ -131,16 +149,16 @@ value | value_header | basic_type |
| |
+-------------------------------------------------+
```
## Basic Type
### Basic Type

The `basic_type` is 2-bit value that represents which basic type the Variant value is.
The [basic types table](#encoding-types) shows what each value represents.

## Value Header
### Value Header

The `value_header` is a 6-bit value that contains more information about the type, and the format depends on the `basic_type`.

### Value Header for Primitive type (`basic_type`=0)
#### Value Header for Primitive type (`basic_type`=0)

When `basic_type` is `0`, `value_header` is a 6-bit `primitive_header`.
The [primitive types table](#encoding-types) shows what each value represents.
Expand All @@ -151,7 +169,7 @@ value_header | primitive_header |
+-----------------------+
```

### Value Header for Short string (`basic_type`=1)
#### Value Header for Short string (`basic_type`=1)

When `basic_type` is `1`, `value_header` is a 6-bit `short_string_header`.
```
Expand All @@ -162,7 +180,7 @@ value_header | short_string_header |
```
The `short_string_header` value is the length of the string.

### Value Header for Object (`basic_type`=2)
#### Value Header for Object (`basic_type`=2)

When `basic_type` is `2`, `value_header` is made up of `field_offset_size_minus_one`, `field_id_size_minus_one`, and `is_large`.
```
Expand All @@ -180,7 +198,7 @@ The actual number of bytes is computed as `field_offset_size_minus_one + 1` and
`is_large` is a 1-bit value that indicates how many bytes are used to encode the number of elements.
If `is_large` is `0`, 1 byte is used, and if `is_large` is `1`, 4 bytes are used.

### Value Header for Array (`basic_type`=3)
#### Value Header for Array (`basic_type`=3)

When `basic_type` is `3`, `value_header` is made up of `field_offset_size_minus_one`, and `is_large`.
```
Expand All @@ -197,21 +215,21 @@ The actual number of bytes is computed as `field_offset_size_minus_one + 1`.
`is_large` is a 1-bit value that indicates how many bytes are used to encode the number of elements.
If `is_large` is `0`, 1 byte is used, and if `is_large` is `1`, 4 bytes are used.

## Value Data
### Value Data

The `value_data` encoding format depends on the type specified by `value_metadata`.
For some types, the `value_data` will be 0-bytes.

### Value Data for Primitive type (`basic_type`=0)
#### Value Data for Primitive type (`basic_type`=0)

When `basic_type` is `0`, `value_data` depends on the `primitive_header` value.
The [primitive types table](#encoding-types) shows the encoding format for each primitive type.

### Value Data for Short string (`basic_type`=1)
#### Value Data for Short string (`basic_type`=1)

When `basic_type` is `1`, `value_data` is the sequence of bytes that represents the string.

### Value Data for Object (`basic_type`=2)
#### Value Data for Object (`basic_type`=2)

When `basic_type` is `2`, `value_data` encodes an object.
The encoding format is shown in the following diagram:
Expand Down Expand Up @@ -281,7 +299,7 @@ The `field_id` list must be `[<id for key "a">, <id for key "b">, <id for key "c
The `field_offset` list must be `[<offset for value 1>, <offset for value 2>, <offset for value 3>, <last offset>]`.
The `value` list can be in any order.

### Value Data for Array (`basic_type`=3)
#### Value Data for Array (`basic_type`=3)

When `basic_type` is `3`, `value_data` encodes an array. The encoding format is shown in the following diagram:
```
Expand Down Expand Up @@ -322,7 +340,7 @@ The `field_offset` list is followed by the `value` list.
There are `num_elements` number of `value` entries and each `value` is an encoded Variant value.
For the i-th array entry, the value is the Variant `value` starting from the i-th `field_offset` byte offset.

## Value encoding grammar
### Value encoding grammar

The grammar for an encoded value is:

Expand Down Expand Up @@ -363,7 +381,7 @@ It is semantically identical to the "string" primitive type.

The Decimal type contains a scale, but no precision. The implied precision of a decimal value is `floor(log_10(val)) + 1`.

# Encoding types
## Encoding types

| Basic Type | ID | Description |
|--------------|-----|---------------------------------------------------|
Expand All @@ -374,7 +392,7 @@ The Decimal type contains a scale, but no precision. The implied precision of a

| Logical Type | Physical Type | Type ID | Equivalent Parquet Type | Binary format |
|----------------------|-----------------------------|---------|-----------------------------|---------------------------------------------------------------------------------------------------------------------|
| NullType | null | `0` | any | none |
| NullType | null | `0` | UNKNOWN | none |
| Boolean | boolean (True) | `1` | BOOLEAN | none |
| Boolean | boolean (False) | `2` | BOOLEAN | none |
| Exact Numeric | int8 | `3` | INT(8, signed) | 1 byte |
Expand Down Expand Up @@ -403,7 +421,7 @@ The *Logical Type* column indicates logical equivalence of physically encoded ty
For example, a user expression operating on a string value containing "hello" should behave the same, whether it is encoded with the short string optimization, or long string encoding.
Similarly, user expressions operating on an *int8* value of 1 should behave the same as a decimal16 with scale 2 and unscaled value 100.

# Field ID order and uniqueness
## Field ID order and uniqueness

For objects, field IDs and offsets must be listed in the order of the corresponding field names, sorted lexicographically.
Note that the fields themselves are not required to follow this order.
Expand All @@ -416,14 +434,36 @@ Field names are case-sensitive.
Field names are required to be unique for each object.
It is an error for an object to contain two fields with the same name, whether or not they have distinct dictionary IDs.

# Versions and extensions
## Versions and extensions

An implementation is not expected to parse a Variant value whose metadata version is higher than the version supported by the implementation.
However, new types may be added to the specification without incrementing the version ID.
In such a situation, an implementation should be able to read the rest of the Variant value if desired.

# Shredding
## Shredding

A single Variant object may have poor read performance when only a small subset of fields are needed.
A better approach is to create separate columns for individual fields, referred to as shredding or subcolumnarization.
[VariantShredding.md](VariantShredding.md) describes the Variant shredding specification in Parquet.

## Conversion to JSON

Values stored in the Variant encoding are a superset of JSON values.
For example, a Variant value can be a date that has no equivalent type in JSON.
To maximize compatibility with readers that can process JSON but not Variant, the following conversions should be used when producing JSON from a Variant:

| Variant type | JSON type | Representation requirements | Example |
|---------------|-----------|----------------------------------------------------------|--------------------------------------|
| Null type | null | `null` | `null` |
| Boolean | boolean | `true` or `false` | `true` |
| Exact Numeric | number | Digits in fraction must match scale, no exponent | `34`, 34.00 |
| Float | number | Fraction must be present | `14.20` |
| Double | number | Fraction must be present | `1.0` |
| Date | string | ISO-8601 formatted date | `"2017-11-16"` |
| Timestamp | string | ISO-8601 formatted UTC timestamp including +00:00 offset | `"2017-11-16T22:31:08.000001+00:00"` |
| TimestampNTZ | string | ISO-8601 formatted UTC timestamp with no offset or zone | `"2017-11-16T22:31:08.000001"` |
| Binary | string | Base64 encoded binary | `"dmFyaWFudAo="` |
| String | string | | `"variant"` |
| Array | array | | `[34, "abc", "2017-11-16]` |
| Object | object | | `{"id": 34, "data": "abc"}` |

Loading

0 comments on commit 8352319

Please sign in to comment.