Skip to content

Generated SQL for subquery dimensions does not respect sql_alias #8561

@igorlukanin

Description

@igorlukanin

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 can help work around that limitation.

However, the SQL that Cube generates for 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:
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
  1. Run the following query:
{
  "dimensions": [
    "my_cube_with_a_very_long_name_xxxxxxxxxxxx.aux_count"
  ]
}
  1. 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

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions