Reputation: 499
Can I store ARRAY column types (e.g. in Postgres) in CockroachDB?
Upvotes: 3
Views: 1219
Reputation: 4645
As in 2022 Dec, CockroachDB support arrays - an ordered collection of values of the same type.
To create an array column in a CockroachDB table, we can use
[]
) to any non-array data type.ARRAY
to any non-array data type.The size of an `ARRAY column is variable but it is recommended to keep value under 1 MB to ensure performance, otherwise performance may be affected.
CREATE TABLE my_table1 (
id INT PRIMARY KEY,
numbers INT ARRAY
);
CREATE TABLE my_table2(
id INT PRIMARY KEY,
numbers INT[]
);
We can insert arrays into thess tables using ARRAY[...]
syntax.
INSERT INTO my_table1(id, numbers) VALUES(1, ARRAY[1,2,3]);
INSERT INTO my_table2(id, numbers) VALUES(1, ARRAY[4,5,6]);
SELECT * from my_table1;
The output is:
id | numbers
-----+----------
1 | {1,2,3}
(1 row)
We use square bracket ([]
) to access 1-indexed array column.
SELECT numbers[1] from my_table2;
The output is:
id | numbers
-----+----------
1 | 4
The two containment operators are:
<@
- is contained by@>
- containsSELECT * FROM my_table1 WHERE numbers <@ ARRAY[1,2,3];
The output is:
id | numbers
-----+----------
1 | {1,2,3}
(1 row)
SELECT * FROM my_table1 WHERE numbers <@ ARRAY[1,2,3,4];
The output is:
id | numbers
-----+----------
1 | {1,2,3}
(1 row)
SELECT * FROM my_table2 WHERE numbers @> ARRAY[4];
The output is:
id | numbers
-----+----------
1 | {4,5,6}
(1 row)
UPDATE my_table1 SET numbers = array_append(numbers, 4) WHERE id = 1;
SELECT * from my_table1;
The output is:
id | numbers
-----+------------
1 | {1,2,3,4}
(1 row)
See more from https://www.cockroachlabs.com/docs/stable/array.html
Upvotes: 0
Reputation: 446
CockroachDB now supports the ARRAY
data type since v1.1 (released Oct 2017).
You can create an array column by adding square brackets to any data type or by using the term ARRAY
CREATE TABLE names (list STRING[]);
INSERT INTO names VALUES (ARRAY['Bertram', 'Wilberforce', 'Wooster']);
SELECT * FROM names;
+-------------------------------------+
| list |
+-------------------------------------+
| {"Bertram","Wilberforce","Wooster"} |
+-------------------------------------+
(1 row)
Details can be found here in the docs: https://www.cockroachlabs.com/docs/stable/array.html
Upvotes: 4
Reputation: 499
CockroachDB doesn’t support arrays yet, but you can follow our progress on this GitHub issue.
Upvotes: 2