on dev help sign in
doctests
commit5d77a25c9566400674586384e562b7b7780144ab authorEitan Chatav <eitan.chatav@gmail.com> dateFri 15 Feb 2019, 22:49 (8 years ago) parent444dcf5
1 file changed+89−41
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+89−41
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+89−41
@@ -194,7 +194,10 @@ module Squeal.PostgreSQL.Expression
194194 , fixarray
195195 -- * Re-export
196196 , (&)
197− , NP ((:*), Nil)
197+ , NP (..)
198+ , (:*:) (..)
199+ , I (..)
200+ , K (..)
198201 ) where
199202
200203import Control.Category
@@ -208,6 +211,7 @@ import Data.Ratio
208211import Data.String
209212import Data.Word
210213import Generics.SOP hiding (from)
214+import GHC.Generics ((:*:) (..))
211215import GHC.OverloadedLabels
212216import GHC.TypeLits
213217import Prelude hiding (id, (.))
@@ -1459,11 +1463,11 @@ class Aggregate expr1 expr2 aggr
14591463
14601464 -- | >>> :{
14611465 -- let
1462− -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1463− -- expression = boolAnd (All #col)
1464− -- in printSQL expression
1466+ -- winFun :: WindowFunction 'Ungrouped commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1467+ -- winFun = boolAnd #col
1468+ -- in printSQL winFun
14651469 -- :}
1466− -- bool_and(ALL "col")
1470+ -- bool_and("col")
14671471 boolAnd
14681472 :: expr1 (nullity 'PGbool)
14691473 -- ^ what to aggregate
@@ -1485,7 +1489,7 @@ class Aggregate expr1 expr2 aggr
14851489 --
14861490 -- >>> :{
14871491 -- 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)
14891493 -- expression = every (Distinct #col)
14901494 -- in printSQL expression
14911495 -- :}
@@ -1513,66 +1517,121 @@ class Aggregate expr1 expr2 aggr
15131517 -- ^ what to average
15141518 -> aggr (nullity (PGAvg ty))
15151519
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+ -}
15171529 corr
15181530 :: expr2 (nullity 'PGfloat8)
15191531 -> aggr (nullity 'PGfloat8)
15201532
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+ -}
15221542 covarPop
15231543 :: expr2 (nullity 'PGfloat8)
15241544 -> aggr (nullity 'PGfloat8)
15251545
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+ -}
15271555 covarSamp
15281556 :: expr2 (nullity 'PGfloat8)
15291557 -> aggr (nullity 'PGfloat8)
15301558
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+ -}
15321568 regrAvgX
15331569 :: expr2 (nullity 'PGfloat8)
15341570 -> aggr (nullity 'PGfloat8)
15351571
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+ -}
15371581 regrAvgY
15381582 :: expr2 (nullity 'PGfloat8)
15391583 -> aggr (nullity 'PGfloat8)
15401584
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+ -}
15421594 regrCount
15431595 :: expr2 (nullity 'PGfloat8)
15441596 -> aggr (nullity 'PGint8)
15451597
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+ -}
15481607 regrIntercept
15491608 :: expr2 (nullity 'PGfloat8)
15501609 -> aggr (nullity 'PGfloat8)
15511610
1552− -- | @regrR2 (y :*: x)@, square of the correlation coefficient
1611+ -- | @regr_r2(Y, X)@, square of the correlation coefficient
15531612 regrR2
15541613 :: expr2 (nullity 'PGfloat8)
15551614 -> aggr (nullity 'PGfloat8)
15561615
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
15581617 -- determined by the (X, Y) pairs
15591618 regrSlope
15601619 :: expr2 (nullity 'PGfloat8)
15611620 -> aggr (nullity 'PGfloat8)
15621621
1563− -- | @regrSxx (y :*: x)@, sum(X^2) - sum(X)^2/N
1622+ -- | @regr_sxx(Y, X)@, sum(X^2) - sum(X)^2/N
15641623 -- (“sum of squares” of the independent variable)
15651624 regrSxx
15661625 :: expr2 (nullity 'PGfloat8)
15671626 -> aggr (nullity 'PGfloat8)
15681627
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
15701629 -- (“sum of products” of independent times dependent variable)
15711630 regrSxy
15721631 :: expr2 (nullity 'PGfloat8)
15731632 -> aggr (nullity 'PGfloat8)
15741633
1575− -- | @regrSyy (y :*: x)@, sum(Y^2) - sum(Y)^2/N
1634+ -- | @regr_syy(Y, X)@, sum(Y^2) - sum(Y)^2/N
15761635 -- (“sum of squares” of the dependent variable)
15771636 regrSyy
15781637 :: expr2 (nullity 'PGfloat8)
@@ -1622,17 +1681,17 @@ instance RenderSQL (Distinction (Expression grp commons schemas params from) ty)
16221681instance RenderSQL
16231682 (Distinction
16241683 ( Expression grp commons schemas params from
1625− GHC.:*:
1684+ :*:
16261685 Expression grp commons schemas params from) xty ) where
16271686 renderSQL = \case
1628− All (x GHC.:*: x') ->
1687+ All (x :*: x') ->
16291688 "ALL" <+> renderSQL x <> ", " <> renderSQL x'
1630− Distinct (x GHC.:*: x') ->
1689+ Distinct (x :*: x') ->
16311690 "DISTINCT" <+> renderSQL x <> ", " <> renderSQL x'
16321691
16331692instance Aggregate
16341693 (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))
16361695 (Expression ('Grouped bys) commons schemas params from) where
16371696 countStar = UnsafeExpression "count(*)"
16381697 count = unsafeAggregate1 "count"
@@ -1665,7 +1724,7 @@ instance Aggregate
16651724 varSamp = unsafeAggregate1 "var_samp"
16661725instance Aggregate
16671726 (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)
16691728 (WindowFunction grp commons schemas params from) where
16701729 countStar = UnsafeWindowFunction "count(*)"
16711730 count = unsafeWindowFunction1 "count"
@@ -1708,7 +1767,7 @@ unsafeAggregate1 fun x = UnsafeExpression $ mconcat
17081767-- | escape hatch to define aggregate binary functions
17091768unsafeAggregate2
17101769 :: 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
17121771 -> Expression ('Grouped bys) commons schemas params from yty
17131772unsafeAggregate2 fun xx = UnsafeExpression $ mconcat
17141773 [fun, "(", renderSQL xx, ")"]
@@ -1994,19 +2053,11 @@ unsafeWindowFunction1 fun x
19942053
19952054unsafeWindowFunction2
19962055 :: 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
19982057 -> WindowFunction grp commons schemas params from ty1
1999−unsafeWindowFunction2 fun (x GHC.:*: x')
2058+unsafeWindowFunction2 fun (x :*: x')
20002059 = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x <> ", " <> renderSQL x')
20012060
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−
20102061{- | rank of the current row with gaps; same as `rowNumber` of its first peer
20112062>>> printSQL rank
20122063rank()
@@ -2050,8 +2101,7 @@ ntile
20502101 :: Expression grp commons schemas params from ('NotNull 'PGint4)
20512102 -- ^ num buckets
20522103 -> WindowFunction grp commons schemas params from ('NotNull 'PGint4)
2053−ntile numBuckets = UnsafeWindowFunction $ "ntile"
2054− <> parenthesized (renderSQL numBuckets)
2104+ntile = unsafeWindowFunction1 "ntile"
20552105
20562106{- | returns value evaluated at the row that is offset rows before the current
20572107row within the partition; if there is no such row, instead return default
@@ -2094,8 +2144,7 @@ firstValue
20942144 :: Expression grp commons schemas params from ty
20952145 -- ^ value
20962146 -> WindowFunction grp commons schemas params from ty
2097−firstValue value = UnsafeWindowFunction $ "first_value"
2098− <> parenthesized (renderSQL value)
2147+firstValue = unsafeWindowFunction1 "first_value"
20992148
21002149{- | returns value evaluated at the row that is the
21012150last row of the window frame
@@ -2104,8 +2153,7 @@ lastValue
21042153 :: Expression grp commons schemas params from ty
21052154 -- ^ value
21062155 -> WindowFunction grp commons schemas params from ty
2107−lastValue value = UnsafeWindowFunction $ "last_value"
2108− <> parenthesized (renderSQL value)
2156+lastValue = unsafeWindowFunction1 "last_value"
21092157
21102158{- | returns value evaluated at the row that is the nth
21112159row of the window frame (counting from 1); null if no such row