Skip to content
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

fix(schema-compiler) support join inheritance #8970

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/cubejs-schema-compiler/src/compiler/CubeSymbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class CubeSymbols {
let measures;
let dimensions;
let segments;
let joins;

const cubeObject = Object.assign({
allDefinitions(type) {
Expand Down Expand Up @@ -106,7 +107,17 @@ export class CubeSymbols {
},
set segments(v) {
// Dont allow to modify
}
},

get joins() {
if (!joins) {
joins = this.allDefinitions('joins');
}
return joins;
},
set joins(v) {
// Dont allow to modify
},
}, cubeDefinition);

if (cubeDefinition.extends) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,65 @@ cubes:
);
});

it('should correctly handle extensions with joins', async () => {
const { compiler, joinGraph, cubeEvaluator } = prepareYamlCompiler(`
cubes:
- name: base_users
sql: "SELECT 1"

dimensions:
- name: time
sql: "{CUBE}.timestamp"
type: time
- name: user_id
sql: "user_id"
type: number
primary_key: true
public: true

joins:
- name: base_user_joins
sql: "{CUBE}.user_id = {base_user_joins}.user_id"
Copy link
Member

@igorlukanin igorlukanin Nov 22, 2024

Choose a reason for hiding this comment

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

What would/should happen if the current cube is referenced by its name instead of CUBE here? If it fails, would the error be comprehensible enough?

I feel like it would be great to have a test for this case as well.

Copy link
Member

Choose a reason for hiding this comment

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

Also, if the error is not very comprehensible and it's not that easy to fix, it might help to leave a note on join inheritance on this page: https://cube.dev/docs/product/data-modeling/concepts/code-reusability-extending-cubes

relationship: one_to_one

- name: base_user_joins
sql: "SELECT 1 as user_id, 2 as second_prop"

dimensions:
- name: user_id
sql: "user_id"
type: number
primary_key: true
- name: second_prop
sql: "second_prop"
type: number

- name: active_users
sql: "SELECT 1 as user_id, '2022-01-01' as timestamp"
extends: base_users

`, { yamlExtension: true });
await compiler.compile();

const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: [],
dimensions: ['active_users.user_id', 'active_users.second_prop'],
timezone: 'UTC'
});

console.log(query.buildSqlAndParams());

const res = await dbRunner.testQuery(query.buildSqlAndParams());
console.log(JSON.stringify(res));

expect(res).toEqual(
[{
active_users__user_id: 1,
base_user_joins__second_prop: 2
}]
);
});

it('COMPILE_CONTEXT', async () => {
const { compiler, joinGraph, cubeEvaluator } = prepareYamlCompiler(`
cubes:
Expand Down