on dev help sign in
Make sum returns a nullable value
35569e2Raveline
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Aggregate.hs+2−2
@@ -79,7 +79,7 @@ class Aggregate expr1 exprN aggr
79
79
80 -- | >>> :{
80 -- | >>> :{
81 -- let
81 -- 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)
83 -- expression = sum_ (Distinct #col)
83 -- expression = sum_ (Distinct #col)
84 -- in printSQL expression
84 -- in printSQL expression
85 -- :}
85 -- :}
@@ -87,7 +87,7 @@ class Aggregate expr1 exprN aggr
87 sum_
87 sum_
88 :: ty `In` PGNum
88 :: ty `In` PGNum
89 => expr1 (null ty)
89 => expr1 (null ty)
90− -> aggr ('NotNull ty)
90+ -> aggr ('Null ty)
91
91
92 -- | input values, including nulls, concatenated into an array
92 -- | input values, including nulls, concatenated into an array
93 arrayAgg
93 arrayAgg
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+5−5
@@ -207,13 +207,13 @@ aggregation query:
207let
207let
208 query :: Query_ (Public Schema) () (Sum Int32, P ("col1" ::: Int32))
208 query :: Query_ (Public Schema) () (Sum Int32, P ("col1" ::: Int32))
209 query =
209 query =
210− select_ (sum_ (All #col2) `as` #getSum :* #col1)
210+ select_ ((fromNull 0 (sum_ (All #col2))) `as` #getSum :* #col1)
211 ( from (table (#tab `as` #table1))
211 ( from (table (#tab `as` #table1))
212 & groupBy #col1
212 & groupBy #col1
213− & having (#col1 + sum_ (Distinct #col2) .> 1) )
213+ & having (#col1 + (fromNull 0 (sum_ (Distinct #col2))) .> 1) )
214in printSQL query
214in printSQL query
215:}
215:}
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)
217
217
218sorted query:
218sorted query:
219
219
@@ -1021,10 +1021,10 @@ instance With (Query outer) where
1021 `unionAll`
1021 `unionAll`
1022 select_ ((#n + 1) `as` #n)
1022 select_ ((#n + 1) `as` #n)
1023 (from (common #t) & where_ (#n .< 100)) `as` #t )
1023 (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))
1025 in printSQL query
1025 in printSQL query
1026:}
1026:}
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"
1028-}
1028-}
1029withRecursive
1029withRecursive
1030 :: Aliased (Query outer (recursive ': commons) schemas params) recursive
1030 :: Aliased (Query outer (recursive ': commons) schemas params) recursive