@@ -145,13 +145,20 @@ records, whose entries will be targeted using overloaded labels.
145145
146146Let's see some examples of queries.
147147
148+>>> :set -XDeriveAnyClass -XDerivingStrategies
149+>>> :{
150+data Row a b = Row { col1 :: a, col2 :: b }
151+ deriving stock (GHC.Generic)
152+ deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
153+:}
154+
148155simple query:
149156
150157>>> type Columns = '["col1" ::: 'NoDef :=> 'NotNull 'PGint4, "col2" ::: 'NoDef :=> 'NotNull 'PGint4]
151158>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
152159>>> :{
153160let
154− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
161+ query :: Query_ (Public Schema) () (Row Int32 Int32)
155162 query = select Star (from (table #tab))
156163in printSQL query
157164:}
@@ -161,21 +168,21 @@ restricted query:
161168
162169>>> :{
163170let
164− query :: Query_ (Public Schema) () (Sum Int32, P ("col1" ::: Int32))
171+ query :: Query_ (Public Schema) () (Row Int32 Int32)
165172 query =
166− select_ ((#col1 + #col2) `as` #getSum :* #col1)
173+ select_ ((#col1 + #col2) `as` #col1 :* #col1 `as` #col2)
167174 ( from (table #tab)
168175 & where_ (#col1 .> #col2)
169176 & where_ (#col2 .> 0) )
170177in printSQL query
171178:}
172−SELECT ("col1" + "col2") AS "getSum", "col1" AS "col1" FROM "tab" AS "tab" WHERE (("col1" > "col2") AND ("col2" > 0))
179+SELECT ("col1" + "col2") AS "col1", "col1" AS "col2" FROM "tab" AS "tab" WHERE (("col1" > "col2") AND ("col2" > 0))
173180
174181subquery:
175182
176183>>> :{
177184let
178− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
185+ query :: Query_ (Public Schema) () (Row Int32 Int32)
179186 query = select Star (from (subquery (select Star (from (table #tab)) `as` #sub)))
180187in printSQL query
181188:}
@@ -185,7 +192,7 @@ limits and offsets:
185192
186193>>> :{
187194let
188− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
195+ query :: Query_ (Public Schema) () (Row Int32 Int32)
189196 query = select Star (from (table #tab) & limit 100 & offset 2 & limit 50 & offset 2)
190197in printSQL query
191198:}
@@ -195,7 +202,7 @@ parameterized query:
195202
196203>>> :{
197204let
198− query :: Query_ (Public Schema) (Only Int32) (P ("col1" ::: Int32), P ("col2" ::: Int32))
205+ query :: Query_ (Public Schema) (Only Int32) (Row Int32 Int32)
199206 query = select Star (from (table #tab) & where_ (#col1 .> param @1))
200207in printSQL query
201208:}
@@ -205,21 +212,21 @@ aggregation query:
205212
206213>>> :{
207214let
208− query :: Query_ (Public Schema) () (Sum Int32, P ("col1" ::: Int32))
215+ query :: Query_ (Public Schema) () (Row Int32 Int32)
209216 query =
210− select_ ((fromNull 0 (sum_ (All #col2))) `as` #getSum :* #col1)
217+ select_ ((fromNull 0 (sum_ (All #col2))) `as` #col1 :* #col1 `as` #col2)
211218 ( from (table (#tab `as` #table1))
212219 & groupBy #col1
213220 & having (#col1 + (fromNull 0 (sum_ (Distinct #col2))) .> 1) )
214221in printSQL query
215222:}
216−SELECT COALESCE(sum(ALL "col2"), 0) AS "getSum", "col1" AS "col1" FROM "tab" AS "table1" GROUP BY "col1" HAVING (("col1" + COALESCE(sum(DISTINCT "col2"), 0)) > 1)
223+SELECT COALESCE(sum(ALL "col2"), 0) AS "col1", "col1" AS "col2" FROM "tab" AS "table1" GROUP BY "col1" HAVING (("col1" + COALESCE(sum(DISTINCT "col2"), 0)) > 1)
217224
218225sorted query:
219226
220227>>> :{
221228let
222− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
229+ query :: Query_ (Public Schema) () (Row Int32 Int32)
223230 query = select Star (from (table #tab) & orderBy [#col1 & Asc])
224231in printSQL query
225232:}
@@ -282,8 +289,10 @@ self-join:
282289
283290>>> :{
284291let
285− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
286− query = select (#t1 & DotStar) (from (table (#tab `as` #t1) & crossJoin (table (#tab `as` #t2))))
292+ query :: Query_ (Public Schema) () (Row Int32 Int32)
293+ query = select
294+ (#t1 & DotStar)
295+ (from (table (#tab `as` #t1) & crossJoin (table (#tab `as` #t2))))
287296in printSQL query
288297:}
289298SELECT "t1".* FROM "tab" AS "t1" CROSS JOIN "tab" AS "t2"
@@ -292,17 +301,19 @@ value queries:
292301
293302>>> :{
294303let
295− query :: Query_ schemas () (P ("foo" ::: Int32), P ("bar" ::: Bool))
296− query = values (1 `as` #foo :* true `as` #bar) [2 `as` #foo :* false `as` #bar]
304+ query :: Query_ schemas () (Row String Bool)
305+ query = values
306+ ("true" `as` #col1 :* true `as` #col2)
307+ ["false" `as` #col1 :* false `as` #col2]
297308in printSQL query
298309:}
299−SELECT * FROM (VALUES (1, TRUE), (2, FALSE)) AS t ("foo", "bar")
310+SELECT * FROM (VALUES (E'true', TRUE), (E'false', FALSE)) AS t ("col1", "col2")
300311
301312set operations:
302313
303314>>> :{
304315let
305− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
316+ query :: Query_ (Public Schema) () (Row Int32 Int32)
306317 query = select Star (from (table #tab)) `unionAll` select Star (from (table #tab))
307318in printSQL query
308319:}
@@ -312,7 +323,7 @@ with queries:
312323
313324>>> :{
314325let
315− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("col2" ::: Int32))
326+ query :: Query_ (Public Schema) () (Row Int32 Int32)
316327 query = with (
317328 select Star (from (table #tab)) `as` #cte1 :>>
318329 select Star (from (common #cte1)) `as` #cte2
@@ -325,27 +336,27 @@ window function queries
325336
326337>>> :{
327338let
328− query :: Query_ (Public Schema) () (P ("col1" ::: Int32), P ("rank" ::: Int64))
339+ query :: Query_ (Public Schema) () (Row Int32 Int64)
329340 query = select
330− (#col1 & Also (rank `as` #rank `Over` (partitionBy #col1 & orderBy [#col2 & Asc])))
341+ (#col1 & Also (rank `as` #col2 `Over` (partitionBy #col1 & orderBy [#col2 & Asc])))
331342 (from (table #tab))
332343in printSQL query
333344:}
334−SELECT "col1" AS "col1", rank() OVER (PARTITION BY "col1" ORDER BY "col2" ASC) AS "rank" FROM "tab" AS "tab"
345+SELECT "col1" AS "col1", rank() OVER (PARTITION BY "col1" ORDER BY "col2" ASC) AS "col2" FROM "tab" AS "tab"
335346
336347correlated subqueries
337348
338349>>> :{
339350let
340− query :: Query_ (Public Schema) () (P ("col1" ::: Int32))
351+ query :: Query_ (Public Schema) () (Only Int32)
341352 query =
342− select #col1 (from (table (#tab `as` #t1))
353+ select (#col1 `as` #fromOnly) (from (table (#tab `as` #t1))
343354 & where_ (exists (
344355 select Star (from (table (#tab `as` #t2))
345356 & where_ (#t2 ! #col2 .== #t1 ! #col1)))))
346357in printSQL query
347358:}
348−SELECT "col1" AS "col1" FROM "tab" AS "t1" WHERE EXISTS (SELECT * FROM "tab" AS "t2" WHERE ("t2"."col2" = "t1"."col1"))
359+SELECT "col1" AS "fromOnly" FROM "tab" AS "t1" WHERE EXISTS (SELECT * FROM "tab" AS "t2" WHERE ("t2"."col2" = "t1"."col1"))
349360
350361-}
351362type family Query_