on dev help sign in
fixes
30cd2deEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+112−128
@@ -58,6 +58,7 @@ module Squeal.PostgreSQL.Expression
58 -- ** Functions
58 -- ** Functions
59 , unsafeBinaryOp
59 , unsafeBinaryOp
60 , unsafeUnaryOpL
60 , unsafeUnaryOpL
61+ , unsafeUnaryOpR
61 , unsafeFunction
62 , unsafeFunction
62 , unsafeVariadicFunction
63 , unsafeVariadicFunction
63 , atan2_
64 , atan2_
@@ -241,7 +242,6 @@ module Squeal.PostgreSQL.Expression
241 -- * Re-export
242 -- * Re-export
242 , (&)
243 , (&)
243 , NP (..)
244 , NP (..)
244− , (:*:) (..)
245 , K (..)
245 , K (..)
246 ) where
246 ) where
247
247
@@ -250,12 +250,12 @@ import Control.DeepSeq
250import Data.ByteString (ByteString)
250import Data.ByteString (ByteString)
251import Data.ByteString.Lazy (toStrict)
251import Data.ByteString.Lazy (toStrict)
252import Data.Function ((&))
252import Data.Function ((&))
253+import Data.Kind
253import Data.Semigroup hiding (All)
254import Data.Semigroup hiding (All)
254import qualified Data.Aeson as JSON
255import qualified Data.Aeson as JSON
255import Data.String
256import Data.String
256import Data.Word
257import Data.Word
257import Generics.SOP hiding (All, from)
258import Generics.SOP hiding (All, from)
258−import GHC.Generics ((:*:) (..))
259import GHC.OverloadedLabels
259import GHC.OverloadedLabels
260import GHC.TypeLits
260import GHC.TypeLits
261import Numeric
261import Numeric
@@ -1552,13 +1552,31 @@ tsHeadline = unsafeFunctionHet "ts_headline"
1552aggregation
1552aggregation
1553-----------------------------------------}
1553-----------------------------------------}
1554
1554
1555+type AggrFun x y
1556+ = forall outer bys commons schemas params from
1557+ . Distinction (Expression outer 'Ungrouped commons schemas params from) x
1558+ -- ^ input
1559+ -> Expression outer ('Grouped bys) commons schemas params from y
1560+ -- ^ output
1561+
1562+type AggrHet xs y
1563+ = forall outer bys commons schemas params from
1564+ . Distinction (NP (Expression outer 'Ungrouped commons schemas params from)) xs
1565+ -- ^ inputs
1566+ -> Expression outer ('Grouped bys) commons schemas params from y
1567+ -- ^ output
1568+
1569+unsafeAggregateHet :: SOP.SListI xs => ByteString -> AggrHet xs y
1570+unsafeAggregateHet fun xs = UnsafeExpression $ mconcat
1571+ [fun, "(", renderSQL xs, ")"]
1572+
1555{- |
1573{- |
1556`Aggregate` functions compute a single result from a set of input values.
1574`Aggregate` functions compute a single result from a set of input values.
1557`Aggregate` functions can be used as `GroupedBy` `Expression`s as well
1575`Aggregate` functions can be used as `GroupedBy` `Expression`s as well
1558as `WindowFunction`s.
1576as `WindowFunction`s.
1559-}
1577-}
1560−class Aggregate expr1 expr2 aggr
1578+class Aggregate expr1 exprHet aggr
1561− | aggr -> expr1, aggr -> expr2 where
1579+ | aggr -> expr1, aggr -> exprHet where
1562
1580
1563 -- | A special aggregation that does not require an input
1581 -- | A special aggregation that does not require an input
1564 --
1582 --
@@ -1671,7 +1689,7 @@ class Aggregate expr1 expr2 aggr
1671 bool_or(ALL "col")
1689 bool_or(ALL "col")
1672 -}
1690 -}
1673 boolOr
1691 boolOr
1674− :: expr1(null 'PGbool)
1692+ :: expr1 (null 'PGbool)
1675 -- ^ what to aggregate
1693 -- ^ what to aggregate
1676 -> aggr ('Null 'PGbool)
1694 -> aggr ('Null 'PGbool)
1677
1695
@@ -1713,120 +1731,120 @@ class Aggregate expr1 expr2 aggr
1713 >>> :{
1731 >>> :{
1714 let
1732 let
1715 expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1733 expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1716− expression = corr (All (#y :*: #x))
1734+ expression = corr (All (#y *: #x))
1717 in printSQL expression
1735 in printSQL expression
1718 :}
1736 :}
1719 corr(ALL "y", "x")
1737 corr(ALL "y", "x")
1720 -}
1738 -}
1721 corr
1739 corr
1722− :: expr2 (null 'PGfloat8)
1740+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1723 -> aggr ('Null 'PGfloat8)
1741 -> aggr ('Null 'PGfloat8)
1724
1742
1725 {- | population covariance
1743 {- | population covariance
1726 >>> :{
1744 >>> :{
1727 let
1745 let
1728 expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1746 expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1729− expression = covarPop (All (#y :*: #x))
1747+ expression = covarPop (All (#y *: #x))
1730 in printSQL expression
1748 in printSQL expression
1731 :}
1749 :}
1732 covar_pop(ALL "y", "x")
1750 covar_pop(ALL "y", "x")
1733 -}
1751 -}
1734 covarPop
1752 covarPop
1735− :: expr2 (null 'PGfloat8)
1753+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1736 -> aggr ('Null 'PGfloat8)
1754 -> aggr ('Null 'PGfloat8)
1737
1755
1738 {- | sample covariance
1756 {- | sample covariance
1739 >>> :{
1757 >>> :{
1740 let
1758 let
1741 winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1759 winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1742− winFun = covarSamp (#y :*: #x)
1760+ winFun = covarSamp (#y *: #x)
1743 in printSQL winFun
1761 in printSQL winFun
1744 :}
1762 :}
1745 covar_samp("y", "x")
1763 covar_samp("y", "x")
1746 -}
1764 -}
1747 covarSamp
1765 covarSamp
1748− :: expr2 (null 'PGfloat8)
1766+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1749 -> aggr ('Null 'PGfloat8)
1767 -> aggr ('Null 'PGfloat8)
1750
1768
1751 {- | average of the independent variable (sum(X)/N)
1769 {- | average of the independent variable (sum(X)/N)
1752 >>> :{
1770 >>> :{
1753 let
1771 let
1754 expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1772 expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1755− expression = regrAvgX (All (#y :*: #x))
1773+ expression = regrAvgX (All (#y *: #x))
1756 in printSQL expression
1774 in printSQL expression
1757 :}
1775 :}
1758 regr_avgx(ALL "y", "x")
1776 regr_avgx(ALL "y", "x")
1759 -}
1777 -}
1760 regrAvgX
1778 regrAvgX
1761− :: expr2 (null 'PGfloat8)
1779+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1762 -> aggr ('Null 'PGfloat8)
1780 -> aggr ('Null 'PGfloat8)
1763
1781
1764 {- | average of the dependent variable (sum(Y)/N)
1782 {- | average of the dependent variable (sum(Y)/N)
1765 >>> :{
1783 >>> :{
1766 let
1784 let
1767 winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1785 winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1768− winFun = regrAvgY (#y :*: #x)
1786+ winFun = regrAvgY (#y *: #x)
1769 in printSQL winFun
1787 in printSQL winFun
1770 :}
1788 :}
1771 regr_avgy("y", "x")
1789 regr_avgy("y", "x")
1772 -}
1790 -}
1773 regrAvgY
1791 regrAvgY
1774− :: expr2 (null 'PGfloat8)
1792+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1775 -> aggr ('Null 'PGfloat8)
1793 -> aggr ('Null 'PGfloat8)
1776
1794
1777 {- | number of input rows in which both expressions are nonnull
1795 {- | number of input rows in which both expressions are nonnull
1778 >>> :{
1796 >>> :{
1779 let
1797 let
1780 winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGint8)
1798 winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGint8)
1781− winFun = regrCount (#y :*: #x)
1799+ winFun = regrCount (#y *: #x)
1782 in printSQL winFun
1800 in printSQL winFun
1783 :}
1801 :}
1784 regr_count("y", "x")
1802 regr_count("y", "x")
1785 -}
1803 -}
1786 regrCount
1804 regrCount
1787− :: expr2 (null 'PGfloat8)
1805+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1788 -> aggr ('Null 'PGint8)
1806 -> aggr ('Null 'PGint8)
1789
1807
1790 {- | y-intercept of the least-squares-fit linear equation determined by the (X, Y) pairs
1808 {- | y-intercept of the least-squares-fit linear equation determined by the (X, Y) pairs
1791 >>> :{
1809 >>> :{
1792 let
1810 let
1793 expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1811 expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('Null 'PGfloat8)
1794− expression = regrIntercept (All (#y :*: #x))
1812+ expression = regrIntercept (All (#y *: #x))
1795 in printSQL expression
1813 in printSQL expression
1796 :}
1814 :}
1797 regr_intercept(ALL "y", "x")
1815 regr_intercept(ALL "y", "x")
1798 -}
1816 -}
1799 regrIntercept
1817 regrIntercept
1800− :: expr2 (null 'PGfloat8)
1818+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1801 -> aggr ('Null 'PGfloat8)
1819 -> aggr ('Null 'PGfloat8)
1802
1820
1803 -- | @regr_r2(Y, X)@, square of the correlation coefficient
1821 -- | @regr_r2(Y, X)@, square of the correlation coefficient
1804 regrR2
1822 regrR2
1805− :: expr2 (null 'PGfloat8)
1823+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1806 -> aggr ('Null 'PGfloat8)
1824 -> aggr ('Null 'PGfloat8)
1807
1825
1808 -- | @regr_slope(Y, X)@, slope of the least-squares-fit linear equation
1826 -- | @regr_slope(Y, X)@, slope of the least-squares-fit linear equation
1809 -- determined by the (X, Y) pairs
1827 -- determined by the (X, Y) pairs
1810 regrSlope
1828 regrSlope
1811− :: expr2 (null 'PGfloat8)
1829+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1812 -> aggr ('Null 'PGfloat8)
1830 -> aggr ('Null 'PGfloat8)
1813
1831
1814 -- | @regr_sxx(Y, X)@, sum(X^2) - sum(X)^2/N
1832 -- | @regr_sxx(Y, X)@, sum(X^2) - sum(X)^2/N
1815 -- (“sum of squares” of the independent variable)
1833 -- (“sum of squares” of the independent variable)
1816 regrSxx
1834 regrSxx
1817− :: expr2 (null 'PGfloat8)
1835+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1818 -> aggr ('Null 'PGfloat8)
1836 -> aggr ('Null 'PGfloat8)
1819
1837
1820 -- | @regr_sxy(Y, X)@, sum(X*Y) - sum(X) * sum(Y)/N
1838 -- | @regr_sxy(Y, X)@, sum(X*Y) - sum(X) * sum(Y)/N
1821 -- (“sum of products” of independent times dependent variable)
1839 -- (“sum of products” of independent times dependent variable)
1822 regrSxy
1840 regrSxy
1823− :: expr2 (null 'PGfloat8)
1841+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1824 -> aggr ('Null 'PGfloat8)
1842 -> aggr ('Null 'PGfloat8)
1825
1843
1826 -- | @regr_syy(Y, X)@, sum(Y^2) - sum(Y)^2/N
1844 -- | @regr_syy(Y, X)@, sum(Y^2) - sum(Y)^2/N
1827 -- (“sum of squares” of the dependent variable)
1845 -- (“sum of squares” of the dependent variable)
1828 regrSyy
1846 regrSyy
1829− :: expr2 (null 'PGfloat8)
1847+ :: exprHet '[null 'PGfloat8, null 'PGfloat8]
1830 -> aggr ('Null 'PGfloat8)
1848 -> aggr ('Null 'PGfloat8)
1831
1849
1832 -- | historical alias for `stddevSamp`
1850 -- | historical alias for `stddevSamp`
@@ -1867,7 +1885,7 @@ class Aggregate expr1 expr2 aggr
1867`Distinct` invokes the aggregate once for each distinct value of the expression
1885`Distinct` invokes the aggregate once for each distinct value of the expression
1868(or distinct set of values, for multiple expressions) found in the input
1886(or distinct set of values, for multiple expressions) found in the input
1869-}
1887-}
1870−data Distinction expr ty
1888+data Distinction (expr :: kind -> Type) (ty :: kind)
1871 = All (expr ty)
1889 = All (expr ty)
1872 | Distinct (expr ty)
1890 | Distinct (expr ty)
1873 deriving (GHC.Generic,Show,Eq,Ord)
1891 deriving (GHC.Generic,Show,Eq,Ord)
@@ -1876,20 +1894,15 @@ instance RenderSQL (Distinction (Expression outer grp commons schemas params fro
1876 renderSQL = \case
1894 renderSQL = \case
1877 All x -> "ALL" <+> renderSQL x
1895 All x -> "ALL" <+> renderSQL x
1878 Distinct x -> "DISTINCT" <+> renderSQL x
1896 Distinct x -> "DISTINCT" <+> renderSQL x
1879−instance RenderSQL
1897+instance SOP.SListI tys => RenderSQL
1880− (Distinction
1898+ (Distinction (NP (Expression outer grp commons schemas params from)) tys) where
1881− ( Expression outer grp commons schemas params from
1882− :*:
1883− Expression outer grp commons schemas params from) xty ) where
1884− renderSQL = \case
1899+ renderSQL = \case
1885− All (x :*: x') ->
1900+ All xs -> "ALL" <+> renderCommaSeparated renderSQL xs
1886− "ALL" <+> renderSQL x <> ", " <> renderSQL x'
1887− Distinct (x :*: x') ->
1888− "DISTINCT" <+> renderSQL x <> ", " <> renderSQL x'
1901+ Distinct xs -> "DISTINCT" <+> renderCommaSeparated renderSQL xs
1889
1902
1890instance Aggregate
1903instance Aggregate
1891 (Distinction (Expression outer 'Ungrouped commons schemas params from))
1904 (Distinction (Expression outer 'Ungrouped commons schemas params from))
1892− (Distinction (Expression outer 'Ungrouped commons schemas params from :*: Expression outer 'Ungrouped commons schemas params from))
1905+ (Distinction (NP (Expression outer 'Ungrouped commons schemas params from)))
1893 (Expression outer ('Grouped bys) commons schemas params from) where
1906 (Expression outer ('Grouped bys) commons schemas params from) where
1894 countStar = UnsafeExpression "count(*)"
1907 countStar = UnsafeExpression "count(*)"
1895 count = unsafeAggregate1 "count"
1908 count = unsafeAggregate1 "count"
@@ -1905,18 +1918,18 @@ instance Aggregate
1905 max_ = unsafeAggregate1 "max"
1918 max_ = unsafeAggregate1 "max"
1906 min_ = unsafeAggregate1 "min"
1919 min_ = unsafeAggregate1 "min"
1907 avg = unsafeAggregate1 "avg"
1920 avg = unsafeAggregate1 "avg"
1908− corr = unsafeAggregate2 "corr"
1921+ corr = unsafeAggregateHet "corr"
1909− covarPop = unsafeAggregate2 "covar_pop"
1922+ covarPop = unsafeAggregateHet "covar_pop"
1910− covarSamp = unsafeAggregate2 "covar_samp"
1923+ covarSamp = unsafeAggregateHet "covar_samp"
1911− regrAvgX = unsafeAggregate2 "regr_avgx"
1924+ regrAvgX = unsafeAggregateHet "regr_avgx"
1912− regrAvgY = unsafeAggregate2 "regr_avgy"
1925+ regrAvgY = unsafeAggregateHet "regr_avgy"
1913− regrCount = unsafeAggregate2 "regr_count"
1926+ regrCount = unsafeAggregateHet "regr_count"
1914− regrIntercept = unsafeAggregate2 "regr_intercept"
1927+ regrIntercept = unsafeAggregateHet "regr_intercept"
1915− regrR2 = unsafeAggregate2 "regr_r2"
1928+ regrR2 = unsafeAggregateHet "regr_r2"
1916− regrSlope = unsafeAggregate2 "regr_slope"
1929+ regrSlope = unsafeAggregateHet "regr_slope"
1917− regrSxx = unsafeAggregate2 "regr_sxx"
1930+ regrSxx = unsafeAggregateHet "regr_sxx"
1918− regrSxy = unsafeAggregate2 "regr_sxy"
1931+ regrSxy = unsafeAggregateHet "regr_sxy"
1919− regrSyy = unsafeAggregate2 "regr_syy"
1932+ regrSyy = unsafeAggregateHet "regr_syy"
1920 stddev = unsafeAggregate1 "stddev"
1933 stddev = unsafeAggregate1 "stddev"
1921 stddevPop = unsafeAggregate1 "stddev_pop"
1934 stddevPop = unsafeAggregate1 "stddev_pop"
1922 stddevSamp = unsafeAggregate1 "stddev_samp"
1935 stddevSamp = unsafeAggregate1 "stddev_samp"
@@ -1925,7 +1938,7 @@ instance Aggregate
1925 varSamp = unsafeAggregate1 "var_samp"
1938 varSamp = unsafeAggregate1 "var_samp"
1926instance Aggregate
1939instance Aggregate
1927 (Expression outer grp commons schemas params from)
1940 (Expression outer grp commons schemas params from)
1928− (Expression outer grp commons schemas params from :*: Expression outer grp commons schemas params from)
1941+ (NP (Expression outer grp commons schemas params from))
1929 (WindowFunction outer grp commons schemas params from) where
1942 (WindowFunction outer grp commons schemas params from) where
1930 countStar = UnsafeWindowFunction "count(*)"
1943 countStar = UnsafeWindowFunction "count(*)"
1931 count = unsafeWindowFunction1 "count"
1944 count = unsafeWindowFunction1 "count"
@@ -1941,18 +1954,18 @@ instance Aggregate
1941 max_ = unsafeWindowFunction1 "max"
1954 max_ = unsafeWindowFunction1 "max"
1942 min_ = unsafeWindowFunction1 "min"
1955 min_ = unsafeWindowFunction1 "min"
1943 avg = unsafeWindowFunction1 "avg"
1956 avg = unsafeWindowFunction1 "avg"
1944− corr = unsafeWindowFunction2 "corr"
1957+ corr = unsafeWindowFunctionHet "corr"
1945− covarPop = unsafeWindowFunction2 "covar_pop"
1958+ covarPop = unsafeWindowFunctionHet "covar_pop"
1946− covarSamp = unsafeWindowFunction2 "covar_samp"
1959+ covarSamp = unsafeWindowFunctionHet "covar_samp"
1947− regrAvgX = unsafeWindowFunction2 "regr_avgx"
1960+ regrAvgX = unsafeWindowFunctionHet "regr_avgx"
1948− regrAvgY = unsafeWindowFunction2 "regr_avgy"
1961+ regrAvgY = unsafeWindowFunctionHet "regr_avgy"
1949− regrCount = unsafeWindowFunction2 "regr_count"
1962+ regrCount = unsafeWindowFunctionHet "regr_count"
1950− regrIntercept = unsafeWindowFunction2 "regr_intercept"
1963+ regrIntercept = unsafeWindowFunctionHet "regr_intercept"
1951− regrR2 = unsafeWindowFunction2 "regr_r2"
1964+ regrR2 = unsafeWindowFunctionHet "regr_r2"
1952− regrSlope = unsafeWindowFunction2 "regr_slope"
1965+ regrSlope = unsafeWindowFunctionHet "regr_slope"
1953− regrSxx = unsafeWindowFunction2 "regr_sxx"
1966+ regrSxx = unsafeWindowFunctionHet "regr_sxx"
1954− regrSxy = unsafeWindowFunction2 "regr_sxy"
1967+ regrSxy = unsafeWindowFunctionHet "regr_sxy"
1955− regrSyy = unsafeWindowFunction2 "regr_syy"
1968+ regrSyy = unsafeWindowFunctionHet "regr_syy"
1956 stddev = unsafeWindowFunction1 "stddev"
1969 stddev = unsafeWindowFunction1 "stddev"
1957 stddevPop = unsafeWindowFunction1 "stddev_pop"
1970 stddevPop = unsafeWindowFunction1 "stddev_pop"
1958 stddevSamp = unsafeWindowFunction1 "stddev_samp"
1971 stddevSamp = unsafeWindowFunction1 "stddev_samp"
@@ -1963,19 +1976,10 @@ instance Aggregate
1963-- | escape hatch to define aggregate functions
1976-- | escape hatch to define aggregate functions
1964unsafeAggregate1
1977unsafeAggregate1
1965 :: ByteString -- ^ aggregate function
1978 :: ByteString -- ^ aggregate function
1966− -> Distinction (Expression outer 'Ungrouped commons schemas params from) xty
1979+ -> AggrFun xty yty
1967− -> Expression outer ('Grouped bys) commons schemas params from yty
1968unsafeAggregate1 fun x = UnsafeExpression $ mconcat
1980unsafeAggregate1 fun x = UnsafeExpression $ mconcat
1969 [fun, "(", renderSQL x, ")"]
1981 [fun, "(", renderSQL x, ")"]
1970
1982
1971−-- | escape hatch to define aggregate binary functions
1972−unsafeAggregate2
1973− :: ByteString -- ^ aggregate function
1974− -> Distinction (Expression outer 'Ungrouped commons schemas params from :*: Expression outer 'Ungrouped commons schemas params from) xty
1975− -> Expression outer ('Grouped bys) commons schemas params from yty
1976−unsafeAggregate2 fun xx = UnsafeExpression $ mconcat
1977− [fun, "(", renderSQL xx, ")"]
1978−
1979-- | A type family that calculates `PGAvg` type of a `PGType`.
1983-- | A type family that calculates `PGAvg` type of a `PGType`.
1980type family PGAvg ty where
1984type family PGAvg ty where
1981 PGAvg 'PGint2 = 'PGnumeric
1985 PGAvg 'PGint2 = 'PGnumeric
@@ -2291,46 +2295,59 @@ newtype WindowFunction
2291instance RenderSQL (WindowFunction outer grp commons schemas params from ty) where
2295instance RenderSQL (WindowFunction outer grp commons schemas params from ty) where
2292 renderSQL = renderWindowFunction
2296 renderSQL = renderWindowFunction
2293
2297
2298+type WinFun0 x
2299+ = forall outer grp commons schemas params from
2300+ . WindowFunction outer grp commons schemas params from x
2301+ -- ^ cannot reference aliases
2302+
2303+type WinFun1 x y
2304+ = forall outer grp commons schemas params from
2305+ . Expression outer grp commons schemas params from x
2306+ -- ^ input
2307+ -> WindowFunction outer grp commons schemas params from y
2308+ -- ^ output
2309+
2310+type WinFunHet xs y
2311+ = forall outer grp commons schemas params from
2312+ . NP (Expression outer grp commons schemas params from) xs
2313+ -- ^ inputs
2314+ -> WindowFunction outer grp commons schemas params from y
2315+ -- ^ output
2316+
2294−unsafeWindowFunction1
2317+unsafeWindowFunction1 :: ByteString -> WinFun1 x y
2295− :: ByteString
2296− -> Expression outer grp commons schemas params from ty0
2297− -> WindowFunction outer grp commons schemas params from ty1
2298unsafeWindowFunction1 fun x
2318unsafeWindowFunction1 fun x
2299 = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x)
2319 = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x)
2300
2320
2301−unsafeWindowFunction2
2321+unsafeWindowFunctionHet :: SOP.SListI xs => ByteString -> WinFunHet xs y
2302− :: ByteString
2322+unsafeWindowFunctionHet fun xs = UnsafeWindowFunction $ fun <>
2303− -> (Expression outer grp commons schemas params from :*: Expression outer grp commons schemas params from) ty0
2304− -> WindowFunction outer grp commons schemas params from ty1
2305−unsafeWindowFunction2 fun (x :*: x')
2306− = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x <> ", " <> renderSQL x')
2323+ parenthesized (renderCommaSeparated renderSQL xs)
2307
2324
2308{- | rank of the current row with gaps; same as `rowNumber` of its first peer
2325{- | rank of the current row with gaps; same as `rowNumber` of its first peer
2309>>> printSQL rank
2326>>> printSQL rank
2310rank()
2327rank()
2311-}
2328-}
2312−rank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2329+rank :: WinFun0 ('NotNull 'PGint8)
2313rank = UnsafeWindowFunction "rank()"
2330rank = UnsafeWindowFunction "rank()"
2314
2331
2315{- | number of the current row within its partition, counting from 1
2332{- | number of the current row within its partition, counting from 1
2316>>> printSQL rowNumber
2333>>> printSQL rowNumber
2317row_number()
2334row_number()
2318-}
2335-}
2319−rowNumber :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2336+rowNumber :: WinFun0 ('NotNull 'PGint8)
2320rowNumber = UnsafeWindowFunction "row_number()"
2337rowNumber = UnsafeWindowFunction "row_number()"
2321
2338
2322{- | rank of the current row without gaps; this function counts peer groups
2339{- | rank of the current row without gaps; this function counts peer groups
2323>>> printSQL denseRank
2340>>> printSQL denseRank
2324dense_rank()
2341dense_rank()
2325-}
2342-}
2326−denseRank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2343+denseRank :: WinFun0 ('NotNull 'PGint8)
2327denseRank = UnsafeWindowFunction "dense_rank()"
2344denseRank = UnsafeWindowFunction "dense_rank()"
2328
2345
2329{- | relative rank of the current row: (rank - 1) / (total partition rows - 1)
2346{- | relative rank of the current row: (rank - 1) / (total partition rows - 1)
2330>>> printSQL percentRank
2347>>> printSQL percentRank
2331percent_rank()
2348percent_rank()
2332-}
2349-}
2333−percentRank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGfloat8)
2350+percentRank :: WinFun0 ('NotNull 'PGfloat8)
2334percentRank = UnsafeWindowFunction "percent_rank()"
2351percentRank = UnsafeWindowFunction "percent_rank()"
2335
2352
2336{- | cumulative distribution: (number of partition rows
2353{- | cumulative distribution: (number of partition rows
@@ -2338,16 +2355,13 @@ preceding or peer with current row) / total partition rows
2338>>> printSQL cumeDist
2355>>> printSQL cumeDist
2339cume_dist()
2356cume_dist()
2340-}
2357-}
2341−cumeDist :: WindowFunction outer grp commons schemas params from ('NotNull 'PGfloat8)
2358+cumeDist :: WinFun0 ('NotNull 'PGfloat8)
2342cumeDist = UnsafeWindowFunction "cume_dist()"
2359cumeDist = UnsafeWindowFunction "cume_dist()"
2343
2360
2344{- | integer ranging from 1 to the argument value,
2361{- | integer ranging from 1 to the argument value,
2345dividing the partition as equally as possible
2362dividing the partition as equally as possible
2346-}
2363-}
2347−ntile
2364+ntile :: WinFun1 ('NotNull 'PGint4) ('NotNull 'PGint4)
2348− :: Expression outer grp commons schemas params from ('NotNull 'PGint4)
2349− -- ^ num buckets
2350− -> WindowFunction outer grp commons schemas params from ('NotNull 'PGint4)
2351ntile = unsafeWindowFunction1 "ntile"
2365ntile = unsafeWindowFunction1 "ntile"
2352
2366
2353{- | returns value evaluated at the row that is offset rows before the current
2367{- | returns value evaluated at the row that is offset rows before the current
@@ -2355,64 +2369,34 @@ row within the partition; if there is no such row, instead return default
2355(which must be of the same type as value). Both offset and default are
2369(which must be of the same type as value). Both offset and default are
2356evaluated with respect to the current row.
2370evaluated with respect to the current row.
2357-}
2371-}
2358−lag
2372+lag :: WinFunHet '[ty, 'NotNull 'PGint4, ty] ty
2359− :: Expression outer grp commons schemas params from ty
2373+lag = unsafeWindowFunctionHet "lag"
2360− -- ^ value
2361− -> Expression outer grp commons schemas params from ('NotNull 'PGint4)
2362− -- ^ offset
2363− -> Expression outer grp commons schemas params from ty
2364− -- ^ default
2365− -> WindowFunction outer grp commons schemas params from ty
2366−lag value offset def = UnsafeWindowFunction $ "lag"
2367− <> parenthesized
2368− (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
2369
2374
2370{- | returns value evaluated at the row that is offset rows after the current
2375{- | returns value evaluated at the row that is offset rows after the current
2371row within the partition; if there is no such row, instead return default
2376row within the partition; if there is no such row, instead return default
2372(which must be of the same type as value). Both offset and default are
2377(which must be of the same type as value). Both offset and default are
2373evaluated with respect to the current row.
2378evaluated with respect to the current row.
2374-}
2379-}
2375−lead
2380+lead :: WinFunHet '[ty, 'NotNull 'PGint4, ty] ty
2376− :: Expression outer grp commons schemas params from ty
2381+lead = unsafeWindowFunctionHet "lag"
2377− -- ^ value
2378− -> Expression outer grp commons schemas params from ('NotNull 'PGint4)
2379− -- ^ offset
2380− -> Expression outer grp commons schemas params from ty
2381− -- ^ default
2382− -> WindowFunction outer grp commons schemas params from ty
2383−lead value offset def = UnsafeWindowFunction $ "lag"
2384− <> parenthesized
2385− (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
2386
2382
2387{- | returns value evaluated at the row that is the
2383{- | returns value evaluated at the row that is the
2388first row of the window frame
2384first row of the window frame
2389-}
2385-}
2390−firstValue
2386+firstValue :: WinFun1 ty ty
2391− :: Expression outer grp commons schemas params from ty
2392− -- ^ value
2393− -> WindowFunction outer grp commons schemas params from ty
2394firstValue = unsafeWindowFunction1 "first_value"
2387firstValue = unsafeWindowFunction1 "first_value"
2395
2388
2396{- | returns value evaluated at the row that is the
2389{- | returns value evaluated at the row that is the
2397last row of the window frame
2390last row of the window frame
2398-}
2391-}
2399−lastValue
2392+lastValue :: WinFun1 ty ty
2400− :: Expression outer grp commons schemas params from ty
2401− -- ^ value
2402− -> WindowFunction outer grp commons schemas params from ty
2403lastValue = unsafeWindowFunction1 "last_value"
2393lastValue = unsafeWindowFunction1 "last_value"
2404
2394
2405{- | returns value evaluated at the row that is the nth
2395{- | returns value evaluated at the row that is the nth
2406row of the window frame (counting from 1); null if no such row
2396row of the window frame (counting from 1); null if no such row
2407-}
2397-}
2408−nthValue
2398+nthValue :: WinFunHet '[null ty, 'NotNull 'PGint4] ('Null ty)
2409− :: Expression outer grp commons schemas params from (null ty)
2410− -- ^ value
2411− -> Expression outer grp commons schemas params from ('NotNull 'PGint4)
2412− -- ^ nth
2413− -> WindowFunction outer grp commons schemas params from ('Null ty)
2414−nthValue value nth = UnsafeWindowFunction $ "nth_value"
2399+nthValue = unsafeWindowFunctionHet "nth_value"
2415− <> parenthesized (commaSeparated [renderSQL value, renderSQL nth])
2416
2400
2417{-|
2401{-|
2418Create date from year, month and day fields
2402Create date from year, month and day fields