import { QueryAST } from '@dxos/echo-protocol'; /** * 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; /** * 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 declare const compareCodeUnits: (a: string, b: string) => number; export declare const GroupBy: Readonly<{ /** * 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; /** * 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; /** * 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; /** * Stable serialization of a composite group key (component order matters). * Used for wire encoding, group identity, and synthetic entry ids. */ serializeGroupKey: (key: GroupKeyValue) => string; /** * 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[]; /** * 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[]; /** * 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[]; /** * 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; /** Members a group stands for: an item carrying a `weight` (an index bucket) counts that many times. */ countMembers: (members: readonly { weight?: number; }[]) => number; /** Adds the numeric values of a `sum` aggregate; anything else counts as 0. */ sum: (values: readonly unknown[]) => number; /** * 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; /** * 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[]; /** * 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[]; /** * 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[]; }>; //# sourceMappingURL=group-by.d.ts.map