// // Copyright 2025 DXOS.org // import { QueryAST } from '@dxos/echo-protocol'; import { invariant } from '@dxos/invariant'; const { isGroupKeyAggregate } = QueryAST; /** * A (possibly composite) group key: one coerced scalar component per grouped property. * Missing/undefined/non-scalar property values coerce to `null`. */ export type GroupKeyValue = Record; /** Scalar result of a `max`/`min` aggregate (`null` when the group has no scalar values). */ export type AggregateValue = string | number | boolean | null; /** Named aggregate values computed for one group, keyed by the aggregate's result name. */ export type GroupAggregates = Record; /** * Execution helpers for the `aggregate` query clause, shared by the host `QueryExecutor` and the * client `WorkingSetQueryExecutor`. The clause itself is declared in `QueryPlan.AggregateStep`; this * module holds the runtime grouping/pagination algorithms that the executors apply. */ const HOUR_MS = 3_600_000; const DAY_MS = 86_400_000; /** * Code-unit order, the collation SQLite's `BINARY` applies. Both executors order strings through * this so a compiled plan and an in-memory one return the same rows — under `limit` the collation * decides which rows are cut, not just their order. */ export const compareCodeUnits = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0); export const GroupBy = Object.freeze({ /** * The start of the UTC hour or day a unix-ms `value` falls in, or `null` when it is not a finite * number: the key of a `timestamp` or `time` aggregate. */ truncateTime: (value: unknown, unit: 'hour' | 'day'): number | null => { if (typeof value !== 'number' || !Number.isFinite(value)) { return null; } const size = unit === 'hour' ? HOUR_MS : DAY_MS; return Math.floor(value / size) * size; }, /** * Coerces a raw property value into a group-key component. * `typeof value` must be `string`, `number`, or `boolean`; anything else (missing, * `undefined`, `null`, objects, arrays, refs) collapses to `null`. */ coerceKeyComponent: (value: unknown): string | number | boolean | null => typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ? value : null, /** * Resolves one `group` entry's key component from its fallback chain: the first property with a * scalar value wins (`a ?? b`), `null` when none do. Shared by both executors and the aggregate * stamping below so a group's key and its result field can never disagree. */ resolveKeyComponent: ( properties: readonly string[], getValue: (property: string) => unknown, ): string | number | boolean | null => { for (const property of properties) { const value = GroupBy.coerceKeyComponent(getValue(property)); if (value !== null) { return value; } } return null; }, /** * Stable serialization of a composite group key (component order matters). * Used for wire encoding, group identity, and synthetic entry ids. */ serializeGroupKey: (key: GroupKeyValue): string => JSON.stringify(key), /** * Partitions `items` into contiguous groups by `getKey`, preserving relative order within * each group. Groups are ordered by the first occurrence of their key in `items`. */ partitionByGroupKey: (items: readonly T[], getKey: (item: T) => string): T[] => { const buckets = new Map(); for (const item of items) { const key = getKey(item); let bucket = buckets.get(key); if (!bucket) { bucket = []; buckets.set(key, bucket); } bucket.push(item); } return Array.from(buckets.values()).flat(); }, /** * Returns the items belonging to the first `count` distinct groups (group-level limit). * Assumes `items` are already partitioned into contiguous groups (see {@link partitionByGroupKey}). */ takeGroups: (items: readonly T[], count: number, getKey: (item: T) => string): T[] => { if (count <= 0) { return []; } const result: T[] = []; let seenGroups = 0; let currentKey: string | undefined; for (const item of items) { const key = getKey(item); if (key !== currentKey) { seenGroups += 1; currentKey = key; if (seenGroups > count) { break; } } result.push(item); } return result; }, /** * Returns the items after dropping the first `count` distinct groups (group-level skip). * Assumes `items` are already partitioned into contiguous groups (see {@link partitionByGroupKey}). */ dropGroups: (items: readonly T[], count: number, getKey: (item: T) => string): T[] => { if (count <= 0) { return items.slice(); } let seenGroups = 0; let currentKey: string | undefined; for (let index = 0; index < items.length; index++) { const key = getKey(items[index]); if (key !== currentKey) { seenGroups += 1; currentKey = key; if (seenGroups > count) { return items.slice(index); } } } return []; }, /** * Compares two scalar aggregate/property values. `null` sorts last (largest), mirroring the * executors' own property comparison so aggregate ordering is consistent with member ordering. */ compareScalar: (a: AggregateValue, b: AggregateValue): number => { if (a == null && b == null) { return 0; } if (a == null) { return 1; } if (b == null) { return -1; } if (typeof a === 'string' && typeof b === 'string') { return compareCodeUnits(a, b); } if (typeof a === 'number' && typeof b === 'number') { return a - b; } if (typeof a === 'boolean' && typeof b === 'boolean') { return a === b ? 0 : a ? 1 : -1; } return compareCodeUnits(String(a), String(b)); }, /** Members a group stands for: an item carrying a `weight` (an index bucket) counts that many times. */ countMembers: (members: readonly { weight?: number }[]): number => members.reduce((total, member) => total + (member.weight ?? 1), 0), /** Adds the numeric values of a `sum` aggregate; anything else counts as 0. */ sum: (values: readonly unknown[]): number => values.reduce( (total, value) => (typeof value === 'number' && Number.isFinite(value) ? total + value : total), 0, ), /** * Reduces a group's member values under a `max`/`min` aggregate. Ignores `null`s (values missing * or non-scalar); a group with no scalar values yields `null`. */ reduceAggregate: (values: readonly AggregateValue[], kind: 'max' | 'min'): AggregateValue => { let result: AggregateValue = null; for (const value of values) { if (value == null) { continue; } if (result == null) { result = value; continue; } const comparison = GroupBy.compareScalar(value, result); if ((kind === 'max' && comparison > 0) || (kind === 'min' && comparison < 0)) { result = value; } } return result; }, /** * Computes each group's scalar aggregates (`group`/`max`/`min`/`count`) and returns the same items * with those values stamped on every member (all members of a group share them), so a following * group-level `OrderStep` can order by them — including by a `group` field whose result name differs * from its source property. Assumes `items` are already partitioned into contiguous groups * (see {@link partitionByGroupKey}). * * If an `items`-kind aggregate declares its own `order`, each group's members are stably * re-sorted by it (via `compareByOrder`) before scalar aggregates are stamped — this ordering is * local to that aggregate's member selection, independent of whatever order the input stream * arrived in. `items` itself is not stamped: it is materialised (and capped to `limit`) at result * assembly, from the (possibly now re-sorted) member order this function returns. * * All members of a group share a single member order, so at most one distinct `order` may be * requested across the group's `items`-kind aggregates — two conflicting orders are rejected * rather than silently honoring only one of them. */ withGroupAggregates: ( items: readonly T[], getKey: (item: T) => string, aggregates: readonly QueryAST.GroupAggregate[], getProperty: (item: T, property: string) => unknown, compareByOrder?: (a: T, b: T, order: QueryAST.Order) => number, ): T[] => { const itemsOrders = aggregates.flatMap((aggregate) => aggregate.kind === 'items' && aggregate.order?.length ? [aggregate.order] : [], ); if (itemsOrders.length > 1) { const serialized = new Set(itemsOrders.map((order) => JSON.stringify(order))); invariant(serialized.size === 1, 'Multiple `items` aggregates with different `order`s are not supported.'); } const itemsOrder = itemsOrders[0]; const needsStamping = aggregates.some((aggregate) => aggregate.kind !== 'items'); // Only group/max/min/count are stamped for ordering; `items` collects members at result assembly. if (!needsStamping && !itemsOrder) { return items.slice(); } const result: T[] = []; let index = 0; while (index < items.length) { const key = getKey(items[index]); let end = index; while (end < items.length && getKey(items[end]) === key) { end += 1; } const members = items.slice(index, end); if (itemsOrder && compareByOrder) { members.sort((a, b) => { for (const order of itemsOrder) { const comparison = compareByOrder(a, b, order); if (comparison !== 0) { return comparison; } } return 0; }); } if (!needsStamping) { result.push(...members); index = end; continue; } const computed: GroupAggregates = {}; for (const aggregate of aggregates) { if (aggregate.kind === 'items') { continue; } computed[aggregate.name] = aggregate.kind === 'count' ? GroupBy.countMembers(members) : aggregate.kind === 'sum' ? GroupBy.sum(members.map((member) => getProperty(member, aggregate.property))) : isGroupKeyAggregate(aggregate) ? // All members of a group share the key, so read the component off any member. (members[0].groupKey?.[aggregate.name] ?? null) : GroupBy.reduceAggregate( members.map((member) => coerceScalar(getProperty(member, aggregate.property))), aggregate.kind, ); } for (const member of members) { result.push({ ...member, aggregates: computed }); } index = end; } return result; }, /** * Keeps one member per group as its stand-in, recording the group size. Used when the query asks * for no members, so nothing downstream has to carry or ship the objects. * Assumes `items` are already partitioned into contiguous groups (see {@link partitionByGroupKey}). */ collapseGroups: ( items: readonly T[], getKey: (item: T) => string, ): T[] => { const result: T[] = []; let index = 0; while (index < items.length) { const key = getKey(items[index]); let end = index; while (end < items.length && getKey(items[end]) === key) { end += 1; } result.push({ ...items[index], collapsed: { size: GroupBy.countMembers(items.slice(index, end)) } }); index = end; } return result; }, /** * Reorders whole groups by `compareGroups` (applied to each group's first member, since all * members share the group's key/aggregates), preserving within-group order and contiguity. * Stable: groups comparing equal keep their incoming relative order. Assumes `items` are already * partitioned into contiguous groups (see {@link partitionByGroupKey}). */ orderGroups: (items: readonly T[], getKey: (item: T) => string, compareGroups: (a: T, b: T) => number): T[] => { const groups: T[][] = []; let currentKey: string | undefined; for (const item of items) { const key = getKey(item); if (groups.length === 0 || key !== currentKey) { groups.push([]); currentKey = key; } groups[groups.length - 1].push(item); } return groups .map((group, index) => ({ group, index })) .sort((a, b) => { const comparison = compareGroups(a.group[0], b.group[0]); return comparison !== 0 ? comparison : a.index - b.index; }) .flatMap(({ group }) => group); }, }); /** Coerces a raw property value to the scalar aggregate domain (non-scalars → `null`). */ const coerceScalar = (value: unknown): AggregateValue => typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ? value : null;