Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/cubejs-schema-compiler/src/adapter/PostgresQuery.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { parseSqlInterval } from '@cubejs-backend/shared';
import { BaseQuery } from './BaseQuery';
import { ParamAllocator } from './ParamAllocator';

Expand Down Expand Up @@ -38,13 +39,26 @@ export class PostgresQuery extends BaseQuery {
* This implementation should also work for AWS RedShift.
*/
public dateBin(interval: string, source: string, origin: string): string {
return `('${origin}'::timestamp + INTERVAL '${interval}' *
const intervalStr = this.intervalString(interval);
return `('${origin}'::timestamp + INTERVAL ${intervalStr} *
FLOOR(
EXTRACT(EPOCH FROM (${source} - '${origin}'::timestamp)) /
EXTRACT(EPOCH FROM INTERVAL '${interval}')
EXTRACT(EPOCH FROM INTERVAL ${intervalStr})
))`;
}

public override intervalString(interval: string): string {
const parsed = parseSqlInterval(interval);
if (parsed.quarter) {
parsed.month = (parsed.month || 0) + parsed.quarter * 3;
delete parsed.quarter;
}
const normalized = Object.entries(parsed)
.map(([unit, value]) => `${value} ${unit}${value !== 1 ? 's' : ''}`)
.join(' ');
return `'${normalized}'`;
}

public hllInit(sql) {
return `hll_add_agg(hll_hash_any(${sql}))`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ describe('SQL Generation', () => {
offset: 'start'
}
},
revenueRollingQuarter: {
type: 'sum',
sql: 'amount',
rollingWindow: {
trailing: '2 quarters',
offset: 'start'
}
},
revenueRollingThreeDay: {
type: 'sum',
sql: 'amount',
Expand Down Expand Up @@ -1143,6 +1151,48 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
{ visitors__created_at_day: '2017-01-10T00:00:00.000Z', visitors__revenue_rolling: null }
]));

it('rolling quarter', async () => runQueryTest({
measures: [
'visitors.revenueRolling'
],
timeDimensions: [{
dimension: 'visitors.created_at',
granularity: 'quarter',
dateRange: ['2016-01-01', '2017-01-10']
}],
order: [{
id: 'visitors.created_at'
}],
timezone: 'America/Los_Angeles'
}, [
{ visitors__created_at_quarter: '2016-01-01T00:00:00.000Z', visitors__revenue_rolling: null },
{ visitors__created_at_quarter: '2016-04-01T00:00:00.000Z', visitors__revenue_rolling: null },
{ visitors__created_at_quarter: '2016-07-01T00:00:00.000Z', visitors__revenue_rolling: null },
{ visitors__created_at_quarter: '2016-10-01T00:00:00.000Z', visitors__revenue_rolling: null },
{ visitors__created_at_quarter: '2017-01-01T00:00:00.000Z', visitors__revenue_rolling: null }
]));

it('rolling over 2 quarters', async () => runQueryTest({
measures: [
'visitors.revenueRollingQuarter'
],
timeDimensions: [{
dimension: 'visitors.created_at',
granularity: 'quarter',
dateRange: ['2016-01-01', '2017-01-10']
}],
order: [{
id: 'visitors.created_at'
}],
timezone: 'America/Los_Angeles'
}, [
{ visitors__created_at_quarter: '2016-01-01T00:00:00.000Z', visitors__revenue_rolling_quarter: null },
{ visitors__created_at_quarter: '2016-04-01T00:00:00.000Z', visitors__revenue_rolling_quarter: null },
{ visitors__created_at_quarter: '2016-07-01T00:00:00.000Z', visitors__revenue_rolling_quarter: null },
{ visitors__created_at_quarter: '2016-10-01T00:00:00.000Z', visitors__revenue_rolling_quarter: '500' },
{ visitors__created_at_quarter: '2017-01-01T00:00:00.000Z', visitors__revenue_rolling_quarter: '500' }
]));

if (getEnv('nativeSqlPlanner')) {
it('rolling day ago', async () => runQueryTest({
measures: [
Expand Down
4 changes: 2 additions & 2 deletions packages/cubejs-schema-compiler/test/unit/base-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,9 +880,9 @@ describe('SQL Generation', () => {
if (q.measures[0].includes('count')) {
expect(queryString.includes('INTERVAL \'6 months\'')).toBeTruthy();
} else if (q.measures[0].includes('rollingCountByTrailing2Day')) {
expect(queryString.includes('- interval \'2 day\'')).toBeTruthy();
expect(queryString.includes('- interval \'2 days\'')).toBeTruthy();
} else if (q.measures[0].includes('rollingCountByLeading2Day')) {
expect(queryString.includes('+ interval \'3 day\'')).toBeTruthy();
expect(queryString.includes('+ interval \'3 days\'')).toBeTruthy();
}
});
});
Expand Down
Loading