@@ -135,7 +135,7 @@ module Squeal.PostgreSQL.Expression
135135 , jsonbPretty
136136 -- ** Aggregation
137137 , Aggregate (..)
138− , DistinctExpression (..)
138+ , Distinction (..)
139139 , PGAvg
140140 , countStar
141141 -- * Window Functions
@@ -1394,15 +1394,15 @@ jsonbPretty = unsafeFunction "jsonb_pretty"
13941394aggregation
13951395-----------------------------------------}
13961396
1397−class Aggregate expr aggr where
1397+class Aggregate expr aggr | aggr -> expr where
13981398
13991399 -- | >>> :{
14001400 -- let
14011401 -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1402− -- expression = count #col
1402+ -- expression = count (All #col)
14031403 -- in printSQL expression
14041404 -- :}
1405− -- count("col")
1405+ -- count(ALL "col")
14061406 count
14071407 :: expr commons schemas params from ty
14081408 -- ^ what to count
@@ -1411,10 +1411,10 @@ class Aggregate expr aggr where
14111411 -- | >>> :{
14121412 -- let
14131413 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1414− -- expression = sum_ #col
1414+ -- expression = sum_ (Distinct #col)
14151415 -- in printSQL expression
14161416 -- :}
1417− -- sum("col")
1417+ -- sum(DISTINCT "col")
14181418 sum_
14191419 :: ty `In` PGNum
14201420 => expr commons schemas params from (nullity ty)
@@ -1423,7 +1423,7 @@ class Aggregate expr aggr where
14231423 -- | >>> :{
14241424 -- let
14251425 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1426− -- expression = bitAndDistinct #col
1426+ -- expression = bitAnd (Distinct #col)
14271427 -- in printSQL expression
14281428 -- :}
14291429 -- bit_and(DISTINCT "col")
@@ -1436,10 +1436,10 @@ class Aggregate expr aggr where
14361436 -- | >>> :{
14371437 -- let
14381438 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1439− -- expression = bitOr #col
1439+ -- expression = bitOr (All #col)
14401440 -- in printSQL expression
14411441 -- :}
1442− -- bit_or("col")
1442+ -- bit_or(ALL "col")
14431443 bitOr
14441444 :: int `In` PGIntegral
14451445 => expr commons schemas params from (nullity int)
@@ -1448,11 +1448,11 @@ class Aggregate expr aggr where
14481448
14491449 -- | >>> :{
14501450 -- let
1451− -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1452− -- expression = boolAnd #col
1451+ -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1452+ -- expression = boolAnd (All #col)
14531453 -- in printSQL expression
14541454 -- :}
1455− -- bool_and("col")
1455+ -- bool_and(ALL "col")
14561456 boolAnd
14571457 :: expr commons schemas params from (nullity 'PGbool)
14581458 -- ^ what to aggregate
@@ -1460,11 +1460,11 @@ class Aggregate expr aggr where
14601460
14611461 -- | >>> :{
14621462 -- let
1463− -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1464− -- expression = boolOr #col
1463+ -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1464+ -- expression = boolOr (All #col)
14651465 -- in printSQL expression
14661466 -- :}
1467− -- bool_or("col")
1467+ -- bool_or(ALL "col")
14681468 boolOr
14691469 :: expr commons schemas params from (nullity 'PGbool)
14701470 -- ^ what to aggregate
@@ -1474,11 +1474,11 @@ class Aggregate expr aggr where
14741474 --
14751475 -- >>> :{
14761476 -- let
1477− -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1478− -- expression = every #col
1477+ -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1478+ -- expression = every (Distinct #col)
14791479 -- in printSQL expression
14801480 -- :}
1481− -- every("col")
1481+ -- every(DISTINCT "col")
14821482 every
14831483 :: expr commons schemas params from (nullity 'PGbool)
14841484 -- ^ what to aggregate
@@ -1502,19 +1502,17 @@ class Aggregate expr aggr where
15021502 -- ^ what to average
15031503 -> aggr commons schemas params from (nullity (PGAvg ty))
15041504
1505−instance {-# OVERLAPPABLE #-} expr ~ (Expression 'Ungrouped)
1506− => Aggregate expr (Expression ('Grouped bys)) where
1507− count = unsafeAggregate "count"
1508− sum_ = unsafeAggregate "sum"
1509− bitAnd = unsafeAggregate "bit_and"
1510− bitOr = unsafeAggregate "bit_or"
1511− boolAnd = unsafeAggregate "bool_and"
1512− boolOr = unsafeAggregate "bool_or"
1513− every = unsafeAggregate "every"
1514− max_ = unsafeAggregate "max"
1515− min_ = unsafeAggregate "min"
1516− avg = unsafeAggregate "avg"
1517−instance {-# OVERLAPPING #-} Aggregate DistinctExpression (Expression ('Grouped bys)) where
1505+data Distinction commons schemas params from ty
1506+ = All (Expression 'Ungrouped commons schemas params from ty)
1507+ | Distinct (Expression 'Ungrouped commons schemas params from ty)
1508+ deriving (GHC.Generic,Show,Eq,Ord)
1509+instance NFData (Distinction commons schemas params from ty)
1510+instance RenderSQL (Distinction commons schemas params from ty) where
1511+ renderSQL = \case
1512+ All x -> "ALL" <+> renderSQL x
1513+ Distinct x -> "DISTINCT" <+> renderSQL x
1514+
1515+instance Aggregate Distinction (Expression ('Grouped bys)) where
15181516 count = unsafeAggregate "count"
15191517 sum_ = unsafeAggregate "sum"
15201518 bitAnd = unsafeAggregate "bit_and"
@@ -1537,12 +1535,6 @@ instance Aggregate (Expression grp) (WindowFunction grp) where
15371535 min_ = unsafeWindowFunction1 "min"
15381536 avg = unsafeWindowFunction1 "avg"
15391537
1540−newtype DistinctExpression commons schemas params from ty
1541− = Distinct (Expression 'Ungrouped commons schemas params from ty)
1542− deriving (GHC.Generic,Show,Eq,Ord,NFData)
1543−instance RenderSQL (DistinctExpression commons schemas params from ty) where
1544− renderSQL (Distinct x) = "DISTINCT" <+> renderSQL x
1545−
15461538-- | escape hatch to define aggregate functions
15471539unsafeAggregate
15481540 :: RenderSQL (expr commons schemas params from xty)
@@ -1561,6 +1553,7 @@ type family PGAvg ty where
15611553 PGAvg 'PGfloat4 = 'PGfloat8
15621554 PGAvg 'PGfloat8 = 'PGfloat8
15631555 PGAvg 'PGinterval = 'PGinterval
1556+ -- PGAvg pg = TypeError
15641557
15651558-- | A special aggregation that does not require an input
15661559--