Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -4009,7 +4009,17 @@ export class BaseQuery {
const dimensionSql = this.dimensionSql(dimension);
return `select ${aggFunction}(${this.convertTz(dimensionSql)}) from ${this.cubeSql(cube)} ${this.asSyntaxTable} ${this.cubeAlias(cube)}`;
}
return null;

// Handle case that requires joins
const subQuery = this.newSubQuery({
dimensions: [dimension.dimension],
rowLimit: null,
});

const dimensionSql = subQuery.dimensionSql(dimension);
const fromClause = subQuery.query();

return `select ${aggFunction}(${subQuery.convertTz(dimensionSql)}) from ${fromClause}`;
}

cubeCardinalityQueries() { // TODO collect sub queries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,49 @@ describe('PreAggregations', () => {
}
]
});
`);

cube('cube_pre_agg_proxy_a', {
sql: \`SELECT '2025-10-01 12:00:00'::timestamp as starts_at\`,

dimensions: {
starts_at: {
sql: \`\${CUBE}.starts_at\`,
type: 'time'
}
}
});

cube('cube_pre_agg_proxy_b', {
sql: \`SELECT 'id' as id\`,

joins: {
cube_pre_agg_proxy_a: {
relationship: 'one_to_one',
sql: '1 = 1'
}
},

dimensions: {
id: {
sql: \`\${CUBE}.id\`,
type: 'string',
primary_key: true
},

terminal_date: {
type: 'time',
sql: \`\${cube_pre_agg_proxy_a.starts_at}\`
}
},

pre_aggregations: {
main: {
time_dimension: terminal_date,
granularity: 'day'
}
}
});
`);

it('simple pre-aggregation', async () => {
await compiler.compile();
Expand Down Expand Up @@ -2773,4 +2815,39 @@ describe('PreAggregations', () => {
expect(loadSql[0]).not.toMatch(/GROUP BY/);
expect(loadSql[0]).toMatch(/THEN 1 END `real_time_lambda_visitors__count`/);
});

it('querying proxied to external cube pre-aggregation time-dimension', async () => {
await compiler.compile();

const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: [],
dimensions: [],
timezone: 'America/Los_Angeles',
preAggregationsSchema: '',
timeDimensions: [{
dimension: 'cube_pre_agg_proxy_b.terminal_date',
granularity: 'day',
}],
order: [],
});

const queryAndParams = query.buildSqlAndParams();
console.log(queryAndParams);
const preAggregationsDescription = query.preAggregations?.preAggregationsDescription();
console.log(JSON.stringify(preAggregationsDescription, null, 2));

expect((<any>preAggregationsDescription)[0].loadSql[0]).toMatch(/main/);

const queries = dbRunner.tempTablePreAggregations(preAggregationsDescription);

console.log(JSON.stringify(queries.concat(queryAndParams)));

return dbRunner.evaluateQueryWithPreAggregations(query).then(res => {
expect(res).toEqual(
[{
cube_pre_agg_proxy_b__terminal_date_day: '2025-10-01T00:00:00.000Z',
}]
);
});
});
});
Loading