on dev help sign in
doctests
5d77a25Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+89−41
@@ -194,7 +194,10 @@ module Squeal.PostgreSQL.Expression
194 , fixarray
194 , fixarray
195 -- * Re-export
195 -- * Re-export
196 , (&)
196 , (&)
197− , NP ((:*), Nil)
197+ , NP (..)
198+ , (:*:) (..)
199+ , I (..)
200+ , K (..)
198 ) where
201 ) where
199
202
200import Control.Category
203import Control.Category
@@ -208,6 +211,7 @@ import Data.Ratio
208import Data.String
211import Data.String
209import Data.Word
212import Data.Word
210import Generics.SOP hiding (from)
213import Generics.SOP hiding (from)
214+import GHC.Generics ((:*:) (..))
211import GHC.OverloadedLabels
215import GHC.OverloadedLabels
212import GHC.TypeLits
216import GHC.TypeLits
213import Prelude hiding (id, (.))
217import Prelude hiding (id, (.))
@@ -1459,11 +1463,11 @@ class Aggregate expr1 expr2 aggr
1459
1463
1460 -- | >>> :{
1464 -- | >>> :{
1461 -- let
1465 -- let
1462− -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1466+ -- winFun :: WindowFunction 'Ungrouped commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1463− -- expression = boolAnd (All #col)
1467+ -- winFun = boolAnd #col
1464− -- in printSQL expression
1468+ -- in printSQL winFun
1465 -- :}
1469 -- :}
1466− -- bool_and(ALL "col")
1470+ -- bool_and("col")
1467 boolAnd
1471 boolAnd
1468 :: expr1 (nullity 'PGbool)
1472 :: expr1 (nullity 'PGbool)
1469 -- ^ what to aggregate
1473 -- ^ what to aggregate
@@ -1485,7 +1489,7 @@ class Aggregate expr1 expr2 aggr
1485 --
1489 --
1486 -- >>> :{
1490 -- >>> :{
1487 -- let
1491 -- let
1488− -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1492+ -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1489 -- expression = every (Distinct #col)
1493 -- expression = every (Distinct #col)
1490 -- in printSQL expression
1494 -- in printSQL expression
1491 -- :}
1495 -- :}
@@ -1513,66 +1517,121 @@ class Aggregate expr1 expr2 aggr
1513 -- ^ what to average
1517 -- ^ what to average
1514 -> aggr (nullity (PGAvg ty))
1518 -> aggr (nullity (PGAvg ty))
1515
1519
1516− -- | @corr (y :*: x)@, correlation coefficient
1520+ {- | correlation coefficient
1521+ >>> :{
1522+ let
1523+ expression :: Expression ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1524+ expression = corr (All (#y :*: #x))
1525+ in printSQL expression
1526+ :}
1527+ corr(ALL "y", "x")
1528+ -}
1517 corr
1529 corr
1518 :: expr2 (nullity 'PGfloat8)
1530 :: expr2 (nullity 'PGfloat8)
1519 -> aggr (nullity 'PGfloat8)
1531 -> aggr (nullity 'PGfloat8)
1520
1532
1521− -- | @covarPop (y :*: x)@, population covariance
1533+ {- | population covariance
1534+ >>> :{
1535+ let
1536+ expression :: Expression ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1537+ expression = covarPop (All (#y :*: #x))
1538+ in printSQL expression
1539+ :}
1540+ covar_pop(ALL "y", "x")
1541+ -}
1522 covarPop
1542 covarPop
1523 :: expr2 (nullity 'PGfloat8)
1543 :: expr2 (nullity 'PGfloat8)
1524 -> aggr (nullity 'PGfloat8)
1544 -> aggr (nullity 'PGfloat8)
1525
1545
1526− -- | @covarPop (y :*: x)@, sample covariance
1546+ {- | sample covariance
1547+ >>> :{
1548+ let
1549+ winFun :: WindowFunction 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1550+ winFun = covarSamp (#y :*: #x)
1551+ in printSQL winFun
1552+ :}
1553+ covar_samp("y", "x")
1554+ -}
1527 covarSamp
1555 covarSamp
1528 :: expr2 (nullity 'PGfloat8)
1556 :: expr2 (nullity 'PGfloat8)
1529 -> aggr (nullity 'PGfloat8)
1557 -> aggr (nullity 'PGfloat8)
1530
1558
1531− -- | @regrAvgX (y :*: x)@, average of the independent variable (sum(X)/N)
1559+ {- | average of the independent variable (sum(X)/N)
1560+ >>> :{
1561+ let
1562+ expression :: Expression ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1563+ expression = regrAvgX (All (#y :*: #x))
1564+ in printSQL expression
1565+ :}
1566+ regr_avgx(ALL "y", "x")
1567+ -}
1532 regrAvgX
1568 regrAvgX
1533 :: expr2 (nullity 'PGfloat8)
1569 :: expr2 (nullity 'PGfloat8)
1534 -> aggr (nullity 'PGfloat8)
1570 -> aggr (nullity 'PGfloat8)
1535
1571
1536− -- | @regrAvgY (y :*: x)@, average of the dependent variable (sum(Y)/N)
1572+ {- | average of the dependent variable (sum(Y)/N)
1573+ >>> :{
1574+ let
1575+ winFun :: WindowFunction 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1576+ winFun = regrAvgY (#y :*: #x)
1577+ in printSQL winFun
1578+ :}
1579+ regr_avgy("y", "x")
1580+ -}
1537 regrAvgY
1581 regrAvgY
1538 :: expr2 (nullity 'PGfloat8)
1582 :: expr2 (nullity 'PGfloat8)
1539 -> aggr (nullity 'PGfloat8)
1583 -> aggr (nullity 'PGfloat8)
1540
1584
1541− -- | @regrCount (y :*: x)@, number of input rows in which both expressions are nonnull
1585+ {- | number of input rows in which both expressions are nonnull
1586+ >>> :{
1587+ let
1588+ winFun :: WindowFunction 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGint8)
1589+ winFun = regrCount (#y :*: #x)
1590+ in printSQL winFun
1591+ :}
1592+ regr_count("y", "x")
1593+ -}
1542 regrCount
1594 regrCount
1543 :: expr2 (nullity 'PGfloat8)
1595 :: expr2 (nullity 'PGfloat8)
1544 -> aggr (nullity 'PGint8)
1596 -> aggr (nullity 'PGint8)
1545
1597
1546− -- | @regrIntercept (y :*: x)@, y-intercept of the least-squares-fit linear
1547− -- equation determined by the (X, Y) pairs
1598+ {- | y-intercept of the least-squares-fit linear equation determined by the (X, Y) pairs
1599+ >>> :{
1600+ let
1601+ expression :: Expression ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1602+ expression = regrIntercept (All (#y :*: #x))
1603+ in printSQL expression
1604+ :}
1605+ regr_intercept(ALL "y", "x")
1606+ -}
1548 regrIntercept
1607 regrIntercept
1549 :: expr2 (nullity 'PGfloat8)
1608 :: expr2 (nullity 'PGfloat8)
1550 -> aggr (nullity 'PGfloat8)
1609 -> aggr (nullity 'PGfloat8)
1551
1610
1552− -- | @regrR2 (y :*: x)@, square of the correlation coefficient
1611+ -- | @regr_r2(Y, X)@, square of the correlation coefficient
1553 regrR2
1612 regrR2
1554 :: expr2 (nullity 'PGfloat8)
1613 :: expr2 (nullity 'PGfloat8)
1555 -> aggr (nullity 'PGfloat8)
1614 -> aggr (nullity 'PGfloat8)
1556
1615
1557− -- | @regrSlope (y :*: x)@, slope of the least-squares-fit linear equation
1616+ -- | @regr_slope(Y, X)@, slope of the least-squares-fit linear equation
1558 -- determined by the (X, Y) pairs
1617 -- determined by the (X, Y) pairs
1559 regrSlope
1618 regrSlope
1560 :: expr2 (nullity 'PGfloat8)
1619 :: expr2 (nullity 'PGfloat8)
1561 -> aggr (nullity 'PGfloat8)
1620 -> aggr (nullity 'PGfloat8)
1562
1621
1563− -- | @regrSxx (y :*: x)@, sum(X^2) - sum(X)^2/N
1622+ -- | @regr_sxx(Y, X)@, sum(X^2) - sum(X)^2/N
1564 -- (“sum of squares” of the independent variable)
1623 -- (“sum of squares” of the independent variable)
1565 regrSxx
1624 regrSxx
1566 :: expr2 (nullity 'PGfloat8)
1625 :: expr2 (nullity 'PGfloat8)
1567 -> aggr (nullity 'PGfloat8)
1626 -> aggr (nullity 'PGfloat8)
1568
1627
1569− -- | @regrSxy (y :*: x)@, sum(X*Y) - sum(X) * sum(Y)/N
1628+ -- | @regr_sxy(Y, X)@, sum(X*Y) - sum(X) * sum(Y)/N
1570 -- (“sum of products” of independent times dependent variable)
1629 -- (“sum of products” of independent times dependent variable)
1571 regrSxy
1630 regrSxy
1572 :: expr2 (nullity 'PGfloat8)
1631 :: expr2 (nullity 'PGfloat8)
1573 -> aggr (nullity 'PGfloat8)
1632 -> aggr (nullity 'PGfloat8)
1574
1633
1575− -- | @regrSyy (y :*: x)@, sum(Y^2) - sum(Y)^2/N
1634+ -- | @regr_syy(Y, X)@, sum(Y^2) - sum(Y)^2/N
1576 -- (“sum of squares” of the dependent variable)
1635 -- (“sum of squares” of the dependent variable)
1577 regrSyy
1636 regrSyy
1578 :: expr2 (nullity 'PGfloat8)
1637 :: expr2 (nullity 'PGfloat8)
@@ -1622,17 +1681,17 @@ instance RenderSQL (Distinction (Expression grp commons schemas params from) ty)
1622instance RenderSQL
1681instance RenderSQL
1623 (Distinction
1682 (Distinction
1624 ( Expression grp commons schemas params from
1683 ( Expression grp commons schemas params from
1625− GHC.:*:
1684+ :*:
1626 Expression grp commons schemas params from) xty ) where
1685 Expression grp commons schemas params from) xty ) where
1627 renderSQL = \case
1686 renderSQL = \case
1628− All (x GHC.:*: x') ->
1687+ All (x :*: x') ->
1629 "ALL" <+> renderSQL x <> ", " <> renderSQL x'
1688 "ALL" <+> renderSQL x <> ", " <> renderSQL x'
1630− Distinct (x GHC.:*: x') ->
1689+ Distinct (x :*: x') ->
1631 "DISTINCT" <+> renderSQL x <> ", " <> renderSQL x'
1690 "DISTINCT" <+> renderSQL x <> ", " <> renderSQL x'
1632
1691
1633instance Aggregate
1692instance Aggregate
1634 (Distinction (Expression 'Ungrouped commons schemas params from))
1693 (Distinction (Expression 'Ungrouped commons schemas params from))
1635− (Distinction (Expression 'Ungrouped commons schemas params from GHC.:*: Expression 'Ungrouped commons schemas params from))
1694+ (Distinction (Expression 'Ungrouped commons schemas params from :*: Expression 'Ungrouped commons schemas params from))
1636 (Expression ('Grouped bys) commons schemas params from) where
1695 (Expression ('Grouped bys) commons schemas params from) where
1637 countStar = UnsafeExpression "count(*)"
1696 countStar = UnsafeExpression "count(*)"
1638 count = unsafeAggregate1 "count"
1697 count = unsafeAggregate1 "count"
@@ -1665,7 +1724,7 @@ instance Aggregate
1665 varSamp = unsafeAggregate1 "var_samp"
1724 varSamp = unsafeAggregate1 "var_samp"
1666instance Aggregate
1725instance Aggregate
1667 (Expression grp commons schemas params from)
1726 (Expression grp commons schemas params from)
1668− (Expression grp commons schemas params from GHC.:*: Expression grp commons schemas params from)
1727+ (Expression grp commons schemas params from :*: Expression grp commons schemas params from)
1669 (WindowFunction grp commons schemas params from) where
1728 (WindowFunction grp commons schemas params from) where
1670 countStar = UnsafeWindowFunction "count(*)"
1729 countStar = UnsafeWindowFunction "count(*)"
1671 count = unsafeWindowFunction1 "count"
1730 count = unsafeWindowFunction1 "count"
@@ -1708,7 +1767,7 @@ unsafeAggregate1 fun x = UnsafeExpression $ mconcat
1708-- | escape hatch to define aggregate binary functions
1767-- | escape hatch to define aggregate binary functions
1709unsafeAggregate2
1768unsafeAggregate2
1710 :: ByteString -- ^ aggregate function
1769 :: ByteString -- ^ aggregate function
1711− -> Distinction (Expression 'Ungrouped commons schemas params from GHC.:*: Expression 'Ungrouped commons schemas params from) xty
1770+ -> Distinction (Expression 'Ungrouped commons schemas params from :*: Expression 'Ungrouped commons schemas params from) xty
1712 -> Expression ('Grouped bys) commons schemas params from yty
1771 -> Expression ('Grouped bys) commons schemas params from yty
1713unsafeAggregate2 fun xx = UnsafeExpression $ mconcat
1772unsafeAggregate2 fun xx = UnsafeExpression $ mconcat
1714 [fun, "(", renderSQL xx, ")"]
1773 [fun, "(", renderSQL xx, ")"]
@@ -1994,19 +2053,11 @@ unsafeWindowFunction1 fun x
1994
2053
1995unsafeWindowFunction2
2054unsafeWindowFunction2
1996 :: ByteString
2055 :: ByteString
1997− -> (Expression grp commons schemas params from GHC.:*: Expression grp commons schemas params from) ty0
2056+ -> (Expression grp commons schemas params from :*: Expression grp commons schemas params from) ty0
1998 -> WindowFunction grp commons schemas params from ty1
2057 -> WindowFunction grp commons schemas params from ty1
1999−unsafeWindowFunction2 fun (x GHC.:*: x')
2058+unsafeWindowFunction2 fun (x :*: x')
2000 = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x <> ", " <> renderSQL x')
2059 = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x <> ", " <> renderSQL x')
2001
2060
2002−-- | escape hatch to define aggregate binary functions
2003−-- unsafeAggregate2
2004−-- :: ByteString -- ^ aggregate function
2005−-- -> Distinction (Expression 'Ungrouped commons schemas params from GHC.:*: Expression 'Ungrouped commons schemas params from) xty
2006−-- -> Expression ('Grouped bys) commons schemas params from yty
2007−-- unsafeAggregate2 fun xx = UnsafeExpression $ mconcat
2008−-- [fun, "(", renderSQL xx, ")"]
2009−
2010{- | rank of the current row with gaps; same as `rowNumber` of its first peer
2061{- | rank of the current row with gaps; same as `rowNumber` of its first peer
2011>>> printSQL rank
2062>>> printSQL rank
2012rank()
2063rank()
@@ -2050,8 +2101,7 @@ ntile
2050 :: Expression grp commons schemas params from ('NotNull 'PGint4)
2101 :: Expression grp commons schemas params from ('NotNull 'PGint4)
2051 -- ^ num buckets
2102 -- ^ num buckets
2052 -> WindowFunction grp commons schemas params from ('NotNull 'PGint4)
2103 -> WindowFunction grp commons schemas params from ('NotNull 'PGint4)
2053−ntile numBuckets = UnsafeWindowFunction $ "ntile"
2104+ntile = unsafeWindowFunction1 "ntile"
2054− <> parenthesized (renderSQL numBuckets)
2055
2105
2056{- | returns value evaluated at the row that is offset rows before the current
2106{- | returns value evaluated at the row that is offset rows before the current
2057row within the partition; if there is no such row, instead return default
2107row within the partition; if there is no such row, instead return default
@@ -2094,8 +2144,7 @@ firstValue
2094 :: Expression grp commons schemas params from ty
2144 :: Expression grp commons schemas params from ty
2095 -- ^ value
2145 -- ^ value
2096 -> WindowFunction grp commons schemas params from ty
2146 -> WindowFunction grp commons schemas params from ty
2097−firstValue value = UnsafeWindowFunction $ "first_value"
2147+firstValue = unsafeWindowFunction1 "first_value"
2098− <> parenthesized (renderSQL value)
2099
2148
2100{- | returns value evaluated at the row that is the
2149{- | returns value evaluated at the row that is the
2101last row of the window frame
2150last row of the window frame
@@ -2104,8 +2153,7 @@ lastValue
2104 :: Expression grp commons schemas params from ty
2153 :: Expression grp commons schemas params from ty
2105 -- ^ value
2154 -- ^ value
2106 -> WindowFunction grp commons schemas params from ty
2155 -> WindowFunction grp commons schemas params from ty
2107−lastValue value = UnsafeWindowFunction $ "last_value"
2156+lastValue = unsafeWindowFunction1 "last_value"
2108− <> parenthesized (renderSQL value)
2109
2157
2110{- | returns value evaluated at the row that is the nth
2158{- | returns value evaluated at the row that is the nth
2111row of the window frame (counting from 1); null if no such row
2159row of the window frame (counting from 1); null if no such row