Skip to content

Commit 762d304

Browse files
committed
feat(schema-compiler): Custom granularities support for AWS Athena/Presto dialect
1 parent 694ca9e commit 762d304

File tree

3 files changed

+561
-7
lines changed

3 files changed

+561
-7
lines changed

packages/cubejs-schema-compiler/src/adapter/PrestodbQuery.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseSqlInterval } from '@cubejs-backend/shared';
12
import { BaseQuery } from './BaseQuery';
23
import { BaseFilter } from './BaseFilter';
34

@@ -55,6 +56,30 @@ export class PrestodbQuery extends BaseQuery {
5556
field;
5657
}
5758

59+
/**
60+
* Returns sql for source expression floored to timestamps aligned with
61+
* intervals relative to origin timestamp point.
62+
* Athena doesn't support INTERVALs directly — using date_diff/date_add
63+
*/
64+
public dateBin(interval: string, source: string, origin: string): string {
65+
const intervalParsed = parseSqlInterval(interval);
66+
const intervalParts = Object.entries(intervalParsed);
67+
68+
if (intervalParts.length > 1) {
69+
throw new Error('Athena/Presto supports only simple intervals with one date part');
70+
}
71+
72+
const [unit, count] = intervalParts[0];
73+
const originExpr = this.timeStampCast(`'${origin}'`);
74+
75+
return `date_add('${unit}',
76+
floor(
77+
date_diff('${unit}', ${originExpr}, ${source}) / ${count}
78+
) * ${count},
79+
${originExpr}
80+
)`;
81+
}
82+
5883
public timeGroupedColumn(granularity, dimension) {
5984
return `date_trunc('${GRANULARITY_TO_INTERVAL[granularity]}', ${dimension})`;
6085
}

packages/cubejs-testing-drivers/fixtures/athena.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,8 @@
151151
"---------------------------------------",
152152
"Custom Granularities ",
153153
"---------------------------------------",
154-
"querying custom granularities ECommerce: count by half_year + no dimension",
155-
"querying custom granularities ECommerce: count by half_year_by_1st_april + no dimension",
156154
"querying custom granularities ECommerce: count by three_months_by_march + no dimension",
157-
"querying custom granularities ECommerce: count by half_year + dimension",
158-
"querying custom granularities ECommerce: count by half_year_by_1st_april + dimension",
159155
"querying custom granularities ECommerce: count by three_months_by_march + dimension",
160-
"querying custom granularities ECommerce: count by two_mo_by_feb + no dimension + rollingCountByUnbounded",
161-
"querying custom granularities ECommerce: count by two_mo_by_feb + no dimension + rollingCountByTrailing",
162-
"querying custom granularities ECommerce: count by two_mo_by_feb + no dimension + rollingCountByLeading",
163156

164157
"SKIPPED SQL API (Need work)",
165158
"---------------------------------------",

0 commit comments

Comments
 (0)