@@ -135,7 +135,7 @@ module Squeal.PostgreSQL.Expression
135 , jsonbPretty
135 , jsonbPretty
136 -- ** Aggregation
136 -- ** Aggregation
137 , Aggregate (..)
137 , Aggregate (..)
138− , DistinctExpression (..)
138+ , Distinction (..)
140 , countStar
140 , countStar
141 -- * Window Functions
141 -- * Window Functions
@@ -1394,15 +1394,15 @@ jsonbPretty = unsafeFunction "jsonb_pretty"
1394aggregation
1394aggregation
1395-----------------------------------------}
1395-----------------------------------------}
1397−class Aggregate expr aggr where
1397+class Aggregate expr aggr | aggr -> expr where
1399 -- | >>> :{
1399 -- | >>> :{
1401 -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1401 -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1402− -- expression = count #col
1402+ -- expression = count (All #col)
1403 -- in printSQL expression
1403 -- in printSQL expression
1405− -- count("col")
1405+ -- count(ALL "col")
1407 :: expr commons schemas params from ty
1407 :: expr commons schemas params from ty
1408 -- ^ what to count
1408 -- ^ what to count
@@ -1411,10 +1411,10 @@ class Aggregate expr aggr where
1411 -- | >>> :{
1411 -- | >>> :{
1413 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1413 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1414− -- expression = sum_ #col
1414+ -- expression = sum_ (Distinct #col)
1415 -- in printSQL expression
1415 -- in printSQL expression
1417− -- sum("col")
1417+ -- sum(DISTINCT "col")
1419 :: ty `In` PGNum
1419 :: ty `In` PGNum
1420 => expr commons schemas params from (nullity ty)
1420 => expr commons schemas params from (nullity ty)
@@ -1423,7 +1423,7 @@ class Aggregate expr aggr where
1423 -- | >>> :{
1423 -- | >>> :{
1425 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1425 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1426− -- expression = bitAndDistinct #col
1426+ -- expression = bitAnd (Distinct #col)
1427 -- in printSQL expression
1427 -- in printSQL expression
1429 -- bit_and(DISTINCT "col")
1429 -- bit_and(DISTINCT "col")
@@ -1436,10 +1436,10 @@ class Aggregate expr aggr where
1436 -- | >>> :{
1436 -- | >>> :{
1438 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1438 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1439− -- expression = bitOr #col
1439+ -- expression = bitOr (All #col)
1440 -- in printSQL expression
1440 -- in printSQL expression
1442− -- bit_or("col")
1442+ -- bit_or(ALL "col")
1444 :: int `In` PGIntegral
1444 :: int `In` PGIntegral
1445 => expr commons schemas params from (nullity int)
1445 => expr commons schemas params from (nullity int)
@@ -1448,11 +1448,11 @@ class Aggregate expr aggr where
1449 -- | >>> :{
1449 -- | >>> :{
1451− -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1451+ -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1452− -- expression = boolAnd #col
1452+ -- expression = boolAnd (All #col)
1453 -- in printSQL expression
1453 -- in printSQL expression
1455− -- bool_and("col")
1455+ -- bool_and(ALL "col")
1457 :: expr commons schemas params from (nullity 'PGbool)
1457 :: expr commons schemas params from (nullity 'PGbool)
1458 -- ^ what to aggregate
1458 -- ^ what to aggregate
@@ -1460,11 +1460,11 @@ class Aggregate expr aggr where
1461 -- | >>> :{
1461 -- | >>> :{
1463− -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1463+ -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1464− -- expression = boolOr #col
1464+ -- expression = boolOr (All #col)
1465 -- in printSQL expression
1465 -- in printSQL expression
1467− -- bool_or("col")
1467+ -- bool_or(ALL "col")
1469 :: expr commons schemas params from (nullity 'PGbool)
1469 :: expr commons schemas params from (nullity 'PGbool)
1470 -- ^ what to aggregate
1470 -- ^ what to aggregate
@@ -1474,11 +1474,11 @@ class Aggregate expr aggr where
1475 -- >>> :{
1475 -- >>> :{
1477− -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1477+ -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1478− -- expression = every #col
1478+ -- expression = every (Distinct #col)
1479 -- in printSQL expression
1479 -- in printSQL expression
1481− -- every("col")
1481+ -- every(DISTINCT "col")
1483 :: expr commons schemas params from (nullity 'PGbool)
1483 :: expr commons schemas params from (nullity 'PGbool)
1484 -- ^ what to aggregate
1484 -- ^ what to aggregate
@@ -1502,19 +1502,17 @@ class Aggregate expr aggr where
1502 -- ^ what to average
1502 -- ^ what to average
1503 -> aggr commons schemas params from (nullity (PGAvg ty))
1503 -> aggr commons schemas params from (nullity (PGAvg ty))
1505+data Distinction commons schemas params from ty
1505−instance {-# OVERLAPPABLE #-} expr ~ (Expression 'Ungrouped)
1506+ = All (Expression 'Ungrouped commons schemas params from ty)
1506− => Aggregate expr (Expression ('Grouped bys)) where
1507+ | Distinct (Expression 'Ungrouped commons schemas params from ty)
1507− count = unsafeAggregate "count"
1508+ deriving (GHC.Generic,Show,Eq,Ord)
1508− sum_ = unsafeAggregate "sum"
1509+instance NFData (Distinction commons schemas params from ty)
1509− bitAnd = unsafeAggregate "bit_and"
1510+instance RenderSQL (Distinction commons schemas params from ty) where
1510− bitOr = unsafeAggregate "bit_or"
1511+ renderSQL = \case
1511− boolAnd = unsafeAggregate "bool_and"
1512+ All x -> "ALL" <+> renderSQL x
1512− boolOr = unsafeAggregate "bool_or"
1513+ Distinct x -> "DISTINCT" <+> renderSQL x
1513− every = unsafeAggregate "every"
1514+
1514− max_ = unsafeAggregate "max"
1515− min_ = unsafeAggregate "min"
1516− avg = unsafeAggregate "avg"
1517−instance {-# OVERLAPPING #-} Aggregate DistinctExpression (Expression ('Grouped bys)) where
1515+instance Aggregate Distinction (Expression ('Grouped bys)) where
1518 count = unsafeAggregate "count"
1516 count = unsafeAggregate "count"
1519 sum_ = unsafeAggregate "sum"
1517 sum_ = unsafeAggregate "sum"
1520 bitAnd = unsafeAggregate "bit_and"
1518 bitAnd = unsafeAggregate "bit_and"
@@ -1537,12 +1535,6 @@ instance Aggregate (Expression grp) (WindowFunction grp) where
1537 min_ = unsafeWindowFunction1 "min"
1535 min_ = unsafeWindowFunction1 "min"
1538 avg = unsafeWindowFunction1 "avg"
1536 avg = unsafeWindowFunction1 "avg"
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
1546-- | escape hatch to define aggregate functions
1538-- | escape hatch to define aggregate functions
1547unsafeAggregate
1539unsafeAggregate
1548 :: RenderSQL (expr commons schemas params from xty)
1540 :: RenderSQL (expr commons schemas params from xty)
@@ -1561,6 +1553,7 @@ type family PGAvg ty where
1561 PGAvg 'PGfloat4 = 'PGfloat8
1553 PGAvg 'PGfloat4 = 'PGfloat8
1562 PGAvg 'PGfloat8 = 'PGfloat8
1554 PGAvg 'PGfloat8 = 'PGfloat8
1563 PGAvg 'PGinterval = 'PGinterval
1555 PGAvg 'PGinterval = 'PGinterval
1556+ -- PGAvg pg = TypeError
1565-- | A special aggregation that does not require an input
1558-- | A special aggregation that does not require an input