> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# VARIANT

> The VARIANT type is supported only for tables in Iceberg Catalog and Paimon Catalog.

**Important**

The VARIANT type is supported only for tables in Iceberg Catalog and Paimon Catalog. It is not supported by native tables.

From v4.1 onwards, the database supports the VARIANT data type for querying semi-structured data from Iceberg tables in Parquet format. From v4.2 onwards, the database also supports the VARIANT data type for querying semi-structured data from Paimon tables. This article introduces the basic concepts of VARIANT, and how the database queries VARIANT-type data and processes it through VARIANT functions.

## What is VARIANT

VARIANT is a semi-structured data type that can store values of different data types, including primitive types (integers, floating-point numbers, strings, booleans, dates, timestamps), and complex types (structs, maps, arrays). VARIANT data is encoded in binary format for efficient storage and querying.

The VARIANT type is particularly useful when working with data from Apache Iceberg tables that use the Parquet format with variant encoding, which allows for flexible schema evolution and efficient storage of heterogeneous data.

For more information on the Parquet variant encoding format, see the [Parquet Variant Encoding specification](https://github.com/apache/parquet-format/blob/master/VariantEncoding.md).

## Using VARIANT Data

### Querying VARIANT-Type Data from Iceberg Tables

The database supports querying VARIANT-type data from Iceberg tables stored in Parquet format. VARIANT columns are automatically recognized when querying Iceberg tables that use Parquet's variant encoding.

Example of querying an Iceberg table with VARIANT columns:

```SQL theme={null}
-- Query a table with VARIANT columns
SELECT
    id,
    variant_col
FROM iceberg_catalog.db.table_with_variants;
```

### Extracting Values from VARIANT Data

The database provides several functions to extract typed values from VARIANT data.

Example 1: Extract primitive values using typed getter functions.

```SQL theme={null}
SELECT
    get_variant_int(variant_col, '$') AS int_value,
    get_variant_string(variant_col, '$') AS string_value,
    get_variant_bool(variant_col, '$') AS bool_value,
    get_variant_double(variant_col, '$') AS double_value
FROM iceberg_catalog.db.table_with_variants;
```

Example 2: Navigate nested structures using JSON path expressions.

```SQL theme={null}
SELECT
    get_variant_string(variant_col, '$.user.name') AS user_name,
    get_variant_int(variant_col, '$.user.age') AS user_age,
    get_variant_string(variant_col, '$.address.city') AS city
FROM iceberg_catalog.db.table_with_variants;
```

Example 3: Access array elements.

```SQL theme={null}
SELECT
    get_variant_int(variant_col, '$.scores[0]') AS first_score,
    get_variant_int(variant_col, '$.scores[1]') AS second_score
FROM iceberg_catalog.db.table_with_variants;
```

Example 4: Query nested variant data and return VARIANT type.

```SQL theme={null}
SELECT
    variant_query(variant_col, '$.metadata') AS metadata,
    variant_query(variant_col, '$.items[0]') AS first_item
FROM iceberg_catalog.db.table_with_variants;
```

Example 5: Check the type of a VARIANT value.

```SQL theme={null}
SELECT
    variant_typeof(variant_col) AS root_type,
    variant_typeof(variant_query(variant_col, '$.data')) AS data_type
FROM iceberg_catalog.db.table_with_variants;
```

### Casting JSON to VARIANT

The database supports casting JSON values to VARIANT. If your input is a STRING, convert it to JSON first.

```SQL theme={null}
SELECT
    CAST(parse_json('{"id": 1, "flags": {"active": true}, "scores": [1.5, null]}') AS VARIANT) AS variant_value;
```

```SQL theme={null}
SELECT
    CAST(json_col AS VARIANT) AS variant_value
FROM db.table_with_json;
```

### Casting VARIANT Data to SQL Types

You can use the CAST function to convert VARIANT data to standard SQL types:

```SQL theme={null}
SELECT
    CAST(variant_query(variant_col, '$.count') AS INT) AS count,
    CAST(variant_query(variant_col, '$.price') AS DECIMAL(10, 2)) AS price,
    CAST(variant_query(variant_col, '$.active') AS BOOLEAN) AS is_active,
    CAST(variant_query(variant_col, '$.name') AS STRING) AS name
FROM iceberg_catalog.db.table_with_variants;
```

Complex types can also be cast from VARIANT:

```SQL theme={null}
SELECT
    CAST(variant_col AS STRUCT<id INT, name STRING>) AS user_struct,
    CAST(variant_col AS MAP<STRING, INT>) AS config_map,
    CAST(variant_col AS ARRAY<DOUBLE>) AS values_array
FROM iceberg_catalog.db.table_with_variants;
```

### Casting SQL Types to VARIANT

You can cast SQL values to VARIANT. Supported input types include BOOLEAN, integer types, FLOAT/DOUBLE, DECIMAL, STRING/CHAR/VARCHAR, JSON, DATE/DATETIME/TIME, and complex types (ARRAY, MAP, STRUCT). For MAP, keys are cast to strings during encoding. Types such as HLL, BITMAP, PERCENTILE, and VARBINARY are not supported.

```SQL theme={null}
SELECT
    CAST(123 AS VARIANT) AS v_int,
    CAST(3.14 AS VARIANT) AS v_double,
    CAST(CAST('12.34' AS DECIMAL(10, 2)) AS VARIANT) AS v_decimal,
    CAST('hello' AS VARIANT) AS v_string,
    CAST(PARSE_JSON('{"k":1}') AS VARIANT) AS v_json;
```

## VARIANT Functions

VARIANT functions can be used to query and extract data from VARIANT columns. For detailed information, please refer to the individual function documentation:

* [variant\_query](/documents/4.2/sql-reference/sql-functions/variant-functions/variant_query.md): Queries a path in a VARIANT value and returns a VARIANT
* [get\_variant](/documents/4.2/sql-reference/sql-functions/variant-functions/get_variant.md): Extracts typed values (int, bool, double, string) from a VARIANT
* [variant\_typeof](/documents/4.2/sql-reference/sql-functions/variant-functions/variant_typeof.md): Returns the type name of a VARIANT value

## VARIANT Path Expressions

VARIANT functions use JSON path expressions to navigate through the data structure. The path syntax is similar to JSON path:

* `$` represents the root of the VARIANT value
* `.` is used to access object fields
* `[index]` is used to access array elements (0-based indexing)
* Field names with special characters (like dots) can be quoted: `$."field.name"`

Examples of path expressions:

```plaintext theme={null}
$                      -- Root element
$.field                -- Object field access
$.nested.field         -- Nested field access
$."field.with.dots"    -- Quoted field name
$[0]                   -- First array element
$.array[1]             -- Second element of array field
$.users[0].name        -- Nested array access
$.config["key"]        -- Map-style access
```

## Data Type Conversion

When data is read from Parquet files with variant encoding, the following type conversions are supported:

| Parquet Variant Type | the database VARIANT Type |
| - | - |
| INT8, INT16, INT32, INT64 | int8, int16, int32, int64 |
| FLOAT, DOUBLE | float, double |
| BOOLEAN | boolean |
| STRING | string |
| DATE | Date |
| TIMESTAMP, TIMESTAMP\_NTZ | Timestamp |
| DECIMAL | Decimal, float, double |
| STRUCT | Object |
| MAP | Object |
| ARRAY | Array |

## Limitations and Considerations

* VARIANT is supported for reading data from Iceberg tables in Parquet format with variant encoding, and for writing Parquet files using the database file writers (unshredded variant encoding).
* VARIANT is also supported for reading data from Paimon tables (Paimon requires Parquet for variant columns), but only for splits read by the native reader (append-only tables and compacted primary-key data). Reading VARIANT from uncompacted primary-key data requires the JNI reader and is not yet supported; such queries are rejected with a plan-time error.
* The size of a VARIANT value is limited to 16 MB.
* Currently only unshredded variant values are supported for both read and write.
* VARIANT can be created by casting from JSON values or supported SQL types (including ARRAY, MAP, and STRUCT).
* The maximum depth of nested structures depends on the underlying Parquet file structure.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.