on dev help sign in
Make sum returns a nullable value

From PostgreSQL documentation: > It should be noted that except for count, these functions return a null value when no rows are selected. In particular, sum of no rows returns null, not zero as one might expect, and array_agg returns null rather than an empty array when there are no input rows. The coalesce function can be used to substitute zero or an empty array for null when necessary. So the `sum` aggregate should not return a `NotNull` column.

commit35569e2df0f56fe8ca1732e90ae8178fa0321301 authorRaveline <eraveline@gmail.com> dateMon 29 Apr 2019, 15:02 (7 years ago) parentb2955a5
2 files changed+7−7
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Aggregate.hs+2−2 squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+5−5
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Aggregate.hs+2−2
@@ -79,7 +79,7 @@ class Aggregate expr1 exprN aggr
7979
8080 -- | >>> :{
8181 -- let
82− -- expression :: Expression '[] commons ('Grouped bys) schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('NotNull 'PGnumeric)
82+ -- expression :: Expression '[] commons ('Grouped bys) schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
8383 -- expression = sum_ (Distinct #col)
8484 -- in printSQL expression
8585 -- :}
@@ -87,7 +87,7 @@ class Aggregate expr1 exprN aggr
8787 sum_
8888 :: ty `In` PGNum
8989 => expr1 (null ty)
90− -> aggr ('NotNull ty)
90+ -> aggr ('Null ty)
9191
9292 -- | input values, including nulls, concatenated into an array
9393 arrayAgg
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+5−5
@@ -207,13 +207,13 @@ aggregation query:
207207let
208208 query :: Query_ (Public Schema) () (Sum Int32, P ("col1" ::: Int32))
209209 query =
210− select_ (sum_ (All #col2) `as` #getSum :* #col1)
210+ select_ ((fromNull 0 (sum_ (All #col2))) `as` #getSum :* #col1)
211211 ( from (table (#tab `as` #table1))
212212 & groupBy #col1
213− & having (#col1 + sum_ (Distinct #col2) .> 1) )
213+ & having (#col1 + (fromNull 0 (sum_ (Distinct #col2))) .> 1) )
214214in printSQL query
215215:}
216−SELECT sum(ALL "col2") AS "getSum", "col1" AS "col1" FROM "tab" AS "table1" GROUP BY "col1" HAVING (("col1" + sum(DISTINCT "col2")) > 1)
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)
217217
218218sorted query:
219219
@@ -1021,10 +1021,10 @@ instance With (Query outer) where
10211021 `unionAll`
10221022 select_ ((#n + 1) `as` #n)
10231023 (from (common #t) & where_ (#n .< 100)) `as` #t )
1024− ( select_ (sum_ (All #n) `as` #getSum) (from (common #t) & groupBy Nil))
1024+ ( select_ (fromNull 0 (sum_ (All #n)) `as` #getSum) (from (common #t) & groupBy Nil))
10251025 in printSQL query
10261026:}
1027−WITH RECURSIVE "t" AS ((SELECT * FROM (VALUES (1)) AS t ("n")) UNION ALL (SELECT ("n" + 1) AS "n" FROM "t" AS "t" WHERE ("n" < 100))) SELECT sum(ALL "n") AS "getSum" FROM "t" AS "t"
1027+WITH RECURSIVE "t" AS ((SELECT * FROM (VALUES (1)) AS t ("n")) UNION ALL (SELECT ("n" + 1) AS "n" FROM "t" AS "t" WHERE ("n" < 100))) SELECT COALESCE(sum(ALL "n"), 0) AS "getSum" FROM "t" AS "t"
10281028-}
10291029withRecursive
10301030 :: Aliased (Query outer (recursive ': commons) schemas params) recursive