-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement a basic
reduce
operator to support group-by
- Loading branch information
Showing
7 changed files
with
444 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import {Entity} from '../../../../generate.js'; | ||
import {Primitive} from '../../../ast/ast.js'; | ||
import {Entry} from '../../multiset.js'; | ||
|
||
// TODO: see comment on join. Maybe we can do better here with somem sort of sorted map. | ||
export class Index<K extends Primitive, V extends Entity> { | ||
readonly #index = new Map<K, Entry<V>[]>(); | ||
|
||
constructor() {} | ||
|
||
add(key: K, value: Entry<V>) { | ||
let existing = this.#index.get(key); | ||
if (existing === undefined) { | ||
existing = []; | ||
this.#index.set(key, existing); | ||
} | ||
existing.push(value); | ||
} | ||
|
||
extend(index: Index<K, V>) { | ||
for (const [key, value] of index.#index) { | ||
for (const entry of value) { | ||
this.add(key, entry); | ||
} | ||
} | ||
} | ||
|
||
get(key: K): Entry<V>[] { | ||
return this.#index.get(key) ?? []; | ||
} | ||
|
||
compact(keys: K[] = []) { | ||
function consolidateValues(values: Entry<V>[]): Entry<V>[] { | ||
const consolidated = new Map<string, Entry<V>>(); | ||
for (const [value, multiplicity] of values) { | ||
if (multiplicity === 0) { | ||
continue; | ||
} | ||
const existing = consolidated.get(value.id); | ||
if (existing === undefined) { | ||
consolidated.set(value.id, [value, multiplicity]); | ||
} else { | ||
const sum = existing[1] + multiplicity; | ||
if (sum === 0) { | ||
consolidated.delete(value.id); | ||
} else { | ||
consolidated.set(value.id, [value, sum]); | ||
} | ||
} | ||
} | ||
|
||
return [...consolidated.values()]; | ||
} | ||
|
||
// spread `keys` b/c if we do not then when we add below the iterator will continue. | ||
const iterableKeys = keys.length !== 0 ? keys : [...this.#index.keys()]; | ||
for (const key of iterableKeys) { | ||
const entries = this.#index.get(key); | ||
if (entries === undefined) { | ||
continue; | ||
} | ||
this.#index.delete(key); | ||
const consolidated = consolidateValues(entries); | ||
if (consolidated.length !== 0) { | ||
this.#index.set(key, consolidated); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import {expect, test} from 'vitest'; | ||
import {DifferenceStreamWriter} from '../difference-stream-writer.js'; | ||
import {ReduceOperator} from './reduce-operator.js'; | ||
import {Multiset} from '../../multiset.js'; | ||
import {NoOp} from './operator.js'; | ||
|
||
type Thing = { | ||
id: string; | ||
a: number; | ||
b: string; | ||
}; | ||
|
||
type Reduction = { | ||
id: string; | ||
sum: number; | ||
}; | ||
|
||
test('collects all things with the same key', () => { | ||
const inputWriter = new DifferenceStreamWriter<Thing>(); | ||
const inputReader = inputWriter.newReader(); | ||
const output = new DifferenceStreamWriter<Reduction>(); | ||
|
||
function getKey(t: Thing) { | ||
return t.b; | ||
} | ||
function getValueIdentity(t: Thing) { | ||
return t.id; | ||
} | ||
|
||
new ReduceOperator( | ||
inputReader, | ||
output, | ||
getValueIdentity, | ||
getKey, | ||
(group: Iterable<Thing>) => { | ||
let sum = 0; | ||
let id = ''; | ||
for (const item of group) { | ||
id = item.b; | ||
sum += item.a; | ||
} | ||
|
||
return { | ||
id, | ||
sum, | ||
}; | ||
}, | ||
); | ||
|
||
const outReader = output.newReader(); | ||
outReader.setOperator(new NoOp()); | ||
|
||
inputWriter.queueData([ | ||
1, | ||
new Multiset([ | ||
[ | ||
{ | ||
id: 'a', | ||
a: 1, | ||
b: 'x', | ||
}, | ||
1, | ||
], | ||
[ | ||
{ | ||
id: 'b', | ||
a: 2, | ||
b: 'x', | ||
}, | ||
2, | ||
], | ||
]), | ||
]); | ||
check([[{id: 'x', sum: 5}, 1]]); | ||
|
||
// retract an item | ||
inputWriter.queueData([ | ||
1, | ||
new Multiset([ | ||
[ | ||
{ | ||
id: 'a', | ||
a: 1, | ||
b: 'x', | ||
}, | ||
-1, | ||
], | ||
]), | ||
]); | ||
check([ | ||
[{id: 'x', sum: 5}, -1], | ||
[{id: 'x', sum: 4}, 1], | ||
]); | ||
|
||
// fully retract items that constitue a grouping | ||
inputWriter.queueData([ | ||
1, | ||
new Multiset([ | ||
[ | ||
{ | ||
id: 'b', | ||
a: 2, | ||
b: 'x', | ||
}, | ||
-2, | ||
], | ||
]), | ||
]); | ||
check([[{id: 'x', sum: 4}, -1]]); | ||
|
||
// add more entries | ||
inputWriter.queueData([ | ||
1, | ||
new Multiset([ | ||
[ | ||
{ | ||
id: 'a', | ||
a: 1, | ||
b: 'c', | ||
}, | ||
1, | ||
], | ||
]), | ||
]); | ||
check([[{id: 'c', sum: 1}, 1]]); | ||
inputWriter.queueData([ | ||
1, | ||
new Multiset([ | ||
[ | ||
{ | ||
id: 'b', | ||
a: 2, | ||
b: 'c', | ||
}, | ||
1, | ||
], | ||
]), | ||
]); | ||
check([ | ||
[{id: 'c', sum: 1}, -1], | ||
[{id: 'c', sum: 3}, 1], | ||
]); | ||
|
||
inputWriter.queueData([ | ||
1, | ||
new Multiset([ | ||
[ | ||
{ | ||
id: 'a', | ||
a: 1, | ||
b: 'c', | ||
}, | ||
-1, | ||
], | ||
[ | ||
{ | ||
id: 'a', | ||
a: 2, | ||
b: 'c', | ||
}, | ||
1, | ||
], | ||
]), | ||
]); | ||
check([ | ||
[{id: 'c', sum: 3}, -1], | ||
[{id: 'c', sum: 4}, 1], | ||
]); | ||
|
||
function check(expected: [Reduction, number][]) { | ||
inputWriter.notify(1); | ||
inputWriter.notifyCommitted(1); | ||
const items = outReader.drain(1); | ||
expect(items.length).toBe(1); | ||
const entry = items[0]; | ||
expect([...entry[1].entries]).toEqual(expected); | ||
} | ||
}); |
Oops, something went wrong.