@@ -1,14 +1,13 @@
2−Module: Squeal.PostgreSQL.Query
2+Module: Squeal.PostgreSQL.Expression
3Description: Squeal expressions
3Description: Squeal expressions
4−Copyright: (c) Eitan Chatav, 2017
4+Copyright: (c) Eitan Chatav, 2019
5Maintainer: eitan@morphism.tech
5Maintainer: eitan@morphism.tech
6Stability: experimental
6Stability: experimental
8Squeal expressions are the atoms used to build statements.
8Squeal expressions are the atoms used to build statements.
11−{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
12{-# LANGUAGE
11{-# LANGUAGE
13 AllowAmbiguousTypes
12 AllowAmbiguousTypes
14 , ConstraintKinds
13 , ConstraintKinds
@@ -34,291 +33,214 @@ Squeal expressions are the atoms used to build statements.
34module Squeal.PostgreSQL.Expression
33module Squeal.PostgreSQL.Expression
35 ( -- * Expression
34 ( -- * Expression
36 Expression (..)
35 Expression (..)
37− , HasParameter (parameter)
36+ , Expr
38− , param
37+ , type (-->)
39− -- ** Null
38+ , FunctionDB
57 , unsafeFunction
39 , unsafeFunction
58− , unsafeVariadicFunction
40+ , function
59− , atan2_
41+ , unsafeLeftOp
61− , quot_
43+ , unsafeRightOp
63− , trunc
45+ , Operator
64− , round_
46+ , OperatorDB
65− , ceiling_
47+ , unsafeBinaryOp
66− , greatest
48+ , binaryOp
67− , least
49+ , FunctionVar
68− -- ** Conditions
50+ , unsafeFunctionVar
69− , true
51+ , type (--->)
70− , false
52+ , FunctionNDB
71− , not_
53+ , unsafeFunctionN
72− , (.&&)
54+ , functionN
73− , (.||)
55+ , PGSubset (..)
74− , Condition
56+ , PGIntersect (..)
96− -- *** Json and Jsonb operators
101− -- *** Jsonb operators
128− , jsonExtractPathAsText
129− , jsonbExtractPathAsText
141− -- * Window Functions
142− , WindowDefinition (..)
144− , WindowFunction (..)
157− , SortExpression (..)
160− , TypeExpression (..)
185− , timestampWithTimeZone
196 -- * Re-export
57 -- * Re-export
203import Control.Category
63import Control.Category
204import Control.DeepSeq
64import Control.DeepSeq
205import Data.ByteString (ByteString)
65import Data.ByteString (ByteString)
206−import Data.ByteString.Lazy (toStrict)
207import Data.Function ((&))
66import Data.Function ((&))
208import Data.Semigroup hiding (All)
67import Data.Semigroup hiding (All)
209−import qualified Data.Aeson as JSON
211import Data.String
68import Data.String
213−import Generics.SOP hiding (from)
69+import Generics.SOP hiding (All, from)
214−import GHC.Generics ((:*:) (..))
215import GHC.OverloadedLabels
70import GHC.OverloadedLabels
216import GHC.TypeLits
71import GHC.TypeLits
217import Prelude hiding (id, (.))
73import Prelude hiding (id, (.))
219−import qualified Data.ByteString as ByteString
220import qualified GHC.Generics as GHC
75import qualified GHC.Generics as GHC
77+import Squeal.PostgreSQL.Alias
78+import Squeal.PostgreSQL.List
222import Squeal.PostgreSQL.Render
79import Squeal.PostgreSQL.Render
223import Squeal.PostgreSQL.Schema
80import Squeal.PostgreSQL.Schema
83+-- >>> import Squeal.PostgreSQL
225{-----------------------------------------
85{-----------------------------------------
226column expressions
86column expressions
227-----------------------------------------}
87-----------------------------------------}
229{- | `Expression`s are used in a variety of contexts,
89{- | `Expression`s are used in a variety of contexts,
90+such as in the target `Squeal.PostgreSQL.Query.List` of the
230−such as in the target list of the `Squeal.PostgreSQL.Query.select` command,
91+`Squeal.PostgreSQL.Query.select` command,
231as new column values in `Squeal.PostgreSQL.Manipulation.insertRow` or
92as new column values in `Squeal.PostgreSQL.Manipulation.insertRow` or
232`Squeal.PostgreSQL.Manipulation.update`,
93`Squeal.PostgreSQL.Manipulation.update`,
233−or in search `Condition`s in a number of commands.
94+or in search `Squeal.PostgreSQL.Logic.Condition`s in a number of commands.
235The expression syntax allows the calculation of
96The expression syntax allows the calculation of
236values from primitive expression using arithmetic, logical,
97values from primitive expression using arithmetic, logical,
237and other operations.
98and other operations.
100+The type parameters of `Expression` are
102+* @outer :: @ `FromType`, the @from@ clauses of any outer queries in which
103+ the `Expression` is a correlated subquery expression;
104+* @commons :: @ `FromType`, the `Squeal.PostgreSQL.Query.CommonTableExpression`s
105+ that are in scope for the `Expression`;
106+* @grp :: @ `Grouping`, the `Grouping` of the @from@ clause which may limit
107+ which columns may be referenced by alias;
108+* @schemas :: @ `SchemasType`, the schemas of your database that are in
109+ scope for the `Expression`;
110+* @from :: @ `FromType`, the @from@ clause which the `Expression` may use
111+ to reference columns by alias;
112+* @ty :: @ `NullityType`, the type of the `Expression`.
239newtype Expression
114newtype Expression
240 (outer :: FromType)
115 (outer :: FromType)
242 (commons :: FromType)
116 (commons :: FromType)
243 (schemas :: SchemasType)
118 (schemas :: SchemasType)
244 (params :: [NullityType])
119 (params :: [NullityType])
245 (from :: FromType)
120 (from :: FromType)
246 (ty :: NullityType)
121 (ty :: NullityType)
247 = UnsafeExpression { renderExpression :: ByteString }
122 = UnsafeExpression { renderExpression :: ByteString }
248 deriving (GHC.Generic,Show,Eq,Ord,NFData)
123 deriving (GHC.Generic,Show,Eq,Ord,NFData)
250−instance RenderSQL (Expression outer grp commons schemas params from ty) where
124+instance RenderSQL (Expression outer commons grp schemas params from ty) where
251 renderSQL = renderExpression
125 renderSQL = renderExpression
253−{- | A `HasParameter` constraint is used to indicate a value that is
127+-- | An `Expr` is a closed `Expression`.
254−supplied externally to a SQL statement.
128+-- It is a F@RankNType@ but don't be scared.
255−`Squeal.PostgreSQL.PQ.manipulateParams`,
129+-- Think of it as an expression which sees no
256−`Squeal.PostgreSQL.PQ.queryParams` and
130+-- namespaces, so you can't use parameters
257−`Squeal.PostgreSQL.PQ.traversePrepared` support specifying data values
131+-- or alias references. It can be used as
258−separately from the SQL command string, in which case `param`s are used to
132+-- a simple piece of more complex `Expression`s.
259−refer to the out-of-line data values.
133+type Expr x
134+ = forall outer commons grp schemas params from
135+ . Expression outer commons grp schemas params from x
136+ -- ^ cannot reference aliases
138+-- | A @RankNType@ for binary operators.
139+type Operator x1 x2 y
140+ = forall outer commons grp schemas params from
141+ . Expression outer commons grp schemas params from x1
143+ -> Expression outer commons grp schemas params from x2
145+ -> Expression outer commons grp schemas params from y
148+type OperatorDB schemas x1 x2 y
149+ = forall outer commons grp params from
150+ . Expression outer commons grp schemas params from x1
152+ -> Expression outer commons grp schemas params from x2
154+ -> Expression outer commons grp schemas params from y
157+-- | A @RankNType@ for functions with a single argument.
158+-- These could be either function calls or unary operators.
159+-- This is a subtype of the usual Haskell function type `Prelude.->`,
160+-- indeed a subcategory as it is closed under the usual
161+-- `Prelude..` and `Prelude.id`.
163+ = forall outer commons grp schemas params from
164+ . Expression outer commons grp schemas params from x
166+ -> Expression outer commons grp schemas params from y
169+type FunctionDB schemas x y
170+ = forall outer commons grp params from
171+ . Expression outer commons grp schemas params from x
173+ -> Expression outer commons grp schemas params from y
176+{- | A @RankNType@ for functions with a fixed-length list of heterogeneous arguments.
177+Use the `*:` operator to end your argument lists, like so.
179+>>> printSQL (unsafeFunctionN "fun" (true :* false :* localTime *: true))
180+fun(TRUE, FALSE, LOCALTIME, TRUE)
261−class KnownNat n => HasParameter
182+type (--->) xs y
262− (n :: Nat)
183+ = forall outer commons grp schemas params from
263− (params :: [NullityType])
184+ . NP (Expression outer commons grp schemas params from) xs
264− (ty :: NullityType)
185+ -- ^ inputs
265− | n params -> ty where
186+ -> Expression outer commons grp schemas params from y
266− -- | `parameter` takes a `Nat` using type application and a `TypeExpression`.
187+ -- ^ output
268− -- >>> let expr = parameter @1 int4 :: Expression outer grp '[] schemas '[ 'Null 'PGint4] from ('Null 'PGint4)
189+type FunctionNDB schemas xs y
269− -- >>> printSQL expr
190+ = forall outer commons grp params from
270− -- ($1 :: int4)
191+ . NP (Expression outer commons grp schemas params from) xs
271− parameter
192+ -- ^ inputs
272− :: TypeExpression schemas ty
193+ -> Expression outer commons grp schemas params from y
196+{- | A @RankNType@ for functions with a variable-length list of
197+homogeneous arguments and at least 1 more argument.
199+type FunctionVar x0 x1 y
200+ = forall outer commons grp schemas params from
201+ . [Expression outer commons grp schemas params from x0]
203+ -> Expression outer commons grp schemas params from x1
204+ -- ^ must have at least 1 input
273− -> Expression outer grp commons schemas params from ty
205+ -> Expression outer commons grp schemas params from y
208+{- | >>> printSQL (unsafeFunctionVar "greatest" [true, null_] false)
209+greatest(TRUE, NULL, FALSE)
211+unsafeFunctionVar :: ByteString -> FunctionVar x0 x1 y
274− parameter ty = UnsafeExpression $ parenthesized $
212+unsafeFunctionVar fun xs x = UnsafeExpression $ fun <> parenthesized
275− "$" <> renderNat @n <+> "::"
213+ (commaSeparated (renderSQL <$> xs) <> ", " <> renderSQL x)
277−instance {-# OVERLAPPING #-} HasParameter 1 (ty1:tys) ty1
278−instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
279− => HasParameter n (ty' : params) ty
281−-- | `param` takes a `Nat` using type application and for basic types,
282−-- infers a `TypeExpression`.
284−-- >>> let expr = param @1 :: Expression outer grp commons schemas '[ 'Null 'PGint4] from ('Null 'PGint4)
288− :: forall n outer commons schemas params from grp ty
289− . (PGTyped schemas ty, HasParameter n params ty)
290− => Expression outer grp commons schemas params from ty -- ^ param
291−param = parameter @n (pgtype @schemas)
293instance (HasUnique tab (Join outer from) row, Has col row ty)
215instance (HasUnique tab (Join outer from) row, Has col row ty)
294− => IsLabel col (Expression outer 'Ungrouped commons schemas params from ty) where
216+ => IsLabel col (Expression outer commons 'Ungrouped schemas params from ty) where
295 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
217 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
296instance (HasUnique tab (Join outer from) row, Has col row ty, tys ~ '[ty])
218instance (HasUnique tab (Join outer from) row, Has col row ty, tys ~ '[ty])
297− => IsLabel col (NP (Expression outer 'Ungrouped commons schemas params from) tys) where
219+ => IsLabel col (NP (Expression outer commons 'Ungrouped schemas params from) tys) where
298 fromLabel = fromLabel @col :* Nil
220 fromLabel = fromLabel @col :* Nil
299instance (HasUnique tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
221instance (HasUnique tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
300 => IsLabel col
222 => IsLabel col
301− (Aliased (Expression outer 'Ungrouped commons schemas params from) column) where
223+ (Aliased (Expression outer commons 'Ungrouped schemas params from) column) where
302 fromLabel = fromLabel @col `As` Alias
224 fromLabel = fromLabel @col `As` Alias
303instance (HasUnique tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
225instance (HasUnique tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
304 => IsLabel col
226 => IsLabel col
305− (NP (Aliased (Expression outer 'Ungrouped commons schemas params from)) columns) where
227+ (NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns) where
306 fromLabel = fromLabel @col :* Nil
228 fromLabel = fromLabel @col :* Nil
308instance (Has tab (Join outer from) row, Has col row ty)
230instance (Has tab (Join outer from) row, Has col row ty)
309− => IsQualified tab col (Expression outer 'Ungrouped commons schemas params from ty) where
231+ => IsQualified tab col (Expression outer commons 'Ungrouped schemas params from ty) where
310 tab ! col = UnsafeExpression $
232 tab ! col = UnsafeExpression $
311 renderSQL tab <> "." <> renderSQL col
233 renderSQL tab <> "." <> renderSQL col
312instance (Has tab (Join outer from) row, Has col row ty, tys ~ '[ty])
234instance (Has tab (Join outer from) row, Has col row ty, tys ~ '[ty])
313− => IsQualified tab col (NP (Expression outer 'Ungrouped commons schemas params from) tys) where
235+ => IsQualified tab col (NP (Expression outer commons 'Ungrouped schemas params from) tys) where
314 tab ! col = tab ! col :* Nil
236 tab ! col = tab ! col :* Nil
315instance (Has tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
237instance (Has tab (Join outer from) row, Has col row ty, column ~ (col ::: ty))
316 => IsQualified tab col
238 => IsQualified tab col
317− (Aliased (Expression outer 'Ungrouped commons schemas params from) column) where
239+ (Aliased (Expression outer commons 'Ungrouped schemas params from) column) where
318 tab ! col = tab ! col `As` col
240 tab ! col = tab ! col `As` col
319instance (Has tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
241instance (Has tab (Join outer from) row, Has col row ty, columns ~ '[col ::: ty])
320 => IsQualified tab col
242 => IsQualified tab col
321− (NP (Aliased (Expression outer 'Ungrouped commons schemas params from)) columns) where
243+ (NP (Aliased (Expression outer commons 'Ungrouped schemas params from)) columns) where
322 tab ! col = tab ! col :* Nil
244 tab ! col = tab ! col :* Nil
@@ -326,7 +248,7 @@ instance
326 , Has col row ty
248 , Has col row ty
327 , GroupedBy tab col bys
249 , GroupedBy tab col bys
328 ) => IsLabel col
250 ) => IsLabel col
329− (Expression outer ('Grouped bys) commons schemas params from ty) where
251+ (Expression outer commons ('Grouped bys) schemas params from ty) where
330 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
252 fromLabel = UnsafeExpression $ renderSQL (Alias @col)
332 ( HasUnique tab (Join outer from) row
254 ( HasUnique tab (Join outer from) row
@@ -334,7 +256,7 @@ instance
334 , GroupedBy tab col bys
256 , GroupedBy tab col bys
335 , tys ~ '[ty]
257 , tys ~ '[ty]
336 ) => IsLabel col
258 ) => IsLabel col
337− (NP (Expression outer ('Grouped bys) commons schemas params from) tys) where
259+ (NP (Expression outer commons ('Grouped bys) schemas params from) tys) where
338 fromLabel = fromLabel @col :* Nil
260 fromLabel = fromLabel @col :* Nil
340 ( HasUnique tab (Join outer from) row
262 ( HasUnique tab (Join outer from) row
@@ -342,7 +264,7 @@ instance
342 , GroupedBy tab col bys
264 , GroupedBy tab col bys
343 , column ~ (col ::: ty)
265 , column ~ (col ::: ty)
344 ) => IsLabel col
266 ) => IsLabel col
345− (Aliased (Expression outer ('Grouped bys) commons schemas params from) column) where
267+ (Aliased (Expression outer commons ('Grouped bys) schemas params from) column) where
346 fromLabel = fromLabel @col `As` Alias
268 fromLabel = fromLabel @col `As` Alias
348 ( HasUnique tab (Join outer from) row
270 ( HasUnique tab (Join outer from) row
@@ -350,7 +272,7 @@ instance
350 , GroupedBy tab col bys
272 , GroupedBy tab col bys
351 , columns ~ '[col ::: ty]
273 , columns ~ '[col ::: ty]
352 ) => IsLabel col
274 ) => IsLabel col
353− (NP (Aliased (Expression outer ('Grouped bys) commons schemas params from)) columns) where
275+ (NP (Aliased (Expression outer commons ('Grouped bys) schemas params from)) columns) where
354 fromLabel = fromLabel @col :* Nil
276 fromLabel = fromLabel @col :* Nil
@@ -358,7 +280,7 @@ instance
358 , Has col row ty
280 , Has col row ty
359 , GroupedBy tab col bys
281 , GroupedBy tab col bys
360 ) => IsQualified tab col
282 ) => IsQualified tab col
361− (Expression outer ('Grouped bys) commons schemas params from ty) where
283+ (Expression outer commons ('Grouped bys) schemas params from ty) where
362 tab ! col = UnsafeExpression $
284 tab ! col = UnsafeExpression $
363 renderSQL tab <> "." <> renderSQL col
285 renderSQL tab <> "." <> renderSQL col
@@ -367,7 +289,7 @@ instance
367 , GroupedBy tab col bys
289 , GroupedBy tab col bys
368 , tys ~ '[ty]
290 , tys ~ '[ty]
369 ) => IsQualified tab col
291 ) => IsQualified tab col
370− (NP (Expression outer ('Grouped bys) commons schemas params from) tys) where
292+ (NP (Expression outer commons ('Grouped bys) schemas params from) tys) where
371 tab ! col = tab ! col :* Nil
293 tab ! col = tab ! col :* Nil
373 ( Has tab (Join outer from) row
295 ( Has tab (Join outer from) row
@@ -375,7 +297,7 @@ instance
375 , GroupedBy tab col bys
297 , GroupedBy tab col bys
376 , column ~ (col ::: ty)
298 , column ~ (col ::: ty)
377 ) => IsQualified tab col
299 ) => IsQualified tab col
378− (Aliased (Expression outer ('Grouped bys) commons schemas params from) column) where
300+ (Aliased (Expression outer commons ('Grouped bys) schemas params from) column) where
379 tab ! col = tab ! col `As` col
301 tab ! col = tab ! col `As` col
381 ( Has tab (Join outer from) row
303 ( Has tab (Join outer from) row
@@ -383,242 +305,78 @@ instance
383 , GroupedBy tab col bys
305 , GroupedBy tab col bys
384 , columns ~ '[col ::: ty]
306 , columns ~ '[col ::: ty]
385 ) => IsQualified tab col
307 ) => IsQualified tab col
386− (NP (Aliased (Expression outer ('Grouped bys) commons schemas params from)) columns) where
308+ (NP (Aliased (Expression outer commons ('Grouped bys) schemas params from)) columns) where
387 tab ! col = tab ! col :* Nil
309 tab ! col = tab ! col :* Nil
389−-- | analagous to `Nothing`
391−-- >>> printSQL null_
393−null_ :: Expression outer grp commons schemas params from ('Null ty)
394−null_ = UnsafeExpression "NULL"
396−-- | analagous to `Just`
398−-- >>> printSQL $ notNull true
401− :: Expression outer grp commons schemas params from ('NotNull ty)
402− -> Expression outer grp commons schemas params from ('Null ty)
403−notNull = UnsafeExpression . renderSQL
405−-- | return the leftmost value which is not NULL
407−-- >>> printSQL $ coalesce [null_, true] false
408−-- COALESCE(NULL, TRUE, FALSE)
410− :: [Expression outer grp commons schemas params from ('Null ty)]
411− -- ^ @NULL@s may be present
412− -> Expression outer grp commons schemas params from ('NotNull ty)
413− -- ^ @NULL@ is absent
414− -> Expression outer grp commons schemas params from ('NotNull ty)
415−coalesce nullxs notNullx = UnsafeExpression $
416− "COALESCE" <> parenthesized (commaSeparated
417− ((renderSQL <$> nullxs) <> [renderSQL notNullx]))
419−-- | analagous to `Data.Maybe.fromMaybe` using @COALESCE@
421−-- >>> printSQL $ fromNull true null_
422−-- COALESCE(NULL, TRUE)
424− :: Expression outer grp commons schemas params from ('NotNull ty)
425− -- ^ what to convert @NULL@ to
426− -> Expression outer grp commons schemas params from ('Null ty)
427− -> Expression outer grp commons schemas params from ('NotNull ty)
428−fromNull notNullx nullx = coalesce [nullx] notNullx
430−-- | >>> printSQL $ null_ & isNull
433− :: Expression outer grp commons schemas params from ('Null ty)
434− -- ^ possibly @NULL@
435− -> Condition outer grp commons schemas params from
436−isNull x = UnsafeExpression $ renderSQL x <+> "IS NULL"
438−-- | >>> printSQL $ null_ & isNotNull
441− :: Expression outer grp commons schemas params from ('Null ty)
442− -- ^ possibly @NULL@
443− -> Condition outer grp commons schemas params from
444−isNotNull x = UnsafeExpression $ renderSQL x <+> "IS NOT NULL"
446−-- | analagous to `maybe` using @IS NULL@
448−-- >>> printSQL $ matchNull true not_ null_
449−-- CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT NULL) END
451− :: Expression outer grp commons schemas params from (nullty)
452− -- ^ what to convert @NULL@ to
453− -> ( Expression outer grp commons schemas params from ('NotNull ty)
454− -> Expression outer grp commons schemas params from (nullty) )
455− -- ^ function to perform when @NULL@ is absent
456− -> Expression outer grp commons schemas params from ('Null ty)
457− -> Expression outer grp commons schemas params from (nullty)
458−matchNull y f x = ifThenElse (isNull x) y
459− (f (UnsafeExpression (renderSQL x)))
461−{-| right inverse to `fromNull`, if its arguments are equal then
462−`nullIf` gives @NULL@.
464−>>> :set -XTypeApplications -XDataKinds
465−>>> let expr = nullIf false (param @1) :: Expression outer grp commons schemas '[ 'NotNull 'PGbool] from ('Null 'PGbool)
467−NULL IF (FALSE, ($1 :: bool))
470− :: Expression outer grp commons schemas params from ('NotNull ty)
471− -- ^ @NULL@ is absent
472− -> Expression outer grp commons schemas params from ('NotNull ty)
473− -- ^ @NULL@ is absent
474− -> Expression outer grp commons schemas params from ('Null ty)
475−nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
476− (renderSQL x <> ", " <> renderSQL y)
478−-- | >>> printSQL $ array [null_, false, true]
479−-- ARRAY[NULL, FALSE, TRUE]
481− :: [Expression outer grp commons schemas params from ty]
483− -> Expression outer grp commons schemas params from (nullity ('PGvararray ty))
484−array xs = UnsafeExpression $
485− "ARRAY[" <> commaSeparated (renderSQL <$> xs) <> "]"
488− :: Expression outer grp commons schemas params from (nullity ('PGvararray ty))
489− -> Expression outer grp commons schemas params from (nullity 'PGint8)
490−cardinality = unsafeFunction "cardinality"
492−-- | >>> printSQL $ array [null_, false, true] & index 2
493−-- (ARRAY[NULL, FALSE, TRUE])[2]
495− :: Word64 -- ^ index
496− -> Expression outer grp commons schemas params from (nullity ('PGvararray ty)) -- ^ array
497− -> Expression outer grp commons schemas params from (NullifyType ty)
498−index n expr = UnsafeExpression $
499− parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
501instance (KnownSymbol label, label `In` labels) => IsPGlabel label
311instance (KnownSymbol label, label `In` labels) => IsPGlabel label
502− (Expression outer grp commons schemas params from (nullity ('PGenum labels))) where
312+ (Expression outer commons grp schemas params from (null ('PGenum labels))) where
503 label = UnsafeExpression $ renderSQL (PGlabel @label)
313 label = UnsafeExpression $ renderSQL (PGlabel @label)
505−-- | A row constructor is an expression that builds a row value
506−-- (also called a composite value) using values for its member fields.
509−-- type Complex = 'PGcomposite
510−-- '[ "real" ::: 'NotNull 'PGfloat8
511−-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
514−-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression outer grp commons schemas params from ('NotNull Complex)
519− => NP (Aliased (Expression outer grp commons schemas params from)) row
520− -- ^ zero or more expressions for the row field values
521− -> Expression outer grp commons schemas params from (nullity ('PGcomposite row))
522−row exprs = UnsafeExpression $ "ROW" <> parenthesized
523− (renderCommaSeparated (\ (expr `As` _) -> renderSQL expr) exprs)
526−-- type Complex = 'PGcomposite
527−-- '[ "real" ::: 'NotNull 'PGfloat8
528−-- , "imaginary" ::: 'NotNull 'PGfloat8 ]
529−-- type Schema = '["complex" ::: 'Typedef Complex]
532−-- >>> let i = row (0 `as` #real :* 1 `as` #imaginary) :: Expression outer grp '[] (Public Schema) from params ('NotNull Complex)
533−-- >>> printSQL $ i & field #complex #imaginary
534−-- (ROW(0, 1)::"complex")."imaginary"
536− :: ( Has sch schemas schema
537− , Has tydef schema ('Typedef ('PGcomposite row))
539− => QualifiedAlias sch tydef -- ^ row type
540− -> Alias field -- ^ field name
541− -> Expression outer grp commons schemas params from ('NotNull ('PGcomposite row))
542− -> Expression outer grp commons schemas params from ty
543−field td fld expr = UnsafeExpression $
544− parenthesized (renderSQL expr <> "::" <> renderSQL td)
545− <> "." <> renderSQL fld
548− (Expression outer grp commons schemas params from (nullity ('PGvararray ty))) where
549− (<>) = unsafeBinaryOp "||"
552− (Expression outer grp commons schemas params from (nullity ('PGvararray ty))) where
556−-- | >>> let expr = greatest currentTimestamp [param @1] :: Expression outer grp commons schemas '[ 'NotNull 'PGtimestamptz] from ('NotNull 'PGtimestamptz)
558−-- GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))
560− :: Expression outer grp commons schemas params from (nullty)
561− -- ^ needs at least 1 argument
562− -> [Expression outer grp commons schemas params from (nullty)]
564− -> Expression outer grp commons schemas params from (nullty)
565−greatest x xs = UnsafeExpression $ "GREATEST("
566− <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
568−-- | >>> printSQL $ least currentTimestamp [null_]
569−-- LEAST(CURRENT_TIMESTAMP, NULL)
571− :: Expression outer grp commons schemas params from (nullty)
572− -- ^ needs at least 1 argument
573− -> [Expression outer grp commons schemas params from (nullty)]
575− -> Expression outer grp commons schemas params from (nullty)
576−least x xs = UnsafeExpression $ "LEAST("
577− <> commaSeparated (renderSQL <$> (x:xs)) <> ")"
579-- | >>> printSQL $ unsafeBinaryOp "OR" true false
315-- | >>> printSQL $ unsafeBinaryOp "OR" true false
580-- (TRUE OR FALSE)
316-- (TRUE OR FALSE)
581−unsafeBinaryOp
317+unsafeBinaryOp :: ByteString -> Operator ty0 ty1 ty2
584− -> Expression outer grp commons schemas params from (ty0)
585− -> Expression outer grp commons schemas params from (ty1)
586− -> Expression outer grp commons schemas params from (ty2)
587unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
318unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
588 renderSQL x <+> op <+> renderSQL y
319 renderSQL x <+> op <+> renderSQL y
322+ :: forall op sch schemas schema x y z.
323+ ( Has sch schemas schema
324+ , Has op schema ('Operator ('BinaryOp x y z)) )
325+ => OperatorDB schemas x y z
326+binaryOp = unsafeBinaryOp $ renderSymbol @op
590−-- | >>> printSQL $ unsafeUnaryOp "NOT" true
328+-- | >>> printSQL $ unsafeLeftOp "NOT" true
591-- (NOT TRUE)
329-- (NOT TRUE)
593− :: ByteString
330+unsafeLeftOp :: ByteString -> x --> y
594− -- ^ operator
331+unsafeLeftOp op x = UnsafeExpression $ parenthesized $ op <+> renderSQL x
595− -> Expression outer grp commons schemas params from (ty0)
332+
596− -> Expression outer grp commons schemas params from (ty1)
333+leftOp
334+ :: forall op sch schemas schema x y.
335+ ( Has sch schemas schema
336+ , Has op schema ('Operator ('LeftOp x y)) )
337+ => FunctionDB schemas x y
338+leftOp = unsafeLeftOp $ renderSymbol @op
340+-- | >>> printSQL $ true & unsafeRightOp "IS NOT TRUE"
341+-- (TRUE IS NOT TRUE)
342+unsafeRightOp :: ByteString -> x --> y
597−unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
343+unsafeRightOp op x = UnsafeExpression $ parenthesized $ renderSQL x <+> op
598− op <+> renderSQL x
344+
346+ :: forall op sch schemas schema x y.
347+ ( Has sch schemas schema
348+ , Has op schema ('Operator ('RightOp x y)) )
349+ => FunctionDB schemas x y
350+rightOp = unsafeRightOp $ renderSymbol @op
600-- | >>> printSQL $ unsafeFunction "f" true
352-- | >>> printSQL $ unsafeFunction "f" true
601-- f(TRUE)
353-- f(TRUE)
602−unsafeFunction
354+unsafeFunction :: ByteString -> x --> y
605− -> Expression outer grp commons schemas params from (xty)
606− -> Expression outer grp commons schemas params from (yty)
607unsafeFunction fun x = UnsafeExpression $
355unsafeFunction fun x = UnsafeExpression $
608 fun <> parenthesized (renderSQL x)
356 fun <> parenthesized (renderSQL x)
610−-- | Helper for defining variadic functions.
358+function
611−unsafeVariadicFunction
359+ :: (Has sch schemas schema, Has fun schema ('Function ('[x] :=> 'Returns y)))
612− :: SListI elems
360+ => QualifiedAlias sch fun
361+ -> FunctionDB schemas x y
362+function = unsafeFunction . renderSQL
364+-- | >>> printSQL $ unsafeFunctionN "f" (currentTime :* localTimestamp :* false *: literal 'a')
365+-- f(CURRENT_TIME, LOCALTIMESTAMP, FALSE, E'a')
613− => ByteString
366+unsafeFunctionN :: SListI xs => ByteString -> xs ---> y
615− -> NP (Expression outer grp commons schemas params from) elems
616− -> Expression outer grp commons schemas params from ret
617−unsafeVariadicFunction fun x = UnsafeExpression $
367+unsafeFunctionN fun xs = UnsafeExpression $
618− fun <> parenthesized (commaSeparated (hcollapse (hmap (K . renderSQL) x)))
368+ fun <> parenthesized (renderCommaSeparated renderSQL xs)
371+ :: ( Has sch schemas schema
372+ , Has fun schema ('Function (xs :=> 'Returns y))
374+ => QualifiedAlias sch fun
375+ -> FunctionNDB schemas xs y
376+functionN = unsafeFunctionN . renderSQL
620instance ty `In` PGNum
378instance ty `In` PGNum
621− => Num (Expression outer grp commons schemas params from (nullity ty)) where
379+ => Num (Expression outer commons grp schemas params from (null ty)) where
622 (+) = unsafeBinaryOp "+"
380 (+) = unsafeBinaryOp "+"
623 (-) = unsafeBinaryOp "-"
381 (-) = unsafeBinaryOp "-"
624 (*) = unsafeBinaryOp "*"
382 (*) = unsafeBinaryOp "*"
@@ -630,12 +388,17 @@ instance ty `In` PGNum
632instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
390instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
633− (Expression outer grp commons schemas params from (nullity ty)) where
391+ (Expression outer commons grp schemas params from (null ty)) where
634 (/) = unsafeBinaryOp "/"
392 (/) = unsafeBinaryOp "/"
635− fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
393+ fromRational
397+ . showFFloat Nothing
637instance (ty `In` PGNum, ty `In` PGFloating) => Floating
400instance (ty `In` PGNum, ty `In` PGFloating) => Floating
638− (Expression outer grp commons schemas params from (nullity ty)) where
401+ (Expression outer commons grp schemas params from (null ty)) where
639 pi = UnsafeExpression "pi()"
402 pi = UnsafeExpression "pi()"
640 exp = unsafeFunction "exp"
403 exp = unsafeFunction "exp"
641 log = unsafeFunction "ln"
404 log = unsafeFunction "ln"
@@ -656,1564 +419,54 @@ instance (ty `In` PGNum, ty `In` PGFloating) => Floating
656 acosh x = log (x + sqrt (x*x - 1))
419 acosh x = log (x + sqrt (x*x - 1))
657 atanh x = log ((1 + x) / (1 - x)) / 2
420 atanh x = log ((1 + x) / (1 - x)) / 2
659−-- | >>> :{
422+-- | Contained by operators
660−-- let
423+class PGSubset ty where
661−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
424+ (@>) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
662−-- expression = atan2_ pi 2
425+ (@>) = unsafeBinaryOp "@>"
663−-- in printSQL expression
426+ (<@) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
664−-- :}
427+ (<@) = unsafeBinaryOp "<@"
665−-- atan2(pi(), 2)
428+instance PGSubset 'PGjsonb
666−atan2_
429+instance PGSubset 'PGtsquery
667− :: float `In` PGFloating
430+instance PGSubset ('PGvararray ty)
668− => Expression outer grp commons schemas params from (nullity float)
431+instance PGSubset ('PGrange ty)
670− -> Expression outer grp commons schemas params from (nullity float)
433+class PGIntersect ty where
671− -- ^ denominator
434+ (@&&) :: Operator (null0 ty) (null1 ty) ('Null 'PGbool)
672− -> Expression outer grp commons schemas params from (nullity float)
435+ (@&&) = unsafeBinaryOp "&&"
673−atan2_ y x = UnsafeExpression $
436+instance PGIntersect ('PGvararray ty)
674− "atan2(" <> renderSQL y <> ", " <> renderSQL x <> ")"
437+instance PGIntersect ('PGrange ty)
676−-- When a `cast` is applied to an `Expression` of a known type, it
677−-- represents a run-time type conversion. The cast will succeed only if a
678−-- suitable type conversion operation has been defined.
680−-- | >>> printSQL $ true & cast int4
683− :: TypeExpression schemas ty1
684− -- ^ type to cast as
685− -> Expression outer grp commons schemas params from ty0
686− -- ^ value to convert
687− -> Expression outer grp commons schemas params from ty1
688−cast ty x = UnsafeExpression $ parenthesized $
689− renderSQL x <+> "::" <+> renderSQL ty
691−-- | integer division, truncates the result
695−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
696−-- expression = 5 `quot_` 2
697−-- in printSQL expression
701− :: int `In` PGIntegral
702− => Expression outer grp commons schemas params from (nullity int)
704− -> Expression outer grp commons schemas params from (nullity int)
706− -> Expression outer grp commons schemas params from (nullity int)
707−quot_ = unsafeBinaryOp "/"
709−-- | remainder upon integer division
713−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
714−-- expression = 5 `rem_` 2
715−-- in printSQL expression
719− :: int `In` PGIntegral
720− => Expression outer grp commons schemas params from (nullity int)
722− -> Expression outer grp commons schemas params from (nullity int)
724− -> Expression outer grp commons schemas params from (nullity int)
725−rem_ = unsafeBinaryOp "%"
729−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
730−-- expression = trunc pi
731−-- in printSQL expression
735− :: frac `In` PGFloating
736− => Expression outer grp commons schemas params from (nullity frac)
737− -- ^ fractional number
738− -> Expression outer grp commons schemas params from (nullity frac)
739−trunc = unsafeFunction "trunc"
743−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
744−-- expression = round_ pi
745−-- in printSQL expression
749− :: frac `In` PGFloating
750− => Expression outer grp commons schemas params from (nullity frac)
751− -- ^ fractional number
752− -> Expression outer grp commons schemas params from (nullity frac)
753−round_ = unsafeFunction "round"
757−-- expression :: Expression outer grp commons schemas params from (nullity 'PGfloat4)
758−-- expression = ceiling_ pi
759−-- in printSQL expression
763− :: frac `In` PGFloating
764− => Expression outer grp commons schemas params from (nullity frac)
765− -- ^ fractional number
766− -> Expression outer grp commons schemas params from (nullity frac)
767−ceiling_ = unsafeFunction "ceiling"
769−-- | A `Condition` is an `Expression`, which can evaluate
770−-- to `true`, `false` or `null_`. This is because SQL uses
771−-- a three valued logic.
772−type Condition outer grp commons schemas params from =
773− Expression outer grp commons schemas params from ('Null 'PGbool)
775−-- | >>> printSQL true
777−true :: Expression outer grp commons schemas params from (nullity 'PGbool)
778−true = UnsafeExpression "TRUE"
780−-- | >>> printSQL false
782−false :: Expression outer grp commons schemas params from (nullity 'PGbool)
783−false = UnsafeExpression "FALSE"
785−-- | >>> printSQL $ not_ true
788− :: Expression outer grp commons schemas params from (nullity 'PGbool)
789− -> Expression outer grp commons schemas params from (nullity 'PGbool)
790−not_ = unsafeUnaryOp "NOT"
792−-- | >>> printSQL $ true .&& false
795− :: Expression outer grp commons schemas params from (nullity 'PGbool)
796− -> Expression outer grp commons schemas params from (nullity 'PGbool)
797− -> Expression outer grp commons schemas params from (nullity 'PGbool)
799−(.&&) = unsafeBinaryOp "AND"
801−-- | >>> printSQL $ true .|| false
804− :: Expression outer grp commons schemas params from (nullity 'PGbool)
805− -> Expression outer grp commons schemas params from (nullity 'PGbool)
806− -> Expression outer grp commons schemas params from (nullity 'PGbool)
808−(.||) = unsafeBinaryOp "OR"
812−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
813−-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
814−-- in printSQL expression
816−-- CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END
818− :: [ ( Condition outer grp commons schemas params from
819− , Expression outer grp commons schemas params from ty
821− -- ^ whens and thens
822− -> Expression outer grp commons schemas params from ty
824− -> Expression outer grp commons schemas params from ty
825−caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
829− [ " WHEN ", renderSQL when_
830− , " THEN ", renderSQL then_
832− | (when_,then_) <- whenThens
834− , " ELSE ", renderSQL else_
840−-- expression :: Expression outer grp commons schemas params from (nullity 'PGint2)
841−-- expression = ifThenElse true 1 0
842−-- in printSQL expression
844−-- CASE WHEN TRUE THEN 1 ELSE 0 END
846− :: Condition outer grp commons schemas params from
847− -> Expression outer grp commons schemas params from ty -- ^ then
848− -> Expression outer grp commons schemas params from ty -- ^ else
849− -> Expression outer grp commons schemas params from ty
850−ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
852−-- | Comparison operations like `.==`, `./=`, `.>`, `.>=`, `.<` and `.<=`
853−-- will produce @NULL@s if one of their arguments is @NULL@.
855−-- >>> printSQL $ true .== null_
858− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
859− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
860− -> Condition outer grp commons schemas params from
861−(.==) = unsafeBinaryOp "="
864−-- | >>> printSQL $ true ./= null_
867− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
868− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
869− -> Condition outer grp commons schemas params from
870−(./=) = unsafeBinaryOp "<>"
873−-- | >>> printSQL $ true .>= null_
876− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
877− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
878− -> Condition outer grp commons schemas params from
879−(.>=) = unsafeBinaryOp ">="
882−-- | >>> printSQL $ true .< null_
885− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
886− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
887− -> Condition outer grp commons schemas params from
888−(.<) = unsafeBinaryOp "<"
891−-- | >>> printSQL $ true .<= null_
894− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
895− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
896− -> Condition outer grp commons schemas params from
897−(.<=) = unsafeBinaryOp "<="
900−-- | >>> printSQL $ true .> null_
903− :: Expression outer grp commons schemas params from (nullity0 ty) -- ^ lhs
904− -> Expression outer grp commons schemas params from (nullity1 ty) -- ^ rhs
905− -> Condition outer grp commons schemas params from
906−(.>) = unsafeBinaryOp ">"
909−-- | >>> printSQL currentDate
912− :: Expression outer grp commons schemas params from (nullity 'PGdate)
913−currentDate = UnsafeExpression "CURRENT_DATE"
915−-- | >>> printSQL currentTime
918− :: Expression outer grp commons schemas params from (nullity 'PGtimetz)
919−currentTime = UnsafeExpression "CURRENT_TIME"
921−-- | >>> printSQL currentTimestamp
924− :: Expression outer grp commons schemas params from (nullity 'PGtimestamptz)
925−currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
927−-- | >>> printSQL localTime
930− :: Expression outer grp commons schemas params from (nullity 'PGtime)
931−localTime = UnsafeExpression "LOCALTIME"
933−-- | >>> printSQL localTimestamp
936− :: Expression outer grp commons schemas params from (nullity 'PGtimestamp)
937−localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
939−{-----------------------------------------
941−-----------------------------------------}
943instance IsString
439instance IsString
944− (Expression outer grp commons schemas params from (nullity 'PGtext)) where
440+ (Expression outer commons grp schemas params from (null 'PGtext)) where
945 fromString str = UnsafeExpression $
441 fromString str = UnsafeExpression $
946 "E\'" <> fromString (escape =<< str) <> "\'"
442 "E\'" <> fromString (escape =<< str) <> "\'"
947− where
443+instance IsString
948− escape = \case
444+ (Expression outer commons grp schemas params from (null 'PGtsvector)) where
949− '\NUL' -> "\\0"
445+ fromString str = UnsafeExpression . parenthesized . (<> " :: tsvector") $
950− '\'' -> "''"
446+ "E\'" <> fromString (escape =<< str) <> "\'"
951− '"' -> "\\\""
447+instance IsString
952− '\b' -> "\\b"
448+ (Expression outer commons grp schemas params from (null 'PGtsquery)) where
953− '\n' -> "\\n"
449+ fromString str = UnsafeExpression . parenthesized . (<> " :: tsquery") $
954− '\r' -> "\\r"
450+ "E\'" <> fromString (escape =<< str) <> "\'"
959instance Semigroup
452instance Semigroup
453+ (Expression outer commons grp schemas params from (null ('PGvararray ty))) where
454+ (<>) = unsafeBinaryOp "||"
456+ (Expression outer commons grp schemas params from (null 'PGjsonb)) where
457+ (<>) = unsafeBinaryOp "||"
960− (Expression outer grp commons schemas params from (nullity 'PGtext)) where
459+ (Expression outer commons grp schemas params from (null 'PGtext)) where
460+ (<>) = unsafeBinaryOp "||"
462+ (Expression outer commons grp schemas params from (null 'PGtsvector)) where
961 (<>) = unsafeBinaryOp "||"
463 (<>) = unsafeBinaryOp "||"
963instance Monoid
465instance Monoid
964− (Expression outer grp commons schemas params from (nullity 'PGtext)) where
466+ (Expression outer commons grp schemas params from (null 'PGtext)) where
467+ mempty = fromString ""
470+ (Expression outer commons grp schemas params from (null 'PGtsvector)) where
965 mempty = fromString ""
471 mempty = fromString ""
966 mappend = (<>)
472 mappend = (<>)
968−-- | >>> printSQL $ lower "ARRRGGG"
971− :: Expression outer grp commons schemas params from (nullity 'PGtext)
972− -- ^ string to lower case
973− -> Expression outer grp commons schemas params from (nullity 'PGtext)
974−lower = unsafeFunction "lower"
976−-- | >>> printSQL $ upper "eeee"
979− :: Expression outer grp commons schemas params from (nullity 'PGtext)
980− -- ^ string to upper case
981− -> Expression outer grp commons schemas params from (nullity 'PGtext)
982−upper = unsafeFunction "upper"
984−-- | >>> printSQL $ charLength "four"
985−-- char_length(E'four')
987− :: Expression outer grp commons schemas params from (nullity 'PGtext)
988− -- ^ string to measure
989− -> Expression outer grp commons schemas params from (nullity 'PGint4)
990−charLength = unsafeFunction "char_length"
992−-- | The `like` expression returns true if the @string@ matches
993−-- the supplied @pattern@. If @pattern@ does not contain percent signs
994−-- or underscores, then the pattern only represents the string itself;
995−-- in that case `like` acts like the equals operator. An underscore (_)
996−-- in pattern stands for (matches) any single character; a percent sign (%)
997−-- matches any sequence of zero or more characters.
999−-- >>> printSQL $ "abc" `like` "a%"
1000−-- (E'abc' LIKE E'a%')
1002− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1004− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1006− -> Expression outer grp commons schemas params from (nullity 'PGbool)
1007−like = unsafeBinaryOp "LIKE"
1009−-- | The key word ILIKE can be used instead of LIKE to make the
1010−-- match case-insensitive according to the active locale.
1012−-- >>> printSQL $ "abc" `ilike` "a%"
1013−-- (E'abc' ILIKE E'a%')
1015− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1017− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1019− -> Expression outer grp commons schemas params from (nullity 'PGbool)
1020−ilike = unsafeBinaryOp "ILIKE"
1022−{-----------------------------------------
1023− -- json and jsonb support
1025−See https://www.postgresql.org/docs/10/static/functions-json.html -- most
1026−comments lifted directly from this page.
1028−Table 9.44: json and jsonb operators
1029−-----------------------------------------}
1031−-- | Get JSON value (object field or array element) at a key.
1033− :: (json `In` PGJsonType, key `In` PGJsonKey)
1034− => Expression outer grp commons schemas params from (nullity json)
1035− -> Expression outer grp commons schemas params from (nullity key)
1036− -> Expression outer grp commons schemas params from ('Null json)
1038−(.->) = unsafeBinaryOp "->"
1040−-- | Get JSON value (object field or array element) at a key, as text.
1042− :: (json `In` PGJsonType, key `In` PGJsonKey)
1043− => Expression outer grp commons schemas params from (nullity json)
1044− -> Expression outer grp commons schemas params from (nullity key)
1045− -> Expression outer grp commons schemas params from ('Null 'PGtext)
1047−(.->>) = unsafeBinaryOp "->>"
1049−-- | Get JSON value at a specified path.
1051− :: (json `In` PGJsonType, PGTextArray "(.#>)" path)
1052− => Expression outer grp commons schemas params from (nullity json)
1053− -> Expression outer grp commons schemas params from (nullity path)
1054− -> Expression outer grp commons schemas params from ('Null json)
1056−(.#>) = unsafeBinaryOp "#>"
1058−-- | Get JSON value at a specified path as text.
1060− :: (json `In` PGJsonType, PGTextArray "(.#>>)" path)
1061− => Expression outer grp commons schemas params from (nullity json)
1062− -> Expression outer grp commons schemas params from (nullity path)
1063− -> Expression outer grp commons schemas params from ('Null 'PGtext)
1065−(.#>>) = unsafeBinaryOp "#>>"
1067−-- Additional jsonb operators
1069−-- | Does the left JSON value contain the right JSON path/value entries at the
1072− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1073− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1074− -> Condition outer grp commons schemas params from
1076−(.@>) = unsafeBinaryOp "@>"
1078−-- | Are the left JSON path/value entries contained at the top level within the
1079−-- right JSON value?
1081− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1082− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1083− -> Condition outer grp commons schemas params from
1085−(.<@) = unsafeBinaryOp "<@"
1087−-- | Does the string exist as a top-level key within the JSON value?
1089− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1090− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1091− -> Condition outer grp commons schemas params from
1093−(.?) = unsafeBinaryOp "?"
1095−-- | Do any of these array strings exist as top-level keys?
1097− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1098− -> Expression outer grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1099− -> Condition outer grp commons schemas params from
1101−(.?|) = unsafeBinaryOp "?|"
1103−-- | Do all of these array strings exist as top-level keys?
1105− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1106− -> Expression outer grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1107− -> Condition outer grp commons schemas params from
1109−(.?&) = unsafeBinaryOp "?&"
1111−-- | Concatenate two jsonb values into a new jsonb value.
1113− (Expression outer grp commons schemas params from (nullity 'PGjsonb)) where
1114− (<>) = unsafeBinaryOp "||"
1116−-- | Delete a key or keys from a JSON object, or remove an array element.
1118−-- If the right operand is..
1120−-- @ text @: Delete key/value pair or string element from left operand. Key/value pairs
1121−-- are matched based on their key value.
1123−-- @ text[] @: Delete multiple key/value pairs or string elements
1124−-- from left operand. Key/value pairs are matched based on their key value.
1126−-- @ integer @: Delete the array element with specified index (Negative integers
1127−-- count from the end). Throws an error if top level container is not an array.
1129− :: (key `In` '[ 'PGtext, 'PGvararray ('NotNull 'PGtext), 'PGint4, 'PGint2 ]) -- hlint error without parens here
1130− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1131− -> Expression outer grp commons schemas params from (nullity key)
1132− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1134−(.-.) = unsafeBinaryOp "-"
1136−-- | Delete the field or element with specified path (for JSON arrays, negative
1137−-- integers count from the end)
1139− :: PGTextArray "(#-.)" arrayty
1140− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1141− -> Expression outer grp commons schemas params from (nullity arrayty)
1142− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1144−(#-.) = unsafeBinaryOp "#-"
1146−{-----------------------------------------
1147−Table 9.45: JSON creation functions
1148−-----------------------------------------}
1150−-- | Literal binary JSON
1153− => x -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1154−jsonbLit = cast jsonb . UnsafeExpression
1155− . singleQuotedUtf8 . toStrict . JSON.encode
1160− => x -> Expression outer grp commons schemas params from (nullity 'PGjson)
1161−jsonLit = cast json . UnsafeExpression
1162− . singleQuotedUtf8 . toStrict . JSON.encode
1164−-- | Returns the value as json. Arrays and composites are converted
1165−-- (recursively) to arrays and objects; otherwise, if there is a cast from the
1166−-- type to json, the cast function will be used to perform the conversion;
1167−-- otherwise, a scalar value is produced. For any scalar type other than a
1168−-- number, a Boolean, or a null value, the text representation will be used, in
1169−-- such a fashion that it is a valid json value.
1171− :: Expression outer grp commons schemas params from (nullity ty)
1172− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1173−toJson = unsafeFunction "to_json"
1175−-- | Returns the value as jsonb. Arrays and composites are converted
1176−-- (recursively) to arrays and objects; otherwise, if there is a cast from the
1177−-- type to json, the cast function will be used to perform the conversion;
1178−-- otherwise, a scalar value is produced. For any scalar type other than a
1179−-- number, a Boolean, or a null value, the text representation will be used, in
1180−-- such a fashion that it is a valid jsonb value.
1182− :: Expression outer grp commons schemas params from (nullity ty)
1183− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1184−toJsonb = unsafeFunction "to_jsonb"
1186−-- | Returns the array as a JSON array. A PostgreSQL multidimensional array
1187−-- becomes a JSON array of arrays.
1189− :: PGArray "arrayToJson" arr
1190− => Expression outer grp commons schemas params from (nullity arr)
1191− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1192−arrayToJson = unsafeFunction "array_to_json"
1194−-- | Returns the row as a JSON object.
1196− :: Expression outer grp commons schemas params from (nullity ('PGcomposite ty))
1197− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1198−rowToJson = unsafeFunction "row_to_json"
1200−-- | Builds a possibly-heterogeneously-typed JSON array out of a variadic
1204− => NP (Expression outer grp commons schemas params from) elems
1205− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1206−jsonBuildArray = unsafeVariadicFunction "json_build_array"
1208−-- | Builds a possibly-heterogeneously-typed (binary) JSON array out of a
1209−-- variadic argument list.
1212− => NP (Expression outer grp commons schemas params from) elems
1213− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1214−jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
1218− => NP (Aliased (Expression outer grp commons schemas params from)) elems
1221− (`appEndo` []) . hcfoldMap (Proxy :: Proxy Top)
1222− (\(col `As` name) -> Endo $ \xs ->
1223− renderAliasString name : renderSQL col : xs)
1225− renderAliasString :: KnownSymbol alias => Alias alias -> ByteString
1226− renderAliasString = singleQuotedText . fromString . symbolVal
1228−-- | Builds a possibly-heterogeneously-typed JSON object out of a variadic
1229−-- argument list. The elements of the argument list must alternate between text
1233− => NP (Aliased (Expression outer grp commons schemas params from)) elems
1234− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1236− = unsafeFunction "json_build_object"
1239− . unsafeRowFunction
1241−-- | Builds a possibly-heterogeneously-typed (binary) JSON object out of a
1242−-- variadic argument list. The elements of the argument list must alternate
1243−-- between text and values.
1246− => NP (Aliased (Expression outer grp commons schemas params from)) elems
1247− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1249− = unsafeFunction "jsonb_build_object"
1252− . unsafeRowFunction
1254−-- | Builds a JSON object out of a text array. The array must have either
1255−-- exactly one dimension with an even number of members, in which case they are
1256−-- taken as alternating key/value pairs, or two dimensions such that each inner
1257−-- array has exactly two elements, which are taken as a key/value pair.
1259− :: PGArrayOf "jsonObject" arr ('NotNull 'PGtext)
1260− => Expression outer grp commons schemas params from (nullity arr)
1261− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1262−jsonObject = unsafeFunction "json_object"
1264−-- | Builds a binary JSON object out of a text array. The array must have either
1265−-- exactly one dimension with an even number of members, in which case they are
1266−-- taken as alternating key/value pairs, or two dimensions such that each inner
1267−-- array has exactly two elements, which are taken as a key/value pair.
1269− :: PGArrayOf "jsonbObject" arr ('NotNull 'PGtext)
1270− => Expression outer grp commons schemas params from (nullity arr)
1271− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1272−jsonbObject = unsafeFunction "jsonb_object"
1274−-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1275−-- keys and one for values, that are zipped pairwise to create a JSON object.
1277− :: ( PGArrayOf "jsonZipObject" keysArray ('NotNull 'PGtext)
1278− , PGArrayOf "jsonZipObject" valuesArray ('NotNull 'PGtext))
1279− => Expression outer grp commons schemas params from (nullity keysArray)
1280− -> Expression outer grp commons schemas params from (nullity valuesArray)
1281− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1282−jsonZipObject ks vs =
1283− unsafeVariadicFunction "json_object" (ks :* vs :* Nil)
1285−-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
1286−-- keys and one for values, that are zipped pairwise to create a binary JSON
1289− :: ( PGArrayOf "jsonbZipObject" keysArray ('NotNull 'PGtext)
1290− , PGArrayOf "jsonbZipObject" valuesArray ('NotNull 'PGtext))
1291− => Expression outer grp commons schemas params from (nullity keysArray)
1292− -> Expression outer grp commons schemas params from (nullity valuesArray)
1293− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1294−jsonbZipObject ks vs =
1295− unsafeVariadicFunction "jsonb_object" (ks :* vs :* Nil)
1297−{-----------------------------------------
1298−Table 9.46: JSON processing functions
1299−-----------------------------------------}
1301−-- | Returns the number of elements in the outermost JSON array.
1303− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1304− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1305−jsonArrayLength = unsafeFunction "json_array_length"
1307−-- | Returns the number of elements in the outermost binary JSON array.
1309− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1310− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1311−jsonbArrayLength = unsafeFunction "jsonb_array_length"
1313−-- | Returns JSON value pointed to by the given path (equivalent to #>
1317− => Expression outer grp commons schemas params from (nullity 'PGjson)
1318− -> NP (Expression outer grp commons schemas params from) elems
1319− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1320−jsonExtractPath x xs =
1321− unsafeVariadicFunction "json_extract_path" (x :* xs)
1323−-- | Returns JSON value pointed to by the given path (equivalent to #>
1327− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1328− -> NP (Expression outer grp commons schemas params from) elems
1329− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1330−jsonbExtractPath x xs =
1331− unsafeVariadicFunction "jsonb_extract_path" (x :* xs)
1333−-- | Returns JSON value pointed to by the given path (equivalent to #>
1334−-- operator), as text.
1335−jsonExtractPathAsText
1337− => Expression outer grp commons schemas params from (nullity 'PGjson)
1338− -> NP (Expression outer grp commons schemas params from) elems
1339− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1340−jsonExtractPathAsText x xs =
1341− unsafeVariadicFunction "json_extract_path_text" (x :* xs)
1343−-- | Returns JSON value pointed to by the given path (equivalent to #>
1344−-- operator), as text.
1345−jsonbExtractPathAsText
1347− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1348− -> NP (Expression outer grp commons schemas params from) elems
1349− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1350−jsonbExtractPathAsText x xs =
1351− unsafeVariadicFunction "jsonb_extract_path_text" (x :* xs)
1353−-- | Returns the type of the outermost JSON value as a text string. Possible
1354−-- types are object, array, string, number, boolean, and null.
1356− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1357− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1358−jsonTypeof = unsafeFunction "json_typeof"
1360−-- | Returns the type of the outermost binary JSON value as a text string.
1361−-- Possible types are object, array, string, number, boolean, and null.
1363− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1364− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1365−jsonbTypeof = unsafeFunction "jsonb_typeof"
1367−-- | Returns its argument with all object fields that have null values omitted.
1368−-- Other null values are untouched.
1370− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1371− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1372−jsonStripNulls = unsafeFunction "json_strip_nulls"
1374−-- | Returns its argument with all object fields that have null values omitted.
1375−-- Other null values are untouched.
1377− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1378− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1379−jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
1381−-- | @ jsonbSet target path new_value create_missing @
1383−-- Returns target with the section designated by path replaced by new_value,
1384−-- or with new_value added if create_missing is true ( default is true) and the
1385−-- item designated by path does not exist. As with the path orientated
1386−-- operators, negative integers that appear in path count from the end of JSON
1389− :: PGTextArray "jsonbSet" arr
1390− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1391− -> Expression outer grp commons schemas params from (nullity arr)
1392− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1393− -> Maybe (Expression outer grp commons schemas params from (nullity 'PGbool))
1394− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1395−jsonbSet tgt path val createMissing = case createMissing of
1396− Just m -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* m :* Nil)
1397− Nothing -> unsafeVariadicFunction "jsonb_set" (tgt :* path :* val :* Nil)
1399−-- | @ jsonbInsert target path new_value insert_after @
1401−-- Returns target with new_value inserted. If target section designated by
1402−-- path is in a JSONB array, new_value will be inserted before target or after
1403−-- if insert_after is true (default is false). If target section designated by
1404−-- path is in JSONB object, new_value will be inserted only if target does not
1405−-- exist. As with the path orientated operators, negative integers that appear
1406−-- in path count from the end of JSON arrays.
1408− :: PGTextArray "jsonbInsert" arr
1409− => Expression outer grp commons schemas params from (nullity 'PGjsonb)
1410− -> Expression outer grp commons schemas params from (nullity arr)
1411− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1412− -> Maybe (Expression outer grp commons schemas params from (nullity 'PGbool))
1413− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1414−jsonbInsert tgt path val insertAfter = case insertAfter of
1415− Just i -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* i :* Nil)
1416− Nothing -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* Nil)
1418−-- | Returns its argument as indented JSON text.
1420− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1421− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1422−jsonbPretty = unsafeFunction "jsonb_pretty"
1424−{-----------------------------------------
1426−-----------------------------------------}
1428−class Aggregate expr1 expr2 aggr
1429− | aggr -> expr1, aggr -> expr2 where
1431− -- | A special aggregation that does not require an input
1435− -- expression :: Expression '[] ('Grouped bys) commons schemas params from ('NotNull 'PGint8)
1436− -- expression = countStar
1437− -- in printSQL expression
1440− countStar :: aggr ('NotNull 'PGint8)
1444− -- expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1445− -- expression = count (All #col)
1446− -- in printSQL expression
1448− -- count(ALL "col")
1452− -> aggr ('NotNull 'PGint8)
1456− -- expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1457− -- expression = sum_ (Distinct #col)
1458− -- in printSQL expression
1460− -- sum(DISTINCT "col")
1463− => expr1 (nullity ty)
1464− -> aggr (nullity ty)
1466− -- | input values, including nulls, concatenated into an array
1469− -> aggr ('NotNull ('PGvararray ty))
1471− -- | aggregates values as a JSON array
1474− -> aggr ('NotNull 'PGjson)
1476− -- | aggregates values as a JSON array
1479− -> aggr ('NotNull 'PGjsonb)
1483− -- expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1484− -- expression = bitAnd (Distinct #col)
1485− -- in printSQL expression
1487− -- bit_and(DISTINCT "col")
1489− :: int `In` PGIntegral
1490− => expr1 (nullity int)
1491− -- ^ what to aggregate
1492− -> aggr (nullity int)
1496− -- expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1497− -- expression = bitOr (All #col)
1498− -- in printSQL expression
1500− -- bit_or(ALL "col")
1502− :: int `In` PGIntegral
1503− => expr1 (nullity int)
1504− -- ^ what to aggregate
1505− -> aggr (nullity int)
1509− -- winFun :: WindowFunction '[] 'Ungrouped commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1510− -- winFun = boolAnd #col
1511− -- in printSQL winFun
1515− :: expr1 (nullity 'PGbool)
1516− -- ^ what to aggregate
1517− -> aggr (nullity 'PGbool)
1521− -- expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1522− -- expression = boolOr (All #col)
1523− -- in printSQL expression
1525− -- bool_or(ALL "col")
1527− :: expr1(nullity 'PGbool)
1528− -- ^ what to aggregate
1529− -> aggr (nullity 'PGbool)
1531− -- | synonym for `boolAnd`
1535− -- expression :: Expression '[] ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1536− -- expression = every (Distinct #col)
1537− -- in printSQL expression
1539− -- every(DISTINCT "col")
1541− :: expr1 (nullity 'PGbool)
1542− -- ^ what to aggregate
1543− -> aggr (nullity 'PGbool)
1545− -- | maximum value of expression across all input values
1547− :: expr1 (nullity ty)
1548− -- ^ what to maximize
1549− -> aggr (nullity ty)
1551− -- | minimum value of expression across all input values
1553− :: expr1 (nullity ty)
1554− -- ^ what to minimize
1555− -> aggr (nullity ty)
1557− -- | average aggregation
1559− :: expr1 (nullity ty)
1560− -- ^ what to average
1561− -> aggr (nullity (PGAvg ty))
1563− {- | correlation coefficient
1566− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1567− expression = corr (All (#y :*: #x))
1568− in printSQL expression
1573− :: expr2 (nullity 'PGfloat8)
1574− -> aggr (nullity 'PGfloat8)
1576− {- | population covariance
1579− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1580− expression = covarPop (All (#y :*: #x))
1581− in printSQL expression
1583− covar_pop(ALL "y", "x")
1586− :: expr2 (nullity 'PGfloat8)
1587− -> aggr (nullity 'PGfloat8)
1589− {- | sample covariance
1592− winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1593− winFun = covarSamp (#y :*: #x)
1596− covar_samp("y", "x")
1599− :: expr2 (nullity 'PGfloat8)
1600− -> aggr (nullity 'PGfloat8)
1602− {- | average of the independent variable (sum(X)/N)
1605− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1606− expression = regrAvgX (All (#y :*: #x))
1607− in printSQL expression
1609− regr_avgx(ALL "y", "x")
1612− :: expr2 (nullity 'PGfloat8)
1613− -> aggr (nullity 'PGfloat8)
1615− {- | average of the dependent variable (sum(Y)/N)
1618− winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1619− winFun = regrAvgY (#y :*: #x)
1622− regr_avgy("y", "x")
1625− :: expr2 (nullity 'PGfloat8)
1626− -> aggr (nullity 'PGfloat8)
1628− {- | number of input rows in which both expressions are nonnull
1631− winFun :: WindowFunction '[] 'Ungrouped c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGint8)
1632− winFun = regrCount (#y :*: #x)
1635− regr_count("y", "x")
1638− :: expr2 (nullity 'PGfloat8)
1639− -> aggr (nullity 'PGint8)
1641− {- | y-intercept of the least-squares-fit linear equation determined by the (X, Y) pairs
1644− expression :: Expression '[] ('Grouped g) c s p '[t ::: '["x" ::: 'NotNull 'PGfloat8, "y" ::: 'NotNull 'PGfloat8]] ('NotNull 'PGfloat8)
1645− expression = regrIntercept (All (#y :*: #x))
1646− in printSQL expression
1648− regr_intercept(ALL "y", "x")
1651− :: expr2 (nullity 'PGfloat8)
1652− -> aggr (nullity 'PGfloat8)
1654− -- | @regr_r2(Y, X)@, square of the correlation coefficient
1656− :: expr2 (nullity 'PGfloat8)
1657− -> aggr (nullity 'PGfloat8)
1659− -- | @regr_slope(Y, X)@, slope of the least-squares-fit linear equation
1660− -- determined by the (X, Y) pairs
1662− :: expr2 (nullity 'PGfloat8)
1663− -> aggr (nullity 'PGfloat8)
1665− -- | @regr_sxx(Y, X)@, sum(X^2) - sum(X)^2/N
1666− -- (“sum of squares” of the independent variable)
1668− :: expr2 (nullity 'PGfloat8)
1669− -> aggr (nullity 'PGfloat8)
1671− -- | @regr_sxy(Y, X)@, sum(X*Y) - sum(X) * sum(Y)/N
1672− -- (“sum of products” of independent times dependent variable)
1674− :: expr2 (nullity 'PGfloat8)
1675− -> aggr (nullity 'PGfloat8)
1677− -- | @regr_syy(Y, X)@, sum(Y^2) - sum(Y)^2/N
1678− -- (“sum of squares” of the dependent variable)
1680− :: expr2 (nullity 'PGfloat8)
1681− -> aggr (nullity 'PGfloat8)
1683− -- | historical alias for `stddevSamp`
1685− :: expr1 (nullity ty)
1686− -> aggr (nullity (PGAvg ty))
1688− -- | population standard deviation of the input values
1690− :: expr1 (nullity ty)
1691− -> aggr (nullity (PGAvg ty))
1693− -- | sample standard deviation of the input values
1695− :: expr1 (nullity ty)
1696− -> aggr (nullity (PGAvg ty))
1698− -- | historical alias for `varSamp`
1700− :: expr1 (nullity ty)
1701− -> aggr (nullity (PGAvg ty))
1703− -- | population variance of the input values
1704− -- (square of the population standard deviation)
1706− :: expr1 (nullity ty)
1707− -> aggr (nullity (PGAvg ty))
1709− -- | sample variance of the input values
1710− -- (square of the sample standard deviation)
1712− :: expr1 (nullity ty)
1713− -> aggr (nullity (PGAvg ty))
1715−data Distinction expr ty
1717− | Distinct (expr ty)
1718− deriving (GHC.Generic,Show,Eq,Ord)
1719−instance NFData (Distinction (Expression outer grp commons schemas params from) ty)
1720−instance RenderSQL (Distinction (Expression outer grp commons schemas params from) ty) where
1722− All x -> "ALL" <+> renderSQL x
1723− Distinct x -> "DISTINCT" <+> renderSQL x
1726− ( Expression outer grp commons schemas params from
1728− Expression outer grp commons schemas params from) xty ) where
1731− "ALL" <+> renderSQL x <> ", " <> renderSQL x'
1732− Distinct (x :*: x') ->
1733− "DISTINCT" <+> renderSQL x <> ", " <> renderSQL x'
1736− (Distinction (Expression outer 'Ungrouped commons schemas params from))
1737− (Distinction (Expression outer 'Ungrouped commons schemas params from :*: Expression outer 'Ungrouped commons schemas params from))
1738− (Expression outer ('Grouped bys) commons schemas params from) where
1739− countStar = UnsafeExpression "count(*)"
1740− count = unsafeAggregate1 "count"
1741− sum_ = unsafeAggregate1 "sum"
1742− arrayAgg = unsafeAggregate1 "array_agg"
1743− jsonAgg = unsafeAggregate1 "json_agg"
1744− jsonbAgg = unsafeAggregate1 "jsonb_agg"
1745− bitAnd = unsafeAggregate1 "bit_and"
1746− bitOr = unsafeAggregate1 "bit_or"
1747− boolAnd = unsafeAggregate1 "bool_and"
1748− boolOr = unsafeAggregate1 "bool_or"
1749− every = unsafeAggregate1 "every"
1750− max_ = unsafeAggregate1 "max"
1751− min_ = unsafeAggregate1 "min"
1752− avg = unsafeAggregate1 "avg"
1753− corr = unsafeAggregate2 "corr"
1754− covarPop = unsafeAggregate2 "covar_pop"
1755− covarSamp = unsafeAggregate2 "covar_samp"
1756− regrAvgX = unsafeAggregate2 "regr_avgx"
1757− regrAvgY = unsafeAggregate2 "regr_avgy"
1758− regrCount = unsafeAggregate2 "regr_count"
1759− regrIntercept = unsafeAggregate2 "regr_intercept"
1760− regrR2 = unsafeAggregate2 "regr_r2"
1761− regrSlope = unsafeAggregate2 "regr_slope"
1762− regrSxx = unsafeAggregate2 "regr_sxx"
1763− regrSxy = unsafeAggregate2 "regr_sxy"
1764− regrSyy = unsafeAggregate2 "regr_syy"
1765− stddev = unsafeAggregate1 "stddev"
1766− stddevPop = unsafeAggregate1 "stddev_pop"
1767− stddevSamp = unsafeAggregate1 "stddev_samp"
1768− variance = unsafeAggregate1 "variance"
1769− varPop = unsafeAggregate1 "var_pop"
1770− varSamp = unsafeAggregate1 "var_samp"
1772− (Expression outer grp commons schemas params from)
1773− (Expression outer grp commons schemas params from :*: Expression outer grp commons schemas params from)
1774− (WindowFunction outer grp commons schemas params from) where
1775− countStar = UnsafeWindowFunction "count(*)"
1776− count = unsafeWindowFunction1 "count"
1777− sum_ = unsafeWindowFunction1 "sum"
1778− arrayAgg = unsafeWindowFunction1 "array_agg"
1779− jsonAgg = unsafeWindowFunction1 "json_agg"
1780− jsonbAgg = unsafeWindowFunction1 "jsonb_agg"
1781− bitAnd = unsafeWindowFunction1 "bit_and"
1782− bitOr = unsafeWindowFunction1 "bit_or"
1783− boolAnd = unsafeWindowFunction1 "bool_and"
1784− boolOr = unsafeWindowFunction1 "bool_or"
1785− every = unsafeWindowFunction1 "every"
1786− max_ = unsafeWindowFunction1 "max"
1787− min_ = unsafeWindowFunction1 "min"
1788− avg = unsafeWindowFunction1 "avg"
1789− corr = unsafeWindowFunction2 "corr"
1790− covarPop = unsafeWindowFunction2 "covar_pop"
1791− covarSamp = unsafeWindowFunction2 "covar_samp"
1792− regrAvgX = unsafeWindowFunction2 "regr_avgx"
1793− regrAvgY = unsafeWindowFunction2 "regr_avgy"
1794− regrCount = unsafeWindowFunction2 "regr_count"
1795− regrIntercept = unsafeWindowFunction2 "regr_intercept"
1796− regrR2 = unsafeWindowFunction2 "regr_r2"
1797− regrSlope = unsafeWindowFunction2 "regr_slope"
1798− regrSxx = unsafeWindowFunction2 "regr_sxx"
1799− regrSxy = unsafeWindowFunction2 "regr_sxy"
1800− regrSyy = unsafeWindowFunction2 "regr_syy"
1801− stddev = unsafeWindowFunction1 "stddev"
1802− stddevPop = unsafeWindowFunction1 "stddev_pop"
1803− stddevSamp = unsafeWindowFunction1 "stddev_samp"
1804− variance = unsafeWindowFunction1 "variance"
1805− varPop = unsafeWindowFunction1 "var_pop"
1806− varSamp = unsafeWindowFunction1 "var_samp"
1808−-- | escape hatch to define aggregate functions
1810− :: ByteString -- ^ aggregate function
1811− -> Distinction (Expression outer 'Ungrouped commons schemas params from) xty
1812− -> Expression outer ('Grouped bys) commons schemas params from yty
1813−unsafeAggregate1 fun x = UnsafeExpression $ mconcat
1814− [fun, "(", renderSQL x, ")"]
1816−-- | escape hatch to define aggregate binary functions
1818− :: ByteString -- ^ aggregate function
1819− -> Distinction (Expression outer 'Ungrouped commons schemas params from :*: Expression outer 'Ungrouped commons schemas params from) xty
1820− -> Expression outer ('Grouped bys) commons schemas params from yty
1821−unsafeAggregate2 fun xx = UnsafeExpression $ mconcat
1822− [fun, "(", renderSQL xx, ")"]
1824−-- | A type family that calculates `PGAvg` type of a `PGType`.
1825−type family PGAvg ty where
1826− PGAvg 'PGint2 = 'PGnumeric
1827− PGAvg 'PGint4 = 'PGnumeric
1828− PGAvg 'PGint8 = 'PGnumeric
1829− PGAvg 'PGnumeric = 'PGnumeric
1830− PGAvg 'PGfloat4 = 'PGfloat8
1831− PGAvg 'PGfloat8 = 'PGfloat8
1832− PGAvg 'PGinterval = 'PGinterval
1833− PGAvg pg = TypeError
1834− ('Text "Squeal type error: No average for " ':<>: 'ShowType pg)
1836−{-----------------------------------------
1838−-----------------------------------------}
1840−-- | `TypeExpression`s are used in `cast`s and `createTable` commands.
1841−newtype TypeExpression (schemas :: SchemasType) (ty :: NullityType)
1842− = UnsafeTypeExpression { renderTypeExpression :: ByteString }
1843− deriving (GHC.Generic,Show,Eq,Ord,NFData)
1844−instance RenderSQL (TypeExpression schemas ty) where
1845− renderSQL = renderTypeExpression
1847−-- | The enum or composite type in a `Typedef` can be expressed by its alias.
1849− :: (Has sch schemas schema, Has td schema ('Typedef ty))
1850− => QualifiedAlias sch td
1851− -> TypeExpression schemas (nullity ty)
1852−typedef = UnsafeTypeExpression . renderSQL
1854−-- | The composite type corresponding to a `Table` definition can be expressed
1857− :: (Has sch schemas schema, Has tab schema ('Table table))
1858− => QualifiedAlias sch tab
1859− -> TypeExpression schemas (nullity ('PGcomposite (TableToRow table)))
1860−typetable = UnsafeTypeExpression . renderSQL
1862−-- | The composite type corresponding to a `View` definition can be expressed
1865− :: (Has sch schemas schema, Has vw schema ('View view))
1866− => QualifiedAlias sch vw
1867− -> TypeExpression schemas (nullity ('PGcomposite view))
1868−typeview = UnsafeTypeExpression . renderSQL
1870−-- | logical Boolean (true/false)
1871−bool :: TypeExpression schemas (nullity 'PGbool)
1872−bool = UnsafeTypeExpression "bool"
1873−-- | signed two-byte integer
1874−int2, smallint :: TypeExpression schemas (nullity 'PGint2)
1875−int2 = UnsafeTypeExpression "int2"
1876−smallint = UnsafeTypeExpression "smallint"
1877−-- | signed four-byte integer
1878−int4, int, integer :: TypeExpression schemas (nullity 'PGint4)
1879−int4 = UnsafeTypeExpression "int4"
1880−int = UnsafeTypeExpression "int"
1881−integer = UnsafeTypeExpression "integer"
1882−-- | signed eight-byte integer
1883−int8, bigint :: TypeExpression schemas (nullity 'PGint8)
1884−int8 = UnsafeTypeExpression "int8"
1885−bigint = UnsafeTypeExpression "bigint"
1886−-- | arbitrary precision numeric type
1887−numeric :: TypeExpression schemas (nullity 'PGnumeric)
1888−numeric = UnsafeTypeExpression "numeric"
1889−-- | single precision floating-point number (4 bytes)
1890−float4, real :: TypeExpression schemas (nullity 'PGfloat4)
1891−float4 = UnsafeTypeExpression "float4"
1892−real = UnsafeTypeExpression "real"
1893−-- | double precision floating-point number (8 bytes)
1894−float8, doublePrecision :: TypeExpression schemas (nullity 'PGfloat8)
1895−float8 = UnsafeTypeExpression "float8"
1896−doublePrecision = UnsafeTypeExpression "double precision"
1897−-- | variable-length character string
1898−text :: TypeExpression schemas (nullity 'PGtext)
1899−text = UnsafeTypeExpression "text"
1900−-- | fixed-length character string
1902− :: forall n schemas nullity. (KnownNat n, 1 <= n)
1903− => TypeExpression schemas (nullity ('PGchar n))
1904−char = UnsafeTypeExpression $ "char(" <> renderNat @n <> ")"
1905−character = UnsafeTypeExpression $ "character(" <> renderNat @n <> ")"
1906−-- | variable-length character string
1907−varchar, characterVarying
1908− :: forall n schemas nullity. (KnownNat n, 1 <= n)
1909− => TypeExpression schemas (nullity ('PGvarchar n))
1910−varchar = UnsafeTypeExpression $ "varchar(" <> renderNat @n <> ")"
1911−characterVarying = UnsafeTypeExpression $
1912− "character varying(" <> renderNat @n <> ")"
1913−-- | binary data ("byte array")
1914−bytea :: TypeExpression schemas (nullity 'PGbytea)
1915−bytea = UnsafeTypeExpression "bytea"
1916−-- | date and time (no time zone)
1917−timestamp :: TypeExpression schemas (nullity 'PGtimestamp)
1918−timestamp = UnsafeTypeExpression "timestamp"
1919−-- | date and time, including time zone
1920−timestampWithTimeZone :: TypeExpression schemas (nullity 'PGtimestamptz)
1921−timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
1922−-- | calendar date (year, month, day)
1923−date :: TypeExpression schemas (nullity 'PGdate)
1924−date = UnsafeTypeExpression "date"
1925−-- | time of day (no time zone)
1926−time :: TypeExpression schemas (nullity 'PGtime)
1927−time = UnsafeTypeExpression "time"
1928−-- | time of day, including time zone
1929−timeWithTimeZone :: TypeExpression schemas (nullity 'PGtimetz)
1930−timeWithTimeZone = UnsafeTypeExpression "time with time zone"
1932−interval :: TypeExpression schemas (nullity 'PGinterval)
1933−interval = UnsafeTypeExpression "interval"
1934−-- | universally unique identifier
1935−uuid :: TypeExpression schemas (nullity 'PGuuid)
1936−uuid = UnsafeTypeExpression "uuid"
1937−-- | IPv4 or IPv6 host address
1938−inet :: TypeExpression schemas (nullity 'PGinet)
1939−inet = UnsafeTypeExpression "inet"
1940−-- | textual JSON data
1941−json :: TypeExpression schemas (nullity 'PGjson)
1942−json = UnsafeTypeExpression "json"
1943−-- | binary JSON data, decomposed
1944−jsonb :: TypeExpression schemas (nullity 'PGjsonb)
1945−jsonb = UnsafeTypeExpression "jsonb"
1946−-- | variable length array
1948− :: TypeExpression schemas pg
1949− -> TypeExpression schemas (nullity ('PGvararray pg))
1950−vararray ty = UnsafeTypeExpression $ renderSQL ty <> "[]"
1951−-- | fixed length array
1953−-- >>> renderSQL (fixarray @'[2] json)
1956− :: forall dims schemas nullity pg. All KnownNat dims
1957− => TypeExpression schemas pg
1958− -> TypeExpression schemas (nullity ('PGfixarray dims pg))
1959−fixarray ty = UnsafeTypeExpression $
1960− renderSQL ty <> renderDims @dims
1962− renderDims :: forall ns. All KnownNat ns => ByteString
1966− . ByteString.intercalate "]["
1968− $ hcmap (Proxy @KnownNat)
1969− (K . fromString . show . natVal)
1970− (hpure Proxy :: NP Proxy ns)
1972−-- | `pgtype` is a demoted version of a `PGType`
1973−class PGTyped schemas (ty :: NullityType) where
1974− pgtype :: TypeExpression schemas ty
1975−instance PGTyped schemas (nullity 'PGbool) where pgtype = bool
1976−instance PGTyped schemas (nullity 'PGint2) where pgtype = int2
1977−instance PGTyped schemas (nullity 'PGint4) where pgtype = int4
1978−instance PGTyped schemas (nullity 'PGint8) where pgtype = int8
1979−instance PGTyped schemas (nullity 'PGnumeric) where pgtype = numeric
1980−instance PGTyped schemas (nullity 'PGfloat4) where pgtype = float4
1981−instance PGTyped schemas (nullity 'PGfloat8) where pgtype = float8
1982−instance PGTyped schemas (nullity 'PGtext) where pgtype = text
1983−instance (KnownNat n, 1 <= n)
1984− => PGTyped schemas (nullity ('PGchar n)) where pgtype = char @n
1985−instance (KnownNat n, 1 <= n)
1986− => PGTyped schemas (nullity ('PGvarchar n)) where pgtype = varchar @n
1987−instance PGTyped schemas (nullity 'PGbytea) where pgtype = bytea
1988−instance PGTyped schemas (nullity 'PGtimestamp) where pgtype = timestamp
1989−instance PGTyped schemas (nullity 'PGtimestamptz) where pgtype = timestampWithTimeZone
1990−instance PGTyped schemas (nullity 'PGdate) where pgtype = date
1991−instance PGTyped schemas (nullity 'PGtime) where pgtype = time
1992−instance PGTyped schemas (nullity 'PGtimetz) where pgtype = timeWithTimeZone
1993−instance PGTyped schemas (nullity 'PGinterval) where pgtype = interval
1994−instance PGTyped schemas (nullity 'PGuuid) where pgtype = uuid
1995−instance PGTyped schemas (nullity 'PGjson) where pgtype = json
1996−instance PGTyped schemas (nullity 'PGjsonb) where pgtype = jsonb
1997−instance PGTyped schemas ty
1998− => PGTyped schemas (nullity ('PGvararray ty)) where
1999− pgtype = vararray (pgtype @schemas @ty)
2000−instance (All KnownNat dims, PGTyped schemas ty)
2001− => PGTyped schemas (nullity ('PGfixarray dims ty)) where
2002− pgtype = fixarray @dims (pgtype @schemas @ty)
2004−{-----------------------------------------
2006−-----------------------------------------}
2008−-- | `SortExpression`s are used by `sortBy` to optionally sort the results
2009−-- of a `Query`. `Asc` or `Desc` set the sort direction of a `NotNull` result
2010−-- column to ascending or descending. Ascending order puts smaller values
2011−-- first, where "smaller" is defined in terms of the `.<` operator. Similarly,
2012−-- descending order is determined with the `.>` operator. `AscNullsFirst`,
2013−-- `AscNullsLast`, `DescNullsFirst` and `DescNullsLast` options are used to
2014−-- determine whether nulls appear before or after non-null values in the sort
2015−-- ordering of a `Null` result column.
2016−data SortExpression outer grp commons schemas params from where
2018− :: Expression outer grp commons schemas params from ('NotNull ty)
2019− -> SortExpression outer grp commons schemas params from
2021− :: Expression outer grp commons schemas params from ('NotNull ty)
2022− -> SortExpression outer grp commons schemas params from
2024− :: Expression outer grp commons schemas params from ('Null ty)
2025− -> SortExpression outer grp commons schemas params from
2027− :: Expression outer grp commons schemas params from ('Null ty)
2028− -> SortExpression outer grp commons schemas params from
2030− :: Expression outer grp commons schemas params from ('Null ty)
2031− -> SortExpression outer grp commons schemas params from
2033− :: Expression outer grp commons schemas params from ('Null ty)
2034− -> SortExpression outer grp commons schemas params from
2035−deriving instance Show (SortExpression outer grp commons schemas params from)
2037−class OrderBy expr where
2039− :: [SortExpression outer grp commons schemas params from]
2040− -> expr outer grp commons schemas params from
2041− -> expr outer grp commons schemas params from
2043−-- | Render a `SortExpression`.
2044−instance RenderSQL (SortExpression outer grp commons schemas params from) where
2046− Asc expression -> renderSQL expression <+> "ASC"
2047− Desc expression -> renderSQL expression <+> "DESC"
2048− AscNullsFirst expression -> renderSQL expression
2049− <+> "ASC NULLS FIRST"
2050− DescNullsFirst expression -> renderSQL expression
2051− <+> "DESC NULLS FIRST"
2052− AscNullsLast expression -> renderSQL expression <+> "ASC NULLS LAST"
2053− DescNullsLast expression -> renderSQL expression <+> "DESC NULLS LAST"
2055−data WindowDefinition outer grp commons schemas params from where
2058− => NP (Expression outer grp commons schemas params from) bys
2059− -> [SortExpression outer grp commons schemas params from]
2060− -> WindowDefinition outer grp commons schemas params from
2062−instance OrderBy WindowDefinition where
2063− orderBy sortsR (WindowDefinition parts sortsL)
2064− = WindowDefinition parts (sortsL ++ sortsR)
2066−instance RenderSQL (WindowDefinition outer commons schemas from grp params) where
2067− renderSQL (WindowDefinition part ord) =
2068− renderPartitionByClause part <> renderOrderByClause ord
2070− renderPartitionByClause = \case
2072− parts -> "PARTITION" <+> "BY" <+> renderCommaSeparated renderExpression parts
2073− renderOrderByClause = \case
2075− srts -> " ORDER" <+> "BY"
2076− <+> commaSeparated (renderSQL <$> srts)
2080− => NP (Expression outer grp commons schemas params from) bys
2081− -> WindowDefinition outer grp commons schemas params from
2082−partitionBy bys = WindowDefinition bys []
2084−newtype WindowFunction
2085− (outer :: FromType)
2087− (commons :: FromType)
2088− (schemas :: SchemasType)
2089− (params :: [NullityType])
2091− (ty :: NullityType)
2092− = UnsafeWindowFunction { renderWindowFunction :: ByteString }
2093− deriving (GHC.Generic,Show,Eq,Ord,NFData)
2095−instance RenderSQL (WindowFunction outer grp commons schemas params from ty) where
2096− renderSQL = renderWindowFunction
2098−unsafeWindowFunction1
2100− -> Expression outer grp commons schemas params from ty0
2101− -> WindowFunction outer grp commons schemas params from ty1
2102−unsafeWindowFunction1 fun x
2103− = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x)
2105−unsafeWindowFunction2
2107− -> (Expression outer grp commons schemas params from :*: Expression outer grp commons schemas params from) ty0
2108− -> WindowFunction outer grp commons schemas params from ty1
2109−unsafeWindowFunction2 fun (x :*: x')
2110− = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x <> ", " <> renderSQL x')
2112−{- | rank of the current row with gaps; same as `rowNumber` of its first peer
2116−rank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2117−rank = UnsafeWindowFunction "rank()"
2119−{- | number of the current row within its partition, counting from 1
2120−>>> printSQL rowNumber
2123−rowNumber :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2124−rowNumber = UnsafeWindowFunction "row_number()"
2126−{- | rank of the current row without gaps; this function counts peer groups
2127−>>> printSQL denseRank
2130−denseRank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGint8)
2131−denseRank = UnsafeWindowFunction "dense_rank()"
2133−{- | relative rank of the current row: (rank - 1) / (total partition rows - 1)
2134−>>> printSQL percentRank
2137−percentRank :: WindowFunction outer grp commons schemas params from ('NotNull 'PGfloat8)
2138−percentRank = UnsafeWindowFunction "percent_rank()"
2140−{- | cumulative distribution: (number of partition rows
2141−preceding or peer with current row) / total partition rows
2142−>>> printSQL cumeDist
2145−cumeDist :: WindowFunction outer grp commons schemas params from ('NotNull 'PGfloat8)
2146−cumeDist = UnsafeWindowFunction "cume_dist()"
2148−{- | integer ranging from 1 to the argument value,
2149−dividing the partition as equally as possible
2152− :: Expression outer grp commons schemas params from ('NotNull 'PGint4)
2154− -> WindowFunction outer grp commons schemas params from ('NotNull 'PGint4)
2155−ntile = unsafeWindowFunction1 "ntile"
2157−{- | returns value evaluated at the row that is offset rows before the current
2158−row within the partition; if there is no such row, instead return default
2159−(which must be of the same type as value). Both offset and default are
2160−evaluated with respect to the current row.
2163− :: Expression outer grp commons schemas params from ty
2165− -> Expression outer grp commons schemas params from ('NotNull 'PGint4)
2167− -> Expression outer grp commons schemas params from ty
2169− -> WindowFunction outer grp commons schemas params from ty
2170−lag value offset def = UnsafeWindowFunction $ "lag"
2172− (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
2174−{- | returns value evaluated at the row that is offset rows after the current
2175−row within the partition; if there is no such row, instead return default
2176−(which must be of the same type as value). Both offset and default are
2177−evaluated with respect to the current row.
2180− :: Expression outer grp commons schemas params from ty
2182− -> Expression outer grp commons schemas params from ('NotNull 'PGint4)
2184− -> Expression outer grp commons schemas params from ty
2186− -> WindowFunction outer grp commons schemas params from ty
2187−lead value offset def = UnsafeWindowFunction $ "lag"
2189− (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
2191−{- | returns value evaluated at the row that is the
2192−first row of the window frame
2195− :: Expression outer grp commons schemas params from ty
2197− -> WindowFunction outer grp commons schemas params from ty
2198−firstValue = unsafeWindowFunction1 "first_value"
2200−{- | returns value evaluated at the row that is the
2201−last row of the window frame
2204− :: Expression outer grp commons schemas params from ty
2206− -> WindowFunction outer grp commons schemas params from ty
2207−lastValue = unsafeWindowFunction1 "last_value"
2209−{- | returns value evaluated at the row that is the nth
2210−row of the window frame (counting from 1); null if no such row
2213− :: Expression outer grp commons schemas params from (nullity ty)
2215− -> Expression outer grp commons schemas params from ('NotNull 'PGint4)
2217− -> WindowFunction outer grp commons schemas params from ('Null ty)
2218−nthValue value nth = UnsafeWindowFunction $ "nth_value"
2219− <> parenthesized (commaSeparated [renderSQL value, renderSQL nth])