forked from graphile/pg-aggregates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AggregateSpecsPlugin.ts
216 lines (201 loc) · 7.91 KB
/
AggregateSpecsPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import type { Plugin } from "graphile-build";
import type { PgType } from "graphile-build-pg";
import { AggregateSpec, AggregateGroupBySpec } from "./interfaces";
const TIMESTAMP_OID = "1114";
const TIMESTAMPTZ_OID = "1184";
const SMALLINT_OID = "21";
const BIGINT_OID = "20";
const INTEGER_OID = "23";
const NUMERIC_OID = "1700";
const REAL_OID = "700";
const DOUBLE_PRECISION_OID = "701";
const INTERVAL_OID = "1186";
const MONEY_OID = "790";
const AggregateSpecsPlugin: Plugin = (builder) => {
builder.hook("build", (build) => {
const { pgSql: sql } = build;
const isNumberLike = (pgType: PgType): boolean => pgType.category === "N";
/** Maps from the data type of the column to the data type of the sum aggregate */
/** BigFloat is our fallback type; it should be valid for almost all numeric types */
const convertWithMapAndFallback = (
dataTypeToAggregateTypeMap: { [key: string]: string },
fallback: string
) => {
return (
pgType: PgType,
_pgTypeModifier: number | string | null
): [PgType, null | number | string] => {
const targetTypeId = dataTypeToAggregateTypeMap[pgType.id] || fallback;
const targetType = build.pgIntrospectionResultsByKind.type.find(
(t: PgType) => t.id === targetTypeId
);
if (!targetType) {
throw new Error(
`Could not find PostgreSQL type with oid '${targetTypeId}' whilst processing aggregate.`
);
}
return [targetType, null];
};
};
const pgAggregateSpecs: AggregateSpec[] = [
{
id: "sum",
humanLabel: "sum",
HumanLabel: "Sum",
isSuitableType: isNumberLike,
// I've wrapped it in `coalesce` so that it cannot be null
sqlAggregateWrap: (sqlFrag) =>
sql.fragment`coalesce(sum(${sqlFrag}), 0)`,
isNonNull: true,
// A SUM(...) often ends up significantly larger than any individual
// value; see
// https://www.postgresql.org/docs/current/functions-aggregate.html for
// how the sum aggregate changes result type.
pgTypeAndModifierModifier: convertWithMapAndFallback(
{
[SMALLINT_OID]: BIGINT_OID, // smallint -> bigint
[INTEGER_OID]: BIGINT_OID, // integer -> bigint
[BIGINT_OID]: NUMERIC_OID, // bigint -> numeric
[REAL_OID]: REAL_OID, // real -> real
[DOUBLE_PRECISION_OID]: DOUBLE_PRECISION_OID, // double precision -> double precision
[INTERVAL_OID]: INTERVAL_OID, // interval -> interval
[MONEY_OID]: MONEY_OID, // money -> money
},
NUMERIC_OID /* numeric */
),
},
{
id: "distinctCount",
humanLabel: "distinct count",
HumanLabel: "Distinct count",
isSuitableType: () => true,
sqlAggregateWrap: (sqlFrag) => sql.fragment`count(distinct ${sqlFrag})`,
pgTypeAndModifierModifier: convertWithMapAndFallback(
{},
BIGINT_OID /* always use bigint */
),
},
{
id: "min",
humanLabel: "minimum",
HumanLabel: "Minimum",
isSuitableType: isNumberLike,
sqlAggregateWrap: (sqlFrag) => sql.fragment`min(${sqlFrag})`,
},
{
id: "max",
humanLabel: "maximum",
HumanLabel: "Maximum",
isSuitableType: isNumberLike,
sqlAggregateWrap: (sqlFrag) => sql.fragment`max(${sqlFrag})`,
},
{
id: "average",
humanLabel: "mean average",
HumanLabel: "Mean average",
isSuitableType: isNumberLike,
sqlAggregateWrap: (sqlFrag) => sql.fragment`avg(${sqlFrag})`,
// An AVG(...) ends up more precise than any individual value; see
// https://www.postgresql.org/docs/current/functions-aggregate.html for
// how the avg aggregate changes result type.
pgTypeAndModifierModifier: convertWithMapAndFallback(
{
[SMALLINT_OID]: NUMERIC_OID, // smallint -> numeric
[INTEGER_OID]: NUMERIC_OID, // integer -> numeric
[BIGINT_OID]: NUMERIC_OID, // bigint -> numeric
[NUMERIC_OID]: NUMERIC_OID, // numeric -> numeric
[REAL_OID]: DOUBLE_PRECISION_OID, // real -> double precision
[DOUBLE_PRECISION_OID]: DOUBLE_PRECISION_OID, // double precision -> double precision
[INTERVAL_OID]: INTERVAL_OID, // interval -> interval
},
"1700" /* numeric */
),
},
{
id: "stddevSample",
humanLabel: "sample standard deviation",
HumanLabel: "Sample standard deviation",
isSuitableType: isNumberLike,
sqlAggregateWrap: (sqlFrag) => sql.fragment`stddev_samp(${sqlFrag})`,
// See https://www.postgresql.org/docs/current/functions-aggregate.html
// for how this aggregate changes result type.
pgTypeAndModifierModifier: convertWithMapAndFallback(
{
[REAL_OID]: DOUBLE_PRECISION_OID, // real -> double precision
[DOUBLE_PRECISION_OID]: DOUBLE_PRECISION_OID, // double precision -> double precision
},
NUMERIC_OID /* numeric */
),
},
{
id: "stddevPopulation",
humanLabel: "population standard deviation",
HumanLabel: "Population standard deviation",
isSuitableType: isNumberLike,
sqlAggregateWrap: (sqlFrag) => sql.fragment`stddev_pop(${sqlFrag})`,
// See https://www.postgresql.org/docs/current/functions-aggregate.html
// for how this aggregate changes result type.
pgTypeAndModifierModifier: convertWithMapAndFallback(
{
[REAL_OID]: DOUBLE_PRECISION_OID, // real -> double precision
[DOUBLE_PRECISION_OID]: DOUBLE_PRECISION_OID, // double precision -> double precision
},
NUMERIC_OID /* numeric */
),
},
{
id: "varianceSample",
humanLabel: "sample variance",
HumanLabel: "Sample variance",
isSuitableType: isNumberLike,
sqlAggregateWrap: (sqlFrag) => sql.fragment`var_samp(${sqlFrag})`,
// See https://www.postgresql.org/docs/current/functions-aggregate.html
// for how this aggregate changes result type.
pgTypeAndModifierModifier: convertWithMapAndFallback(
{
[REAL_OID]: DOUBLE_PRECISION_OID, // real -> double precision
[DOUBLE_PRECISION_OID]: DOUBLE_PRECISION_OID, // double precision -> double precision
},
NUMERIC_OID /* numeric */
),
},
{
id: "variancePopulation",
humanLabel: "population variance",
HumanLabel: "Population variance",
isSuitableType: isNumberLike,
sqlAggregateWrap: (sqlFrag) => sql.fragment`var_pop(${sqlFrag})`,
// See https://www.postgresql.org/docs/current/functions-aggregate.html
// for how this aggregate changes result type.
pgTypeAndModifierModifier: convertWithMapAndFallback(
{
[REAL_OID]: DOUBLE_PRECISION_OID, // real -> double precision
[DOUBLE_PRECISION_OID]: DOUBLE_PRECISION_OID, // double precision -> double precision
},
NUMERIC_OID /* numeric */
),
},
];
const pgAggregateGroupBySpecs: AggregateGroupBySpec[] = [
{
id: "truncated-to-hour",
isSuitableType: (pgType) =>
/* timestamp or timestamptz */
pgType.id === TIMESTAMP_OID || pgType.id === TIMESTAMPTZ_OID,
sqlWrap: (sqlFrag) => sql.fragment`date_trunc('hour', ${sqlFrag})`,
},
{
id: "truncated-to-day",
isSuitableType: (pgType) =>
/* timestamp or timestamptz */
pgType.id === TIMESTAMP_OID || pgType.id === TIMESTAMPTZ_OID,
sqlWrap: (sqlFrag) => sql.fragment`date_trunc('day', ${sqlFrag})`,
},
];
return build.extend(build, {
pgAggregateSpecs,
pgAggregateGroupBySpecs,
});
});
};
export default AggregateSpecsPlugin;