Skip to content

Commit 9a537fd

Browse files
committed
fix(schema-compiler): Fix incorrect cache that affects query joins
+ tests
1 parent f4b51b9 commit 9a537fd

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2794,12 +2794,22 @@ export class BaseQuery {
27942794
*/
27952795
collectFrom(membersToCollectFrom, fn, methodName, cache) {
27962796
const methodCacheKey = Array.isArray(methodName) ? methodName : [methodName];
2797+
const memberCubesCacheKey = membersToCollectFrom
2798+
.reduce((acc, m) => {
2799+
const name = m.cube?.().name;
2800+
if (name) {
2801+
acc.push(name);
2802+
}
2803+
return acc;
2804+
}, [])
2805+
.sort();
2806+
27972807
return R.pipe(
27982808
R.map(f => f.getMembers()),
27992809
R.flatten,
28002810
R.map(s => (
28012811
(cache || this.compilerCache).cache(
2802-
['collectFrom'].concat(methodCacheKey).concat(
2812+
['collectFrom'].concat(methodCacheKey).concat(memberCubesCacheKey).concat(
28032813
s.path() ? [s.path().join('.')] : [s.cube().name, s.expression?.toString() || s.expressionName || s.definition().sql]
28042814
),
28052815
() => fn(() => this.traverseSymbol(s))

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)