on dev help sign in
more bivariate stats
commitc3c9a0e01b2497d3cf9fcaf0baed4de7cdb63cb0 authorEitan Chatav <eitan.chatav@gmail.com> dateMon 28 Jan 2019, 23:15 (8 years ago) parent574aeb2
1 file changed+80−12
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+80−12
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+80−12
@@ -1515,6 +1515,31 @@ class Aggregate expr1 expr2 aggr
15151515 -- ^ what to average
15161516 -> aggr (nullity (PGAvg ty))
15171517
1518+ -- | correlation coefficient
1519+ corr
1520+ :: expr2 (nullity 'PGfloat8)
1521+ -> aggr (nullity 'PGfloat8)
1522+
1523+ -- | population covariance
1524+ covarPop
1525+ :: expr2 (nullity 'PGfloat8)
1526+ -> aggr (nullity 'PGfloat8)
1527+
1528+ -- | sample covariance
1529+ covarSamp
1530+ :: expr2 (nullity 'PGfloat8)
1531+ -> aggr (nullity 'PGfloat8)
1532+
1533+ -- | average of the independent variable (sum(X)/N)
1534+ regrAvgX
1535+ :: expr2 (nullity 'PGfloat8)
1536+ -> aggr (nullity 'PGfloat8)
1537+
1538+ -- | average of the independent variable (sum(Y)/N)
1539+ regrAvgY
1540+ :: expr2 (nullity 'PGfloat8)
1541+ -> aggr (nullity 'PGfloat8)
1542+
15181543data Distinction expr ty
15191544 = All (expr ty)
15201545 | Distinct (expr ty)
@@ -1524,22 +1549,37 @@ instance RenderSQL (Distinction (Expression grp commons schemas params from) ty)
15241549 renderSQL = \case
15251550 All x -> "ALL" <+> renderSQL x
15261551 Distinct x -> "DISTINCT" <+> renderSQL x
1552+instance RenderSQL
1553+ (Distinction
1554+ ( Expression grp commons schemas params from
1555+ GHC.:*:
1556+ Expression grp commons schemas params from) xty ) where
1557+ renderSQL = \case
1558+ All (x GHC.:*: x') ->
1559+ "ALL" <+> renderSQL x <> ", " <> renderSQL x'
1560+ Distinct (x GHC.:*: x') ->
1561+ "DISTINCT" <+> renderSQL x <> ", " <> renderSQL x'
15271562
15281563instance Aggregate
15291564 (Distinction (Expression 'Ungrouped commons schemas params from))
15301565 (Distinction (Expression 'Ungrouped commons schemas params from GHC.:*: Expression 'Ungrouped commons schemas params from))
15311566 (Expression ('Grouped bys) commons schemas params from) where
15321567 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"
1568+ count = unsafeAggregate1 "count"
1569+ sum_ = unsafeAggregate1 "sum"
1570+ bitAnd = unsafeAggregate1 "bit_and"
1571+ bitOr = unsafeAggregate1 "bit_or"
1572+ boolAnd = unsafeAggregate1 "bool_and"
1573+ boolOr = unsafeAggregate1 "bool_or"
1574+ every = unsafeAggregate1 "every"
1575+ max_ = unsafeAggregate1 "max"
1576+ min_ = unsafeAggregate1 "min"
1577+ avg = unsafeAggregate1 "avg"
1578+ corr = unsafeAggregate2 "corr"
1579+ covarPop = unsafeAggregate2 "covar_pop"
1580+ covarSamp = unsafeAggregate2 "covar_samp"
1581+ regrAvgX = unsafeAggregate2 "regr_avgx"
1582+ regrAvgY = unsafeAggregate2 "regr_avgy"
15431583instance Aggregate
15441584 (Expression grp commons schemas params from)
15451585 (Expression grp commons schemas params from GHC.:*: Expression grp commons schemas params from)
@@ -1555,15 +1595,28 @@ instance Aggregate
15551595 max_ = unsafeWindowFunction1 "max"
15561596 min_ = unsafeWindowFunction1 "min"
15571597 avg = unsafeWindowFunction1 "avg"
1598+ corr = unsafeWindowFunction2 "corr"
1599+ covarPop = unsafeWindowFunction2 "covar_pop"
1600+ covarSamp = unsafeWindowFunction2 "covar_samp"
1601+ regrAvgX = unsafeWindowFunction2 "regr_avgx"
1602+ regrAvgY = unsafeWindowFunction2 "regr_avgy"
15581603
15591604-- | escape hatch to define aggregate functions
1560−unsafeAggregate
1605+unsafeAggregate1
15611606 :: ByteString -- ^ aggregate function
15621607 -> Distinction (Expression 'Ungrouped commons schemas params from) xty
15631608 -> Expression ('Grouped bys) commons schemas params from yty
1564−unsafeAggregate fun x = UnsafeExpression $ mconcat
1609+unsafeAggregate1 fun x = UnsafeExpression $ mconcat
15651610 [fun, "(", renderSQL x, ")"]
15661611
1612+-- | escape hatch to define aggregate binary functions
1613+unsafeAggregate2
1614+ :: ByteString -- ^ aggregate function
1615+ -> Distinction (Expression 'Ungrouped commons schemas params from GHC.:*: Expression 'Ungrouped commons schemas params from) xty
1616+ -> Expression ('Grouped bys) commons schemas params from yty
1617+unsafeAggregate2 fun xx = UnsafeExpression $ mconcat
1618+ [fun, "(", renderSQL xx, ")"]
1619+
15671620-- | A type family that calculates `PGAvg` type of a `PGType`.
15681621type family PGAvg ty where
15691622 PGAvg 'PGint2 = 'PGnumeric
@@ -1843,6 +1896,21 @@ unsafeWindowFunction1
18431896unsafeWindowFunction1 fun x
18441897 = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x)
18451898
1899+unsafeWindowFunction2
1900+ :: ByteString
1901+ -> (Expression grp commons schemas params from GHC.:*: Expression grp commons schemas params from) ty0
1902+ -> WindowFunction grp commons schemas params from ty1
1903+unsafeWindowFunction2 fun (x GHC.:*: x')
1904+ = UnsafeWindowFunction $ fun <> parenthesized (renderSQL x <> ", " <> renderSQL x')
1905+
1906+-- | escape hatch to define aggregate binary functions
1907+-- unsafeAggregate2
1908+-- :: ByteString -- ^ aggregate function
1909+-- -> Distinction (Expression 'Ungrouped commons schemas params from GHC.:*: Expression 'Ungrouped commons schemas params from) xty
1910+-- -> Expression ('Grouped bys) commons schemas params from yty
1911+-- unsafeAggregate2 fun xx = UnsafeExpression $ mconcat
1912+-- [fun, "(", renderSQL xx, ")"]
1913+
18461914{- | rank of the current row with gaps; same as `rowNumber` of its first peer
18471915>>> printSQL rank
18481916rank()