@@ -134,15 +134,10 @@ module Squeal.PostgreSQL.Expression
134 , jsonbInsert
134 , jsonbInsert
135 , jsonbPretty
135 , jsonbPretty
136 -- ** Aggregation
136 -- ** Aggregation
137− , unsafeAggregate, unsafeAggregateDistinct
137+ , Aggregate (..)
138− , sum_, sumDistinct
138+ , DistinctExpression (..)
139 , PGAvg (avg, avgDistinct)
139 , PGAvg (avg, avgDistinct)
140− , bitAnd, bitOr, boolAnd, boolOr
141− , bitAndDistinct, bitOrDistinct, boolAndDistinct, boolOrDistinct
142 , countStar
140 , countStar
143− , count, countDistinct
144− , every, everyDistinct
145− , max_, maxDistinct, min_, minDistinct
146 -- * Window Functions
141 -- * Window Functions
147 , WindowDefinition (..)
142 , WindowDefinition (..)
148 , partitionBy
143 , partitionBy
@@ -1399,10 +1394,144 @@ jsonbPretty = unsafeFunction "jsonb_pretty"
1399aggregation
1394aggregation
1400-----------------------------------------}
1395-----------------------------------------}
1397+class Aggregate expr aggr where
1401+ -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1402+ -- expression = count #col
1403+ -- in printSQL expression
1407+ :: expr commons schemas params from ty
1409+ -> aggr commons schemas params from ('NotNull 'PGint8)
1413+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1414+ -- expression = sum_ #col
1415+ -- in printSQL expression
1420+ => expr commons schemas params from (nullity ty)
1421+ -> aggr commons schemas params from (nullity ty)
1425+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1426+ -- expression = bitAndDistinct #col
1427+ -- in printSQL expression
1429+ -- bit_and(DISTINCT "col")
1431+ :: int `In` PGIntegral
1432+ => expr commons schemas params from (nullity int)
1433+ -- ^ what to aggregate
1434+ -> aggr commons schemas params from (nullity int)
1438+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1439+ -- expression = bitOr #col
1440+ -- in printSQL expression
1444+ :: int `In` PGIntegral
1445+ => expr commons schemas params from (nullity int)
1446+ -- ^ what to aggregate
1447+ -> aggr commons schemas params from (nullity int)
1451+ -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1452+ -- expression = boolAnd #col
1453+ -- in printSQL expression
1457+ :: expr commons schemas params from (nullity 'PGbool)
1458+ -- ^ what to aggregate
1459+ -> aggr commons schemas params from (nullity 'PGbool)
1463+ -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1464+ -- expression = boolOr #col
1465+ -- in printSQL expression
1469+ :: expr commons schemas params from (nullity 'PGbool)
1470+ -- ^ what to aggregate
1471+ -> aggr commons schemas params from (nullity 'PGbool)
1473+ -- | synonym for `boolAnd`
1477+ -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1478+ -- expression = every #col
1479+ -- in printSQL expression
1483+ :: expr commons schemas params from (nullity 'PGbool)
1484+ -- ^ what to aggregate
1485+ -> aggr commons schemas params from (nullity 'PGbool)
1487+ -- | minimum and maximum aggregation
1489+ :: expr commons schemas params from (nullity ty)
1490+ -- ^ what to aggregate
1491+ -> aggr commons schemas params from (nullity ty)
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"
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
1402-- | escape hatch to define aggregate functions
1530-- | escape hatch to define aggregate functions
1403unsafeAggregate
1531unsafeAggregate
1532+ :: RenderSQL (expr commons schemas params from xty)
1404− :: ByteString -- ^ aggregate function
1533+ => ByteString -- ^ aggregate function
1405− -> Expression 'Ungrouped commons schemas params from xty
1534+ -> expr commons schemas params from xty
1406 -> Expression ('Grouped bys) commons schemas params from yty
1535 -> Expression ('Grouped bys) commons schemas params from yty
1407unsafeAggregate fun x = UnsafeExpression $ mconcat
1536unsafeAggregate fun x = UnsafeExpression $ mconcat
1408 [fun, "(", renderSQL x, ")"]
1537 [fun, "(", renderSQL x, ")"]
@@ -1415,34 +1544,6 @@ unsafeAggregateDistinct
1415unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
1544unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
1416 [fun, "(DISTINCT ", renderSQL x, ")"]
1545 [fun, "(DISTINCT ", renderSQL x, ")"]
1420−-- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1421−-- expression = sum_ #col
1422−-- in printSQL expression
1427− => Expression 'Ungrouped commons schemas params from (nullity ty)
1429− -> Expression ('Grouped bys) commons schemas params from (nullity ty)
1430−sum_ = unsafeAggregate "sum"
1434−-- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGnumeric]] (nullity 'PGnumeric)
1435−-- expression = sumDistinct #col
1436−-- in printSQL expression
1438−-- sum(DISTINCT "col")
1441− => Expression 'Ungrouped commons schemas params from (nullity ty)
1443− -> Expression ('Grouped bys) commons schemas params from (nullity ty)
1444−sumDistinct = unsafeAggregateDistinct "sum"
1446-- | A constraint for `PGType`s that you can take averages of and the resulting
1547-- | A constraint for `PGType`s that you can take averages of and the resulting
1447-- `PGType`.
1548-- `PGType`.
1448class PGAvg ty avg | ty -> avg where
1549class PGAvg ty avg | ty -> avg where
@@ -1460,114 +1561,6 @@ instance PGAvg 'PGfloat4 'PGfloat8
1460instance PGAvg 'PGfloat8 'PGfloat8
1561instance PGAvg 'PGfloat8 'PGfloat8
1461instance PGAvg 'PGinterval 'PGinterval
1562instance PGAvg 'PGinterval 'PGinterval
1465−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1466−-- expression = bitAnd #col
1467−-- in printSQL expression
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"
1479−-- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1480−-- expression = bitOr #col
1481−-- in printSQL expression
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"
1493−-- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1494−-- expression = bitAndDistinct #col
1495−-- in printSQL expression
1497−-- bit_and(DISTINCT "col")
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"
1507−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1508−-- expression = bitOrDistinct #col
1509−-- in printSQL expression
1511−-- bit_or(DISTINCT "col")
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"
1521−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1522−-- expression = boolAnd #col
1523−-- in printSQL expression
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"
1534−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1535−-- expression = boolOr #col
1536−-- in printSQL expression
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"
1547−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1548−-- expression = boolAndDistinct #col
1549−-- in printSQL expression
1551−-- bool_and(DISTINCT "col")
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"
1560−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1561−-- expression = boolOrDistinct #col
1562−-- in printSQL expression
1564−-- bool_or(DISTINCT "col")
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"
1571-- | A special aggregation that does not require an input
1564-- | A special aggregation that does not require an input
1573-- >>> printSQL countStar
1566-- >>> printSQL countStar
@@ -1576,72 +1569,6 @@ countStar
1576 :: Expression ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1569 :: Expression ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1577countStar = UnsafeExpression $ "count(*)"
1570countStar = UnsafeExpression $ "count(*)"
1581−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1582−-- expression = count #col
1583−-- in printSQL expression
1587− :: Expression 'Ungrouped commons schemas params from ty
1589− -> Expression ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1590−count = unsafeAggregate "count"
1594−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1595−-- expression = countDistinct #col
1596−-- in printSQL expression
1598−-- count(DISTINCT "col")
1600− :: Expression 'Ungrouped commons schemas params from ty
1602− -> Expression ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1603−countDistinct = unsafeAggregateDistinct "count"
1605−-- | synonym for `boolAnd`
1609−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1610−-- expression = every #col
1611−-- in printSQL expression
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"
1620−-- | synonym for `boolAndDistinct`
1624−-- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1625−-- expression = everyDistinct #col
1626−-- in printSQL expression
1628−-- every(DISTINCT "col")
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"
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"
1645{-----------------------------------------
1572{-----------------------------------------
1646type expressions
1573type expressions
1647-----------------------------------------}
1574-----------------------------------------}
@@ -1903,6 +1830,13 @@ newtype WindowFunction
1903instance RenderSQL (WindowFunction grp commons schemas params from ty) where
1830instance RenderSQL (WindowFunction grp commons schemas params from ty) where
1904 renderSQL = renderWindowFunction
1831 renderSQL = renderWindowFunction
1833+unsafeWindowFunction1
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)
1906{- | rank of the current row with gaps; same as `rowNumber` of its first peer
1840{- | rank of the current row with gaps; same as `rowNumber` of its first peer
1907>>> printSQL rank
1841>>> printSQL rank
@@ -1944,7 +1878,7 @@ dividing the partition as equally as possible
1946 :: Expression grp commons schemas params from ('NotNull 'PGint4)
1880 :: Expression grp commons schemas params from ('NotNull 'PGint4)
1947− -- ^ numuckets
1881+ -- ^ num buckets
1948 -> WindowFunction grp commons schemas params from ('NotNull 'PGint4)
1882 -> WindowFunction grp commons schemas params from ('NotNull 'PGint4)
1949ntile numBuckets = UnsafeWindowFunction $ "ntile"
1883ntile numBuckets = UnsafeWindowFunction $ "ntile"
1950 <> parenthesized (renderSQL numBuckets)
1884 <> parenthesized (renderSQL numBuckets)