cube-js / cube-js/cube

Generated SQL for subquery dimensions does not respect sql_alias

Open
#8,561 0 comments 0 reactions 1 assignee Claimed by @paveltiunov View on GitHub
data modeling driver:postgres
Dominant language
Rust
Stars
20.8k
Forks
2.1k
Avg merge
1d 2h
Merged PRs (30d)
181

Description

**Describe the bug**
Some databases don't support aliases in SQL queries that are longer than certain number of symbols (it's 64 in case of Postgres). The [`sql_alias` parameter](https://cube.dev/docs/reference/data-model/cube#sql_alias) can help work around that limitation.

However, the SQL that Cube generates for [subquery dimensions](https://cube.dev/docs/product/data-modeling/concepts/calculated-members#subquery-dimensions) does not respect `sql_alias`.

It results in queries that Postgres fails to understand and raises the following error:
> table name "[redacted]" specified more than once

**To Reproduce**
Steps to reproduce the behavior:
1. Take this data model:
```yaml
cubes:
- name: my_auxiliary_cube
sql: SELECT 1 AS id, 123 AS value

dimensions:
- name: id
sql: id
type: number
primary_key: true

measures:
- name: count
type: count

- name: my_cube_with_a_very_long_name_xxxxxxxxxxxx
sql: SELECT 1 AS id, 123 AS value
sql_alias: x

joins:
- name: my_auxiliary_cube
relationship: one_to_one
sql: "{CUBE.id} = {my_auxiliary_cube.id}"

dimensions:
- name: id
sql: id
type: number
primary_key: true

- name: aux_count
sql: "{my_auxiliary_cube.count}"
type: number
sub_query: true
```
2. Run the following query:
```json
{
"dimensions": [
"my_cube_with_a_very_long_name_xxxxxxxxxxxx.aux_count"
]
}
```
3. See the error.

**Expected behavior**
`sql_alias` is respected by subquery dimensions. Shorter aliases are generated.

**Screenshots**
Screenshot 2024-08-09 at 17 49 45

**Version:**
0.35.67

**Additional context**
Reported in Slack: https://cube-js.slack.com/archives/C04NYBJP7RQ/p1722447253496259

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.