Skip to content

Commit ef3829b

Browse files
authored
fix: groupBy to correctly remove groups when a key is completely removed (#70)
* fix: index compaction for reduce * changeset * repo of bug * fix groupby bug * changeset
1 parent fb90328 commit ef3829b

File tree

3 files changed

+488
-0
lines changed

3 files changed

+488
-0
lines changed

.changeset/odd-garlics-chew.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@electric-sql/d2mini': patch
3+
---
4+
5+
fix a bug where groupBy would not remove a group if it's key was completely removed from the stream

packages/d2mini/src/operators/groupBy.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ export function groupBy<
8787
// Then reduce to compute aggregates
8888
const reduced = withKeysAndValues.pipe(
8989
reduce((values) => {
90+
// Calculate total multiplicity to check if the group should exist
91+
let totalMultiplicity = 0
92+
for (const [_, multiplicity] of values) {
93+
totalMultiplicity += multiplicity
94+
}
95+
96+
// If total multiplicity is 0 or negative, the group should be removed completely
97+
if (totalMultiplicity <= 0) {
98+
return []
99+
}
100+
90101
const result: Record<string, unknown> = {}
91102

92103
// Get the original key from first value in group

0 commit comments

Comments
 (0)