Skip to content

Collection types: SET, MULTISET, LIST/SEQUENCE

View as Markdown

A collection column stores several values of the same element type in one column of one row. CoraDB has three collection types, which differ in how they treat duplicates and order:

Type Duplicates Order Input Stored
SET Removed Sorted {'c','c','c','b','b','a'} {'a', 'b', 'c'}
MULTISET Kept Sorted {'c','c','c','b','b','a'} {'a', 'b', 'b', 'c', 'c', 'c'}
LIST or SEQUENCE Kept As entered {'c','c','c','b','b','a'} {'c', 'c', 'c', 'b', 'b', 'a'}

A collection column is declared with the element type after the collection keyword, with or without parentheses:

col_name SET VARCHAR(20)
col_name MULTISET (VARCHAR(20))
col_name LIST SHORT
col_name SEQUENCE (INT)

A collection value is written in braces, with the elements separated by commas: {'running', 'shoes'}, {42, 40, 41}. A literal in braces on its own is a SEQUENCE; it is converted to the column's type on INSERT or UPDATE, or with CAST:

SELECT CAST({'c','c','c','b','b','a'} AS SET) AS s, CAST({'c','c','c','b','b','a'} AS MULTISET) AS m, CAST({'c','c','c','b','b','a'} AS LIST) AS l;
s m l
{'a', 'b', 'c'} {'a', 'b', 'b', 'c', 'c', 'c'} {'c', 'c', 'c', 'b', 'b', 'a'}

Restrictions

  • Elements can be of any type except BLOB and CLOB (Can not define 'set of clob' as a column type).
  • A collection column cannot have a collation: SET (STRING) COLLATE utf8_en_ci is a syntax error.
  • A scalar value is not a collection: INSERT INTO t VALUES ('') into a SET column fails with Casting '' to type set is not supported. Write {''}.
  • Collections are never converted implicitly to other types. Use CAST between collection types.

Converting between collection types

Any collection type can be cast to any other. Converting to SET removes duplicates, and converting to SET or MULTISET sorts the elements. Converting a SET or MULTISET to LIST keeps the sorted order; the original input order of a MULTISET is not kept.

The following table stores tags and available sizes for products of the e-commerce sample database:

CREATE TABLE product_extra (
product_id INT PRIMARY KEY,
tags SET VARCHAR(20),
sizes LIST SHORT
);
INSERT INTO product_extra VALUES (1618, {'running', 'shoes', 'running'}, {42, 40, 41, 40});
SELECT product_id, tags, sizes FROM product_extra;
product_id tags sizes
1618 {'running', 'shoes'} {42, 40, 41, 40}

The duplicate tag is removed by the SET; the LIST keeps the sizes as entered, duplicates included.

Collections are queried with the containment operators, such as IN and SUBSETEQ, described in Containment operators:

SELECT product_id FROM product_extra WHERE 'shoes' IN tags;
SELECT product_id FROM product_extra WHERE tags SUBSETEQ {'running', 'shoes', 'sale'};

Both queries return product 1618.

A SET holds distinct values, sorted. NULL and the empty string are values like any other:

Inserted into a SET (CHAR(1)) column Stored
{'c','c','c','b','b','a'} {'a', 'b', 'c'}
{NULL} {NULL}
{''} {' '} (CHAR(1) pads the empty string to one space)

A MULTISET keeps duplicates and sorts the elements. {'c','c','c','b','b','a'} is stored as {'a', 'b', 'b', 'c', 'c', 'c'}; cast to SET it is {'a', 'b', 'c'}.

A LIST (or SEQUENCE, the same type) keeps duplicates and the order in which the elements were entered. {'c','c','c','b','b','a'} is stored as is; cast to SET it is {'a', 'b', 'c'}, cast to MULTISET {'a', 'b', 'b', 'c', 'c', 'c'}.

The list functions work on LIST values, and TYPEOF reports a LIST as sequence.