-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Allow using sub query dimensions in join conditions (#2419)
* fix: Allow using sub query dimensions in join conditions * chore: Allow using sub query dimensions in join conditions: lint and TODO
- Loading branch information
1 parent
9212fb9
commit 496a075
Showing
2 changed files
with
168 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
packages/cubejs-schema-compiler/test/integration/postgres/sub-query-dimensions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { PostgresQuery } from '../../../src/adapter/PostgresQuery'; | ||
import { prepareCompiler } from '../../unit/PrepareCompiler'; | ||
import { dbRunner } from './PostgresDBRunner'; | ||
|
||
describe('Sub Query Dimensions', () => { | ||
jest.setTimeout(200000); | ||
|
||
const { compiler, joinGraph, cubeEvaluator } = prepareCompiler(` | ||
cube(\`A\`, { | ||
sql: \` | ||
SELECT 79 AS id, 1 AS foo_id UNION ALL | ||
SELECT 80 AS id, 2 AS foo_id UNION ALL | ||
SELECT 81 AS id, 3 AS foo_id UNION ALL | ||
SELECT 82 AS id, 4 AS foo_id UNION ALL | ||
SELECT 83 AS id, 5 AS foo_id UNION ALL | ||
SELECT 84 AS id, 6 AS foo_id | ||
\`, | ||
measures: { | ||
maxFooId: { | ||
sql: \`foo_id\`, | ||
type: 'max' | ||
} | ||
}, | ||
dimensions: { | ||
id: { | ||
sql: \`id\`, | ||
type: \`number\`, | ||
primaryKey: true, | ||
}, | ||
fooId: { | ||
sql: \`\${CUBE}.foo_id\`, | ||
type: \`number\`, | ||
}, | ||
}, | ||
}); | ||
cube(\`B\`, { | ||
sql: \` | ||
SELECT 100 AS id, 1 AS foo_id, 450 AS bar_id UNION ALL | ||
SELECT 101 AS id, 2 AS foo_id, 450 AS bar_id UNION ALL | ||
SELECT 102 AS id, 3 AS foo_id, 452 AS bar_id UNION ALL | ||
SELECT 103 AS id, 4 AS foo_id, 452 AS bar_id UNION ALL | ||
SELECT 104 AS id, 5 AS foo_id, 478 AS bar_id | ||
\`, | ||
joins: { | ||
A: { | ||
relationship: \`hasOne\`, | ||
sql: \`\${A}.foo_id = \${B}.foo_id\`, | ||
}, | ||
C: { | ||
relationship: \`hasMany\`, | ||
sql: \`\${B}.bar_id = \${C}.bar_id AND \${B.fooId} > 3\`, | ||
}, | ||
}, | ||
measures: { | ||
count: { | ||
type: \`count\` | ||
}, | ||
}, | ||
dimensions: { | ||
id: { | ||
sql: \`id\`, | ||
type: \`number\`, | ||
primaryKey: true, | ||
shown: true | ||
}, | ||
fooId: { | ||
sql: \`\${A.maxFooId}\`, | ||
type: \`number\`, | ||
subQuery: true | ||
} | ||
} | ||
}); | ||
cube(\`C\`, { | ||
sql: \` | ||
SELECT 789 AS id, 450 AS bar_id, 0.2 AS important_value UNION ALL | ||
SELECT 790 AS id, 450 AS bar_id, 0.3 AS important_value UNION ALL | ||
SELECT 791 AS id, 452 AS bar_id, 5.6 AS important_value UNION ALL | ||
SELECT 792 AS id, 452 AS bar_id, 5.6 AS important_value UNION ALL | ||
SELECT 793 AS id, 478 AS bar_id, 38.0 AS important_value UNION ALL | ||
SELECT 794 AS id, 478 AS bar_id, 43.5 AS important_value | ||
\`, | ||
measures: { | ||
importantValue: { | ||
sql: \`important_value\`, | ||
type: \`sum\`, | ||
}, | ||
}, | ||
dimensions: { | ||
id: { | ||
sql: \`id\`, | ||
type: \`number\`, | ||
primaryKey: true, | ||
}, | ||
barId: { | ||
sql: \`\${CUBE}.bar_id\`, | ||
type: \`number\`, | ||
}, | ||
}, | ||
}); | ||
`); | ||
|
||
async function runQueryTest(q, expectedResult) { | ||
await compiler.compile(); | ||
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, q); | ||
|
||
console.log(query.buildSqlAndParams()); | ||
|
||
const res = await dbRunner.testQuery(query.buildSqlAndParams()); | ||
console.log(JSON.stringify(res)); | ||
|
||
expect(res).toEqual( | ||
expectedResult | ||
); | ||
} | ||
|
||
it('inserted at the right place of a join', async () => runQueryTest({ | ||
measures: ['C.importantValue'], | ||
dimensions: [ | ||
'B.id' | ||
], | ||
order: [{ id: 'B.id' }] | ||
}, [{ | ||
b__id: 100, | ||
c__important_value: null, | ||
}, { | ||
b__id: 101, | ||
c__important_value: null, | ||
}, { | ||
b__id: 102, | ||
c__important_value: null, | ||
}, { | ||
b__id: 103, | ||
c__important_value: '11.2', | ||
}, { | ||
b__id: 104, | ||
c__important_value: '81.5', | ||
}])); | ||
}); |