DuckDB 2.0 VARIANT expression helpers for Ecto queries.
VARIANT columns decode to Elixir terms: objects become maps with string keys, arrays become lists, and scalars decode like the matching column type. These helpers inspect and extract inside a VARIANT on the server:
import Ecto.Query
import QuackDB.Ecto.Variant
from event in "events",
where: variant_type(event.payload) == "OBJECT",
select: %{
kind: variant_extract_string(event.payload, "kind"),
first_tag: variant_extract(event.payload, [:tags, 1]),
keys: variant_keys(event.payload)
}A key is a field name; an integer extracts from an array, counting from 1.
A list such as [:tags, 1] extracts one step at a time.
Summary
Functions
Builds variant_array_length(variant), or variant_array_length(variant, key).
Builds variant_contains(haystack, needle); the needle is cast to VARIANT.
Builds variant_extract(variant, key), which returns a VARIANT. Pass a list
to extract along a path.
Builds variant_extract_string(variant, key), the field rendered as text.
Builds variant_keys(variant), or variant_keys(variant, key) for a nested object.
Builds variant_type(variant), the VARIANT type name such as "OBJECT",
"ARRAY", or "INT64", or variant_type(variant, key) for a field.
Builds variant_typeof(variant), the DuckDB type the value would have unwrapped.
Functions
Builds variant_array_length(variant), or variant_array_length(variant, key).
Builds variant_contains(haystack, needle); the needle is cast to VARIANT.
Builds variant_extract(variant, key), which returns a VARIANT. Pass a list
to extract along a path.
Builds variant_extract_string(variant, key), the field rendered as text.
Builds variant_keys(variant), or variant_keys(variant, key) for a nested object.
Builds variant_type(variant), the VARIANT type name such as "OBJECT",
"ARRAY", or "INT64", or variant_type(variant, key) for a field.
Builds variant_typeof(variant), the DuckDB type the value would have unwrapped.