Feature request: custom aggregate functions #1558
jakeboone02
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
TanStack DB's aggregate function support is hardcoded in
packages/db/src/query/compiler/group-by.ts(getAggregateFunctionswitch). Onlysum,count,avg,min,maxare supported. Any other aggregate name throwsUnsupportedAggregateFunctionError.This makes it impossible for users to define domain-specific aggregations (e.g.
group_concat,array_agg,json_agg, bitwise OR, geometric mean, etc.) without forking the package.Related
STRING_AGG/group_concatProposal
Expose a registration API for custom aggregate functions, leveraging the existing
{ preMap, reduce, postMap? }contract from@tanstack/db-ivm'sgroupByOperators.API
Types
Implementation notes
valueExtractorpassed to the factory is the raw value extractor (preserves the original value without numeric coercion). Internally this is([, namespacedRow]) => compiledExpr(namespacedRow)— but for users it's opaque.AggregateIR node (e.g.new Aggregate("group_concat", [toExpression(field), toExpression(separator)])) are compiled and evaluated against an empty context, then passed asadditionalArgs[0],additionalArgs[1], etc. This allows parameterized aggregates (custom separators, precision values, flags, etc.).reducecallback's array are[V, number]where the number is multiplicity (how many rows mapped to that value). Users must iteratemultiplicitytimes or account for it in their reduction.value as string) are needed inreducesince the generic types flow asunknownat the boundary.unregisterAggregate(name)returnsbooleanindicating whether the aggregate existed.getRegisteredAggregates()returns aReadonlySet<string>of registered names.Implementation sketch
Minimal change to
getAggregateFunctioningroup-by.ts:Beta Was this translation helpful? Give feedback.
All reactions