@@ -134,15 +134,10 @@ module Squeal.PostgreSQL.Expression
134134 , jsonbInsert
135135 , jsonbPretty
136136 -- ** Aggregation
137− , unsafeAggregate, unsafeAggregateDistinct
138− , sum_, sumDistinct
137+ , Aggregate (..)
138+ , DistinctExpression (..)
139139 , PGAvg (avg, avgDistinct)
140− , bitAnd, bitOr, boolAnd, boolOr
141− , bitAndDistinct, bitOrDistinct, boolAndDistinct, boolOrDistinct
142140 , countStar
143− , count, countDistinct
144− , every, everyDistinct
145− , max_, maxDistinct, min_, minDistinct
146141 -- * Window Functions
147142 , WindowDefinition (..)
148143 , partitionBy
@@ -1399,10 +1394,144 @@ jsonbPretty = unsafeFunction "jsonb_pretty"
13991394aggregation
14001395-----------------------------------------}
14011396
1397+class Aggregate expr aggr where
1398+
1399+ -- | >>> :{
1400+ -- let
1401+ -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1402+ -- expression = count #col
1403+ -- in printSQL expression
1404+ -- :}
1405+ -- count("col")
1406+ count
1407+ :: expr commons schemas params from ty
1408+ -- ^ what to count
1409+ -> aggr commons schemas params from ('NotNull 'PGint8)
1410+
1411+ -- | >>> :{
1412+ -- let
1413+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1414+ -- expression = sum_ #col
1415+ -- in printSQL expression
1416+ -- :}
1417+ -- sum("col")
1418+ sum_
1419+ :: ty `In` PGNum
1420+ => expr commons schemas params from (nullity ty)
1421+ -> aggr commons schemas params from (nullity ty)
1422+
1423+ -- | >>> :{
1424+ -- let
1425+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1426+ -- expression = bitAndDistinct #col
1427+ -- in printSQL expression
1428+ -- :}
1429+ -- bit_and(DISTINCT "col")
1430+ bitAnd
1431+ :: int `In` PGIntegral
1432+ => expr commons schemas params from (nullity int)
1433+ -- ^ what to aggregate
1434+ -> aggr commons schemas params from (nullity int)
1435+
1436+ -- | >>> :{
1437+ -- let
1438+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1439+ -- expression = bitOr #col
1440+ -- in printSQL expression
1441+ -- :}
1442+ -- bit_or("col")
1443+ bitOr
1444+ :: int `In` PGIntegral
1445+ => expr commons schemas params from (nullity int)
1446+ -- ^ what to aggregate
1447+ -> aggr commons schemas params from (nullity int)
1448+
1449+ -- | >>> :{
1450+ -- let
1451+ -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1452+ -- expression = boolAnd #col
1453+ -- in printSQL expression
1454+ -- :}
1455+ -- bool_and("col")
1456+ boolAnd
1457+ :: expr commons schemas params from (nullity 'PGbool)
1458+ -- ^ what to aggregate
1459+ -> aggr commons schemas params from (nullity 'PGbool)
1460+
1461+ -- | >>> :{
1462+ -- let
1463+ -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1464+ -- expression = boolOr #col
1465+ -- in printSQL expression
1466+ -- :}
1467+ -- bool_or("col")
1468+ boolOr
1469+ :: expr commons schemas params from (nullity 'PGbool)
1470+ -- ^ what to aggregate
1471+ -> aggr commons schemas params from (nullity 'PGbool)
1472+
1473+ -- | synonym for `boolAnd`
1474+ --
1475+ -- >>> :{
1476+ -- let
1477+ -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1478+ -- expression = every #col
1479+ -- in printSQL expression
1480+ -- :}
1481+ -- every("col")
1482+ every
1483+ :: expr commons schemas params from (nullity 'PGbool)
1484+ -- ^ what to aggregate
1485+ -> aggr commons schemas params from (nullity 'PGbool)
1486+
1487+ -- | minimum and maximum aggregation
1488+ max_, min_
1489+ :: expr commons schemas params from (nullity ty)
1490+ -- ^ what to aggregate
1491+ -> aggr commons schemas params from (nullity ty)
1492+
1493+instance Aggregate (Expression 'Ungrouped) (Expression ('Grouped bys)) where
1494+ count = unsafeAggregate "count"
1495+ sum_ = unsafeAggregate "sum"
1496+ bitAnd = unsafeAggregate "bit_and"
1497+ bitOr = unsafeAggregate "bit_or"
1498+ boolAnd = unsafeAggregate "bool_and"
1499+ boolOr = unsafeAggregate "bool_or"
1500+ every = unsafeAggregate "every"
1501+ max_ = unsafeAggregate "max"
1502+ min_ = unsafeAggregate "min"
1503+instance Aggregate DistinctExpression (Expression ('Grouped bys)) where
1504+ count = unsafeAggregate "count"
1505+ sum_ = unsafeAggregate "sum"
1506+ bitAnd = unsafeAggregate "bit_and"
1507+ bitOr = unsafeAggregate "bit_or"
1508+ boolAnd = unsafeAggregate "bool_and"
1509+ boolOr = unsafeAggregate "bool_or"
1510+ every = unsafeAggregate "every"
1511+ max_ = unsafeAggregate "max"
1512+ min_ = unsafeAggregate "min"
1513+instance Aggregate (Expression grp) (WindowFunction grp) where
1514+ count = unsafeWindowFunction1 "count"
1515+ sum_ = unsafeWindowFunction1 "sum"
1516+ bitAnd = unsafeWindowFunction1 "bit_and"
1517+ bitOr = unsafeWindowFunction1 "bit_or"
1518+ boolAnd = unsafeWindowFunction1 "bool_and"
1519+ boolOr = unsafeWindowFunction1 "bool_or"
1520+ every = unsafeWindowFunction1 "every"
1521+ max_ = unsafeWindowFunction1 "max"
1522+ min_ = unsafeWindowFunction1 "min"
1523+
1524+newtype DistinctExpression commons schemas params from ty
1525+ = Distinct (Expression 'Ungrouped commons schemas params from ty)
1526+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
1527+instance RenderSQL (DistinctExpression commons schemas params from ty) where
1528+ renderSQL (Distinct x) = "DISTINCT" <+> renderSQL x
1529+
14021530-- | escape hatch to define aggregate functions
14031531unsafeAggregate
1404− :: ByteString -- ^ aggregate function
1405− -> Expression 'Ungrouped commons schemas params from xty
1532+ :: RenderSQL (expr commons schemas params from xty)
1533+ => ByteString -- ^ aggregate function
1534+ -> expr commons schemas params from xty
14061535 -> Expression ('Grouped bys) commons schemas params from yty
14071536unsafeAggregate fun x = UnsafeExpression $ mconcat
14081537 [fun, "(", renderSQL x, ")"]
@@ -1415,34 +1544,6 @@ unsafeAggregateDistinct
14151544unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
14161545 [fun, "(DISTINCT ", renderSQL x, ")"]
14171546
1418−-- | >>> :{
1419−-- let
1420−-- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1421−-- expression = sum_ #col
1422−-- in printSQL expression
1423−-- :}
1424−-- sum("col")
1425−sum_
1426− :: ty `In` PGNum
1427− => Expression 'Ungrouped commons schemas params from (nullity ty)
1428− -- ^ what to sum
1429− -> Expression ('Grouped bys) commons schemas params from (nullity ty)
1430−sum_ = unsafeAggregate "sum"
1431−
1432−-- | >>> :{
1433−-- let
1434−-- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGnumeric]] (nullity 'PGnumeric)
1435−-- expression = sumDistinct #col
1436−-- in printSQL expression
1437−-- :}
1438−-- sum(DISTINCT "col")
1439−sumDistinct
1440− :: ty `In` PGNum
1441− => Expression 'Ungrouped commons schemas params from (nullity ty)
1442− -- ^ what to sum
1443− -> Expression ('Grouped bys) commons schemas params from (nullity ty)
1444−sumDistinct = unsafeAggregateDistinct "sum"
1445−
14461547-- | A constraint for `PGType`s that you can take averages of and the resulting
14471548-- `PGType`.
14481549class PGAvg ty avg | ty -> avg where
@@ -1460,114 +1561,6 @@ instance PGAvg 'PGfloat4 'PGfloat8
14601561instance PGAvg 'PGfloat8 'PGfloat8
14611562instance PGAvg 'PGinterval 'PGinterval
14621563
1463−-- | >>> :{
1464−-- let
1465−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1466−-- expression = bitAnd #col
1467−-- in printSQL expression
1468−-- :}
1469−-- bit_and("col")
1470−bitAnd
1471− :: int `In` PGIntegral
1472− => Expression 'Ungrouped commons schemas params from (nullity int)
1473− -- ^ what to aggregate
1474− -> Expression ('Grouped bys) commons schemas params from (nullity int)
1475−bitAnd = unsafeAggregate "bit_and"
1476−
1477−-- | >>> :{
1478−-- let
1479−-- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1480−-- expression = bitOr #col
1481−-- in printSQL expression
1482−-- :}
1483−-- bit_or("col")
1484−bitOr
1485− :: int `In` PGIntegral
1486− => Expression 'Ungrouped commons schemas params from (nullity int)
1487− -- ^ what to aggregate
1488− -> Expression ('Grouped bys) commons schemas params from (nullity int)
1489−bitOr = unsafeAggregate "bit_or"
1490−
1491−-- | >>> :{
1492−-- let
1493−-- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1494−-- expression = bitAndDistinct #col
1495−-- in printSQL expression
1496−-- :}
1497−-- bit_and(DISTINCT "col")
1498−bitAndDistinct
1499− :: int `In` PGIntegral
1500− => Expression 'Ungrouped commons schemas params from (nullity int)
1501− -- ^ what to aggregate
1502− -> Expression ('Grouped bys) commons schemas params from (nullity int)
1503−bitAndDistinct = unsafeAggregateDistinct "bit_and"
1504−
1505−-- | >>> :{
1506−-- let
1507−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1508−-- expression = bitOrDistinct #col
1509−-- in printSQL expression
1510−-- :}
1511−-- bit_or(DISTINCT "col")
1512−bitOrDistinct
1513− :: int `In` PGIntegral
1514− => Expression 'Ungrouped commons schemas params from (nullity int)
1515− -- ^ what to aggregate
1516− -> Expression ('Grouped bys) commons schemas params from (nullity int)
1517−bitOrDistinct = unsafeAggregateDistinct "bit_or"
1518−
1519−-- | >>> :{
1520−-- let
1521−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1522−-- expression = boolAnd #col
1523−-- in printSQL expression
1524−-- :}
1525−-- bool_and("col")
1526−boolAnd
1527− :: Expression 'Ungrouped commons schemas params from (nullity 'PGbool)
1528− -- ^ what to aggregate
1529− -> Expression ('Grouped bys) commons schemas params from (nullity 'PGbool)
1530−boolAnd = unsafeAggregate "bool_and"
1531−
1532−-- | >>> :{
1533−-- let
1534−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1535−-- expression = boolOr #col
1536−-- in printSQL expression
1537−-- :}
1538−-- bool_or("col")
1539−boolOr
1540− :: Expression 'Ungrouped commons schemas params from (nullity 'PGbool)
1541− -- ^ what to aggregate
1542− -> Expression ('Grouped bys) commons schemas params from (nullity 'PGbool)
1543−boolOr = unsafeAggregate "bool_or"
1544−
1545−-- | >>> :{
1546−-- let
1547−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1548−-- expression = boolAndDistinct #col
1549−-- in printSQL expression
1550−-- :}
1551−-- bool_and(DISTINCT "col")
1552−boolAndDistinct
1553− :: Expression 'Ungrouped commons schemas params from (nullity 'PGbool)
1554− -- ^ what to aggregate
1555− -> Expression ('Grouped bys) commons schemas params from (nullity 'PGbool)
1556−boolAndDistinct = unsafeAggregateDistinct "bool_and"
1557−
1558−-- | >>> :{
1559−-- let
1560−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1561−-- expression = boolOrDistinct #col
1562−-- in printSQL expression
1563−-- :}
1564−-- bool_or(DISTINCT "col")
1565−boolOrDistinct
1566− :: Expression 'Ungrouped commons schemas params from (nullity 'PGbool)
1567− -- ^ what to aggregate
1568− -> Expression ('Grouped bys) commons schemas params from (nullity 'PGbool)
1569−boolOrDistinct = unsafeAggregateDistinct "bool_or"
1570−
15711564-- | A special aggregation that does not require an input
15721565--
15731566-- >>> printSQL countStar
@@ -1576,72 +1569,6 @@ countStar
15761569 :: Expression ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
15771570countStar = UnsafeExpression $ "count(*)"
15781571
1579−-- | >>> :{
1580−-- let
1581−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1582−-- expression = count #col
1583−-- in printSQL expression
1584−-- :}
1585−-- count("col")
1586−count
1587− :: Expression 'Ungrouped commons schemas params from ty
1588− -- ^ what to count
1589− -> Expression ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1590−count = unsafeAggregate "count"
1591−
1592−-- | >>> :{
1593−-- let
1594−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1595−-- expression = countDistinct #col
1596−-- in printSQL expression
1597−-- :}
1598−-- count(DISTINCT "col")
1599−countDistinct
1600− :: Expression 'Ungrouped commons schemas params from ty
1601− -- ^ what to count
1602− -> Expression ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1603−countDistinct = unsafeAggregateDistinct "count"
1604−
1605−-- | synonym for `boolAnd`
1606−--
1607−-- >>> :{
1608−-- let
1609−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1610−-- expression = every #col
1611−-- in printSQL expression
1612−-- :}
1613−-- every("col")
1614−every
1615− :: Expression 'Ungrouped commons schemas params from (nullity 'PGbool)
1616− -- ^ what to aggregate
1617− -> Expression ('Grouped bys) commons schemas params from (nullity 'PGbool)
1618−every = unsafeAggregate "every"
1619−
1620−-- | synonym for `boolAndDistinct`
1621−--
1622−-- >>> :{
1623−-- let
1624−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1625−-- expression = everyDistinct #col
1626−-- in printSQL expression
1627−-- :}
1628−-- every(DISTINCT "col")
1629−everyDistinct
1630− :: Expression 'Ungrouped commons schemas params from (nullity 'PGbool)
1631− -- ^ what to aggregate
1632− -> Expression ('Grouped bys) commons schemas params from (nullity 'PGbool)
1633−everyDistinct = unsafeAggregateDistinct "every"
1634−
1635−-- | minimum and maximum aggregation
1636−max_, min_, maxDistinct, minDistinct
1637− :: Expression 'Ungrouped commons schemas params from (nullity ty)
1638− -- ^ what to aggregate
1639− -> Expression ('Grouped bys) commons schemas params from (nullity ty)
1640−max_ = unsafeAggregate "max"
1641−min_ = unsafeAggregate "min"
1642−maxDistinct = unsafeAggregateDistinct "max"
1643−minDistinct = unsafeAggregateDistinct "min"
1644−
16451572{-----------------------------------------
16461573type expressions
16471574-----------------------------------------}
@@ -1903,6 +1830,13 @@ newtype WindowFunction
19031830instance RenderSQL (WindowFunction grp commons schemas params from ty) where
19041831 renderSQL = renderWindowFunction
19051832
1833+unsafeWindowFunction1
1834+ :: ByteString
1835+ -> Expression grp commons schemas params from ty0
1836+ -> WindowFunction grp commons schemas params from ty1
1837+unsafeWindowFunction1 fun x
1838+ = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x)
1839+
19061840{- | rank of the current row with gaps; same as `rowNumber` of its first peer
19071841>>> printSQL rank
19081842rank()
@@ -1944,7 +1878,7 @@ dividing the partition as equally as possible
19441878-}
19451879ntile
19461880 :: Expression grp commons schemas params from ('NotNull 'PGint4)
1947− -- ^ numuckets
1881+ -- ^ num buckets
19481882 -> WindowFunction grp commons schemas params from ('NotNull 'PGint4)
19491883ntile numBuckets = UnsafeWindowFunction $ "ntile"
19501884 <> parenthesized (renderSQL numBuckets)