This section describes functions that operate on the VARIANT type.
New Most variant_* functions were introduced in DuckDB 2.0. The variant_extract, variant_normalize, and variant_typeof functions were already available in DuckDB 1.5.
Aggregate. Collects all values of x into a VARIANT array.
The functions variant_type, variant_keys, variant_extract_string, variant_array_length and variant_exists take a path argument that refers to a field inside the VARIANT value. A path is the name of a field; the empty string '' refers to the value itself. Instead of a single path, a list of paths can be passed. In that case, the result is a list with one result per path. If a path does not exist, the result for that path is NULL, except for variant_exists, which returns false. A NULL path returns NULL, while a list of paths that contains NULL raises an error.
Returns the detailed type of a VARIANT value. For objects, the result lists the keys in sorted order, e.g., OBJECT(a, b, c). For arrays, it includes the number of elements, e.g., ARRAY(3). For primitive values, it names the stored type, e.g., INT32 or DECIMAL(18, 6). A NULL field inside a VARIANT is reported as VARIANT_NULL. This is useful for inspecting the structure of semi-structured data.
Returns the top-level type category of a VARIANT value, such as OBJECT, ARRAY, VARCHAR or INT32. Unlike variant_typeof, the result does not describe nested structure. With path, the type of the value at that path is returned; a field that holds NULL returns VARIANT_NULL, while a path that does not exist returns NULL.
Returns the keys of a VARIANT object as a list, in sorted order. Arrays and primitive values have no keys and return an empty list. With path, the keys of the object at that path are returned.
Returns the value of an object field or a list element as a VARIANT. field is either a field name or a list index. List indexes are 1-based; index 0 raises an error. Because the result is a VARIANT, calls can be chained, also in method syntax: v.variant_extract('a').variant_extract(1).
Returns the value at path as a VARCHAR. Objects and arrays are rendered as JSON text. Unlike variant_extract, which returns a VARIANT, the result can be used directly where a string is expected. A field that holds NULL returns NULL.
Returns whether needle is contained in haystack. For objects, containment is subset matching: every field of needle must occur in haystack, regardless of key order. For arrays, every element of needle must occur in haystack, regardless of element order. needle can match a value at any depth of haystack; object keys are not values and do not match. Numbers compare by value across integer and decimal types, but integer and floating-point values do not match each other. This is useful for filtering semi-structured data on a partial structure.
Returns a sort key for a VARIANT value. Values with equal logical values produce equal keys: object key order does not matter, integer and decimal values of equal value are equal, and so are FLOAT and DOUBLE values of equal value. Integer and floating-point values remain distinct. NULL returns NULL. This is useful for comparing VARIANT values by content, e.g., variant_comparator(a) = variant_comparator(b).
VARIANT values sort by their logical value, also across integer and decimal types, so that sorting a VARIANT column matches the order of the underlying values:
Returns a normalized representation of a VARIANT value. The normalized value compares equal to the original value, including for less common types such as TIME_NS, TIMESTAMPTZ_NS and BIGNUM.
Aggregate function. Returns a VARIANT object with one field per row of the group, using key (a VARCHAR) as the field name and value as its value. This is useful for turning key-value rows into a single object. A NULL key or a duplicate key raises an error; with DISTINCT, identical key-value pairs are removed before duplicate keys are checked. A NULL value becomes a NULL field. An empty group returns NULL.
Aggregate function. Collects all values of x into a VARIANT array. Values of different types keep their own types, and NULL values are kept as NULL elements. An empty group returns NULL. variant_group_array can also be used as a window function.