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
55 changes: 50 additions & 5 deletions docs-mintlify/reference/data-modeling/pre-aggregations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1571,9 +1571,8 @@ cube(`orders`, {

</CodeGroup>

In the case that you want to reference the time dimension in the index,
the column name as a string in the following format can be used:
`<cube name>__<time dimension name>_<granularity>`:
To reference a time dimension column with its granularity in the index,
use the `CUBE.<time_dimension>.<granularity>` syntax:

<CodeGroup>

Expand All @@ -1594,8 +1593,7 @@ cubes:
- name: category_created_at_index
columns:
- CUBE.category
# special syntax needed
- "{'orders__created_at_day'}"
- CUBE.created_at.day
# ...
```

Expand All @@ -1613,6 +1611,53 @@ cube(`orders`, {
],
time_dimension: CUBE.created_at,
granularity: `day`,
indexes: {
category_created_at_index: {
columns: [
CUBE.category,
CUBE.created_at.day
]
}
}
}
},

// ...
})
```

</CodeGroup>

Alternatively, you can reference the time dimension column as a string
using the `<cube_name>__<time_dimension_name>_<granularity>` format:

<CodeGroup>

```yaml title="YAML"
cubes:
- name: orders
# ...

pre_aggregations:
- name: category_and_date
# ...

indexes:
- name: category_created_at_index
columns:
- CUBE.category
- "{'orders__created_at_day'}"
# ...
```

```javascript title="JavaScript"
cube(`orders`, {
// ...

pre_aggregations: {
category_and_date: {
// ...

indexes: {
category_created_at_index: {
columns: [
Expand Down
55 changes: 50 additions & 5 deletions docs/content/product/data-modeling/reference/pre-aggregations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1571,9 +1571,8 @@ cubes:

</CodeTabs>

In the case that you want to reference the time dimension in the index,
the column name as a string in the following format can be used:
`<cube name>__<time dimension name>_<granularity>`:
To reference a time dimension column with its granularity in the index,
use the `CUBE.<time_dimension>.<granularity>` syntax:

<CodeTabs>

Expand All @@ -1595,7 +1594,7 @@ cube(`orders`, {
category_created_at_index: {
columns: [
CUBE.category,
`orders__created_at_day`
CUBE.created_at.day
]
}
}
Expand Down Expand Up @@ -1623,7 +1622,53 @@ cubes:
- name: category_created_at_index
columns:
- CUBE.category
# special syntax needed
- CUBE.created_at.day
# ...
```

</CodeTabs>

Alternatively, you can reference the time dimension column as a string
using the `<cube_name>__<time_dimension_name>_<granularity>` format:

<CodeTabs>

```javascript
cube(`orders`, {
// ...

pre_aggregations: {
category_and_date: {
// ...

indexes: {
category_created_at_index: {
columns: [
CUBE.category,
`orders__created_at_day`
]
}
}
}
},

// ...
})
```

```yaml
cubes:
- name: orders
# ...

pre_aggregations:
- name: category_and_date
# ...

indexes:
- name: category_created_at_index
columns:
- CUBE.category
- "{'orders__created_at_day'}"
# ...
```
Expand Down
11 changes: 11 additions & 0 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -4273,6 +4273,17 @@ export class BaseQuery {
this.cubeEvaluator.isSegment(path)
)
) {
if (path.length === 3 && this.cubeEvaluator.isDimension(path.slice(0, 2))) {
const dimensionDef = this.cubeEvaluator.dimensionByPath(path.slice(0, 2));
if (dimensionDef.type === 'time' &&
this.cubeEvaluator.resolveGranularity([path[0], path[1], 'granularities', path[2]])) {
const td = this.newTimeDimension({
dimension: `${path[0]}.${path[1]}`,
granularity: path[2],
});
return td.unescapedAliasName();
}
}
return this.aliasName(column);
} else {
return column;
Expand Down
6 changes: 3 additions & 3 deletions packages/cubejs-schema-compiler/src/compiler/CubeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,15 +832,15 @@ export class CubeEvaluator extends CubeSymbols {
return this.isInstanceOfType('segments', path);
}

public measureByPath(measurePath: string): MeasureDefinition {
public measureByPath(measurePath: string | string[]): MeasureDefinition {
return this.byPath('measures', measurePath) as MeasureDefinition;
}

public dimensionByPath(dimensionPath: string): DimensionDefinition {
public dimensionByPath(dimensionPath: string | string[]): DimensionDefinition {
return this.byPath('dimensions', dimensionPath) as DimensionDefinition;
}

public segmentByPath(segmentPath: string): SegmentDefinition {
public segmentByPath(segmentPath: string | string[]): SegmentDefinition {
return this.byPath('segments', segmentPath) as SegmentDefinition;
}

Expand Down
144 changes: 144 additions & 0 deletions packages/cubejs-schema-compiler/test/unit/pre-aggregations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,150 @@ describe('pre-aggregations', () => {
expect(indexesSql[1].indexName).toEqual('orders_indexes_orders_by_day_with_day_by_status_agg_index');
});

it('pre-aggregation index with time dimension granularity column', async () => {
const { compiler, cubeEvaluator, joinGraph } = prepareJsCompiler(
`
cube('Orders', {
sql: \`SELECT * FROM orders\`,

measures: {
count: {
type: 'count',
},
},

dimensions: {
created_at: {
sql: \`created_at\`,
type: 'time',
},
status: {
sql: \`status\`,
type: 'string',
},
},

preAggregations: {
ordersByHour: {
measures: [CUBE.count],
dimensions: [CUBE.status],
timeDimension: CUBE.created_at,
granularity: 'hour',
indexes: {
time_index: {
columns: [CUBE.created_at.hour],
},
time_and_status_index: {
columns: [CUBE.created_at.hour, CUBE.status],
},
},
},
},
});
`
);

await compiler.compile();

const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: ['Orders.count'],
timeDimensions: [{
dimension: 'Orders.created_at',
granularity: 'hour',
dateRange: ['2023-01-01', '2023-01-10']
}],
dimensions: ['Orders.status']
});

const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription();
expect(preAggregationsDescription.length).toBeGreaterThan(0);

const { indexesSql, createTableIndexes } = preAggregationsDescription[0];
expect(indexesSql.length).toEqual(2);

const [timeIndexSql] = indexesSql[0].sql;
expect(timeIndexSql).toContain('"orders__created_at_hour"');
expect(timeIndexSql).not.toContain('"orders__created_at__hour"');

const [timeAndStatusIndexSql] = indexesSql[1].sql;
expect(timeAndStatusIndexSql).toContain('"orders__created_at_hour"');
expect(timeAndStatusIndexSql).toContain('"orders__status"');

expect(createTableIndexes[0].columns).toContain('"orders__created_at_hour"');
expect(createTableIndexes[1].columns).toContain('"orders__created_at_hour"');
expect(createTableIndexes[1].columns).toContain('"orders__status"');
});

it('pre-aggregation index with time dimension granularity column (YAML)', async () => {
const { compiler, cubeEvaluator, joinGraph } = prepareYamlCompiler(
createSchemaYaml({
cubes: [
{
name: 'orders',
sql_table: 'orders',
measures: [{
name: 'count',
type: 'count',
}],
dimensions: [
{
name: 'created_at',
sql: 'created_at',
type: 'time',
},
{
name: 'status',
sql: 'status',
type: 'string',
}
],
preAggregations: [
{
name: 'orders_by_hour',
measures: ['count'],
dimensions: ['status'],
timeDimension: 'created_at',
granularity: 'hour',
indexes: [
{
name: 'time_granularity_index',
columns: ['created_at.hour', 'status']
}
]
}
]
},
]
})
);

await compiler.compile();

const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: ['orders.count'],
timeDimensions: [{
dimension: 'orders.created_at',
granularity: 'hour',
dateRange: ['2023-01-01', '2023-01-10']
}],
dimensions: ['orders.status']
});

const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription();
expect(preAggregationsDescription.length).toBeGreaterThan(0);

const { indexesSql, createTableIndexes } = preAggregationsDescription[0];
expect(indexesSql.length).toEqual(1);

const [indexSql] = indexesSql[0].sql;
expect(indexSql).toContain('"orders__created_at_hour"');
expect(indexSql).not.toContain('"orders__created_at__hour"');
expect(indexSql).toContain('"orders__status"');

expect(createTableIndexes[0].columns).toContain('"orders__created_at_hour"');
expect(createTableIndexes[0].columns).toContain('"orders__status"');
});

it('pre-aggregation with FILTER_PARAMS', async () => {
const { compiler, cubeEvaluator, joinGraph } = prepareYamlCompiler(
createSchemaYaml({
Expand Down
Loading
Loading