on dev help sign in
set the stage for bivariate stats
commit574aeb23a188da3714992cd26a38c38065a6bdb2 authorEitan Chatav <eitan.chatav@gmail.com> dateMon 28 Jan 2019, 22:40 (8 years ago) parent9585150
1 file changed+60−54
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+60−54
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+60−54
@@ -1393,7 +1393,8 @@ jsonbPretty = unsafeFunction "jsonb_pretty"
13931393aggregation
13941394-----------------------------------------}
13951395
1396−class Aggregate expr aggr | aggr -> expr where
1396+class Aggregate expr1 expr2 aggr
1397+ | aggr -> expr1, aggr -> expr2 where
13971398
13981399
13991400
@@ -1406,7 +1407,7 @@ class Aggregate expr aggr | aggr -> expr where
14061407 -- in printSQL expression
14071408 -- :}
14081409 -- count(*)
1409− countStar :: aggr commons schemas params from ('NotNull 'PGint8)
1410+ countStar :: aggr ('NotNull 'PGint8)
14101411
14111412 -- | >>> :{
14121413 -- let
@@ -1416,9 +1417,9 @@ class Aggregate expr aggr | aggr -> expr where
14161417 -- :}
14171418 -- count(ALL "col")
14181419 count
1419− :: expr commons schemas params from ty
1420+ :: expr1 ty
14201421 -- ^ what to count
1421− -> aggr commons schemas params from ('NotNull 'PGint8)
1422+ -> aggr ('NotNull 'PGint8)
14221423
14231424 -- | >>> :{
14241425 -- let
@@ -1429,8 +1430,8 @@ class Aggregate expr aggr | aggr -> expr where
14291430 -- sum(DISTINCT "col")
14301431 sum_
14311432 :: ty `In` PGNum
1432− => expr commons schemas params from (nullity ty)
1433− -> aggr commons schemas params from (nullity ty)
1433+ => expr1 (nullity ty)
1434+ -> aggr (nullity ty)
14341435
14351436 -- | >>> :{
14361437 -- let
@@ -1441,9 +1442,9 @@ class Aggregate expr aggr | aggr -> expr where
14411442 -- bit_and(DISTINCT "col")
14421443 bitAnd
14431444 :: int `In` PGIntegral
1444− => expr commons schemas params from (nullity int)
1445+ => expr1 (nullity int)
14451446 -- ^ what to aggregate
1446− -> aggr commons schemas params from (nullity int)
1447+ -> aggr (nullity int)
14471448
14481449 -- | >>> :{
14491450 -- let
@@ -1454,9 +1455,9 @@ class Aggregate expr aggr | aggr -> expr where
14541455 -- bit_or(ALL "col")
14551456 bitOr
14561457 :: int `In` PGIntegral
1457− => expr commons schemas params from (nullity int)
1458+ => expr1 (nullity int)
14581459 -- ^ what to aggregate
1459− -> aggr commons schemas params from (nullity int)
1460+ -> aggr (nullity int)
14601461
14611462 -- | >>> :{
14621463 -- let
@@ -1466,9 +1467,9 @@ class Aggregate expr aggr | aggr -> expr where
14661467 -- :}
14671468 -- bool_and(ALL "col")
14681469 boolAnd
1469− :: expr commons schemas params from (nullity 'PGbool)
1470+ :: expr1 (nullity 'PGbool)
14701471 -- ^ what to aggregate
1471− -> aggr commons schemas params from (nullity 'PGbool)
1472+ -> aggr (nullity 'PGbool)
14721473
14731474 -- | >>> :{
14741475 -- let
@@ -1478,9 +1479,9 @@ class Aggregate expr aggr | aggr -> expr where
14781479 -- :}
14791480 -- bool_or(ALL "col")
14801481 boolOr
1481− :: expr commons schemas params from (nullity 'PGbool)
1482+ :: expr1(nullity 'PGbool)
14821483 -- ^ what to aggregate
1483− -> aggr commons schemas params from (nullity 'PGbool)
1484+ -> aggr (nullity 'PGbool)
14841485
14851486 -- | synonym for `boolAnd`
14861487 --
@@ -1492,68 +1493,73 @@ class Aggregate expr aggr | aggr -> expr where
14921493 -- :}
14931494 -- every(DISTINCT "col")
14941495 every
1495− :: expr commons schemas params from (nullity 'PGbool)
1496+ :: expr1 (nullity 'PGbool)
14961497 -- ^ what to aggregate
1497− -> aggr commons schemas params from (nullity 'PGbool)
1498+ -> aggr (nullity 'PGbool)
14981499
14991500 -- | maximum aggregation
15001501 max_
1501− :: expr commons schemas params from (nullity ty)
1502+ :: expr1 (nullity ty)
15021503 -- ^ what to maximize
1503− -> aggr commons schemas params from (nullity ty)
1504+ -> aggr (nullity ty)
15041505
15051506 -- | minimum aggregation
15061507 min_
1507− :: expr commons schemas params from (nullity ty)
1508+ :: expr1 (nullity ty)
15081509 -- ^ what to minimize
1509− -> aggr commons schemas params from (nullity ty)
1510+ -> aggr (nullity ty)
15101511
15111512 -- | average aggregation
15121513 avg
1513− :: expr commons schemas params from (nullity ty)
1514+ :: expr1 (nullity ty)
15141515 -- ^ what to average
1515− -> aggr commons schemas params from (nullity (PGAvg ty))
1516+ -> aggr (nullity (PGAvg ty))
15161517
1517−data Distinction commons schemas params from ty
1518− = All (Expression 'Ungrouped commons schemas params from ty)
1519− | Distinct (Expression 'Ungrouped commons schemas params from ty)
1518+data Distinction expr ty
1519+ = All (expr ty)
1520+ | Distinct (expr ty)
15201521 deriving (GHC.Generic,Show,Eq,Ord)
1521−instance NFData (Distinction commons schemas params from ty)
1522−instance RenderSQL (Distinction commons schemas params from ty) where
1522+instance NFData (Distinction (Expression grp commons schemas params from) ty)
1523+instance RenderSQL (Distinction (Expression grp commons schemas params from) ty) where
15231524 renderSQL = \case
15241525 All x -> "ALL" <+> renderSQL x
15251526 Distinct x -> "DISTINCT" <+> renderSQL x
15261527
1527−instance Aggregate Distinction (Expression ('Grouped bys)) where
1528− countStar = UnsafeExpression "count(*)"
1529− count = unsafeAggregate "count"
1530− sum_ = unsafeAggregate "sum"
1531− bitAnd = unsafeAggregate "bit_and"
1532− bitOr = unsafeAggregate "bit_or"
1533− boolAnd = unsafeAggregate "bool_and"
1534− boolOr = unsafeAggregate "bool_or"
1535− every = unsafeAggregate "every"
1536− max_ = unsafeAggregate "max"
1537− min_ = unsafeAggregate "min"
1538− avg = unsafeAggregate "avg"
1539−instance Aggregate (Expression grp) (WindowFunction grp) where
1540− countStar = UnsafeWindowFunction "count(*)"
1541− count = unsafeWindowFunction1 "count"
1542− sum_ = unsafeWindowFunction1 "sum"
1543− bitAnd = unsafeWindowFunction1 "bit_and"
1544− bitOr = unsafeWindowFunction1 "bit_or"
1545− boolAnd = unsafeWindowFunction1 "bool_and"
1546− boolOr = unsafeWindowFunction1 "bool_or"
1547− every = unsafeWindowFunction1 "every"
1548− max_ = unsafeWindowFunction1 "max"
1549− min_ = unsafeWindowFunction1 "min"
1550− avg = unsafeWindowFunction1 "avg"
1528+instance Aggregate
1529+ (Distinction (Expression 'Ungrouped commons schemas params from))
1530+ (Distinction (Expression 'Ungrouped commons schemas params from GHC.:*: Expression 'Ungrouped commons schemas params from))
1531+ (Expression ('Grouped bys) commons schemas params from) where
1532+ countStar = UnsafeExpression "count(*)"
1533+ count = unsafeAggregate "count"
1534+ sum_ = unsafeAggregate "sum"
1535+ bitAnd = unsafeAggregate "bit_and"
1536+ bitOr = unsafeAggregate "bit_or"
1537+ boolAnd = unsafeAggregate "bool_and"
1538+ boolOr = unsafeAggregate "bool_or"
1539+ every = unsafeAggregate "every"
1540+ max_ = unsafeAggregate "max"
1541+ min_ = unsafeAggregate "min"
1542+ avg = unsafeAggregate "avg"
1543+instance Aggregate
1544+ (Expression grp commons schemas params from)
1545+ (Expression grp commons schemas params from GHC.:*: Expression grp commons schemas params from)
1546+ (WindowFunction grp commons schemas params from) where
1547+ countStar = UnsafeWindowFunction "count(*)"
1548+ count = unsafeWindowFunction1 "count"
1549+ sum_ = unsafeWindowFunction1 "sum"
1550+ bitAnd = unsafeWindowFunction1 "bit_and"
1551+ bitOr = unsafeWindowFunction1 "bit_or"
1552+ boolAnd = unsafeWindowFunction1 "bool_and"
1553+ boolOr = unsafeWindowFunction1 "bool_or"
1554+ every = unsafeWindowFunction1 "every"
1555+ max_ = unsafeWindowFunction1 "max"
1556+ min_ = unsafeWindowFunction1 "min"
1557+ avg = unsafeWindowFunction1 "avg"
15511558
15521559-- | escape hatch to define aggregate functions
15531560unsafeAggregate
1554− :: RenderSQL (expr commons schemas params from xty)
1555− => ByteString -- ^ aggregate function
1556− -> expr commons schemas params from xty
1561+ :: ByteString -- ^ aggregate function
1562+ -> Distinction (Expression 'Ungrouped commons schemas params from) xty
15571563 -> Expression ('Grouped bys) commons schemas params from yty
15581564unsafeAggregate fun x = UnsafeExpression $ mconcat
15591565 [fun, "(", renderSQL x, ")"]