Skip to content

Commit 30076f4

Browse files
committed
fix(schema-compiler): Use join hints from views for join graph
1 parent 10139b3 commit 30076f4

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,16 @@ export class JoinGraph {
209209
return null;
210210
}
211211

212+
const tunedGraph = this.getFixedWeightsGraph([root, ...cubesToJoin]);
213+
212214
if (Array.isArray(root)) {
213215
const [newRoot, ...additionalToJoin] = root;
214216
if (additionalToJoin.length > 0) {
215217
cubesToJoin = [additionalToJoin, ...cubesToJoin];
216218
}
217219
root = newRoot;
218220
}
221+
219222
const nodesJoined = {};
220223
const result = cubesToJoin.map(joinHints => {
221224
if (!Array.isArray(joinHints)) {
@@ -228,7 +231,7 @@ export class JoinGraph {
228231
return { joins: [] };
229232
}
230233

231-
const path = graph.path(prevNode, toJoin);
234+
const path = tunedGraph.path(prevNode, toJoin);
232235
if (!path) {
233236
return null;
234237
}
@@ -281,6 +284,38 @@ export class JoinGraph {
281284
};
282285
}
283286

287+
/**
288+
* Returns compiled graph with updated weights for view join-hints
289+
*/
290+
protected getFixedWeightsGraph(joinHints: JoinHints): Graph {
291+
const PRIORITY_WEIGHT = 20; // Lower weight for preferred paths
292+
293+
// Create a deep copy of this.nodes to avoid modifying the original
294+
const tunedNodes: Record<string, Record<string, number>> = {};
295+
for (const [from, destinations] of Object.entries(this.nodes)) {
296+
tunedNodes[from] = {};
297+
for (const [to, weight] of Object.entries(destinations)) {
298+
tunedNodes[from][to] = weight;
299+
}
300+
}
301+
302+
// Update weights only for array hints (view join hints)
303+
for (const hint of joinHints) {
304+
if (Array.isArray(hint) && hint.length > 1) {
305+
for (let i = 0; i < hint.length - 1; i++) {
306+
const from = hint[i];
307+
const to = hint[i + 1];
308+
309+
if (tunedNodes[from]?.[to] !== undefined) {
310+
tunedNodes[from][to] = PRIORITY_WEIGHT;
311+
}
312+
}
313+
}
314+
}
315+
316+
return new Graph(tunedNodes);
317+
}
318+
284319
protected findMultiplicationFactorFor(cube: string, joins: JoinTreeJoins): boolean {
285320
const visited = {};
286321
const self = this;

0 commit comments

Comments
 (0)