deno.land / x / pg_mem@2.8.1 / transforms / aggregation.ts

aggregation.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import { TransformBase } from './transform-base.ts';import { _ISelection, _Transaction, IValue, _IIndex, _Explainer, _SelectExplanation, _IType, IndexKey, _ITable, Stats, AggregationComputer, AggregationGroupComputer, setId, _IAggregation } from '../interfaces-private.ts';import { Expr, ExprRef, ExprCall } from 'https://deno.land/x/pgsql_ast_parser@12.0.1/mod.ts';import { buildValue } from '../parser/expression-builder.ts';import { nil, NotSupported } from '../interfaces.ts';import hash from 'https://deno.land/x/object_hash@2.0.3.1/mod.ts';import { Evaluator } from '../evaluator.ts';import { buildCount } from './aggregations/count.ts';import { buildMinMax } from './aggregations/max-min.ts';import { buildSum } from './aggregations/sum.ts';import { buildArrayAgg } from './aggregations/array_agg.ts';import { buildAvg } from './aggregations/avg.ts';import { Selection } from './selection.ts';import { buildCtx, withSelection } from '../parser/context.ts';import { buildJsonAgg } from './aggregations/json_aggs.ts';import { nullIsh } from '../utils.ts';import { buildBoolAgg } from './aggregations/bool-aggregs.ts';
export const aggregationFunctions = new Set([ 'array_agg', 'avg', 'bit_and', 'bit_or', 'bool_and', 'bool_or', 'count', 'every', 'json_agg', 'jsonb_agg', 'json_object_agg', 'jsonb_object_agg', 'max', 'min', 'string_agg', 'sum', 'xmlagg',])
export function buildGroupBy(on: _ISelection, groupBy: Expr[]) { const agg = new Aggregation(on, groupBy); return agg;}
let idCnt = 0;
export function getAggregator(): _IAggregation | null { const on = buildCtx().selection; if (!on) { return null; } if (on.isAggregation()) { return on; } if (!(on instanceof Selection)) { return null; } if (!(on.base.isAggregation())) { return null; } return on.base;}
type AggregItem = any;
interface AggregationInstance { getter: IValue; id: symbol; computer: AggregationComputer; distinct: IValue[] | nil;}
function isIntegralType(value: any): boolean { return typeof value === 'number' || typeof value === 'string' || value instanceof Date;}
export class Aggregation<T> extends TransformBase<T> implements _ISelection<T>, _IAggregation {
columns: readonly IValue<any>[]; /** * Group-by values * - key: column in source hash * - value: column in this, evaluated against temporary entity. **/ private readonly symbol = Symbol(); private readonly aggId = idCnt++; private readonly groupIndex?: _IIndex<any> | nil; private aggregations = new Map<string, AggregationInstance>();
/** How to get grouping values on the base table raw items (not on "this.enumerate()" raw items) */ private groupingValuesOnbase: IValue[];
/** How to get the grouped values on "this.enumerate()" raw items output */ private groupByMapping = new Map<string, IValue>();
isAggregation() { return true; }
constructor(on: _ISelection, _groupedBy: Expr[]) { super(on);

// === preassign columns that are reachable (grouped by) this.groupingValuesOnbase = withSelection(on, () => _groupedBy.map(x => buildValue(x))); for (let _i = 0; _i < this.groupingValuesOnbase.length; _i++) { const i = _i; const g = this.groupingValuesOnbase[i]; this.groupByMapping.set(g.hash!, new Evaluator( g.type , g.id , g.hash! , [g] // keys are stored wrapped in a symbol (because not necessarily selected) , v => v[this.symbol][i] )); }
// try to find an index matching our groupby clause this.groupIndex = on.getIndex(...this.groupingValuesOnbase); this.columns = []; }
getColumn(column: string | ExprRef): IValue; getColumn(column: string | ExprRef, nullIfNotFound?: boolean): IValue | nil; getColumn(column: string | ExprRef, nullIfNotFound?: boolean): IValue<any> | nil { return this.base.getColumn(column, nullIfNotFound); }
checkIfIsKey(got: IValue<any>): IValue<any> { return this.groupByMapping.get(got.hash!) ?? got; }

entropy(t: _Transaction): number { return this.groupByMapping.size || 1; }
stats(): Stats | null { // cannot be computed without iterating return null; }
*enumerate(t: _Transaction): Iterable<T> { for (const item of this._enumerateAggregationKeys(t)) { const sym = item[this.symbol]; setId(item, `agg_${this.aggId}_${hash(sym)}`) yield item as any; } }
private _enumerateAggregationKeys(t: _Transaction): Iterable<AggregItem> { // ===== try to compute directly (will only succeed when no grouping, and simple statements like count(*)) const ret = this.computeDirect(t); if (ret) { return [ret]; }
// ===== try to compute base on index const fromIndex = this.iterateFromIndex(t); if (fromIndex) { return fromIndex; }
// ==== seq-scan computation return this.seqScan(t); }
private iterateFromIndex(t: _Transaction): AggregItem[] | null { if (!this.groupIndex) { return null; } const aggs = [...this.aggregations.values()]; const allByGroup = !aggs.some(x => !x.computer.computeFromIndex);
if (!allByGroup) { return null; }
const indexKeys = this.groupIndex.iterateKeys(t); if (!indexKeys) { return null; }
// iterate all index keys const computed: AggregItem[] = [] for (const k of indexKeys) { const ret: any = { [this.symbol]: k }; // try to compute from index for (const agg of aggs) { const val = agg.computer.computeFromIndex?.(k, this.groupIndex, t); if (typeof val === 'undefined') { if (computed.length) { throw new Error('Compute from index has succeeded on an index key, but failed on another (which must not happen)'); } return null; } ret[agg.id] = val; } computed.push(ret); } return computed; }

private *seqScan(t: _Transaction): Iterable<AggregItem> { const aggs = [...this.aggregations.values()]; const groups = new Map<string, { key: IndexKey; aggs: { computer: AggregationGroupComputer; distinctHash: Set<any>; instance: AggregationInstance, }[]; }>();
// === feed all items for (const item of this.base.enumerate(t)) { // get group-by values const key: IndexKey = this.groupingValuesOnbase.map(g => g.get(item, t));
// add group if necessary const groupingKey = hash(key); let group = groups.get(groupingKey); if (!group) { groups.set(groupingKey, group = { key, aggs: aggs.map(x => ({ computer: x.computer.createGroup(t), instance: x, distinctHash: new Set(), })), }); }
// process aggregators in group for (const g of group.aggs) { if (!g.computer) { continue; } if (g.instance.distinct) { const distinctValues = g.instance.distinct.map(x => x.get(item, t)); if (distinctValues.length === 1 && nullIsh(distinctValues[0])) { // ignore single nulls. continue; } let valuesHash: any; if (distinctValues.length === 1 && isIntegralType(distinctValues[0])) { // optimization to avoid hashing on objects supported by "Set" valuesHash = distinctValues[0]; } else { valuesHash = hash(distinctValues); } if (g.distinctHash.has(valuesHash)) { continue; } g.distinctHash.add(valuesHash); } g.computer.feedItem(item); } }
// if this.base is empty, and this is not a group by... // 👉 Must return a result. // ex: // - select max(a) from empty 👉 [{max: null}] // - select max(a) from empty group by id 👉 [] if (groups.size === 0 && !this.groupingValuesOnbase.length) { const key: IndexKey = []; const groupingKey = hash(key); groups.set(groupingKey, { key, aggs: aggs.map(x => ({ computer: x.computer.createGroup(t), instance: x, distinctHash: new Set(), })), }); }
// === return results for (const g of groups.values()) { const ret: AggregItem = { // specify the grouping key [this.symbol]: g.key };
// fill aggregations values for (const { instance: { id }, computer } of g.aggs) { ret[id] = computer.finish() ?? null; } yield ret; } }
private computeDirect(t: _Transaction): AggregItem | null { // When there is no grouping... if (this.groupByMapping.size) { return null; } // check if all selected aggregations can be computed directly (typically: count(*)) const aggs = [...this.aggregations.values()]; const allNoGroup = !aggs.some(x => !x.computer.computeNoGroup); if (!allNoGroup) { return null; } const ret: AggregItem = { [this.symbol]: [], }; for (const agg of this.aggregations.values()) { const val = agg.computer.computeNoGroup?.(t); if (typeof val === 'undefined') { return null; } ret[agg.id] = val; } return ret; }
getAggregation(name: string, call: ExprCall): IValue { const hashed = hash(call); const agg = this.aggregations.get(hashed); if (agg) { return agg.getter; } const got = this._getAggregation(name, call);
const id = Symbol(name); const getter = new Evaluator( got.type , null , hashed , [] , raw => raw[id] , { forceNotConstant: true, });
let distinct: IValue[] | null = null; if (call.distinct === 'distinct') { if (call.args.length === 1 && call.args[0].type === 'list') { // hack in case we get a record-like thing - ex: select count(distinct (a,b)) // cf UT behaves nicely with nulls on multiple count distinct = call.args[0].expressions.map(x => buildValue(x)); } else { distinct = call.args.map(x => buildValue(x)); } }
this.aggregations.set(hashed, { id, getter, computer: got, distinct, }); return getter; }
private _getAggregation(name: string, call: ExprCall): AggregationComputer { switch (name) { case 'count': return buildCount(this.base, call); case 'max': case 'min': return buildMinMax(this.base, call.args, name); case 'sum': return buildSum(this.base, call); case 'array_agg': return buildArrayAgg(this.base, call); case 'avg': return buildAvg(this.base, call); case 'jsonb_agg': case 'json_agg': return buildJsonAgg(this.base, call, name); case 'bool_and': case 'bool_or': return buildBoolAgg(this.base, call, name); default: throw new NotSupported('aggregation function ' + name); } }

hasItem(value: T, t: _Transaction): boolean { return !!(value as any)[this.symbol]; }
getIndex(forValue: IValue<any>): _IIndex<any> | nil { // there is no index on aggregations return null; }
explain(e: _Explainer): _SelectExplanation { return { _: 'aggregate', id: e.idFor(this), aggregator: null as any, } }
}
pg_mem

Version Info

Tagged at
4 months ago