diff --git a/VariantEncoding.md b/VariantEncoding.md index 1eac3bcbe..e835f7741 100644 --- a/VariantEncoding.md +++ b/VariantEncoding.md @@ -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. ``` @@ -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 @@ -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`. ``` @@ -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. @@ -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`. ``` @@ -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`. ``` @@ -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`. ``` @@ -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: @@ -281,7 +299,7 @@ The `field_id` list must be `[, , , , , ]`. 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: ``` @@ -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: @@ -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 | |--------------|-----|---------------------------------------------------| @@ -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 | @@ -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. @@ -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"}` | + diff --git a/VariantShredding.md b/VariantShredding.md index 51160a9bc..86e15401a 100644 --- a/VariantShredding.md +++ b/VariantShredding.md @@ -23,7 +23,7 @@ > **This specification is still under active development, and has not been formally adopted.** The Variant type is designed to store and process semi-structured data efficiently, even with heterogeneous values. -Query engines encode each Variant value in a self-describing format, and store it as a group containing `value` and `metadata` binary fields in Parquet. +Query engines encode each Variant value in a self-describing format, and store it as a group containing required `value` and `metadata` binary fields in Parquet. Since data is often partially homogenous, it can be beneficial to extract certain fields into separate Parquet columns to further improve performance. We refer to this process as **shredding**. Each Parquet file remains fully self-describing, with no additional metadata required to read or fully reconstruct the Variant data from the file. @@ -33,140 +33,245 @@ This document focuses on the shredding semantics, Parquet representation, implic For now, it does not discuss which fields to shred, user-facing API changes, or any engine-specific considerations like how to use shredded columns. The approach builds upon the [Variant Binary Encoding](VariantEncoding.md), and leverages the existing Parquet specification. -At a high level, we replace the `value` field of the Variant Parquet group with one or more fields called `object`, `array`, `typed_value`, and `variant_value`. -These represent a fixed schema suitable for constructing the full Variant value for each row. - Shredding allows a query engine to reap the full benefits of Parquet's columnar representation, such as more compact data encoding, min/max statistics for data skipping, and I/O and CPU savings from pruning unnecessary fields not accessed by a query (including the non-shredded Variant binary data). Without shredding, any query that accesses a Variant column must fetch all bytes of the full binary buffer. -With shredding, we can get nearly equivalent performance as in a relational (scalar) data model. +With shredding, readers can get nearly equivalent performance as in a relational (scalar) data model. -For example, `select variant_get(variant_col, ‘$.field1.inner_field2’, ‘string’) from tbl` only needs to access `inner_field2`, and the file scan could avoid fetching the rest of the Variant value if this field was shredded into a separate column in the Parquet schema. -Similarly, for the query `select * from tbl where variant_get(variant_col, ‘$.id’, ‘integer’) = 123`, the scan could first decode the shredded `id` column, and only fetch/decode the full Variant value for rows that pass the filter. +For example, `SELECT variant_get(variant_event, '$.event_ts', 'timestamp') FROM tbl` only needs to access `event_ts`, and the file scan could avoid fetching the rest of the Variant value if this field was shredded into a separate column in the Parquet schema. +Similarly, for the query `SELECT * FROM tbl WHERE variant_get(variant_event, '$.event_type', 'string') = 'signup'`, the scan could first decode the shredded `event_type` column, and only fetch/decode the full Variant event value for rows that pass the filter. -# Parquet Example +## Variant Metadata -Consider the following Parquet schema together with how Variant values might be mapped to it. -Notice that we represent each shredded field in `object` as a group of two fields, `typed_value` and `variant_value`. -We extract all homogenous data items of a certain path into `typed_value`, and set aside incompatible data items in `variant_value`. -Intuitively, incompatibilities within the same path may occur because we store the shredding schema per Parquet file, and each file can contain several row groups. -Selecting a type for each field that is acceptable for all rows would be impractical because it would require buffering the contents of an entire file before writing. +Variant metadata is stored in the top-level Variant group in a binary `metadata` column regardless of whether the Variant value is shredded. +All `variant_value` columns within the Variant must use the same `metadata`. -Typically, the expectation is that `variant_value` exists at every level as an option, along with one of `object`, `array` or `typed_value`. -If the actual Variant value contains a type that does not match the provided schema, it is stored in `variant_value`. -An `variant_value` may also be populated if an object can be partially represented: any fields that are present in the schema must be written to those fields, and any missing fields are written to `variant_value`. +## Value Shredding -The `metadata` column is unchanged from its unshredded representation, and may be referenced in `variant_value` fields in the shredded data. +Variant values are stored in Parquet fields named `variant_value`. +Each `variant_value` field may have an associated shredded field named `typed_value` that stores the value when it matches a specific type. +For example, a Variant field, `measurement` may be shredded as long values by adding `typed_value` with type `int64`: ``` -optional group variant_col { - required binary metadata; - optional binary variant_value; - optional group object { - optional group a { - optional binary variant_value; - optional int64 typed_value; - } - optional group b { - optional binary variant_value; - optional group object { - optional group c { - optional binary variant_value; - optional binary typed_value (STRING); +optional group measurement (VARIANT) { + required binary metadata; + optional binary variant_value; + optional int64 typed_value; +} +``` + +Both `variant_value` and `typed_value` are optional fields used together to encode a single value. +Values in the two fields must be interpreted according to the following table: + +| `variant_value` | `typed_value` | Meaning | +| null | null | The value is missing | +| non-null | null | The value is present and may be any type, including null | +| null | non-null | The value is present and the shredded type | +| non-null | non-null | The value is present and a partially shredded object | + +An object is _partially shredded_ when the `variant_value` is an object and the `typed_value` is a shredded object. + +If both fields are non-null and either is not an object, the value is invalid. Readers must either fail or return the `variant_value`. + +### Shredded Value Types + +Shredded values must use the following Parquet types: + +| Variant Type | Equivalent Parquet Type | +|-----------------------------|------------------------------| +| boolean | BOOLEAN | +| int8 | INT(8, signed=true) | +| int16 | INT(16, signed=true) | +| int32 | INT32 / INT(32, signed=true) | +| int64 | INT64 / INT(64, signed=true) | +| float | FLOAT | +| double | DOUBLE | +| decimal4 | DECIMAL(precision, scale) | +| decimal8 | DECIMAL(precision, scale) | +| decimal16 | DECIMAL(precision, scale) | +| date | DATE | +| timestamp | TIMESTAMP(true, MICROS) | +| timestamp without time zone | TIMESTAMP(false, MICROS) | +| binary | BINARY | +| string | STRING | +| array | LIST; see Arrays below | +| object | GROUP; see Objects below | + +#### Primitive Types + +Primitive values can be shredded using the equivalent Parquet primitive type from the table above for `typed_object`. + +Unless the value is shredded in an object field, `typed_value` or `variant_value` (but not both) must be non-null. + +#### Arrays + +Arrays can be shredded using a 3-level Parquet list for `typed_value`. + +If the value is not an array, `typed_value` must be null. +If the value is an array, `variant_value` must be null. + +The list `element` must be a required group that contains a `variant_type` (`binary`) and may contain a shredded `typed_value` field. + +For example, a `tags` Variant may be shredded as a list of strings using the following definition: +``` +optional group tags (VARIANT) { + required binary metadata; + optional binary variant_value; + optional group typed_value (LIST) { // must be optional to allow a null list + repeated group list { + required group element { + optional binary variant_value; + optional binary typed_value (STRING); + } } - } } - } } ``` -| Variant Value | Top-level variant_value | b.variant_value | a.typed_value | a.variant_value | b.object.c.typed_value | b.object.c.variant_value | Notes | -|---------------|-------------------------|-----------------|---------------|-----------------|------------------------|--------------------------|-------| -| {a: 123, b: {c: “hello”}} | null | null | 123 | null | hello | null | All values shredded | -| {a: 1.23, b: {c: “123”}} | null | null | null | 1.23 | 123 | null | a is not an integer | -| {a: 123, b: {c: null}} | null | null | null | 123 | null | null | b.object.c set to non-null to indicate VariantNull | -| {a: 123, b: {} | null | null | null | 123 | null | null | b.object.c set to null, to indicate that c is missing | -| {a: 123, d: 456} | {d: 456} | null | 123 | null | null | null | Extra field d is stored as variant_value | -| [{a: 1, b: {c: 2}}, {a: 3, b: {c: 4}}] | [{a: 1, b: {c: 2}}, {a: 3, b: {c: 4}}] | null | null | null | null | null | Not an object | +All elements of an array must be non-null, since `array` elements cannote be missing. +Either `typed_value` or `variant_value` (but not both) must be non-null. -# Parquet Layout +#### Objects (Option 1) -The `array` and `object` fields represent Variant array and object types, respectively. -Arrays must use the three-level list structure described in https://github.com/apache/parquet-format/blob/master/LogicalTypes.md. +Fields of an object can be shredded using a Parquet group for `typed_value` that contains shredded fields. -An `object` field must be a group. -Each field name of this inner group corresponds to the Variant value's object field name. -Each inner field's type is a recursively shredded variant value: that is, the fields of each object field must be one or more of `object`, `array`, `typed_value` or `variant_value`. +If the value is not an object, `typed_value` must be null. -Similarly the elements of an `array` must be a group containing one or more of `object`, `array`, `typed_value` or `variant_value`. + +If the value is a partially shredded object, the `variant_value` must not contain shredded fields. If such fields are present, the object is invalid and readers must either fail or use the values from the `variant_value`. -Each leaf in the schema can store an arbitrary Variant value. -It contains an `variant_value` binary field and a `typed_value` field. -If non-null, `variant_value` represents the value stored as a Variant binary. -The `typed_value` field may be any type that has a corresponding Variant type. -For each value in the data, at most one of the `typed_value` and `variant_value` may be non-null. -A writer may omit either field, which is equivalent to all rows being null. +Each shredded field is represented as a required group that contains a `variant_value` and a `typed_value` field. -Dictionary IDs in a `variant_value` field refer to entries in the top-level `metadata` field. +For example, a Variant `event` field may shred `event_type` (`string`) and `event_ts` (`timestamp`) columns using the following definition: +``` +optional group event (VARIANT) { + required binary metadata; + optional binary variant_value; + optional group typed_value { + required group event_type { + optional binary variant_value; + optional binary typed_value (STRING); + } + required group event_ts { + optional binary variant_value; + optional int64 typed_value (TIMESTAMP(true, MICROS)); + } + } +} +``` -For an `object`, a null field means that the field does not exist in the reconstructed Variant object. -All elements of an `array` must be non-null, since array elements cannote be missing. +The group for each named field is required. +A field's `variant_value` and `typed_value` are set to null (missing) to indicate that the field does not exist in the variant. -| typed_value | variant_value | Meaning | -|-------------|----------------|---------| -| null | null | Field is Variant Null (not missing) in the reconstructed Variant. | -| null | non-null | Field may be any type in the reconstructed Variant. | -| non-null | null | Field has this column’s type in the reconstructed Variant. | -| non-null | non-null | Invalid | +#### Objects (Option 2) -The `typed_value` may be absent from the Parquet schema for any field, which is equivalent to its value being always null (in which case the shredded field is always stored as a Variant binary). -By the same token, `variant_value` may be absent, which is equivalent to their value being always null (in which case the field will always have the value Null or have the type of the `typed_value` column). +Fields of an object can be shredded using a Parquet group for `typed_value` that contains shredded fields. -# Unshredded values +If the value is not an object, `typed_value` must be null. -If all values can be represented at a given level by whichever of `object`, `array`, or `typed_value` is present, `variant_value` is set to null. +If the value is a partially shredded object, the `variant_value` contains non-shredded fields. If a field name is defined in the `variant_value` and the shredded field is non-null, the object is invalid and readers must either fail or return the value contained in `variant_value`. -If a value cannot be represented by whichever of `object`, `array`, or `typed_value` is present in the schema, then it is stored in `variant_value`, and the other fields are set to null. -In the Parquet example above, if field `a` was an object or array, or a non-integer scalar, it would be stored in `variant_value`. +Each shredded field of the object is shredded as an optional field using the equivalent Parquet type from the table above using the field name. -If a value is an object, and the `object` field is present but does not contain all of the fields in the value, then any remaining fields are stored in an object in `variant_value`. -In the Parquet example above, if field `b` was an object of the form `{"c": 1, "d": 2}"`, then the object `{"d": 2}` would be stored in `variant_value`, and the `c` field would be shredded recursively under `object.c`. +For example, a Variant `event` field may shred `event_type` (`string`) and `event_ts` (`timestamp`) columns using the following definition: +``` +optional group event (VARIANT) { + required binary metadata; + optional binary variant_value; + optional group typed_value { + optional binary event_type (STRING); + optional int64 event_ts (TIMESTAMP(true, MICROS)); + } +} +``` + +A shredded field set to null to indicate that the field does not exist in the variant or is defined in `variant_value`. + +## Nesting -Note that an array is always fully shredded if there is an `array` field, so the above consideration for `object` is not relevant for arrays: only one of `array` or `variant_value` may be non-null at a given level. +``` +optional group event (VARIANT) { + required binary metadata; + optional binary variant_value; + optional group typed_value { + required group event_type { + optional binary variant_value; + optional binary typed_value (STRING); + } + required group event_ts { + optional binary variant_value; + optional int64 typed_value (TIMESTAMP(true, MICROS)); + } + required group location { + optional binary variant_value; + optional group typed_value { + required group latitude { + optional binary variant_value; + optional double typed_value; + } + required group longitude { + optional binary variant_value; + optional double typed_value; + } + } + } + required group tags { + optional binary variant_value; + optional group typed_value (LIST) { + repeated group list { + required group element { + optional binary variant_value; + optional binary typed_value (STRING); + } + } + } + } + } +} +``` -# Using variant_value vs. typed_value + +``` +optional group event (VARIANT) { + required binary metadata; + optional binary variant_value; + optional group typed_value { + optional binary event_type (STRING); + optional int64 event_ts (TIMESTAMP(true, MICROS)); + optional group location { + optional double latitude; + optional double longitude; + } + optional group tags (LIST) { + repeated group list { + required group element { + optional binary variant_value; + optional binary typed_value (STRING); + } + } + } + } +} +``` -In general, it is desirable to store values in the `typed_value` field rather than the `variant_value` whenever possible. -This will typically improve encoding efficiency, and allow the use of Parquet statistics to filter at the row group or page level. -In the best case, the `variant_value` fields are all null and the engine does not need to read them (or it can omit them from the schema on write entirely). -There are two main motivations for including the `variant_value` column: -1) In a case where there are rare type mismatches (for example, a numeric field with rare strings like “n/a”), we allow the field to be shredded, which could still be a significant performance benefit compared to fetching and decoding the full value/metadata binary. -2) Since there is a single schema per file, there would be no easy way to recover from a type mismatch encountered late in a file write. Parquet files can be large, and buffering all file data before starting to write could be expensive. Including a variant column for every field guarantees we can adhere to the requested shredding schema. +| Variant Value | Top-level variant_value | b.variant_value | a.typed_value | a.variant_value | b.object.c.typed_value | b.object.c.variant_value | Notes | +|---------------|-------------------------|-----------------|---------------|-----------------|------------------------|--------------------------|-------| +| {a: 123, b: {c: "hello"}} | null | null | 123 | null | hello | null | All values shredded | +| {a: 1.23, b: {c: "123"}} | null | null | null | 1.23 | 123 | null | a is not an integer | +| {a: 123, b: {c: null}} | null | null | null | 123 | null | null | b.object.c set to non-null to indicate VariantNull | +| {a: 123, b: {} | null | null | null | 123 | null | null | b.object.c set to null, to indicate that c is missing | +| {a: 123, d: 456} | {d: 456} | null | 123 | null | null | null | Extra field d is stored as variant_value | +| [{a: 1, b: {c: 2}}, {a: 3, b: {c: 4}}] | [{a: 1, b: {c: 2}}, {a: 3, b: {c: 4}}] | null | null | null | null | null | Not an object | -# Data Skipping +## Data Skipping Shredded columns are expected to store statistics in the same format as a normal Parquet column. In general, the engine can only skip a row group or page if all rows in the `variant_value` field are null, since it is possible for a `variant_get` expression to successfully cast a value from the `variant_value` to the target type. -For example, if `typed_value` is of type `int64`, then the string “123” might be contained in `variant_value`, which would not be reflected in statistics, but could be retained by a filter like `where variant_get(col, “$.field”, “long”) = 123`. +For example, if `typed_value` is of type `int64`, then the string "123" might be contained in `variant_value`, which would not be reflected in statistics, but could be retained by a filter like `where variant_get(col, "$.field", "long") = 123`. If `variant_value` is all-null, then the engine can prune pages or row groups based on `typed_value`. This specification is not strict about what values may be stored in `variant_value` rather than `typed_value`, so it is not safe to skip rows based on `typed_value` unless the corresponding `variant_value` column is all-null, or the engine has specific knowledge of the behavior of the writer that produced the shredded data. -# Shredding Semantics - -Reconstruction of Variant value from a shredded representation is not expected to produce a bit-for-bit identical binary to the original unshredded value. -For example, the order of fields in the binary may change, as may the physical representation of scalar values. - -In particular, the [Variant Binary Encoding](VariantEncoding.md) considers all integer and decimal representations to represent a single logical type. -As a result, it is valid to shred a decimal into a decimal column with a different scale, or to shred an integer as a decimal, as long as no numeric precision is lost. -For example, it would be valid to write the value 123 to a Decimal(9, 2) column, but the value 1.234 would need to be written to the **variant_value** column. -When reconstructing, it would be valid for a reader to reconstruct 123 as an integer, or as a Decimal(9, 2). -Engines should not depend on the physical type of a Variant value, only the logical type. - -On the other hand, shredding as a different logical type is not allowed. -For example, the integer value 123 could not be shredded to a string `typed_value` column as the string "123", since that would lose type information. -It would need to be written to the `variant_value` column. - -# Reconstructing a Variant +## Reconstructing a Variant It is possible to recover a full Variant value using a recursive algorithm, where the initial call is to `ConstructVariant` with the top-level fields, which are assumed to be null if they are not present in the schema. @@ -202,7 +307,7 @@ def ConstructArray(array): newVariantArray.append(ConstructVariant(array[i].variant_value, array[i].object, array[i].array, array[i].typed_value) ``` -# Nested Parquet Example +## Nested Parquet Example This section describes a more deeply nested example, using a top-level array as the shredding type. @@ -223,7 +328,7 @@ It contains an array of objects, containing an `a` field shredded as an array, a ``` -The corresponding Parquet schema with “a” and “b” as leaf types is: +The corresponding Parquet schema with "a" and "b" as leaf types is: ``` optional group variant_col { @@ -256,7 +361,7 @@ optional group variant_col { } ``` -In the above example schema, if “a” is an array containing a mix of integer and non-integer values, the engine will shred individual elements appropriately into either `typed_value` or `variant_value`. +In the above example schema, if "a" is an array containing a mix of integer and non-integer values, the engine will shred individual elements appropriately into either `typed_value` or `variant_value`. If the top-level Variant is not an array (for example, an object), the engine cannot shred the value and it will store it in the top-level `variant_value`. Similarly, if "a" is not an array, it will be stored in the `variant_value` under "a". @@ -267,13 +372,13 @@ Consider the following example: { "a": [1, 2, 3], "b": 100, - “c”: “unexpected” + "c": "unexpected" }, { "a": [4, 5, 6], "b": 200 }, - “not an object” + "not an object" ] ``` @@ -281,13 +386,13 @@ The second array element can be fully shredded, but the first and third cannot b ``` [ - { “c”: “unexpected” }, + { "c": "unexpected" }, NULL, - “not an object” + "not an object" ] ``` -# Backward and forward compatibility +## Backward and forward compatibility Shredding is an optional feature of Variant, and readers must continue to be able to read a group containing only a `value` and `metadata` field.