Skip to content

Commit 0fed188

Browse files
Copilothotlong
andcommitted
Add window functions and enhanced aggregations to QuerySchema
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 9009045 commit 0fed188

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

packages/core/src/query/query-ast.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
WindowNode,
2020
WindowFunction,
2121
WindowFrame,
22+
WindowConfig,
2223
FieldNode,
2324
LiteralNode,
2425
OperatorNode,
@@ -81,8 +82,10 @@ export class QueryASTBuilder {
8182
fields.push(...query.aggregations.map(agg => this.buildAggregation(agg)));
8283
}
8384

84-
// Add window functions if they exist (future extension point)
85-
// query.windows?.forEach(win => fields.push(this.buildWindow(win)));
85+
// Add window functions (ObjectStack Spec v0.7.1)
86+
if (query.windows && query.windows.length > 0) {
87+
fields.push(...query.windows.map(win => this.buildWindow(win)));
88+
}
8689

8790
return {
8891
type: 'select',
@@ -289,16 +292,7 @@ export class QueryASTBuilder {
289292
/**
290293
* Build window function node (ObjectStack Spec v0.7.1)
291294
*/
292-
private buildWindow(config: {
293-
function: WindowFunction;
294-
field?: string;
295-
alias: string;
296-
partitionBy?: string[];
297-
orderBy?: Array<{ field: string; direction: 'asc' | 'desc' }>;
298-
frame?: WindowFrame;
299-
offset?: number;
300-
defaultValue?: any;
301-
}): WindowNode {
295+
private buildWindow(config: WindowConfig): WindowNode {
302296
const node: WindowNode = {
303297
type: 'window',
304298
function: config.function,

packages/types/src/data-protocol.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ export interface QuerySchema {
340340
*/
341341
group_by?: string[];
342342

343+
/**
344+
* Window functions (ObjectStack Spec v0.7.1)
345+
*/
346+
windows?: WindowConfig[];
347+
343348
/**
344349
* Related objects to expand
345350
*/
@@ -375,10 +380,40 @@ export interface JoinConfig {
375380
* Aggregation configuration
376381
*/
377382
export interface AggregationConfig {
378-
function: 'count' | 'sum' | 'avg' | 'min' | 'max';
383+
function: 'count' | 'sum' | 'avg' | 'min' | 'max' | 'count_distinct' | 'array_agg' | 'string_agg';
379384
field?: string;
380385
alias?: string;
381386
distinct?: boolean;
387+
separator?: string; // For string_agg function
388+
}
389+
390+
/**
391+
* Window function configuration (ObjectStack Spec v0.7.1)
392+
*/
393+
export interface WindowConfig {
394+
/** Window function name */
395+
function: WindowFunction;
396+
397+
/** Field to operate on (not required for row_number, rank, etc.) */
398+
field?: string;
399+
400+
/** Result alias */
401+
alias: string;
402+
403+
/** PARTITION BY fields */
404+
partitionBy?: string[];
405+
406+
/** ORDER BY clause */
407+
orderBy?: Array<{ field: string; direction: 'asc' | 'desc' }>;
408+
409+
/** Window frame specification */
410+
frame?: WindowFrame;
411+
412+
/** Offset for lag/lead functions */
413+
offset?: number;
414+
415+
/** Default value for lag/lead when no previous/next row */
416+
defaultValue?: any;
382417
}
383418

384419
/**

packages/types/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ export type {
384384
QuerySortConfig,
385385
JoinConfig,
386386
AggregationConfig,
387+
WindowConfig,
387388
// Filter Schema (Phase 3.4)
388389
AdvancedFilterSchema,
389390
AdvancedFilterCondition,

0 commit comments

Comments
 (0)