More SqlDelight (UnNest with PostgreSql)
15 May 2025Initial Support for UnNest in SqlDelight 2.1.0-SNAPSHOT
NOTE: Only unnest
table function is supported
- UNNEST function that takes one array
- UNNEST table row function that takes one or more array
- UNNEST bulk insert, delete and update using arrays
Example
https://github.com/griffio/sqldelight-postgres-unnest
Schema
CREATE TABLE Business(
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
zipcodes TEXT[] NOT NULL,
headcounts INTEGER[] NOT NULL
);
CREATE TABLE Users (
name TEXT NOT NULL,
age INTEGER NOT NULL
);
Queries
select:
SELECT name, location.headcount, location.zipcode
FROM Business, UNNEST(zipcodes, headcounts) AS location(zipcode, headcount);
counts:
SELECT name, UNNEST(headcounts) AS headcount
FROM Business
ORDER BY headcount DESC;
array:
SELECT unnest(ARRAY[1,2]);
insertUsers:
INSERT INTO Users (name, age)
SELECT * FROM UNNEST(?::TEXT[], ?::INTEGER[]);
updateUsers:
UPDATE Users
SET age=updates.updated_age
FROM UNNEST(?::TEXT[], ?::INTEGER[]) AS updates(name, updated_age)
WHERE Users.name = updates.name;
deleteUsers:
DELETE FROM Users
WHERE (name, age) IN (
SELECT *
FROM UNNEST(?::TEXT[], ?::INTEGER[]) AS u(name, age)
);
selectLocations:
SELECT DISTINCT b.*
FROM Business b
JOIN LATERAL UNNEST(b.zipcodes) AS loc(zipcode) ON loc.zipcode ILIKE '%' || :query::TEXT || '%';
--Same as above can also be written without explict join
--SELECT DISTINCT b.*
--FROM Business b, UNNEST(b.zipcodes) AS loc(zipcode) WHERE loc.zipcode ILIKE '%' || :query::TEXT || '%';