@@ -1,5 +1,5 @@
2−Module: Squeal.PostgreSQL.Query
2+Module: Squeal.PostgreSQL.Expression
3Description: Squeal expressions
3Description: Squeal expressions
4Copyright: (c) Eitan Chatav, 2017
4Copyright: (c) Eitan Chatav, 2017
5Maintainer: eitan@morphism.tech
5Maintainer: eitan@morphism.tech
@@ -210,7 +210,7 @@ import qualified Data.Aeson as JSON
210import Data.Ratio
210import Data.Ratio
211import Data.String
211import Data.String
212import Data.Word
212import Data.Word
213−import Generics.SOP hiding (from)
213+import Generics.SOP hiding (All, from)
214import GHC.Generics ((:*:) (..))
214import GHC.Generics ((:*:) (..))
215import GHC.OverloadedLabels
215import GHC.OverloadedLabels
216import GHC.TypeLits
216import GHC.TypeLits
@@ -218,6 +218,7 @@ import Prelude hiding (id, (.))
219import qualified Data.ByteString as ByteString
219import qualified Data.ByteString as ByteString
220import qualified GHC.Generics as GHC
220import qualified GHC.Generics as GHC
221+import qualified Generics.SOP as SOP
222import Squeal.PostgreSQL.Render
223import Squeal.PostgreSQL.Render
223import Squeal.PostgreSQL.Schema
224import Squeal.PostgreSQL.Schema
@@ -1216,11 +1217,11 @@ jsonbBuildArray
1216jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
1217jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
1218unsafeRowFunction
1219unsafeRowFunction
1219− :: All Top elems
1220+ :: SOP.All SOP.Top elems
1220 => NP (Aliased (Expression outer grp commons schemas params from)) elems
1221 => NP (Aliased (Expression outer grp commons schemas params from)) elems
1221 -> [ByteString]
1222 -> [ByteString]
1222unsafeRowFunction =
1223unsafeRowFunction =
1223− (`appEndo` []) . hcfoldMap (Proxy :: Proxy Top)
1224+ (`appEndo` []) . hcfoldMap (Proxy :: SOP.Proxy SOP.Top)
1224 (\(col `As` name) -> Endo $ \xs ->
1225 (\(col `As` name) -> Endo $ \xs ->
1225 renderAliasString name : renderSQL col : xs)
1226 renderAliasString name : renderSQL col : xs)
@@ -1231,7 +1232,7 @@ unsafeRowFunction =
1231-- argument list. The elements of the argument list must alternate between text
1232-- argument list. The elements of the argument list must alternate between text
1232-- and values.
1233-- and values.
1233jsonBuildObject
1234jsonBuildObject
1234− :: All Top elems
1235+ :: SOP.All SOP.Top elems
1235 => NP (Aliased (Expression outer grp commons schemas params from)) elems
1236 => NP (Aliased (Expression outer grp commons schemas params from)) elems
1236 -> Expression outer grp commons schemas params from (nullity 'PGjson)
1237 -> Expression outer grp commons schemas params from (nullity 'PGjson)
1237jsonBuildObject
1238jsonBuildObject
@@ -1244,7 +1245,7 @@ jsonBuildObject
1244-- variadic argument list. The elements of the argument list must alternate
1245-- variadic argument list. The elements of the argument list must alternate
1245-- between text and values.
1246-- between text and values.
1246jsonbBuildObject
1247jsonbBuildObject
1247− :: All Top elems
1248+ :: SOP.All SOP.Top elems
1248 => NP (Aliased (Expression outer grp commons schemas params from)) elems
1249 => NP (Aliased (Expression outer grp commons schemas params from)) elems
1249 -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1250 -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1250jsonbBuildObject
1251jsonbBuildObject
@@ -1427,6 +1428,11 @@ jsonbPretty = unsafeFunction "jsonb_pretty"
1427aggregation
1428aggregation
1428-----------------------------------------}
1429-----------------------------------------}
1432+`Aggregate` functions compute a single result from a set of input values.
1433+`Aggregate` functions can be used as `GroupedBy` `Expression`s as well
1434+as `WindowFunction`s.
1430class Aggregate expr1 expr2 aggr
1436class Aggregate expr1 expr2 aggr
1431 | aggr -> expr1, aggr -> expr2 where
1437 | aggr -> expr1, aggr -> expr2 where
@@ -1714,6 +1720,12 @@ class Aggregate expr1 expr2 aggr
1714 :: expr1 (nullity ty)
1720 :: expr1 (nullity ty)
1715 -> aggr (nullity (PGAvg ty))
1721 -> aggr (nullity (PGAvg ty))
1724+`Distinction`s are used for the input of `Aggregate` `Expression`s.
1725+`All` invokes the aggregate once for each input row.
1726+`Distinct` invokes the aggregate once for each distinct value of the expression
1727+(or distinct set of values, for multiple expressions) found in the input
1717data Distinction expr ty
1729data Distinction expr ty
1718 = All (expr ty)
1730 = All (expr ty)
1719 | Distinct (expr ty)
1731 | Distinct (expr ty)
@@ -1839,7 +1851,8 @@ type family PGAvg ty where
1839type expressions
1851type expressions
1840-----------------------------------------}
1852-----------------------------------------}
1842−-- | `TypeExpression`s are used in `cast`s and `createTable` commands.
1854+-- | `TypeExpression`s are used in `cast`s and
1855+-- `Squeal.PostgreSQL.Definition.createTable` commands.
1843newtype TypeExpression (schemas :: SchemasType) (ty :: NullityType)
1856newtype TypeExpression (schemas :: SchemasType) (ty :: NullityType)
1844 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
1857 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
1845 deriving (GHC.Generic,Show,Eq,Ord,NFData)
1858 deriving (GHC.Generic,Show,Eq,Ord,NFData)
@@ -1955,21 +1968,21 @@ vararray ty = UnsafeTypeExpression $ renderSQL ty <> "[]"
1955-- >>> renderSQL (fixarray @'[2] json)
1968-- >>> renderSQL (fixarray @'[2] json)
1956-- "json[2]"
1969-- "json[2]"
1958− :: forall dims schemas nullity pg. All KnownNat dims
1971+ :: forall dims schemas nullity pg. SOP.All KnownNat dims
1959 => TypeExpression schemas pg
1972 => TypeExpression schemas pg
1960 -> TypeExpression schemas (nullity ('PGfixarray dims pg))
1973 -> TypeExpression schemas (nullity ('PGfixarray dims pg))
1961fixarray ty = UnsafeTypeExpression $
1974fixarray ty = UnsafeTypeExpression $
1962 renderSQL ty <> renderDims @dims
1975 renderSQL ty <> renderDims @dims
1964− renderDims :: forall ns. All KnownNat ns => ByteString
1977+ renderDims :: forall ns. SOP.All KnownNat ns => ByteString
1965 renderDims =
1978 renderDims =
1966 ("[" <>)
1979 ("[" <>)
1967 . (<> "]")
1980 . (<> "]")
1968 . ByteString.intercalate "]["
1981 . ByteString.intercalate "]["
1969 . hcollapse
1982 . hcollapse
1970− $ hcmap (Proxy @KnownNat)
1983+ $ hcmap (SOP.Proxy @KnownNat)
1971 (K . fromString . show . natVal)
1984 (K . fromString . show . natVal)
1972− (hpure Proxy :: NP Proxy ns)
1985+ (hpure SOP.Proxy :: NP SOP.Proxy ns)
1974-- | `pgtype` is a demoted version of a `PGType`
1987-- | `pgtype` is a demoted version of a `PGType`
1975class PGTyped schemas (ty :: NullityType) where
1988class PGTyped schemas (ty :: NullityType) where
@@ -1999,7 +2012,7 @@ instance PGTyped schemas (nullity 'PGjsonb) where pgtype = jsonb
1999instance PGTyped schemas ty
2012instance PGTyped schemas ty
2000 => PGTyped schemas (nullity ('PGvararray ty)) where
2013 => PGTyped schemas (nullity ('PGvararray ty)) where
2001 pgtype = vararray (pgtype @schemas @ty)
2014 pgtype = vararray (pgtype @schemas @ty)
2002−instance (All KnownNat dims, PGTyped schemas ty)
2015+instance (SOP.All KnownNat dims, PGTyped schemas ty)
2003 => PGTyped schemas (nullity ('PGfixarray dims ty)) where
2016 => PGTyped schemas (nullity ('PGfixarray dims ty)) where
2004 pgtype = fixarray @dims (pgtype @schemas @ty)
2017 pgtype = fixarray @dims (pgtype @schemas @ty)
@@ -2007,8 +2020,9 @@ instance (All KnownNat dims, PGTyped schemas ty)
2008-----------------------------------------}
2021-----------------------------------------}
2010−-- | `SortExpression`s are used by `sortBy` to optionally sort the results
2023+-- | `SortExpression`s are used by `orderBy` to optionally sort the results
2024+-- of a `Squeal.PostgreSQL.Query.Query`. `Asc` or `Desc`
2011−-- of a `Query`. `Asc` or `Desc` set the sort direction of a `NotNull` result
2025+-- set the sort direction of a `NotNull` result
2012-- column to ascending or descending. Ascending order puts smaller values
2026-- column to ascending or descending. Ascending order puts smaller values
2013-- first, where "smaller" is defined in terms of the `.<` operator. Similarly,
2027-- first, where "smaller" is defined in terms of the `.<` operator. Similarly,
2014-- descending order is determined with the `.>` operator. `AscNullsFirst`,
2028-- descending order is determined with the `.>` operator. `AscNullsFirst`,
@@ -2016,26 +2030,37 @@ Sorting
2016-- determine whether nulls appear before or after non-null values in the sort
2030-- determine whether nulls appear before or after non-null values in the sort
2017-- ordering of a `Null` result column.
2031-- ordering of a `Null` result column.
2018data SortExpression outer grp commons schemas params from where
2032data SortExpression outer grp commons schemas params from where
2020− :: Expression outer grp commons schemas params from ('NotNull ty)
2034+ :: Expression outer grp commons schemas params from ('NotNull ty)
2021− -> SortExpression outer grp commons schemas params from
2035+ -> SortExpression outer grp commons schemas params from
2023− :: Expression outer grp commons schemas params from ('NotNull ty)
2037+ :: Expression outer grp commons schemas params from ('NotNull ty)
2024− -> SortExpression outer grp commons schemas params from
2038+ -> SortExpression outer grp commons schemas params from
2025− AscNullsFirst
2039+ AscNullsFirst
2026− :: Expression outer grp commons schemas params from ('Null ty)
2040+ :: Expression outer grp commons schemas params from ('Null ty)
2027− -> SortExpression outer grp commons schemas params from
2041+ -> SortExpression outer grp commons schemas params from
2028− AscNullsLast
2042+ AscNullsLast
2029− :: Expression outer grp commons schemas params from ('Null ty)
2043+ :: Expression outer grp commons schemas params from ('Null ty)
2030− -> SortExpression outer grp commons schemas params from
2044+ -> SortExpression outer grp commons schemas params from
2031− DescNullsFirst
2045+ DescNullsFirst
2032− :: Expression outer grp commons schemas params from ('Null ty)
2046+ :: Expression outer grp commons schemas params from ('Null ty)
2033− -> SortExpression outer grp commons schemas params from
2047+ -> SortExpression outer grp commons schemas params from
2034− DescNullsLast
2048+ DescNullsLast
2035− :: Expression outer grp commons schemas params from ('Null ty)
2049+ :: Expression outer grp commons schemas params from ('Null ty)
2036− -> SortExpression outer grp commons schemas params from
2050+ -> SortExpression outer grp commons schemas params from
2037deriving instance Show (SortExpression outer grp commons schemas params from)
2051deriving instance Show (SortExpression outer grp commons schemas params from)
2054+The `orderBy` clause causes the result rows of a `Squeal.PostgreSQL.Query.TableExpression`
2055+to be sorted according to the specified `SortExpression`(s).
2056+If two rows are equal according to the leftmost expression,
2057+they are compared according to the next expression and so on.
2058+If they are equal according to all specified expressions,
2059+they are returned in an implementation-dependent order.
2061+You can also control the order in which rows are processed by window functions
2062+using `orderBy` within `Squeal.PostgreSQL.Query.Over`.
2039class OrderBy expr where
2064class OrderBy expr where
2041 :: [SortExpression outer grp commons schemas params from]
2066 :: [SortExpression outer grp commons schemas params from]
@@ -2054,6 +2079,8 @@ instance RenderSQL (SortExpression outer grp commons schemas params from) where
2054 AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
2079 AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
2055 DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"
2080 DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"
2082+-- | A `WindowDefinition` is a set of table rows that are somehow related
2083+-- to the current row
2057data WindowDefinition outer grp commons schemas params from where
2084data WindowDefinition outer grp commons schemas params from where
2058 WindowDefinition
2085 WindowDefinition
2059 :: SListI bys
2086 :: SListI bys
@@ -2077,12 +2104,28 @@ instance RenderSQL (WindowDefinition outer commons schemas from grp params) wher
2077 srts -> " ORDER" <+> "BY"
2104 srts -> " ORDER" <+> "BY"
2078 <+> commaSeparated (renderSQL <$> srts)
2105 <+> commaSeparated (renderSQL <$> srts)
2108+The `partitionBy` clause within `Squeal.PostgreSQL.Query.Over` divides the rows into groups,
2109+or partitions, that share the same values of the `partitionBy` `Expression`(s).
2110+For each row, the window function is computed across the rows that fall into
2111+the same partition as the current row.
2080partitionBy
2113partitionBy
2081 :: SListI bys
2114 :: SListI bys
2082 => NP (Expression outer grp commons schemas params from) bys
2115 => NP (Expression outer grp commons schemas params from) bys
2083 -> WindowDefinition outer grp commons schemas params from
2116 -> WindowDefinition outer grp commons schemas params from
2084partitionBy bys = WindowDefinition bys []
2117partitionBy bys = WindowDefinition bys []
2120+A window function performs a calculation across a set of table rows
2121+that are somehow related to the current row. This is comparable to the type
2122+of calculation that can be done with an aggregate function.
2123+However, window functions do not cause rows to become grouped into a single
2124+output row like non-window aggregate calls would.
2125+Instead, the rows retain their separate identities.
2126+Behind the scenes, the window function is able to access more than
2127+just the current row of the query result.
2086newtype WindowFunction
2129newtype WindowFunction
2087 (outer :: FromType)
2130 (outer :: FromType)
2088 (grp :: Grouping)
2131 (grp :: Grouping)