I have an H2 column with type TEXT ARRAY
.
CREATE TABLE app.worklist
(
id INT,
profession TEXT ARRAY
);
I would like to query if any array element matches a LIKE
parameter.
The following works in Postgres:
SELECT * FROM app.worklist
WHERE EXISTS (
SELECT
FROM unnest(profession) AS p
WHERE p LIKE 'A%'
);
However, it fails in H2 org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "PROFESSION" not found;
due to a long unfixed bug.
How can I accomplish this in H2?
After days of research, I determined that it is not possible to do this cleanly in H2.
Our team used a workaround. When the data is ingested, we flatten the array.
CREATE TABLE app.worklist
(
id INT,
profession VARCHAR(65536)
);
We joined each element with control character 0x1a
. ["foo", "bar"]
becomes "{0x1a}foo{0x1a}bar{0x1a}"
val profession = professionArray.joinToString(
separator = "\u001A",
prefix = "\u001A",
postfix = "\u001A",
)
To search, we execute:
SELECT *
FROM app.worklist
WHERE profession LIKE '{0x1a}A%';
This solution only works if
'A%'
, '%A'
, or '%A%'
; it doesn't work for combined expressions like 'A%B'
.