-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathgroup_and_combine.ts
428 lines (399 loc) · 12.7 KB
/
group_and_combine.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { KV } from "../values";
import {
PTransform,
PTransformClass,
withName,
extractName,
} from "./transform";
import { flatten } from "./flatten";
import { PCollection } from "../pvalue";
import { P } from "../pvalue";
import { Coder } from "../coders/coders";
import * as internal from "./internal";
import { count } from "./combiners";
import { requireForSerialization } from "../serialization";
import { packageName } from "../utils/packageJson";
// TODO: (API) Consider groupBy as a top-level method on PCollections.
// TBD how to best express the combiners.
// - Idea 1: We could allow these as extra arguments to groupBy
// - Idea 2: We could return a special GroupedPCollection that has a nice,
// chain-able combining() method. We'd want the intermediates to
// still be usable, but lazy.
/**
* An interface for performing possibly distributed, commutative, associative
* combines (such as sum) to a set of values
* (e.g. in `groupBy(...).combining(...)`.
*
* Several implementations (such as summation) are provided in the combiners
* module.
*
* See also https://beam.apache.org/documentation/programming-guide/#transforms
*/
export interface CombineFn<I, A, O> {
createAccumulator: () => A;
addInput: (A, I) => A;
mergeAccumulators: (accumulators: Iterable<A>) => A;
extractOutput: (A) => O;
accumulatorCoder?(inputCoder: Coder<I>): Coder<A>;
}
// TODO: (Typescript) When typing this as ((a: I, b: I) => I), types are not inferred well.
type Combiner<I> = CombineFn<I, any, any> | ((a: any, b: any) => any);
/**
* A PTransformClass that takes a PCollection of elements, and returns a PCollection
* of elements grouped by a field, multiple fields, an expression that is used
* as the grouping key.
*
* @extends PTransformClass
*/
export class GroupBy<T, K> extends PTransformClass<
PCollection<T>,
PCollection<KV<K, Iterable<T>>>
> {
keyFn: (element: T) => K;
keyNames: string | string[];
keyName: string;
/**
* Create a GroupBy transform.
*
* @param key: The name of the key in the JSON object, or a function that returns the key for a given element.
*/
constructor(
key: string | string[] | ((element: T) => K),
keyName: string | undefined = undefined,
) {
super();
[this.keyFn, this.keyNames] = extractFnAndName(key, keyName || "key");
// XXX: Actually use this.
this.keyName = typeof this.keyNames === "string" ? this.keyNames : "key";
}
/** @internal */
expand(input: PCollection<T>): PCollection<KV<K, Iterable<T>>> {
const keyFn = this.keyFn;
return input
.map((x) => ({ key: keyFn(x), value: x }))
.apply(internal.groupByKey());
}
combining<I>(
expr: string | ((element: T) => I),
combiner: Combiner<I>,
resultName: string,
) {
return withName(
extractName(this),
new GroupByAndCombine(this.keyFn, this.keyNames, []).combining(
expr,
combiner,
resultName,
),
);
}
}
/**
* Returns a PTransform that takes a PCollection of elements, and returns a
* PCollection of elements grouped by a field, multiple fields, an expression
* that is used as the grouping key.
*
* Various fields may be further aggregated with `CombineFns` by invoking
* `groupBy(...).combining(...)`.
*/
export function groupBy<T, K>(
key: string | string[] | ((element: T) => K),
keyName: string | undefined = undefined,
): GroupBy<T, K> {
return withName(
`groupBy(${extractName(key)}`,
new GroupBy<T, K>(key, keyName),
);
}
/**
* Groups all elements of the input PCollection together.
*
* This is generally used with one or more combining specifications, as one
* loses parallelization benefits in bringing all elements of a distributed
* PCollection together on a single machine.
*/
export class GroupGlobally<T> extends PTransformClass<
PCollection<T>,
PCollection<Iterable<T>>
> {
constructor() {
super();
}
/** @internal */
expand(input) {
return input.apply(new GroupBy((_) => null)).map((kv) => kv[1]);
}
combining<I>(
expr: string | ((element: T) => I),
combiner: Combiner<I>,
resultName: string,
) {
return withName(
extractName(this),
new GroupByAndCombine((_) => null, undefined, []).combining(
expr,
combiner,
resultName,
),
);
}
}
/**
* Returns a PTransform grouping all elements of the input PCollection together.
*
* This is generally used with one or more combining specifications, as one
* loses parallelization benefits in bringing all elements of a distributed
* PCollection together on a single machine.
*
* Various fields may be further aggregated with `CombineFns` by invoking
* `groupGlobally(...).combining(...)`.
*/
export function groupGlobally<T>() {
return new GroupGlobally<T>();
}
class GroupByAndCombine<T, O> extends PTransformClass<
PCollection<T>,
PCollection<O>
> {
keyFn: (element: T) => any;
keyNames: string | string[] | undefined;
combiners: CombineSpec<T, any, any>[];
constructor(
keyFn: (element: T) => any,
keyNames: string | string[] | undefined,
combiners: CombineSpec<T, any, any>[],
) {
super();
this.keyFn = keyFn;
this.keyNames = keyNames;
this.combiners = combiners;
}
// TODO: (Naming) Name this combine?
combining<I, O>(
expr: string | ((element: T) => I),
combiner: Combiner<I>,
resultName: string, // TODO: (Unique names) Optionally derive from expr and combineFn?
) {
return withName(
extractName(this),
new GroupByAndCombine(
this.keyFn,
this.keyNames,
this.combiners.concat([
{
expr: extractFn(expr),
combineFn: toCombineFn(combiner),
resultName: resultName,
},
]),
),
);
}
expand(input: PCollection<T>) {
const this_ = this;
return input
.map(function extractKeys(element) {
return {
key: this_.keyFn(element),
value: this_.combiners.map((c) => c.expr(element)),
};
})
.apply(
internal.combinePerKey(
multiCombineFn(this_.combiners.map((c) => c.combineFn)),
),
)
.map(function constructResult(kv) {
const result = {};
if (this_.keyNames === null || this_.keyNames === undefined) {
// Don't populate a key at all.
} else if (typeof this_.keyNames === "string") {
result[this_.keyNames] = kv.key;
} else {
for (let i = 0; i < this_.keyNames.length; i++) {
result[this_.keyNames[i]] = kv.key[i];
}
}
for (let i = 0; i < this_.combiners.length; i++) {
result[this_.combiners[i].resultName] = kv.value[i];
}
return result;
});
}
}
export function countPerElement<T>(): PTransform<
PCollection<T>,
PCollection<{ element: T; count: number }>
> {
return withName(
"countPerElement",
groupBy((e) => e, "element").combining((e) => e, count, "count"),
);
}
export function countGlobally<T>(): PTransform<
PCollection<T>,
PCollection<number>
> {
return withName("countGlobally", (input) =>
input
.apply(new GroupGlobally().combining((e) => e, count, "count"))
.map((o) => o.count),
);
}
function toCombineFn<I>(combiner: Combiner<I>): CombineFn<I, any, any> {
if (typeof combiner === "function") {
return binaryCombineFn<I>(combiner);
} else {
return combiner;
}
}
interface CombineSpec<T, I, O> {
expr: (T) => I;
combineFn: CombineFn<I, any, O>;
resultName: string;
}
/**
* Creates a CombineFn<I, ..., I> out of a binary operator (which must be
* commutative and associative).
*/
export function binaryCombineFn<I>(
combiner: (a: I, b: I) => I,
): CombineFn<I, I | undefined, I> {
return {
createAccumulator: () => undefined,
addInput: (a, b) => (a === undefined ? b : combiner(a, b)),
mergeAccumulators: (accs) =>
[...accs].filter((a) => a !== null && a !== undefined).reduce(combiner),
extractOutput: (a) => a,
};
}
// TODO: (Typescript) Is there a way to indicate type parameters match the above?
function multiCombineFn(
combineFns: CombineFn<any, any, any>[],
batchSize: number = 100,
): CombineFn<any[], any[], any[]> {
return {
createAccumulator: () => combineFns.map((fn) => fn.createAccumulator()),
addInput: (accumulators: any[], inputs: any[]) => {
// TODO: (Cleanup) Does javascript have a clean zip?
let result: any[] = [];
for (let i = 0; i < combineFns.length; i++) {
result.push(combineFns[i].addInput(accumulators[i], inputs[i]));
}
return result;
},
mergeAccumulators: (accumulators: Iterable<any[]>) => {
let batches = combineFns.map((fn) => [fn.createAccumulator()]);
for (let acc of accumulators) {
for (let i = 0; i < combineFns.length; i++) {
batches[i].push(acc[i]);
if (batches[i].length > batchSize) {
batches[i] = [combineFns[i].mergeAccumulators(batches[i])];
}
}
}
for (let i = 0; i < combineFns.length; i++) {
if (batches[i].length > 1) {
batches[i] = [combineFns[i].mergeAccumulators(batches[i])];
}
}
return batches.map((batch) => batch[0]);
},
extractOutput: (accumulators: any[]) => {
// TODO: (Cleanup) Does javascript have a clean zip?
let result: any[] = [];
for (let i = 0; i < combineFns.length; i++) {
result.push(combineFns[i].extractOutput(accumulators[i]));
}
return result;
},
};
}
// TODO: Consider adding valueFn(s) rather than using the full value.
export function coGroupBy<T, K>(
key: string | string[] | ((element: T) => K),
keyName: string | undefined = undefined,
): PTransform<
{ [key: string]: PCollection<any> },
PCollection<{ key: K; values: { [key: string]: Iterable<any> } }>
> {
return withName(
`coGroupBy(${extractName(key)})`,
function coGroupBy(inputs: { [key: string]: PCollection<any> }) {
const [keyFn, keyNames] = extractFnAndName(key, keyName || "key");
keyName = typeof keyNames === "string" ? keyNames : "key";
const tags = [...Object.keys(inputs)];
const tagged = [...Object.entries(inputs)].map(([tag, pcoll]) =>
pcoll.map(
withName(`map[${tag}]`, (element) => ({
key: keyFn(element),
tag,
element,
})),
),
);
return P(tagged)
.apply(flatten())
.apply(groupBy("key"))
.map(function groupValues({ key, value }) {
const groupedValues: { [key: string]: any[] } = Object.fromEntries(
tags.map((tag) => [tag, []]),
);
for (const { tag, element } of value) {
groupedValues[tag].push(element);
}
return { key, values: groupedValues };
});
},
);
}
// TODO: (Typescript) Can I type T as "something that has this key" and/or,
// even better, ensure it has the correct type?
// Should be possible to get rid of the cast somehow.
// function extractFnAndName<T, P extends keyof T, K = T[P]>(
// extractor: P | P[] | ((element: T) => K),
// defaultName: P
// ): [(element: T) => K, P | P[]] {
function extractFnAndName<T, K>(
extractor: string | string[] | ((T) => K),
defaultName: string,
): [(T) => K, string | string[]] {
if (
typeof extractor === "string" ||
typeof extractor === "number" ||
typeof extractor === "symbol"
) {
return [(element: T) => element[extractor] as unknown as K, extractor];
} else if (extractor instanceof Array) {
return [
(element: T) => extractor.map((field) => element[field]) as any,
extractor,
];
} else {
return [extractor, defaultName];
}
}
function extractFn<T, K>(extractor: string | string[] | ((T) => K)) {
return extractFnAndName(extractor, undefined!)[0];
}
requireForSerialization(`${packageName}/transforms/group_and_combine`, exports);
requireForSerialization(`${packageName}/transforms/group_and_combine`, {
GroupByAndCombine: GroupByAndCombine,
});