{-# LANGUAGE
DataKinds
, DeriveGeneric
, DerivingStrategies
, FlexibleContexts
, FlexibleInstances
, GADTs
, GeneralizedNewtypeDeriving
, KindSignatures
, LambdaCase
, MultiParamTypeClasses
, OverloadedStrings
, PatternSynonyms
, RankNTypes
, ScopedTypeVariables
, TypeApplications
, TypeOperators
, UndecidableInstances
#-}
module Squeal.PostgreSQL.Expression.Window
(
WindowDefinition (..)
, partitionBy
, WindowFunction (..)
, WindowArg (..)
, pattern Window
, pattern Windows
, WinFun0
, type (-#->)
, type (--#->)
, rank
, rowNumber
, denseRank
, percentRank
, cumeDist
, ntile
, lag
, lead
, firstValue
, lastValue
, nthValue
, unsafeWindowFunction1
, unsafeWindowFunctionN
) where
import Control.DeepSeq
import Data.ByteString (ByteString)
import qualified GHC.Generics as GHC
import qualified Generics.SOP as SOP
import Squeal.PostgreSQL.Type.Alias
import Squeal.PostgreSQL.Expression
import Squeal.PostgreSQL.Expression.Aggregate
import Squeal.PostgreSQL.Expression.Logic
import Squeal.PostgreSQL.Expression.Sort
import Squeal.PostgreSQL.Type.List
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.Schema
instance Aggregate (WindowArg grp) (WindowFunction grp) where
countStar :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
WindowFunction grp lat with db params from ('NotNull 'PGint8)
countStar = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> WindowFunction grp lat with db params from ty
UnsafeWindowFunction ByteString
"count(*)"
count :: forall (ty :: NullType) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType).
WindowArg grp '[ty] lat with db params from
-> WindowFunction grp lat with db params from ('NotNull 'PGint8)
count = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"count"
sum_ :: forall (null :: PGType -> NullType) (ty :: PGType)
(lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
WindowArg grp '[null ty] lat with db params from
-> WindowFunction grp lat with db params from ('Null (PGSum ty))
sum_ = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"sum"
arrayAgg :: forall (ty :: NullType) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType).
WindowArg grp '[ty] lat with db params from
-> WindowFunction
grp lat with db params from ('Null ('PGvararray ty))
arrayAgg = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"array_agg"
jsonAgg :: forall (ty :: NullType) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType).
WindowArg grp '[ty] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGjson)
jsonAgg = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"json_agg"
jsonbAgg :: forall (ty :: NullType) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType).
WindowArg grp '[ty] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGjsonb)
jsonbAgg = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"jsonb_agg"
bitAnd :: forall (int :: PGType) (null :: PGType -> NullType)
(lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
In int PGIntegral =>
WindowArg grp '[null int] lat with db params from
-> WindowFunction grp lat with db params from ('Null int)
bitAnd = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"bit_and"
bitOr :: forall (int :: PGType) (null :: PGType -> NullType)
(lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
In int PGIntegral =>
WindowArg grp '[null int] lat with db params from
-> WindowFunction grp lat with db params from ('Null int)
bitOr = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"bit_or"
boolAnd :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg grp '[null 'PGbool] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGbool)
boolAnd = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"bool_and"
boolOr :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg grp '[null 'PGbool] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGbool)
boolOr = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"bool_or"
every :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg grp '[null 'PGbool] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGbool)
every = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"every"
max_ :: forall (null :: PGType -> NullType) (ty :: PGType)
(lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
WindowArg grp '[null ty] lat with db params from
-> WindowFunction grp lat with db params from ('Null ty)
max_ = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"max"
min_ :: forall (null :: PGType -> NullType) (ty :: PGType)
(lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
WindowArg grp '[null ty] lat with db params from
-> WindowFunction grp lat with db params from ('Null ty)
min_ = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"min"
avg :: forall (null :: PGType -> NullType) (ty :: PGType)
(lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
WindowArg grp '[null ty] lat with db params from
-> WindowFunction grp lat with db params from ('Null (PGAvg ty))
avg = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"avg"
corr :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg
grp '[null 'PGfloat8, null 'PGfloat8] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGfloat8)
corr = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"corr"
covarPop :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg
grp '[null 'PGfloat8, null 'PGfloat8] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGfloat8)
covarPop = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"covar_pop"
covarSamp :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg
grp '[null 'PGfloat8, null 'PGfloat8] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGfloat8)
covarSamp = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"covar_samp"
regrAvgX :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg
grp '[null 'PGfloat8, null 'PGfloat8] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGfloat8)
regrAvgX = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"regr_avgx"
regrAvgY :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg
grp '[null 'PGfloat8, null 'PGfloat8] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGfloat8)
regrAvgY = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"regr_avgy"
regrCount :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg
grp '[null 'PGfloat8, null 'PGfloat8] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGint8)
regrCount = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"regr_count"
regrIntercept :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg
grp '[null 'PGfloat8, null 'PGfloat8] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGfloat8)
regrIntercept = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"regr_intercept"
regrR2 :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg
grp '[null 'PGfloat8, null 'PGfloat8] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGfloat8)
regrR2 = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"regr_r2"
regrSlope :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg
grp '[null 'PGfloat8, null 'PGfloat8] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGfloat8)
regrSlope = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"regr_slope"
regrSxx :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg
grp '[null 'PGfloat8, null 'PGfloat8] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGfloat8)
regrSxx = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"regr_sxx"
regrSxy :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg
grp '[null 'PGfloat8, null 'PGfloat8] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGfloat8)
regrSxy = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"regr_sxy"
regrSyy :: forall (null :: PGType -> NullType) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg
grp '[null 'PGfloat8, null 'PGfloat8] lat with db params from
-> WindowFunction grp lat with db params from ('Null 'PGfloat8)
regrSyy = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"regr_syy"
stddev :: forall (null :: PGType -> NullType) (ty :: PGType)
(lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
WindowArg grp '[null ty] lat with db params from
-> WindowFunction grp lat with db params from ('Null (PGAvg ty))
stddev = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"stddev"
stddevPop :: forall (null :: PGType -> NullType) (ty :: PGType)
(lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
WindowArg grp '[null ty] lat with db params from
-> WindowFunction grp lat with db params from ('Null (PGAvg ty))
stddevPop = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"stddev_pop"
stddevSamp :: forall (null :: PGType -> NullType) (ty :: PGType)
(lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
WindowArg grp '[null ty] lat with db params from
-> WindowFunction grp lat with db params from ('Null (PGAvg ty))
stddevSamp = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"stddev_samp"
variance :: forall (null :: PGType -> NullType) (ty :: PGType)
(lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
WindowArg grp '[null ty] lat with db params from
-> WindowFunction grp lat with db params from ('Null (PGAvg ty))
variance = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"variance"
varPop :: forall (null :: PGType -> NullType) (ty :: PGType)
(lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
WindowArg grp '[null ty] lat with db params from
-> WindowFunction grp lat with db params from ('Null (PGAvg ty))
varPop = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"var_pop"
varSamp :: forall (null :: PGType -> NullType) (ty :: PGType)
(lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
WindowArg grp '[null ty] lat with db params from
-> WindowFunction grp lat with db params from ('Null (PGAvg ty))
varSamp = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"var_samp"
data WindowDefinition grp lat with db params from where
WindowDefinition
:: SOP.SListI bys
=> NP (Expression grp lat with db params from) bys
-> [SortExpression grp lat with db params from]
-> WindowDefinition grp lat with db params from
instance OrderBy (WindowDefinition grp) grp where
orderBy :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType).
[SortExpression grp lat with db params from]
-> WindowDefinition grp lat with db params from
-> WindowDefinition grp lat with db params from
orderBy [SortExpression grp lat with db params from]
sortsR (WindowDefinition NP (Expression grp lat with db params from) bys
parts [SortExpression grp lat with db params from]
sortsL)
= forall (bys :: [NullType]) (grp :: Grouping) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
SListI bys =>
NP (Expression grp lat with db params from) bys
-> [SortExpression grp lat with db params from]
-> WindowDefinition grp lat with db params from
WindowDefinition NP (Expression grp lat with db params from) bys
parts ([SortExpression grp lat with db params from]
sortsL forall a. [a] -> [a] -> [a]
++ [SortExpression grp lat with db params from]
sortsR)
instance RenderSQL (WindowDefinition grp lat with db params from) where
renderSQL :: WindowDefinition grp lat with db params from -> ByteString
renderSQL (WindowDefinition NP (Expression grp lat with db params from) bys
part [SortExpression grp lat with db params from]
ord) =
forall {grp :: Grouping} {lat :: FromType} {with :: FromType}
{db :: SchemasType} {params :: [NullType]} {from :: FromType}.
NP (Expression grp lat with db params from) bys -> ByteString
renderPartitionByClause NP (Expression grp lat with db params from) bys
part forall a. Semigroup a => a -> a -> a
<> forall sql. RenderSQL sql => sql -> ByteString
renderSQL [SortExpression grp lat with db params from]
ord
where
renderPartitionByClause :: NP (Expression grp lat with db params from) bys -> ByteString
renderPartitionByClause = \case
NP (Expression grp lat with db params from) bys
Nil -> ByteString
""
NP (Expression grp lat with db params from) bys
parts -> ByteString
"PARTITION" ByteString -> ByteString -> ByteString
<+> ByteString
"BY" ByteString -> ByteString -> ByteString
<+> forall {k} (xs :: [k]) (expression :: k -> *).
SListI xs =>
(forall (x :: k). expression x -> ByteString)
-> NP expression xs -> ByteString
renderCommaSeparated forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
Expression grp lat with db params from ty -> ByteString
renderExpression NP (Expression grp lat with db params from) bys
parts
partitionBy
:: SOP.SListI bys
=> NP (Expression grp lat with db params from) bys
-> WindowDefinition grp lat with db params from
partitionBy :: forall (bys :: [NullType]) (grp :: Grouping) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
SListI bys =>
NP (Expression grp lat with db params from) bys
-> WindowDefinition grp lat with db params from
partitionBy NP (Expression grp lat with db params from) bys
bys = forall (bys :: [NullType]) (grp :: Grouping) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
SListI bys =>
NP (Expression grp lat with db params from) bys
-> [SortExpression grp lat with db params from]
-> WindowDefinition grp lat with db params from
WindowDefinition NP (Expression grp lat with db params from) bys
bys []
newtype WindowFunction
(grp :: Grouping)
(lat :: FromType)
(with :: FromType)
(db :: SchemasType)
(params :: [NullType])
(from :: FromType)
(ty :: NullType)
= UnsafeWindowFunction { forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty -> ByteString
renderWindowFunction :: ByteString }
deriving stock (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType) x.
Rep (WindowFunction grp lat with db params from ty) x
-> WindowFunction grp lat with db params from ty
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType) x.
WindowFunction grp lat with db params from ty
-> Rep (WindowFunction grp lat with db params from ty) x
$cto :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType) x.
Rep (WindowFunction grp lat with db params from ty) x
-> WindowFunction grp lat with db params from ty
$cfrom :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType) x.
WindowFunction grp lat with db params from ty
-> Rep (WindowFunction grp lat with db params from ty) x
GHC.Generic,Int -> WindowFunction grp lat with db params from ty -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
Int -> WindowFunction grp lat with db params from ty -> ShowS
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
[WindowFunction grp lat with db params from ty] -> ShowS
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty -> String
showList :: [WindowFunction grp lat with db params from ty] -> ShowS
$cshowList :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
[WindowFunction grp lat with db params from ty] -> ShowS
show :: WindowFunction grp lat with db params from ty -> String
$cshow :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty -> String
showsPrec :: Int -> WindowFunction grp lat with db params from ty -> ShowS
$cshowsPrec :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
Int -> WindowFunction grp lat with db params from ty -> ShowS
Show,WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
/= :: WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
$c/= :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
== :: WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
$c== :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
Eq,WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Ordering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
Eq (WindowFunction grp lat with db params from ty)
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Ordering
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty
min :: WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty
$cmin :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty
max :: WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty
$cmax :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty
>= :: WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
$c>= :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
> :: WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
$c> :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
<= :: WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
$c<= :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
< :: WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
$c< :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Bool
compare :: WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Ordering
$ccompare :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty
-> WindowFunction grp lat with db params from ty -> Ordering
Ord)
deriving newtype (WindowFunction grp lat with db params from ty -> ()
forall a. (a -> ()) -> NFData a
forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty -> ()
rnf :: WindowFunction grp lat with db params from ty -> ()
$crnf :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty -> ()
NFData)
data WindowArg
(grp :: Grouping)
(args :: [NullType])
(lat :: FromType)
(with :: FromType)
(db :: SchemasType)
(params :: [NullType])
(from :: FromType)
= WindowArg
{ forall (grp :: Grouping) (args :: [NullType]) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg grp args lat with db params from
-> NP (Expression grp lat with db params from) args
windowArgs :: NP (Expression grp lat with db params from) args
, forall (grp :: Grouping) (args :: [NullType]) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
WindowArg grp args lat with db params from
-> [Condition grp lat with db params from]
windowFilter :: [Condition grp lat with db params from]
} deriving stock (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (grp :: Grouping) (args :: [NullType]) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType) x.
Rep (WindowArg grp args lat with db params from) x
-> WindowArg grp args lat with db params from
forall (grp :: Grouping) (args :: [NullType]) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType) x.
WindowArg grp args lat with db params from
-> Rep (WindowArg grp args lat with db params from) x
$cto :: forall (grp :: Grouping) (args :: [NullType]) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType) x.
Rep (WindowArg grp args lat with db params from) x
-> WindowArg grp args lat with db params from
$cfrom :: forall (grp :: Grouping) (args :: [NullType]) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType) x.
WindowArg grp args lat with db params from
-> Rep (WindowArg grp args lat with db params from) x
GHC.Generic)
instance (HasUnique tab (Join from lat) row, Has col row ty)
=> IsLabel col (WindowArg 'Ungrouped '[ty] lat with db params from) where
fromLabel :: WindowArg 'Ungrouped '[ty] lat with db params from
fromLabel = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(arg :: NullType).
Expression grp lat with db params from arg
-> WindowArg grp '[arg] lat with db params from
Window (forall (x :: Symbol) a. IsLabel x a => a
fromLabel @col)
instance (Has tab (Join from lat) row, Has col row ty)
=> IsQualified tab col (WindowArg 'Ungrouped '[ty] lat with db params from) where
Alias tab
tab ! :: Alias tab
-> Alias col -> WindowArg 'Ungrouped '[ty] lat with db params from
! Alias col
col = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(arg :: NullType).
Expression grp lat with db params from arg
-> WindowArg grp '[arg] lat with db params from
Window (Alias tab
tab forall (qualifier :: Symbol) (alias :: Symbol) expression.
IsQualified qualifier alias expression =>
Alias qualifier -> Alias alias -> expression
! Alias col
col)
instance (HasUnique tab (Join from lat) row, Has col row ty, GroupedBy tab col bys)
=> IsLabel col (WindowArg ('Grouped bys) '[ty] lat with db params from) where
fromLabel :: WindowArg ('Grouped bys) '[ty] lat with db params from
fromLabel = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(arg :: NullType).
Expression grp lat with db params from arg
-> WindowArg grp '[arg] lat with db params from
Window (forall (x :: Symbol) a. IsLabel x a => a
fromLabel @col)
instance (Has tab (Join from lat) row, Has col row ty, GroupedBy tab col bys)
=> IsQualified tab col (WindowArg ('Grouped bys) '[ty] lat with db params from) where
Alias tab
tab ! :: Alias tab
-> Alias col
-> WindowArg ('Grouped bys) '[ty] lat with db params from
! Alias col
col = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(arg :: NullType).
Expression grp lat with db params from arg
-> WindowArg grp '[arg] lat with db params from
Window (Alias tab
tab forall (qualifier :: Symbol) (alias :: Symbol) expression.
IsQualified qualifier alias expression =>
Alias qualifier -> Alias alias -> expression
! Alias col
col)
instance SOP.SListI args
=> RenderSQL (WindowArg grp args lat with db params from) where
renderSQL :: WindowArg grp args lat with db params from -> ByteString
renderSQL (WindowArg NP (Expression grp lat with db params from) args
args [Condition grp lat with db params from]
filters) =
ByteString -> ByteString
parenthesized (forall {k} (xs :: [k]) (expression :: k -> *).
SListI xs =>
(forall (x :: k). expression x -> ByteString)
-> NP expression xs -> ByteString
renderCommaSeparated forall sql. RenderSQL sql => sql -> ByteString
renderSQL NP (Expression grp lat with db params from) args
args)
forall a b. a -> (a -> b) -> b
& forall {grp :: Grouping} {lat :: FromType} {with :: FromType}
{db :: SchemasType} {params :: [NullType]} {from :: FromType}
{null :: PGType -> NullType}.
[Expression grp lat with db params from (null 'PGbool)]
-> ByteString -> ByteString
renderFilters [Condition grp lat with db params from]
filters
where
renderFilter :: ByteString -> ByteString
renderFilter ByteString
wh = ByteString
"FILTER" ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized (ByteString
"WHERE" ByteString -> ByteString -> ByteString
<+> ByteString
wh)
renderFilters :: [Expression grp lat with db params from (null 'PGbool)]
-> ByteString -> ByteString
renderFilters = \case
[] -> forall a. a -> a
id
Expression grp lat with db params from (null 'PGbool)
wh:[Expression grp lat with db params from (null 'PGbool)]
whs -> (ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
renderFilter (forall sql. RenderSQL sql => sql -> ByteString
renderSQL (forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall (null :: PGType -> NullType).
Operator (null 'PGbool) (null 'PGbool) (null 'PGbool)
(.&&) Expression grp lat with db params from (null 'PGbool)
wh [Expression grp lat with db params from (null 'PGbool)]
whs)))
instance FilterWhere (WindowArg grp) grp where
filterWhere :: forall (lat :: FromType) (with :: FromType) (db :: SchemasType)
(params :: [NullType]) (from :: FromType) (xs :: [NullType]).
Condition grp lat with db params from
-> WindowArg grp xs lat with db params from
-> WindowArg grp xs lat with db params from
filterWhere Condition grp lat with db params from
wh (WindowArg NP (Expression grp lat with db params from) xs
args [Condition grp lat with db params from]
filters) = forall (grp :: Grouping) (args :: [NullType]) (lat :: FromType)
(with :: FromType) (db :: SchemasType) (params :: [NullType])
(from :: FromType).
NP (Expression grp lat with db params from) args
-> [Condition grp lat with db params from]
-> WindowArg grp args lat with db params from
WindowArg NP (Expression grp lat with db params from) xs
args (Condition grp lat with db params from
wh forall a. a -> [a] -> [a]
: [Condition grp lat with db params from]
filters)
pattern Window
:: Expression grp lat with db params from arg
-> WindowArg grp '[arg] lat with db params from
pattern $bWindow :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(arg :: NullType).
Expression grp lat with db params from arg
-> WindowArg grp '[arg] lat with db params from
$mWindow :: forall {r} {grp :: Grouping} {lat :: FromType} {with :: FromType}
{db :: SchemasType} {params :: [NullType]} {from :: FromType}
{arg :: NullType}.
WindowArg grp '[arg] lat with db params from
-> (Expression grp lat with db params from arg -> r)
-> ((# #) -> r)
-> r
Window x = Windows (x :* Nil)
pattern Windows
:: NP (Expression grp lat with db params from) args
-> WindowArg grp args lat with db params from
pattern $bWindows :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(args :: [NullType]).
NP (Expression grp lat with db params from) args
-> WindowArg grp args lat with db params from
$mWindows :: forall {r} {grp :: Grouping} {lat :: FromType} {with :: FromType}
{db :: SchemasType} {params :: [NullType]} {from :: FromType}
{args :: [NullType]}.
WindowArg grp args lat with db params from
-> (NP (Expression grp lat with db params from) args -> r)
-> ((# #) -> r)
-> r
Windows xs = WindowArg xs []
instance RenderSQL (WindowFunction grp lat with db params from ty) where
renderSQL :: WindowFunction grp lat with db params from ty -> ByteString
renderSQL = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
WindowFunction grp lat with db params from ty -> ByteString
renderWindowFunction
type WinFun0 x
= forall grp lat with db params from
. WindowFunction grp lat with db params from x
type (-#->) x y
= forall grp lat with db params from
. WindowArg grp '[x] lat with db params from
-> WindowFunction grp lat with db params from y
type (--#->) xs y
= forall grp lat with db params from
. WindowArg grp xs lat with db params from
-> WindowFunction grp lat with db params from y
unsafeWindowFunction1 :: ByteString -> x -#-> y
unsafeWindowFunction1 :: forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
fun WindowArg grp '[x] lat with db params from
x
= forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> WindowFunction grp lat with db params from ty
UnsafeWindowFunction forall a b. (a -> b) -> a -> b
$ ByteString
fun forall a. Semigroup a => a -> a -> a
<> forall sql. RenderSQL sql => sql -> ByteString
renderSQL WindowArg grp '[x] lat with db params from
x
unsafeWindowFunctionN :: SOP.SListI xs => ByteString -> xs --#-> y
unsafeWindowFunctionN :: forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
fun WindowArg grp xs lat with db params from
xs = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> WindowFunction grp lat with db params from ty
UnsafeWindowFunction forall a b. (a -> b) -> a -> b
$ ByteString
fun forall a. Semigroup a => a -> a -> a
<> forall sql. RenderSQL sql => sql -> ByteString
renderSQL WindowArg grp xs lat with db params from
xs
rank :: WinFun0 ('NotNull 'PGint8)
rank :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType).
WindowFunction grp lat with db params from ('NotNull 'PGint8)
rank = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> WindowFunction grp lat with db params from ty
UnsafeWindowFunction ByteString
"rank()"
rowNumber :: WinFun0 ('NotNull 'PGint8)
rowNumber :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType).
WindowFunction grp lat with db params from ('NotNull 'PGint8)
rowNumber = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> WindowFunction grp lat with db params from ty
UnsafeWindowFunction ByteString
"row_number()"
denseRank :: WinFun0 ('NotNull 'PGint8)
denseRank :: forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType).
WindowFunction grp lat with db params from ('NotNull 'PGint8)
denseRank = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> WindowFunction grp lat with db params from ty
UnsafeWindowFunction ByteString
"dense_rank()"
percentRank :: WinFun0 ('NotNull 'PGfloat8)
percentRank :: WinFun0 ('NotNull 'PGfloat8)
percentRank = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> WindowFunction grp lat with db params from ty
UnsafeWindowFunction ByteString
"percent_rank()"
cumeDist :: WinFun0 ('NotNull 'PGfloat8)
cumeDist :: WinFun0 ('NotNull 'PGfloat8)
cumeDist = forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
(db :: SchemasType) (params :: [NullType]) (from :: FromType)
(ty :: NullType).
ByteString -> WindowFunction grp lat with db params from ty
UnsafeWindowFunction ByteString
"cume_dist()"
ntile :: 'NotNull 'PGint4 -#-> 'NotNull 'PGint4
ntile :: 'NotNull 'PGint4 -#-> 'NotNull 'PGint4
ntile = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"ntile"
lag :: '[ty, 'NotNull 'PGint4, ty] --#-> ty
lag :: forall (ty :: NullType). '[ty, 'NotNull 'PGint4, ty] --#-> ty
lag = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"lag"
lead :: '[ty, 'NotNull 'PGint4, ty] --#-> ty
lead :: forall (ty :: NullType). '[ty, 'NotNull 'PGint4, ty] --#-> ty
lead = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"lead"
firstValue :: ty -#-> ty
firstValue :: forall (ty :: NullType). ty -#-> ty
firstValue = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"first_value"
lastValue :: ty -#-> ty
lastValue :: forall (ty :: NullType). ty -#-> ty
lastValue = forall (x :: NullType) (y :: NullType). ByteString -> x -#-> y
unsafeWindowFunction1 ByteString
"last_value"
nthValue :: '[null ty, 'NotNull 'PGint4] --#-> 'Null ty
nthValue :: forall (null :: PGType -> NullType) (ty :: PGType).
'[null ty, 'NotNull 'PGint4] --#-> 'Null ty
nthValue = forall (xs :: [NullType]) (y :: NullType).
SListI xs =>
ByteString -> xs --#-> y
unsafeWindowFunctionN ByteString
"nth_value"