liveBook · Manning

Enum array

As explained in DuckDB in Action

An enum array is an ordered collection whose elements have an enum data type.

An enum array is an ordered collection whose elements use an enum data type. It replaces awkward delimited tag strings with typed values that support list operations and enum functions. section 10.1.7

A practical conversion workflow creates the enum from known tag values, splits the original string into a string array, adds an enum-array column, and assigns the string array to it. DuckDB performs the cast automatically. section 10.1.7

Create and query an enum array

CREATE TYPE tag AS enum (SELECT DISTINCT tagname FROM tags); ALTER TABLE posts ADD tagEnums tag[]; UPDATE posts SET tagEnums = tagNames WHERE posttypeid = 1; SELECT count(*) FROM posts WHERE posttypeid = 1 AND list_contains(tagEnums, 'java'); For aggregations, unnest(tagEnums), group by the resulting tag, and order by the aggregate. section 10.1.7

Listing 10.15 Creating an enum type for tags from the tags table
CREATE TYPE tag AS enum (SELECT DISTINCT tagname FROM tags);

Create the tag enum directly from distinct values in the tags table. section 10.1.7

Listing 10.23 Counting posts with the java tag, using list_contains on the enum list
select count(*)
from posts
where postTypeId=1 and list_contains(tagEnums, 'java');

Use list_contains to count rows containing a particular enum value. section 10.1.7

How DuckDB in Action covers it

How deep it goes
Works through schema conversion, population, membership checks, and aggregation, but does not detail enum internals.
Examples
Uses post tags, DuckDB enum creation, Java membership counts, and top-tag statistics.
What it assumes
Assumes SQL tables and arrays, while demonstrating enum creation, string splitting, casting, and list operations.
What you can do after
Apply it via DuckDB to migrate tag strings and query enum-array membership or aggregates.
How it connects
Connects enum arrays with enums, lists, list_contains, unnest, string splitting, and analytical aggregation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of DuckDB in Action. The section links point to the parts of the book it draws on.