Skip to content

Commit 2594421

Browse files
KSDaemonigorlukanin
authored andcommitted
fix(schema-compiler): Fix incorrect cache that affects query joins (#10084)
+ tests
1 parent 83ae784 commit 2594421

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

packages/cubejs-schema-compiler/src/adapter/BaseQuery.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2745,9 +2745,15 @@ export class BaseQuery {
27452745
}
27462746

27472747
collectJoinHintsFromMembers(members) {
2748+
// Extract cube names from members to make cache key member-cubes-specific
2749+
const memberCubes = members
2750+
.map(m => m.cube?.()?.name)
2751+
.filter(Boolean)
2752+
.sort();
2753+
27482754
return [
27492755
...members.map(m => m.joinHint).filter(h => h?.length > 0),
2750-
...this.collectFrom(members, this.collectJoinHintsFor.bind(this), 'collectJoinHintsFromMembers'),
2756+
...this.collectFrom(members, this.collectJoinHintsFor.bind(this), ['collectJoinHintsFromMembers', ...memberCubes]),
27512757
];
27522758
}
27532759

packages/cubejs-schema-compiler/test/unit/base-query.test.ts

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ describe('SQL Generation', () => {
961961

962962
]);
963963

964-
it('Base joins - one-one join', async () => {
964+
it('one-one join', async () => {
965965
await compilers.compiler.compile();
966966

967967
const query = new PostgresQuery(compilers, {
@@ -980,7 +980,7 @@ describe('SQL Generation', () => {
980980
expect(queryAndParams[0]).toContain('LEFT JOIN card3_tbl AS "cards_c" ON "cards_b".other_id = "cards_c".id');
981981
});
982982

983-
it('Base joins - multiplied join', async () => {
983+
it('multiplied join', async () => {
984984
await compilers.compiler.compile();
985985

986986
const query = new PostgresQuery(compilers, {
@@ -994,12 +994,59 @@ describe('SQL Generation', () => {
994994
timezone: 'America/Los_Angeles',
995995
});
996996

997-
const queryAndParams = query.buildSqlAndParams();
997+
const _queryAndParams = query.buildSqlAndParams();
998998

999999
/* expect(queryAndParams[0]).toContain('LEFT JOIN card2_tbl AS "cards_b" ON "cards_a".other_id = "cards_b".id');
10001000
expect(queryAndParams[0]).toContain('LEFT JOIN card3_tbl AS "cards_c" ON "cards_b".other_id = "cards_c".id'); */
10011001
});
1002+
1003+
it('join hint cache', async () => {
1004+
// Create a schema with a segment that uses FILTER_PARAMS
1005+
const filterParamsCompilers = /** @type Compilers */ prepareJsCompiler([
1006+
createCubeSchema({
1007+
name: 'cardsA',
1008+
sqlTable: 'card_tbl',
1009+
joins: `{
1010+
cardsB: {
1011+
sql: \`\${CUBE}.other_id = \${cardsB}.id\`,
1012+
relationship: 'one_to_one'
1013+
},
1014+
}`
1015+
}).replace(`sql: \`\${CUBE}.location = 'San Francisco'\``, `sql: \`\${FILTER_PARAMS.cardsA.location.filter('location')}\``),
1016+
createCubeSchema({
1017+
name: 'cardsB',
1018+
sqlTable: 'card2_tbl',
1019+
}),
1020+
]);
1021+
await filterParamsCompilers.compiler.compile();
1022+
1023+
// First query requires a join
1024+
const queryWithJoin = new PostgresQuery(filterParamsCompilers, {
1025+
dimensions: [
1026+
'cardsA.id',
1027+
'cardsB.id',
1028+
],
1029+
segments: [
1030+
'cardsA.sfUsers',
1031+
],
1032+
});
1033+
const queryAndParamsWithJoin = queryWithJoin.buildSqlAndParams();
1034+
expect(queryAndParamsWithJoin[0]).toContain('LEFT JOIN card2_tbl AS "cards_b" ON "cards_a".other_id = "cards_b".id');
1035+
1036+
// Second query does not require a join and should not be impacted by the first query
1037+
const queryWithoutJoin = new PostgresQuery(filterParamsCompilers, {
1038+
dimensions: [
1039+
'cardsA.id',
1040+
],
1041+
segments: [
1042+
'cardsA.sfUsers',
1043+
],
1044+
});
1045+
const queryAndParamsWithoutJoin = queryWithoutJoin.buildSqlAndParams();
1046+
expect(queryAndParamsWithoutJoin[0]).not.toContain('JOIN');
1047+
});
10021048
});
1049+
10031050
describe('Common - JS', () => {
10041051
const compilers = /** @type Compilers */ prepareJsCompiler(
10051052
createCubeSchema({

0 commit comments

Comments
 (0)