on dev help sign in
Distinction
bc9bc31Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+30−37
@@ -135,7 +135,7 @@ module Squeal.PostgreSQL.Expression
135 , jsonbPretty
135 , jsonbPretty
136 -- ** Aggregation
136 -- ** Aggregation
137 , Aggregate (..)
137 , Aggregate (..)
138− , DistinctExpression (..)
138+ , Distinction (..)
139 , PGAvg
139 , PGAvg
140 , countStar
140 , countStar
141 -- * Window Functions
141 -- * Window Functions
@@ -1394,15 +1394,15 @@ jsonbPretty = unsafeFunction "jsonb_pretty"
1394aggregation
1394aggregation
1395-----------------------------------------}
1395-----------------------------------------}
1396
1396
1397−class Aggregate expr aggr where
1397+class Aggregate expr aggr | aggr -> expr where
1398
1398
1399 -- | >>> :{
1399 -- | >>> :{
1400 -- let
1400 -- let
1401 -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1401 -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity ty]] ('NotNull 'PGint8)
1402− -- expression = count #col
1402+ -- expression = count (All #col)
1403 -- in printSQL expression
1403 -- in printSQL expression
1404 -- :}
1404 -- :}
1405− -- count("col")
1405+ -- count(ALL "col")
1406 count
1406 count
1407 :: expr commons schemas params from ty
1407 :: expr commons schemas params from ty
1408 -- ^ what to count
1408 -- ^ what to count
@@ -1411,10 +1411,10 @@ class Aggregate expr aggr where
1411 -- | >>> :{
1411 -- | >>> :{
1412 -- let
1412 -- let
1413 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1413 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: 'Null 'PGnumeric]] ('Null 'PGnumeric)
1414− -- expression = sum_ #col
1414+ -- expression = sum_ (Distinct #col)
1415 -- in printSQL expression
1415 -- in printSQL expression
1416 -- :}
1416 -- :}
1417− -- sum("col")
1417+ -- sum(DISTINCT "col")
1418 sum_
1418 sum_
1419 :: ty `In` PGNum
1419 :: ty `In` PGNum
1420 => expr commons schemas params from (nullity ty)
1420 => expr commons schemas params from (nullity ty)
@@ -1423,7 +1423,7 @@ class Aggregate expr aggr where
1423 -- | >>> :{
1423 -- | >>> :{
1424 -- let
1424 -- let
1425 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1425 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1426− -- expression = bitAndDistinct #col
1426+ -- expression = bitAnd (Distinct #col)
1427 -- in printSQL expression
1427 -- in printSQL expression
1428 -- :}
1428 -- :}
1429 -- bit_and(DISTINCT "col")
1429 -- bit_and(DISTINCT "col")
@@ -1436,10 +1436,10 @@ class Aggregate expr aggr where
1436 -- | >>> :{
1436 -- | >>> :{
1437 -- let
1437 -- let
1438 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1438 -- expression :: Expression ('Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGint4]] (nullity 'PGint4)
1439− -- expression = bitOr #col
1439+ -- expression = bitOr (All #col)
1440 -- in printSQL expression
1440 -- in printSQL expression
1441 -- :}
1441 -- :}
1442− -- bit_or("col")
1442+ -- bit_or(ALL "col")
1443 bitOr
1443 bitOr
1444 :: int `In` PGIntegral
1444 :: int `In` PGIntegral
1445 => expr commons schemas params from (nullity int)
1445 => expr commons schemas params from (nullity int)
@@ -1448,11 +1448,11 @@ class Aggregate expr aggr where
1448
1448
1449 -- | >>> :{
1449 -- | >>> :{
1450 -- let
1450 -- let
1451− -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1451+ -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1452− -- expression = boolAnd #col
1452+ -- expression = boolAnd (All #col)
1453 -- in printSQL expression
1453 -- in printSQL expression
1454 -- :}
1454 -- :}
1455− -- bool_and("col")
1455+ -- bool_and(ALL "col")
1456 boolAnd
1456 boolAnd
1457 :: expr commons schemas params from (nullity 'PGbool)
1457 :: expr commons schemas params from (nullity 'PGbool)
1458 -- ^ what to aggregate
1458 -- ^ what to aggregate
@@ -1460,11 +1460,11 @@ class Aggregate expr aggr where
1460
1460
1461 -- | >>> :{
1461 -- | >>> :{
1462 -- let
1462 -- let
1463− -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1463+ -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1464− -- expression = boolOr #col
1464+ -- expression = boolOr (All #col)
1465 -- in printSQL expression
1465 -- in printSQL expression
1466 -- :}
1466 -- :}
1467− -- bool_or("col")
1467+ -- bool_or(ALL "col")
1468 boolOr
1468 boolOr
1469 :: expr commons schemas params from (nullity 'PGbool)
1469 :: expr commons schemas params from (nullity 'PGbool)
1470 -- ^ what to aggregate
1470 -- ^ what to aggregate
@@ -1474,11 +1474,11 @@ class Aggregate expr aggr where
1474 --
1474 --
1475 -- >>> :{
1475 -- >>> :{
1476 -- let
1476 -- let
1477− -- expression :: Expression commons schemas params (Grouped bys) '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1477+ -- expression :: Expression (Grouped bys) commons schemas params '[tab ::: '["col" ::: nullity 'PGbool]] (nullity 'PGbool)
1478− -- expression = every #col
1478+ -- expression = every (Distinct #col)
1479 -- in printSQL expression
1479 -- in printSQL expression
1480 -- :}
1480 -- :}
1481− -- every("col")
1481+ -- every(DISTINCT "col")
1482 every
1482 every
1483 :: expr commons schemas params from (nullity 'PGbool)
1483 :: expr commons schemas params from (nullity 'PGbool)
1484 -- ^ what to aggregate
1484 -- ^ what to aggregate
@@ -1502,19 +1502,17 @@ class Aggregate expr aggr where
1502 -- ^ what to average
1502 -- ^ what to average
1503 -> aggr commons schemas params from (nullity (PGAvg ty))
1503 -> aggr commons schemas params from (nullity (PGAvg ty))
1504
1504
1505+data Distinction commons schemas params from ty
1505−instance {-# OVERLAPPABLE #-} expr ~ (Expression 'Ungrouped)
1506+ = All (Expression 'Ungrouped commons schemas params from ty)
1506− => Aggregate expr (Expression ('Grouped bys)) where
1507+ | Distinct (Expression 'Ungrouped commons schemas params from ty)
1507− count = unsafeAggregate "count"
1508+ deriving (GHC.Generic,Show,Eq,Ord)
1508− sum_ = unsafeAggregate "sum"
1509+instance NFData (Distinction commons schemas params from ty)
1509− bitAnd = unsafeAggregate "bit_and"
1510+instance RenderSQL (Distinction commons schemas params from ty) where
1510− bitOr = unsafeAggregate "bit_or"
1511+ renderSQL = \case
1511− boolAnd = unsafeAggregate "bool_and"
1512+ All x -> "ALL" <+> renderSQL x
1512− boolOr = unsafeAggregate "bool_or"
1513+ Distinct x -> "DISTINCT" <+> renderSQL x
1513− every = unsafeAggregate "every"
1514+
1514− max_ = unsafeAggregate "max"
1515− min_ = unsafeAggregate "min"
1516− avg = unsafeAggregate "avg"
1517−instance {-# OVERLAPPING #-} Aggregate DistinctExpression (Expression ('Grouped bys)) where
1515+instance Aggregate Distinction (Expression ('Grouped bys)) where
1518 count = unsafeAggregate "count"
1516 count = unsafeAggregate "count"
1519 sum_ = unsafeAggregate "sum"
1517 sum_ = unsafeAggregate "sum"
1520 bitAnd = unsafeAggregate "bit_and"
1518 bitAnd = unsafeAggregate "bit_and"
@@ -1537,12 +1535,6 @@ instance Aggregate (Expression grp) (WindowFunction grp) where
1537 min_ = unsafeWindowFunction1 "min"
1535 min_ = unsafeWindowFunction1 "min"
1538 avg = unsafeWindowFunction1 "avg"
1536 avg = unsafeWindowFunction1 "avg"
1539
1537
1540−newtype DistinctExpression commons schemas params from ty
1541− = Distinct (Expression 'Ungrouped commons schemas params from ty)
1542− deriving (GHC.Generic,Show,Eq,Ord,NFData)
1543−instance RenderSQL (DistinctExpression commons schemas params from ty) where
1544− renderSQL (Distinct x) = "DISTINCT" <+> renderSQL x
1545−
1546-- | escape hatch to define aggregate functions
1538-- | escape hatch to define aggregate functions
1547unsafeAggregate
1539unsafeAggregate
1548 :: RenderSQL (expr commons schemas params from xty)
1540 :: RenderSQL (expr commons schemas params from xty)
@@ -1561,6 +1553,7 @@ type family PGAvg ty where
1561 PGAvg 'PGfloat4 = 'PGfloat8
1553 PGAvg 'PGfloat4 = 'PGfloat8
1562 PGAvg 'PGfloat8 = 'PGfloat8
1554 PGAvg 'PGfloat8 = 'PGfloat8
1563 PGAvg 'PGinterval = 'PGinterval
1555 PGAvg 'PGinterval = 'PGinterval
1556+ -- PGAvg pg = TypeError
1564
1557
1565-- | A special aggregation that does not require an input
1558-- | A special aggregation that does not require an input
1566--
1559--
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+3−3
@@ -200,13 +200,13 @@ aggregation query:
200let
200let
201 query :: Query '[] (Public Schema) '[] '["sum" ::: 'NotNull 'PGint4, "col1" ::: 'NotNull 'PGint4 ]
201 query :: Query '[] (Public Schema) '[] '["sum" ::: 'NotNull 'PGint4, "col1" ::: 'NotNull 'PGint4 ]
202 query =
202 query =
203− select_ (sum_ #col2 `as` #sum :* #col1)
203+ select_ (sum_ (All #col2) `as` #sum :* #col1)
204 ( from (table (#tab `as` #table1))
204 ( from (table (#tab `as` #table1))
205 & groupBy #col1
205 & groupBy #col1
206− & having (#col1 + sum_ #col2 .> 1) )
206+ & having (#col1 + sum_ (Distinct #col2) .> 1) )
207in printSQL query
207in printSQL query
208:}
208:}
209−SELECT sum("col2") AS "sum", "col1" AS "col1" FROM "tab" AS "table1" GROUP BY "col1" HAVING (("col1" + sum("col2")) > 1)
209+SELECT sum(ALL "col2") AS "sum", "col1" AS "col1" FROM "tab" AS "table1" GROUP BY "col1" HAVING (("col1" + sum(DISTINCT "col2")) > 1)
210
210
211sorted query:
211sorted query:
212
212