Skip to content

fix(schema-compiler): Avoid ambiguous dimension column mappings for ClickHouse queries #9674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dragosrep
Copy link

@dragosrep dragosrep commented Jun 12, 2025

Check List

  • Tests have been run in packages where changes made if available
  • Linter has been run for changed code
  • Tests for the changes have been added if not covered yet

This fixes #9383

The root cause of this issue is the ClickHouse server that sometimes includes the table prefix in the column name when returning the result set. For example, the query:

SELECT
    q_0.id,
    q_0.cat,
    value_0,
    value_1
FROM  (
    SELECT 1 as id, 'a' as cat, 100 as value_0
) q_0
INNER JOIN (
    SELECT 1 as id, 'b' as cat, 200 as value_1
) q_1 ON q_0.id=q_1.id

returns:
image
,while:

SELECT
    q_0.id,
    q_0.cat,
    value_0,
    value_1
FROM  (
    SELECT 1 as id, 'a' as cat, 100 as value_0
) q_0
INNER JOIN (
    SELECT 1 as id, 'b' as cat, 200 as value_1
) q_1 ON q_0.id=q_1.id
INNER JOIN (
    SELECT 1 as id, 'c' as cat, 300 as value_2
) q_2 ON q_0.id=q_2.id

has the following result set:
image

Similarly, SQL result sets from complex analytics queries with cube joins sometimes end up with column names having the q_0 prefix, leading to an error when trying to map them to a dimension.

The overriden dimensionColumns explicitly sets the column name to avoid this problem. Without it, one of the two provided integration tests fails.

@dragosrep dragosrep requested a review from a team as a code owner June 12, 2025 01:35
@github-actions github-actions bot added the pr:community Contribution from Cube.js community members. label Jun 12, 2025
@KSDaemon KSDaemon self-assigned this Jun 18, 2025
@KSDaemon KSDaemon added driver:clickhouse Issues related to the ClickHouse driver data source driver labels Jun 18, 2025
// Clickhouse sometimes includes the "q_0" prefix in the column name, and this
// leads to errors during the result mapping.
if (cubeAlias === 'q_0') {
return this.dimensionAliasNames().map(alias => `${cubeAlias && `${cubeAlias}.` || ''}${alias} ${alias}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think checking for cubeAlias inside template string (cubeAlias && ${cubeAlias}. || '') is not necessary, as you can get here only if cubeAlias === 'q_0'

Suggested change
return this.dimensionAliasNames().map(alias => `${cubeAlias && `${cubeAlias}.` || ''}${alias} ${alias}`);
return this.dimensionAliasNames().map(alias => `${cubeAlias}.${alias} ${alias}`);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to get what the difference between this and impl in baseQuery... Okay, I see.. You add another plain alias without cubeAlias... What if there are more query members from other subqueries with the same plain alias? It seems that it leads to ambiguity, and the query will fail... Nope?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data source driver driver:clickhouse Issues related to the ClickHouse driver pr:community Contribution from Cube.js community members.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error mapping Clickhouse result set when using joins
2 participants