Skip to content

Commit 66f7c17

Browse files
authored
fix: View tries to join cube twice in case there's join ambiguity (#7487)
1 parent a6d3579 commit 66f7c17

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

packages/cubejs-schema-compiler/src/compiler/JoinGraph.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,23 @@ export class JoinGraph {
126126
}
127127
root = newRoot;
128128
}
129+
const nodesJoined = {};
129130
const result = cubesToJoin.map(joinHints => {
130131
if (!Array.isArray(joinHints)) {
131132
joinHints = [joinHints];
132133
}
133134
let prevNode = root;
134135
return joinHints.filter(toJoin => toJoin !== prevNode).map(toJoin => {
136+
if (nodesJoined[toJoin]) {
137+
return { joins: [] };
138+
}
135139
const path = this.graph.path(prevNode, toJoin);
136140
if (!path) {
137141
return null;
138142
}
139143
const foundJoins = self.joinsByPath(path);
140144
prevNode = toJoin;
145+
nodesJoined[toJoin] = true;
141146
return { cubes: path, joins: foundJoins };
142147
});
143148
}).reduce((a, b) => a.concat(b), []).reduce((joined, res) => {

packages/cubejs-schema-compiler/test/integration/postgres/yaml-compiler.test.ts

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,4 +511,128 @@ cubes:
511511
);
512512
});
513513
});
514+
515+
it('view join ambiguity', async () => {
516+
const { compiler, joinGraph, cubeEvaluator } = prepareYamlCompiler(`
517+
cubes:
518+
- name: W
519+
sql: |
520+
SELECT 1 as w_id, 1 as z_id
521+
522+
measures: []
523+
524+
dimensions:
525+
526+
- name: w_id
527+
type: string
528+
sql: w_id
529+
primary_key: true
530+
531+
joins:
532+
533+
- name: Z
534+
sql: "{CUBE}.z_id = {Z}.z_id"
535+
relationship: many_to_one
536+
537+
- name: M
538+
sql: |
539+
SELECT 1 as m_id, 1 as v_id, 1 as w_id
540+
541+
measures:
542+
543+
- name: count
544+
type: countDistinct
545+
sql: "{CUBE}.m_id"
546+
547+
dimensions:
548+
549+
- name: m_id
550+
type: string
551+
sql: m_id
552+
primary_key: true
553+
554+
joins:
555+
556+
- name: V
557+
sql: "{CUBE}.v_id = {V}.v_id"
558+
relationship: many_to_one
559+
560+
- name: W
561+
sql: "{CUBE}.w_id = {W}.w_id"
562+
relationship: many_to_one
563+
564+
- name: Z
565+
sql: >
566+
SELECT 1 as z_id, 'US' as COUNTRY
567+
568+
dimensions:
569+
- name: country
570+
sql: "{CUBE}.COUNTRY"
571+
type: string
572+
573+
- name: z_id
574+
sql: "{CUBE}.z_id"
575+
type: string
576+
primaryKey: true
577+
578+
- name: V
579+
sql: |
580+
SELECT 1 as v_id, 1 as z_id
581+
582+
dimensions:
583+
584+
- name: v_id
585+
sql: "{CUBE}.v_id"
586+
type: string
587+
primary_key: true
588+
589+
joins:
590+
591+
- name: Z
592+
sql: "{CUBE}.z_id = {Z}.z_id"
593+
relationship: many_to_one
594+
595+
596+
views:
597+
- name: m_view
598+
599+
cubes:
600+
601+
- join_path: M
602+
includes: "*"
603+
prefix: false
604+
605+
- join_path: M.V
606+
includes: "*"
607+
prefix: true
608+
alias: v
609+
610+
- join_path: M.W
611+
includes: "*"
612+
prefix: true
613+
alias: w
614+
615+
- join_path: M.W.Z
616+
includes: "*"
617+
prefix: true
618+
alias: w_z
619+
`);
620+
await compiler.compile();
621+
622+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
623+
measures: ['m_view.count'],
624+
dimensions: ['m_view.w_z_country'],
625+
timezone: 'UTC',
626+
preAggregationsSchema: ''
627+
});
628+
629+
const res = await dbRunner.evaluateQueryWithPreAggregations(query);
630+
631+
expect(res).toEqual(
632+
[{
633+
m_view__count: '1',
634+
m_view__w_z_country: 'US',
635+
}]
636+
);
637+
});
514638
});

0 commit comments

Comments
 (0)