Skip to content

Commit 9ce0807

Browse files
committed
add tests
1 parent 0647a95 commit 9ce0807

File tree

1 file changed

+142
-1
lines changed

1 file changed

+142
-1
lines changed

packages/cubejs-schema-compiler/test/integration/postgres/sql-generation.test.ts

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { UserError } from '../../../src/compiler/UserError';
33
import { PostgresQuery } from '../../../src/adapter/PostgresQuery';
44
import { BigqueryQuery } from '../../../src/adapter/BigqueryQuery';
55
import { PrestodbQuery } from '../../../src/adapter/PrestodbQuery';
6-
import { prepareJsCompiler } from '../../unit/PrepareCompiler';
6+
import { prepareJsCompiler, prepareYamlCompiler } from '../../unit/PrepareCompiler';
77
import { dbRunner } from './PostgresDBRunner';
88
import { createJoinedCubesSchema } from '../../unit/utils';
99
import { testWithPreAggregation } from './pre-aggregation-utils';
@@ -4087,4 +4087,145 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
40874087
}]
40884088
);
40894089
});
4090+
4091+
describe('Transitive join paths', () => {
4092+
// eslint-disable-next-line @typescript-eslint/no-shadow
4093+
const { compiler, joinGraph, cubeEvaluator } = prepareYamlCompiler(`
4094+
cubes:
4095+
- name: merchant_dims
4096+
sql: |
4097+
(
4098+
SELECT 101 AS merchant_sk, 'M1' AS merchant_id
4099+
UNION ALL
4100+
SELECT 102 AS merchant_sk, 'M2' AS merchant_id
4101+
)
4102+
dimensions:
4103+
- name: merchant_sk
4104+
sql: merchant_sk
4105+
type: number
4106+
primary_key: true
4107+
- name: merchant_id
4108+
sql: merchant_id
4109+
type: string
4110+
4111+
- name: product_dims
4112+
sql: |
4113+
(
4114+
SELECT 201 AS product_sk, 'P1' AS product_id
4115+
UNION ALL
4116+
SELECT 202 AS product_sk, 'P2' AS product_id
4117+
)
4118+
dimensions:
4119+
- name: product_sk
4120+
sql: product_sk
4121+
type: number
4122+
primary_key: true
4123+
- name: product_id
4124+
sql: product_id
4125+
type: string
4126+
4127+
- name: merchant_and_product_dims
4128+
sql: |
4129+
(
4130+
SELECT 'M1' AS merchant_id, 'P1' AS product_id, 'Organic' AS acquisition_channel
4131+
UNION ALL
4132+
SELECT 'M1' AS merchant_id, 'P2' AS product_id, 'Paid' AS acquisition_channel
4133+
UNION ALL
4134+
SELECT 'M2' AS merchant_id, 'P1' AS product_id, 'Referral' AS acquisition_channel
4135+
)
4136+
dimensions:
4137+
- name: product_id
4138+
sql: product_id
4139+
type: string
4140+
primary_key: true
4141+
- name: merchant_id
4142+
sql: merchant_id
4143+
type: string
4144+
primary_key: true
4145+
- name: acquisition_channel
4146+
sql: acquisition_channel
4147+
type: string
4148+
4149+
- name: test_facts
4150+
sql: |
4151+
(
4152+
SELECT DATE '2023-01-01' AS reporting_date, 101 AS merchant_sk, 201 AS product_sk, 100 AS amount
4153+
UNION ALL
4154+
SELECT DATE '2023-01-01' AS reporting_date, 101 AS merchant_sk, 202 AS product_sk, 150 AS amount
4155+
UNION ALL
4156+
SELECT DATE '2023-01-02' AS reporting_date, 102 AS merchant_sk, 201 AS product_sk, 200 AS amount
4157+
)
4158+
joins:
4159+
- name: merchant_dims
4160+
relationship: many_to_one
4161+
sql: "{CUBE}.merchant_sk = {merchant_dims.merchant_sk}"
4162+
- name: product_dims
4163+
relationship: many_to_one
4164+
sql: "{CUBE}.product_sk = {product_dims.product_sk}"
4165+
- name: merchant_and_product_dims # This join depends on merchant_dims and product_dims
4166+
relationship: many_to_one
4167+
sql: "{merchant_dims.merchant_id} = {merchant_and_product_dims.merchant_id} AND {product_dims.product_id} = {merchant_and_product_dims.product_id}"
4168+
dimensions:
4169+
- name: reporting_date
4170+
sql: reporting_date
4171+
type: time
4172+
primary_key: true
4173+
- name: merchant_sk
4174+
sql: merchant_sk
4175+
type: number
4176+
primary_key: true
4177+
- name: product_sk
4178+
sql: product_sk
4179+
type: number
4180+
primary_key: true
4181+
- name: acquisition_channel # This dimension triggers the join to merchant_and_product_dims
4182+
sql: "{merchant_and_product_dims.acquisition_channel}"
4183+
type: string
4184+
measures:
4185+
- name: amount_sum
4186+
sql: amount
4187+
type: sum
4188+
`);
4189+
4190+
it('querying cube dimension that require transitive joins', async () => {
4191+
await compiler.compile();
4192+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
4193+
measures: [],
4194+
dimensions: [
4195+
'test_facts.reporting_date',
4196+
'test_facts.merchant_sk',
4197+
'test_facts.product_sk',
4198+
'test_facts.acquisition_channel'
4199+
],
4200+
order: [{
4201+
id: 'test_facts.acquisition_channel'
4202+
}],
4203+
timezone: 'America/Los_Angeles'
4204+
});
4205+
4206+
const res = await dbRunner.testQuery(query.buildSqlAndParams());
4207+
console.log(JSON.stringify(res));
4208+
4209+
expect(res).toEqual([
4210+
{
4211+
test_facts__acquisition_channel: 'Organic',
4212+
test_facts__merchant_sk: 101,
4213+
test_facts__product_sk: 201,
4214+
test_facts__reporting_date: '2023-01-01T00:00:00.000Z',
4215+
},
4216+
{
4217+
test_facts__acquisition_channel: 'Paid',
4218+
test_facts__merchant_sk: 101,
4219+
test_facts__product_sk: 202,
4220+
test_facts__reporting_date: '2023-01-01T00:00:00.000Z',
4221+
},
4222+
{
4223+
test_facts__acquisition_channel: 'Referral',
4224+
test_facts__merchant_sk: 102,
4225+
test_facts__product_sk: 201,
4226+
test_facts__reporting_date: '2023-01-02T00:00:00.000Z',
4227+
},
4228+
]);
4229+
});
4230+
});
40904231
});

0 commit comments

Comments
 (0)