Skip to content

Commit fd69d1a

Browse files
committed
almost work
1 parent f7d1c32 commit fd69d1a

File tree

21 files changed

+438
-216
lines changed

21 files changed

+438
-216
lines changed

packages/cubejs-schema-compiler/test/integration/postgres/calc-groups.test.ts

Lines changed: 329 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,178 @@ cubes:
246246
measures:
247247
- name: count
248248
type: count
249+
250+
- name: source_root
251+
sql: >
252+
SELECT 1
253+
public: false
254+
255+
joins:
256+
- name: source_a
257+
sql: "1 = 1"
258+
relationship: one_to_many
259+
- name: source_b
260+
sql: "1 = 1"
261+
relationship: one_to_many
262+
263+
dimensions:
264+
- name: pk
265+
type: number
266+
sql: "1"
267+
primary_key: true
268+
269+
- name: source
270+
type: switch
271+
values: ["A", "B"]
272+
273+
- name: currency
274+
type: switch
275+
values: ["USD", "EUR"]
276+
277+
- name: product_category
278+
type: string
279+
case:
280+
switch: "{CUBE.source}"
281+
when:
282+
- value: A
283+
sql: "{source_a.product_category}"
284+
- value: B
285+
sql: "{source_b.product_category}"
286+
else:
287+
sql: "{source_a.product_category}"
288+
289+
290+
measures:
291+
- name: count
292+
type: sum
293+
multi_stage: true
294+
case:
295+
switch: "{CUBE.source}"
296+
when:
297+
- value: A
298+
sql: "{source_a.count}"
299+
- value: B
300+
sql: "{source_b.count}"
301+
else:
302+
sql: "{source_a.count}"
303+
304+
- name: price_eur
305+
type: sum
306+
multi_stage: true
307+
case:
308+
switch: "{CUBE.source}"
309+
when:
310+
- value: A
311+
sql: "{source_a.price_eur}"
312+
- value: B
313+
sql: "{source_b.price_eur}"
314+
else:
315+
sql: "{source_a.price_eur}"
316+
317+
- name: price_usd
318+
type: sum
319+
multi_stage: true
320+
case:
321+
switch: "{CUBE.source}"
322+
when:
323+
- value: A
324+
sql: "{source_a.price_usd}"
325+
- value: B
326+
sql: "{source_b.price_usd}"
327+
else:
328+
sql: "{source_a.price_usd}"
329+
330+
- name: price
331+
type: sum
332+
multi_stage: true
333+
case:
334+
switch: "{CUBE.currency}"
335+
when:
336+
- value: USD
337+
sql: "{CUBE.price_usd}"
338+
- value: EUR
339+
sql: "{CUBE.price_eur}"
340+
else:
341+
sql: "{CUBE.price_usd}"
342+
343+
344+
- name: source_a
345+
sql: >
346+
SELECT 10 as ID, 'some category' as PRODUCT_CATEGORY, 'some name' as NAME, 100 as PRICE_USD, 0 as PRICE_EUR
347+
union all
348+
SELECT 11 as ID, 'some category' as PRODUCT_CATEGORY, 'some name' as NAME, 500 as PRICE_USD, 0 as PRICE_EUR
349+
union all
350+
SELECT 12 as ID, 'some category A' as PRODUCT_CATEGORY, 'some name' as NAME, 200 as PRICE_USD, 0 as PRICE_EUR
351+
union all
352+
SELECT 13 as ID, 'some category A' as PRODUCT_CATEGORY, 'some name' as NAME, 300 as PRICE_USD, 0 as PRICE_EUR
353+
public: false
354+
355+
dimensions:
356+
- name: pk
357+
type: number
358+
sql: ID
359+
primary_key: true
360+
361+
- name: product_category
362+
sql: PRODUCT_CATEGORY
363+
type: string
364+
365+
measures:
366+
- name: count
367+
type: 'count'
368+
369+
- name: price_usd
370+
type: 'sum'
371+
sql: PRICE_USD
372+
373+
- name: price_eur
374+
type: 'sum'
375+
sql: PRICE_EUR
376+
377+
378+
- name: source_b
379+
sql: >
380+
SELECT 10 as ID, 'some category' as PRODUCT_CATEGORY, 'some name' as NAME, 0 as PRICE_USD, 100 as PRICE_EUR
381+
union all
382+
SELECT 11 as ID, 'some category' as PRODUCT_CATEGORY, 'some name' as NAME, 0 as PRICE_USD, 500 as PRICE_EUR
383+
union all
384+
SELECT 12 as ID, 'some category B' as PRODUCT_CATEGORY, 'some name' as NAME, 0 as PRICE_USD, 200 as PRICE_EUR
385+
union all
386+
SELECT 13 as ID, 'some category B' as PRODUCT_CATEGORY, 'some name' as NAME, 0 as PRICE_USD, 300 as PRICE_EUR
387+
union all
388+
SELECT 14 as ID, 'some category B' as PRODUCT_CATEGORY, 'some name' as NAME, 0 as PRICE_USD, 300 as PRICE_EUR
389+
public: false
390+
391+
dimensions:
392+
- name: pk
393+
type: number
394+
sql: ID
395+
primary_key: true
396+
397+
- name: product_category
398+
sql: PRODUCT_CATEGORY
399+
type: string
400+
401+
measures:
402+
- name: count
403+
type: 'count'
404+
405+
- name: price_usd
406+
type: 'sum'
407+
sql: PRICE_USD
408+
409+
- name: price_eur
410+
type: 'sum'
411+
sql: PRICE_EUR
412+
249413
views:
414+
- name: source
415+
cubes:
416+
- join_path: source_root
417+
includes: "*"
418+
419+
420+
250421
- name: orders_view
251422
252423
cubes:
@@ -825,37 +996,37 @@ views:
825996
},
826997
],
827998
}, [
828-
{
829-
orders__currency: 'EUR',
830-
orders__date_year: '2024-01-01T00:00:00.000Z',
831-
orders__amount_in_currency_percent_of_usd: '97'
832-
},
833-
{
834-
orders__currency: 'GBP',
835-
orders__date_year: '2024-01-01T00:00:00.000Z',
836-
orders__amount_in_currency_percent_of_usd: '80'
837-
},
838-
{
839-
orders__currency: 'USD',
840-
orders__date_year: '2024-01-01T00:00:00.000Z',
841-
orders__amount_in_currency_percent_of_usd: '100'
842-
},
843-
{
844-
orders__currency: 'EUR',
845-
orders__date_year: '2025-01-01T00:00:00.000Z',
846-
orders__amount_in_currency_percent_of_usd: '95'
847-
},
848-
{
849-
orders__currency: 'GBP',
850-
orders__date_year: '2025-01-01T00:00:00.000Z',
851-
orders__amount_in_currency_percent_of_usd: '82'
852-
},
853-
{
854-
orders__currency: 'USD',
855-
orders__date_year: '2025-01-01T00:00:00.000Z',
856-
orders__amount_in_currency_percent_of_usd: '100'
857-
}
858-
],
999+
{
1000+
orders__currency: 'EUR',
1001+
orders__date_year: '2024-01-01T00:00:00.000Z',
1002+
orders__amount_in_currency_percent_of_usd: '97'
1003+
},
1004+
{
1005+
orders__currency: 'GBP',
1006+
orders__date_year: '2024-01-01T00:00:00.000Z',
1007+
orders__amount_in_currency_percent_of_usd: '80'
1008+
},
1009+
{
1010+
orders__currency: 'USD',
1011+
orders__date_year: '2024-01-01T00:00:00.000Z',
1012+
orders__amount_in_currency_percent_of_usd: '100'
1013+
},
1014+
{
1015+
orders__currency: 'EUR',
1016+
orders__date_year: '2025-01-01T00:00:00.000Z',
1017+
orders__amount_in_currency_percent_of_usd: '95'
1018+
},
1019+
{
1020+
orders__currency: 'GBP',
1021+
orders__date_year: '2025-01-01T00:00:00.000Z',
1022+
orders__amount_in_currency_percent_of_usd: '82'
1023+
},
1024+
{
1025+
orders__currency: 'USD',
1026+
orders__date_year: '2025-01-01T00:00:00.000Z',
1027+
orders__amount_in_currency_percent_of_usd: '100'
1028+
}
1029+
],
8591030
{ joinGraph, cubeEvaluator, compiler }));
8601031

8611032
it('measure switch with filter', async () => {
@@ -937,6 +1108,133 @@ views:
9371108
expect(sqlAndParams[0]).not.toMatch(/CASE/);
9381109
expect(sqlAndParams[0]).not.toMatch(/CROSS.+JOIN/);
9391110
});
1111+
it('source switch cross join', async () => {
1112+
await dbRunner.runQueryTest({
1113+
dimensions: ['source.source'],
1114+
measures: ['source.count'],
1115+
order: [{
1116+
id: 'source.source'
1117+
}
1118+
],
1119+
}, [
1120+
{ source__source: 'A', source__count: '4' },
1121+
{ source__source: 'B', source__count: '5' }
1122+
],
1123+
{ joinGraph, cubeEvaluator, compiler });
1124+
});
1125+
it('source switch cross join without dimension', async () => {
1126+
await dbRunner.runQueryTest({
1127+
dimensions: ['source.product_category'],
1128+
measures: ['source.count'],
1129+
order: [{
1130+
id: 'source.product_category'
1131+
}
1132+
],
1133+
}, [
1134+
{ source__product_category: 'some category', source__count: '4' },
1135+
{ source__product_category: 'some category A', source__count: '2' },
1136+
{ source__product_category: 'some category B', source__count: '3' }
1137+
],
1138+
{ joinGraph, cubeEvaluator, compiler });
1139+
});
1140+
it('source full switch', async () => {
1141+
await dbRunner.runQueryTest({
1142+
dimensions: ['source.currency', 'source.product_category'],
1143+
measures: ['source.price'],
1144+
order: [{
1145+
id: 'source.product_category'
1146+
},
1147+
{
1148+
id: 'source.currency'
1149+
}
1150+
],
1151+
}, [
1152+
{
1153+
source__currency: 'EUR',
1154+
source__product_category: 'some category',
1155+
source__price: '600'
1156+
},
1157+
{
1158+
source__currency: 'USD',
1159+
source__product_category: 'some category',
1160+
source__price: '600'
1161+
},
1162+
{
1163+
source__currency: 'EUR',
1164+
source__product_category: 'some category A',
1165+
source__price: '0'
1166+
},
1167+
{
1168+
source__currency: 'USD',
1169+
source__product_category: 'some category A',
1170+
source__price: '500'
1171+
},
1172+
{
1173+
source__currency: 'EUR',
1174+
source__product_category: 'some category B',
1175+
source__price: '800'
1176+
},
1177+
{
1178+
source__currency: 'USD',
1179+
source__product_category: 'some category B',
1180+
source__price: '0'
1181+
}
1182+
],
1183+
{ joinGraph, cubeEvaluator, compiler });
1184+
});
1185+
it('source switch - source_a + usd', async () => {
1186+
await dbRunner.runQueryTest({
1187+
dimensions: ['source.currency', 'source.product_category'],
1188+
measures: ['source.price'],
1189+
order: [{
1190+
id: 'source.product_category'
1191+
},
1192+
],
1193+
filters: [
1194+
{ dimension: 'source.currency', operator: 'equals', values: ['USD'] },
1195+
{ dimension: 'source.source', operator: 'equals', values: ['A'] }
1196+
],
1197+
}, [
1198+
{
1199+
source__currency: 'USD',
1200+
source__product_category: 'some category',
1201+
source__price: '600'
1202+
},
1203+
{
1204+
source__currency: 'USD',
1205+
source__product_category: 'some category A',
1206+
source__price: '500'
1207+
},
1208+
],
1209+
{ joinGraph, cubeEvaluator, compiler });
1210+
});
1211+
1212+
it('source switch - source_b + eur', async () => {
1213+
await dbRunner.runQueryTest({
1214+
dimensions: ['source.currency', 'source.product_category'],
1215+
measures: ['source.price'],
1216+
order: [{
1217+
id: 'source.product_category'
1218+
},
1219+
],
1220+
filters: [
1221+
{ dimension: 'source.currency', operator: 'equals', values: ['EUR'] },
1222+
{ dimension: 'source.source', operator: 'equals', values: ['B'] }
1223+
],
1224+
}, [
1225+
{
1226+
source__currency: 'EUR',
1227+
source__product_category: 'some category',
1228+
source__price: '600'
1229+
},
1230+
{
1231+
source__currency: 'EUR',
1232+
source__product_category: 'some category B',
1233+
source__price: '800'
1234+
},
1235+
],
1236+
{ joinGraph, cubeEvaluator, compiler });
1237+
});
9401238
} else {
9411239
// This test is working only in tesseract
9421240
test.skip('calc groups testst', () => { expect(1).toBe(1); });

packages/cubejs-schema-compiler/test/integration/utils/BaseDbRunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class BaseDbRunner {
3030

3131
const res = await this.testQuery(sqlAndParams);
3232
console.log(JSON.stringify(res));
33-
console.log("!!!! res: ", res);
33+
console.log('!!!! res: ', res);
3434

3535
expect(res).toEqual(
3636
expectedResult

0 commit comments

Comments
 (0)