Skip to content

Commit a381afa

Browse files
committed
in work
1 parent cf8ccfe commit a381afa

File tree

3 files changed

+120
-11
lines changed

3 files changed

+120
-11
lines changed

packages/cubejs-schema-compiler/test/integration/postgres/bucketing.test.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ cubes:
8484
type: string
8585
add_group_by: [orders.customerId]
8686
87+
- name: changeTypeComplexWithJoin
88+
sql: >
89+
CASE
90+
WHEN {revenueYearAgo} IS NULL THEN 'New'
91+
WHEN {revenue} > {revenueYearAgo} THEN 'Grow'
92+
ELSE 'Down'
93+
END
94+
multi_stage: true
95+
type: string
96+
add_group_by: [first_date.customerId]
97+
8798
- name: changeTypeConcat
8899
sql: "CONCAT({changeTypeComplex}, '-test')"
89100
type: string
@@ -115,6 +126,45 @@ cubes:
115126
END
116127
type: string
117128
129+
- name: first_date
130+
sql: >
131+
SELECT 1 AS id, '2023-03-01T00:00:00Z'::timestamptz AS createdAt, 1 AS customerId UNION ALL
132+
SELECT 8 AS id, '2023-09-01T00:00:00Z'::timestamptz AS createdAt, 2 AS customerId UNION ALL
133+
SELECT 16 AS id, '2024-09-01T00:00:00Z'::timestamptz AS createdAt, 3 AS customerId UNION ALL
134+
SELECT 23 AS id, '2025-03-01T00:00:00Z'::timestamptz AS createdAt, 4 AS customerId UNION ALL
135+
SELECT 29 AS id, '2025-03-01T00:00:00Z'::timestamptz AS createdAt, 5 AS customerId UNION ALL
136+
SELECT 36 AS id, '2025-09-01T00:00:00Z'::timestamptz AS createdAt, 6 AS customerId
137+
138+
joins:
139+
- name: orders
140+
sql: "{first_date.customerId} = {orders.customerId}"
141+
relationship: one_to_many
142+
143+
dimensions:
144+
- name: customerId
145+
sql: customerId
146+
type: number
147+
148+
- name: createdAt
149+
sql: createdAt
150+
type: time
151+
152+
- name: customerType
153+
sql: >
154+
CASE
155+
WHEN {orders.revenue} < 10000 THEN 'Low'
156+
WHEN {orders.revenue} < 20000 THEN 'Medium'
157+
ELSE 'Top'
158+
END
159+
multi_stage: true
160+
type: string
161+
add_group_by: [first_date.customerId]
162+
163+
- name: customerTypeConcat
164+
sql: "CONCAT('Customer type: ', {customerType})"
165+
multi_stage: true
166+
type: string
167+
add_group_by: [first_date.customerId]
118168
119169
120170
`);
@@ -288,6 +338,62 @@ cubes:
288338
},
289339
],
290340
{ joinGraph, cubeEvaluator, compiler }));
341+
it('bucketing with join and bucket dimension', async () => dbRunner.runQueryTest({
342+
dimensions: ['orders.changeTypeComplexWithJoin'],
343+
measures: ['orders.revenue', 'orders.revenueYearAgo'],
344+
timeDimensions: [
345+
{
346+
dimension: 'orders.createdAt',
347+
granularity: 'year',
348+
dateRange: ['2024-01-02T00:00:00', '2026-01-01T00:00:00']
349+
}
350+
],
351+
timezone: 'UTC',
352+
order: [{
353+
id: 'orders.changeTypeComplexWithJoin'
354+
}, { id: 'orders.createdAt' }],
355+
},
356+
[
357+
{
358+
orders__change_type_complex_with_join: 'Down',
359+
orders__created_at_year: '2024-01-01T00:00:00.000Z',
360+
orders__revenue: '20400',
361+
orders__revenue_year_ago: '22800'
362+
},
363+
{
364+
orders__change_type_complex_with_join: 'Down',
365+
orders__created_at_year: '2025-01-01T00:00:00.000Z',
366+
orders__revenue: '17800',
367+
orders__revenue_year_ago: '20400'
368+
},
369+
{
370+
orders__change_type_complex_with_join: 'Grow',
371+
orders__created_at_year: '2024-01-01T00:00:00.000Z',
372+
orders__revenue: '11700',
373+
orders__revenue_year_ago: '9400'
374+
},
375+
{
376+
orders__change_type_complex_with_join: 'Grow',
377+
orders__created_at_year: '2025-01-01T00:00:00.000Z',
378+
orders__revenue: '14100',
379+
orders__revenue_year_ago: '11700'
380+
},
381+
],
382+
{ joinGraph, cubeEvaluator, compiler }));
383+
it('bucketing dim reference other cube measure', async () => dbRunner.runQueryTest({
384+
dimensions: ['first_date.customerType'],
385+
measures: ['orders.revenue'],
386+
timezone: 'UTC',
387+
order: [{
388+
id: 'first_date.customerType'
389+
}],
390+
},
391+
[
392+
{ first_date__customer_type: 'Low', orders__revenue: '8100' },
393+
{ first_date__customer_type: 'Medium', orders__revenue: '41700' },
394+
{ first_date__customer_type: 'Top', orders__revenue: '46400' }
395+
],
396+
{ joinGraph, cubeEvaluator, compiler }));
291397
} else {
292398
// This test is working only in tesseract
293399
test.skip('multi stage over sub query', () => { expect(1).toBe(1); });

rust/cubesqlplanner/cubesqlplanner/src/planner/sql_evaluator/collectors/join_hints_collector.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,20 @@ impl TraversalVisitor for JoinHintsCollector {
2727
_: &Self::State,
2828
) -> Result<Option<Self::State>, CubeError> {
2929
if node.is_multi_stage() {
30-
//We don't add multi-stage members childs to join hints
30+
if let Ok(dim) = node.as_dimension() {
31+
if let Some(add_group_by) = dim.add_group_by() {
32+
for item in add_group_by.iter() {
33+
self.apply(item, &())?;
34+
}
35+
for (dep, path) in dim.get_dependencies_with_path().into_iter() {
36+
if let Ok(dim) = dep.as_dimension() {
37+
if dim.is_multi_stage() {
38+
self.on_node_traverse(&dep, &path, &())?;
39+
}
40+
}
41+
}
42+
}
43+
}
3144
return Ok(None);
3245
}
3346

rust/cubesqlplanner/cubesqlplanner/src/planner/sql_evaluator/symbols/dimension_symbol.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,6 @@ impl DimensionSymbol {
238238
if let Some(member_sql) = &self.longitude {
239239
member_sql.extract_symbol_deps(&mut deps);
240240
}
241-
if let Some(add_group_by) = &self.add_group_by {
242-
for member_sql in add_group_by {
243-
deps.extend(member_sql.get_dependencies().into_iter());
244-
}
245-
}
246241
if let Some(case) = &self.case {
247242
case.extract_symbol_deps(&mut deps);
248243
}
@@ -263,11 +258,6 @@ impl DimensionSymbol {
263258
if let Some(case) = &self.case {
264259
case.extract_symbol_deps_with_path(&mut deps);
265260
}
266-
if let Some(add_group_by) = &self.add_group_by {
267-
for member_sql in add_group_by {
268-
deps.extend(member_sql.get_dependencies_with_path().into_iter());
269-
}
270-
}
271261
deps
272262
}
273263

0 commit comments

Comments
 (0)