Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hierarchical continuous aggregates (@Rollup) #19

Merged
merged 14 commits into from
Feb 10, 2025
Merged

Hierarchical continuous aggregates (@Rollup) #19

merged 14 commits into from
Feb 10, 2025

Conversation

danstarns
Copy link
Collaborator

@danstarns danstarns commented Feb 6, 2025

Related:

This PR adds the ability to make a continuous aggregate on top of a continuous aggregate, its called a Rollup and you can use it via the @Rollup decorator on a TypeORM model

Example Usage

First, define your source continuous aggregate:

import { ContinuousAggregate, BucketColumn, AggregateColumn } from '@timescaledb/typeorm';

@ContinuousAggregate(PageLoad, {
  name: 'hourly_page_views',
  bucket_interval: '1 hour',
})
export class HourlyPageViews {
  @BucketColumn({
    source_column: 'time',
  })
  bucket!: Date;

  @AggregateColumn({
    type: 'count',
  })
  total_views!: number;
}

Then define your rollup:

import { Rollup, RollupColumn, BucketColumn } from '@timescaledb/typeorm';

@Rollup(HourlyPageViews, {
  name: 'daily_page_stats',
  bucket_interval: '1 day',
})
export class DailyPageStats {
  @BucketColumn({
    source_column: 'bucket',
  })
  bucket!: Date;

  @RollupColumn({
    type: 'sum',
    source_column: 'total_views',
  })
  daily_total!: number;
}

Query the rollup:

const stats = await AppDataSource.getRepository(DailyPageStats)
  .createQueryBuilder()
  .where('bucket >= :start', { start })
  .andWhere('bucket < :end', { end })
  .getMany();

Code Changes

  • Add @Rollup decorator
  • Add @RollupColumn decorator
  • Add rollup example + test to typeorm and sequlize examples
  • Add rollup builder + tests in core
  • Add rollup schemas
  • Document rollups

What's next?

  1. Candlestick Rollup - Rollup candlesticks via @CandlestickColumn #20

@danstarns danstarns merged commit d9c4f5d into main Feb 10, 2025
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant