cube-js / cube-js/cube

Pre-aggregations doesn't work well with sub-queries

Open
#8,732 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
20.8k
Forks
2.1k
Avg merge
1d 2h
Merged PRs (30d)
181

Description

**Describe the bug**
I have a measure that uses a dimension with `sub_query` and `propagate_filters_to_sub_query` set to `true`. When creating a pre-aggregation in the same cube and querying the measure, it errors out with the following error message

```
RequestError: Error: Internal: Error during planning: Table or CTE with name '' not found
```

**To Reproduce**
I used the following schema to create the cube
```yaml
cubes:
- name: forecasts
sql: select * from schema.forecasts
pre_aggregations:
- name: first
measures:
- max_cutoff
dimensions:
- product
- cutoff
external: true
measures:
- name: max_cutoff
sql: cutoff
type: max
- name: mean_absolute_percentage
sql: abs((y - yhat) / greatest(y, 1e-16))
type: avg
filters:
- sql: "{CUBE}.cutoff >= {tmp_max_cutoff} - INTERVAL '12 Weeks'"
dimensions:
- name: product
sql: product_id
type: string
primary_key: true
shown: true
- name: cutoff
sql: cutoff
type: time
primary_key: false
- name: tmp_max_cutoff
sql: "{max_cutoff}"
type: number
sub_query: true
propagate_filters_to_sub_query: true
- name: date
sql: date
type: time
```

I am then running the following query
```js
const resultSet = await cubeApi.load({
measures: ['forecasts.mean_absolute_percentage'],
dimensions: ['forecasts.product'],
});
```

This error out with the message given above.
I think this is happening because, Cube is detecting that it can be satisfied via the pre-aggregation and thus runs query on pre-aggregation only. This causes the sub-query to fail.

**Expected behavior**
Cube should detect the sub-query and not run this specific query on pre-aggregations but use the source database.

**Version:**
0.36.1

**Additional Comments**
- One workaround is to create two different cubes one with the pre-aggregation and one without it and add the measure with sub-query to the one without pre-aggregation. If there's a better workaround, please let me know.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.