on dev help sign in
set the stage for bivariate stats
574aeb2Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+60−54
@@ -1393,7 +1393,8 @@ jsonbPretty = unsafeFunction "jsonb_pretty"
1393aggregation
1393aggregation
1394-----------------------------------------}
1394-----------------------------------------}
1395
1395
1396−class Aggregate expr aggr | aggr -> expr where
1396+class Aggregate expr1 expr2 aggr
1397+ | aggr -> expr1, aggr -> expr2 where
1397
1398
1398
1399
1399
1400
@@ -1406,7 +1407,7 @@ class Aggregate expr aggr | aggr -> expr where
1406 -- in printSQL expression
1407 -- in printSQL expression
1407 -- :}
1408 -- :}
1408 -- count(*)
1409 -- count(*)
1409− countStar :: aggr commons schemas params from ('NotNull 'PGint8)
1410+ countStar :: aggr ('NotNull 'PGint8)
1410
1411
1411 -- | >>> :{
1412 -- | >>> :{
1412 -- let
1413 -- let
@@ -1416,9 +1417,9 @@ class Aggregate expr aggr | aggr -> expr where
1416 -- :}
1417 -- :}
1417 -- count(ALL "col")
1418 -- count(ALL "col")
1418 count
1419 count
1419− :: expr commons schemas params from ty
1420+ :: expr1 ty
1420 -- ^ what to count
1421 -- ^ what to count
1421− -> aggr commons schemas params from ('NotNull 'PGint8)
1422+ -> aggr ('NotNull 'PGint8)
1422
1423
1423 -- | >>> :{
1424 -- | >>> :{
1424 -- let
1425 -- let
@@ -1429,8 +1430,8 @@ class Aggregate expr aggr | aggr -> expr where
1429 -- sum(DISTINCT "col")
1430 -- sum(DISTINCT "col")
1430 sum_
1431 sum_
1431 :: ty `In` PGNum
1432 :: ty `In` PGNum
1432− => expr commons schemas params from (nullity ty)
1433+ => expr1 (nullity ty)
1433− -> aggr commons schemas params from (nullity ty)
1434+ -> aggr (nullity ty)
1434
1435
1435 -- | >>> :{
1436 -- | >>> :{
1436 -- let
1437 -- let
@@ -1441,9 +1442,9 @@ class Aggregate expr aggr | aggr -> expr where
1441 -- bit_and(DISTINCT "col")
1442 -- bit_and(DISTINCT "col")
1442 bitAnd
1443 bitAnd
1443 :: int `In` PGIntegral
1444 :: int `In` PGIntegral
1444− => expr commons schemas params from (nullity int)
1445+ => expr1 (nullity int)
1445 -- ^ what to aggregate
1446 -- ^ what to aggregate
1446− -> aggr commons schemas params from (nullity int)
1447+ -> aggr (nullity int)
1447
1448
1448 -- | >>> :{
1449 -- | >>> :{
1449 -- let
1450 -- let
@@ -1454,9 +1455,9 @@ class Aggregate expr aggr | aggr -> expr where
1454 -- bit_or(ALL "col")
1455 -- bit_or(ALL "col")
1455 bitOr
1456 bitOr
1456 :: int `In` PGIntegral
1457 :: int `In` PGIntegral
1457− => expr commons schemas params from (nullity int)
1458+ => expr1 (nullity int)
1458 -- ^ what to aggregate
1459 -- ^ what to aggregate
1459− -> aggr commons schemas params from (nullity int)
1460+ -> aggr (nullity int)
1460
1461
1461 -- | >>> :{
1462 -- | >>> :{
1462 -- let
1463 -- let
@@ -1466,9 +1467,9 @@ class Aggregate expr aggr | aggr -> expr where
1466 -- :}
1467 -- :}
1467 -- bool_and(ALL "col")
1468 -- bool_and(ALL "col")
1468 boolAnd
1469 boolAnd
1469− :: expr commons schemas params from (nullity 'PGbool)
1470+ :: expr1 (nullity 'PGbool)
1470 -- ^ what to aggregate
1471 -- ^ what to aggregate
1471− -> aggr commons schemas params from (nullity 'PGbool)
1472+ -> aggr (nullity 'PGbool)
1472
1473
1473 -- | >>> :{
1474 -- | >>> :{
1474 -- let
1475 -- let
@@ -1478,9 +1479,9 @@ class Aggregate expr aggr | aggr -> expr where
1478 -- :}
1479 -- :}
1479 -- bool_or(ALL "col")
1480 -- bool_or(ALL "col")
1480 boolOr
1481 boolOr
1481− :: expr commons schemas params from (nullity 'PGbool)
1482+ :: expr1(nullity 'PGbool)
1482 -- ^ what to aggregate
1483 -- ^ what to aggregate
1483− -> aggr commons schemas params from (nullity 'PGbool)
1484+ -> aggr (nullity 'PGbool)
1484
1485
1485 -- | synonym for `boolAnd`
1486 -- | synonym for `boolAnd`
1486 --
1487 --
@@ -1492,68 +1493,73 @@ class Aggregate expr aggr | aggr -> expr where
1492 -- :}
1493 -- :}
1493 -- every(DISTINCT "col")
1494 -- every(DISTINCT "col")
1494 every
1495 every
1495− :: expr commons schemas params from (nullity 'PGbool)
1496+ :: expr1 (nullity 'PGbool)
1496 -- ^ what to aggregate
1497 -- ^ what to aggregate
1497− -> aggr commons schemas params from (nullity 'PGbool)
1498+ -> aggr (nullity 'PGbool)
1498
1499
1499 -- | maximum aggregation
1500 -- | maximum aggregation
1500 max_
1501 max_
1501− :: expr commons schemas params from (nullity ty)
1502+ :: expr1 (nullity ty)
1502 -- ^ what to maximize
1503 -- ^ what to maximize
1503− -> aggr commons schemas params from (nullity ty)
1504+ -> aggr (nullity ty)
1504
1505
1505 -- | minimum aggregation
1506 -- | minimum aggregation
1506 min_
1507 min_
1507− :: expr commons schemas params from (nullity ty)
1508+ :: expr1 (nullity ty)
1508 -- ^ what to minimize
1509 -- ^ what to minimize
1509− -> aggr commons schemas params from (nullity ty)
1510+ -> aggr (nullity ty)
1510
1511
1511 -- | average aggregation
1512 -- | average aggregation
1512 avg
1513 avg
1513− :: expr commons schemas params from (nullity ty)
1514+ :: expr1 (nullity ty)
1514 -- ^ what to average
1515 -- ^ what to average
1515− -> aggr commons schemas params from (nullity (PGAvg ty))
1516+ -> aggr (nullity (PGAvg ty))
1516
1517
1517−data Distinction commons schemas params from ty
1518+data Distinction expr ty
1518− = All (Expression 'Ungrouped commons schemas params from ty)
1519+ = All (expr ty)
1519− | Distinct (Expression 'Ungrouped commons schemas params from ty)
1520+ | Distinct (expr ty)
1520 deriving (GHC.Generic,Show,Eq,Ord)
1521 deriving (GHC.Generic,Show,Eq,Ord)
1521−instance NFData (Distinction commons schemas params from ty)
1522+instance NFData (Distinction (Expression grp commons schemas params from) ty)
1522−instance RenderSQL (Distinction commons schemas params from ty) where
1523+instance RenderSQL (Distinction (Expression grp commons schemas params from) ty) where
1523 renderSQL = \case
1524 renderSQL = \case
1524 All x -> "ALL" <+> renderSQL x
1525 All x -> "ALL" <+> renderSQL x
1525 Distinct x -> "DISTINCT" <+> renderSQL x
1526 Distinct x -> "DISTINCT" <+> renderSQL x
1526
1527
1527−instance Aggregate Distinction (Expression ('Grouped bys)) where
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
1528− countStar = UnsafeExpression "count(*)"
1532+ countStar = UnsafeExpression "count(*)"
1529− count = unsafeAggregate "count"
1533+ count = unsafeAggregate "count"
1530− sum_ = unsafeAggregate "sum"
1534+ sum_ = unsafeAggregate "sum"
1531− bitAnd = unsafeAggregate "bit_and"
1535+ bitAnd = unsafeAggregate "bit_and"
1532− bitOr = unsafeAggregate "bit_or"
1536+ bitOr = unsafeAggregate "bit_or"
1533− boolAnd = unsafeAggregate "bool_and"
1537+ boolAnd = unsafeAggregate "bool_and"
1534− boolOr = unsafeAggregate "bool_or"
1538+ boolOr = unsafeAggregate "bool_or"
1535− every = unsafeAggregate "every"
1539+ every = unsafeAggregate "every"
1536− max_ = unsafeAggregate "max"
1540+ max_ = unsafeAggregate "max"
1537− min_ = unsafeAggregate "min"
1541+ min_ = unsafeAggregate "min"
1538− avg = unsafeAggregate "avg"
1542+ avg = unsafeAggregate "avg"
1539−instance Aggregate (Expression grp) (WindowFunction grp) where
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
1540− countStar = UnsafeWindowFunction "count(*)"
1547+ countStar = UnsafeWindowFunction "count(*)"
1541− count = unsafeWindowFunction1 "count"
1548+ count = unsafeWindowFunction1 "count"
1542− sum_ = unsafeWindowFunction1 "sum"
1549+ sum_ = unsafeWindowFunction1 "sum"
1543− bitAnd = unsafeWindowFunction1 "bit_and"
1550+ bitAnd = unsafeWindowFunction1 "bit_and"
1544− bitOr = unsafeWindowFunction1 "bit_or"
1551+ bitOr = unsafeWindowFunction1 "bit_or"
1545− boolAnd = unsafeWindowFunction1 "bool_and"
1552+ boolAnd = unsafeWindowFunction1 "bool_and"
1546− boolOr = unsafeWindowFunction1 "bool_or"
1553+ boolOr = unsafeWindowFunction1 "bool_or"
1547− every = unsafeWindowFunction1 "every"
1554+ every = unsafeWindowFunction1 "every"
1548− max_ = unsafeWindowFunction1 "max"
1555+ max_ = unsafeWindowFunction1 "max"
1549− min_ = unsafeWindowFunction1 "min"
1556+ min_ = unsafeWindowFunction1 "min"
1550− avg = unsafeWindowFunction1 "avg"
1557+ avg = unsafeWindowFunction1 "avg"
1551
1558
1552-- | escape hatch to define aggregate functions
1559-- | escape hatch to define aggregate functions
1553unsafeAggregate
1560unsafeAggregate
1554− :: RenderSQL (expr commons schemas params from xty)
1555− => ByteString -- ^ aggregate function
1561+ :: ByteString -- ^ aggregate function
1556− -> expr commons schemas params from xty
1562+ -> Distinction (Expression 'Ungrouped commons schemas params from) xty
1557 -> Expression ('Grouped bys) commons schemas params from yty
1563 -> Expression ('Grouped bys) commons schemas params from yty
1558unsafeAggregate fun x = UnsafeExpression $ mconcat
1564unsafeAggregate fun x = UnsafeExpression $ mconcat
1559 [fun, "(", renderSQL x, ")"]
1565 [fun, "(", renderSQL x, ")"]