docs/src/main/sphinx/functions/variant.md
The VARIANT type represents a semi-structured value as defined by the
Apache Iceberg Variant specification.
VARIANT values are created using casts, decoded using casts, and dereferenced
using the SQL subscript operator ([]).
Two VARIANT values are equal when they represent the same logical value,
regardless of internal encoding details.
This means equality is based on value semantics, not byte-for-byte encoding. For example:
TIMESTAMP and TIMESTAMP WITH TIME ZONE remain distinct timestamp kinds
and are not equal to each other.For numbers, additional edge-case rules apply:
1, 1.0, and 1.00 are equal.REAL, DOUBLE) are equal to exact numerics only
when the floating-point value can be represented exactly as a variant decimal.
Example: 0.5 equals DECIMAL '0.5', but 0.1 does not equal
DECIMAL '0.1', because binary floating-point cannot represent 0.1 exactly.+0.0 and -0.0 are equal.NaN is not equal to any value, including itself.Elements of a VARIANT value can be accessed using the SQL subscript operator
([]). The result of a subscript operation is always a VARIANT value.
When the underlying value is an object, use a VARCHAR key:
variant_expression['key']
If the specified key does not exist in the object, the result is SQL NULL.
When the underlying value is an array, use a bigint with one-based indexing:
variant_expression[index]
The same SQL array indexing rules apply:
10 or negative indexes are invalid and result in an error:::{function} variant_is_null(variant) -> boolean
Returns true if the input value represents a variant null.
This function distinguishes a variant null value from SQL NULL.
true if the value is a variant nullfalse for all other variant valuesNULL if the input is SQL NULLExample:
SELECT variant_is_null(CAST(JSON 'null' AS VARIANT)); -- true
SELECT variant_is_null(CAST(42 AS VARIANT)); -- false
SELECT variant_is_null(NULL); -- NULL
:::
The following SQL types can be cast to VARIANT:
BOOLEANTINYINTSMALLINTINTEGERBIGINTREALDOUBLEDECIMALVARCHARVARBINARYDATETIME(p)TIMESTAMP(p)TIMESTAMP(p) WITH TIME ZONEUUIDJSONVARIANTARRAYMAP (with VARCHAR key type)ROWContainer values may contain any supported scalar or container type, including
nested containers, JSON, and VARIANT values.
A VARIANT value can be cast to the following SQL types when the underlying
value is compatible with the target type.
Standard Trino cast coercions apply. For example, a VARIANT value containing
a string can be cast to a numeric type if the string represents a valid value
for the target type and fits within its range.
BOOLEANTINYINTSMALLINTINTEGERBIGINTREALDOUBLEDECIMALVARCHARVARBINARYDATETIME(p)TIMESTAMP(p)TIMESTAMP(p) WITH TIME ZONEUUIDJSONVARIANTARRAYMAP (with VARCHAR key type)ROWCasting to container types is supported when the structure of the target type
is compatible with the contents of the VARIANT value. If the underlying value
is incompatible with the requested type, the cast fails.