Skip to content

Commit

Permalink
fix: Rollup match results for rollupJoin
Browse files Browse the repository at this point in the history
  • Loading branch information
paveltiunov committed Dec 19, 2020
1 parent fa0cae0 commit 0279b13
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
6 changes: 3 additions & 3 deletions packages/cubejs-cubestore-driver/driver/CubeStoreQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CubeStoreQuery extends BaseQuery {
}

dateTimeCast(value) {
return `TIMESTAMP(${value})`;
return `to_timestamp(${value})`;
}

subtractInterval(date, interval) {
Expand All @@ -63,9 +63,9 @@ class CubeStoreQuery extends BaseQuery {

seriesSql(timeDimension) {
const values = timeDimension.timeSeries().map(
([from, to]) => `select '${from}' f, '${to}' t`
([from, to]) => `select to_timestamp('${from}') date_from, to_timestamp('${to}') date_to`
).join(' UNION ALL ');
return `SELECT TIMESTAMP(dates.f) date_from, TIMESTAMP(dates.t) date_to FROM (${values}) AS dates`;
return values;
}

concatStringsSql(strings) {
Expand Down
6 changes: 3 additions & 3 deletions packages/cubejs-playground/src/components/CachePane.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ const CachePane = ({ query, cubejsApi }) => (
scroll={{ x: true }}
columns={[
{
title: 'Rollup Table Name',
key: 'tableName',
dataIndex: 'tableName',
title: 'Rollup Name',
key: 'name',
dataIndex: 'name',
render: (text) => <b>{text}</b>,
},
{
Expand Down
69 changes: 37 additions & 32 deletions packages/cubejs-schema-compiler/adapter/PreAggregations.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PreAggregations {
if (!isInPreAggregationQuery) {
const preAggregationForQuery = this.findPreAggregationForQuery();
if (preAggregationForQuery) {
return this.preAggregationDescriptionsFor(preAggregationForQuery.cube, preAggregationForQuery);
return this.preAggregationDescriptionsFor(preAggregationForQuery);
}
}
if (
Expand All @@ -33,7 +33,7 @@ class PreAggregations {
R.map(cube => {
const foundPreAggregation = this.findPreAggregationToUseForCube(cube);
if (foundPreAggregation) {
return this.preAggregationDescriptionsFor(cube, foundPreAggregation);
return this.preAggregationDescriptionsFor(foundPreAggregation);
}
return null;
}),
Expand All @@ -49,7 +49,7 @@ class PreAggregations {
return join.joins.map(j => j.originalTo).concat([join.root]);
}

preAggregationDescriptionsFor(cube, foundPreAggregation) {
preAggregationDescriptionsFor(foundPreAggregation) {
let preAggregations = [foundPreAggregation];
if (foundPreAggregation.preAggregation.type === 'rollupJoin') {
preAggregations = foundPreAggregation.preAggregationsToJoin;
Expand Down Expand Up @@ -382,7 +382,7 @@ class PreAggregations {
);
if (preAggregation.type === 'rollupJoin') {
// TODO evaluation optimizations. Should be cached or moved to compile time.
const preAggregationsToJoin = preAggObj.canUsePreAggregation ? preAggObj.references.rollups.map(
const preAggregationsToJoin = preAggObj.references.rollups.map(
name => {
const [joinCube, joinPreAggregationName] = this.query.cubeEvaluator.parsePath('preAggregations', name);
return this.evaluatedPreAggregationObj(
Expand All @@ -392,12 +392,11 @@ class PreAggregations {
canUsePreAggregation
);
}
) : null;
);
return {
...preAggObj,
preAggregationsToJoin,
// TODO evaluation optimizations. Should be cached or moved to compile time.
rollupJoin: preAggObj.canUsePreAggregation ? this.buildRollupJoin(preAggObj, preAggregationsToJoin) : null
rollupJoin: this.buildRollupJoin(preAggObj, preAggregationsToJoin)
};
} else {
return preAggObj;
Expand All @@ -406,32 +405,37 @@ class PreAggregations {
)(preAggregations);
}

// TODO cache, check multiplication factor didn't change
// TODO check multiplication factor didn't change
buildRollupJoin(preAggObj, preAggObjsToJoin) {
const targetJoins = this.resolveJoinMembers(
this.query.joinGraph.buildJoin(this.cubesFromPreAggregation(preAggObj))
return this.query.cacheValue(
['buildRollupJoin', JSON.stringify(preAggObj), JSON.stringify(preAggObjsToJoin)],
() => {
const targetJoins = this.resolveJoinMembers(
this.query.joinGraph.buildJoin(this.cubesFromPreAggregation(preAggObj))
);
const existingJoins = R.unnest(preAggObjsToJoin.map(
p => this.resolveJoinMembers(this.query.joinGraph.buildJoin(this.cubesFromPreAggregation(p)))
));
const nonExistingJoins = targetJoins.filter(target => !existingJoins.find(
existing => existing.originalFrom === target.originalFrom &&
existing.originalTo === target.originalTo &&
R.eq(existing.fromMembers, target.fromMembers) &&
R.eq(existing.toMembers, target.toMembers)
));
if (!nonExistingJoins.length) {
throw new UserError(`Nothing to join in rollup join. Target joins ${JSON.stringify(targetJoins)} are included in existing rollup joins ${JSON.stringify(existingJoins)}`);
}
return nonExistingJoins.map(join => {
const fromPreAggObj = this.preAggObjForJoin(preAggObjsToJoin, join.fromMembers, join);
const toPreAggObj = this.preAggObjForJoin(preAggObjsToJoin, join.toMembers, join);
return {
...join,
fromPreAggObj,
toPreAggObj
};
});
}
);
const existingJoins = R.unnest(preAggObjsToJoin.map(
p => this.resolveJoinMembers(this.query.joinGraph.buildJoin(this.cubesFromPreAggregation(p)))
));
const nonExistingJoins = targetJoins.filter(target => !existingJoins.find(
existing => existing.originalFrom === target.originalFrom &&
existing.originalTo === target.originalTo &&
R.eq(existing.fromMembers, target.fromMembers) &&
R.eq(existing.toMembers, target.toMembers)
));
if (!nonExistingJoins.length) {
throw new UserError(`Nothing to join in rollup join. Target joins ${JSON.stringify(targetJoins)} are included in existing rollup joins ${JSON.stringify(existingJoins)}`);
}
return nonExistingJoins.map(join => {
const fromPreAggObj = this.preAggObjForJoin(preAggObjsToJoin, join.fromMembers, join);
const toPreAggObj = this.preAggObjForJoin(preAggObjsToJoin, join.toMembers, join);
return {
...join,
fromPreAggObj,
toPreAggObj
};
});
}

preAggObjForJoin(preAggObjsToJoin, joinMembers, join) {
Expand Down Expand Up @@ -492,7 +496,8 @@ class PreAggregations {

rollupMatchResultDescriptions() {
return this.rollupMatchResults().map(p => ({
...this.preAggregationDescriptionFor(p.cube, p),
name: this.query.cubeEvaluator.pathFromArray([p.cube, p.preAggregationName]),
tableName: this.preAggregationTableName(p.cube, p.preAggregationName, p.preAggregation),
references: p.references,
canUsePreAggregation: p.canUsePreAggregation
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,8 @@ describe('PreAggregations', function test() {
const preAggregationsDescription = query.preAggregations.preAggregationsDescription();
console.log(preAggregationsDescription);

console.log(query.preAggregations.rollupMatchResultDescriptions());

const queries = tempTablePreAggregations(preAggregationsDescription);

console.log(JSON.stringify(queries.concat(queryAndParams)));
Expand Down Expand Up @@ -1079,6 +1081,8 @@ describe('PreAggregations', function test() {
const preAggregationsDescription = query.preAggregations.preAggregationsDescription();
console.log(preAggregationsDescription);

console.log(query.preAggregations.rollupMatchResultDescriptions());

const queries = tempTablePreAggregations(preAggregationsDescription);

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

0 comments on commit 0279b13

Please sign in to comment.