Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

Aggregate.md

222 lines4.2 KB

Defined in: packages/db/src/query/ir.ts:265

Extends

  • BaseExpression<T>

Type Parameters

T

T = any

Constructors

Constructor

new Aggregate<T>(
   name, 
   args, 
grouping): Aggregate<T>;

Defined in: packages/db/src/query/ir.ts:267

Parameters

name

string

args

BasicExpression<any>[]

grouping
processGroupBy

(pipeline, groupByClause, valueIdentity, isSourceRef, havingClauses?, selectClause?, fnHavingClauses?, aggregateCollectionId?, mainSource?, sanitizeCallbackRows) => NamespacedAndKeyedStream

replaceAggregatesByRefs

(havingExpr, selectClause, resultAlias) => BasicExpression

Returns

Aggregate<T>

Overrides

BaseExpression<T>.constructor

Properties

__returnType

readonly __returnType: T;

Defined in: packages/db/src/query/ir.ts:88

Internal

  • Type brand for TypeScript inference

Inherited from

BaseExpression.__returnType

args

args: BasicExpression<any>[];

Defined in: packages/db/src/query/ir.ts:269


grouping

grouping: object;

Defined in: packages/db/src/query/ir.ts:271

processGroupBy()

processGroupBy: (pipeline, groupByClause, valueIdentity, isSourceRef, havingClauses?, selectClause?, fnHavingClauses?, aggregateCollectionId?, mainSource?, sanitizeCallbackRows) => NamespacedAndKeyedStream;

Processes the GROUP BY clause with optional HAVING and SELECT Works with the new $selected structure from early SELECT processing

Parameters
pipeline

NamespacedAndKeyedStream

groupByClause

GroupBy

valueIdentity

ValueIdentity

isSourceRef

(ref) => boolean

Whether a ref reads a field of the query's own rows.

havingClauses?

Where[]

selectClause?

Select

fnHavingClauses?

(row) => any[]

aggregateCollectionId?

string

mainSource?

string

sanitizeCallbackRows?

boolean = false

Returns

NamespacedAndKeyedStream

replaceAggregatesByRefs()

replaceAggregatesByRefs: (havingExpr, selectClause, resultAlias) => BasicExpression;

Transforms expressions to replace aggregate functions with references to computed values.

For aggregate expressions, finds matching aggregates in the SELECT clause and replaces them with PropRef([resultAlias, alias]) to reference the computed aggregate value.

Ref expressions (table columns and $selected fields) and value expressions are passed through unchanged. Function expressions are recursively transformed.

Parameters
havingExpr

The expression to transform (can be aggregate, ref, func, or val)

Aggregate<any> | BasicExpression

selectClause

Select

The SELECT clause containing aliases and aggregate definitions

resultAlias

string = ...

The namespace alias for SELECT results (default: '$selected')

Returns

BasicExpression

A transformed BasicExpression that references computed values instead of raw expressions


name

name: string;

Defined in: packages/db/src/query/ir.ts:268


type

type: "agg";

Defined in: packages/db/src/query/ir.ts:266

Overrides

BaseExpression.type