Skip to content

Commit

Permalink
fix: Error: column does not exist during in case of subQuery for roll…
Browse files Browse the repository at this point in the history
…ing window measure
  • Loading branch information
paveltiunov committed Jan 22, 2022
1 parent 25d5a28 commit 6084407
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Expand Up @@ -646,7 +646,8 @@ export class BaseQuery {
);

// TODO all having filters should be pushed down
if (toJoin.length === 1 && this.measureFilters.length === 0) {
// subQuery dimensions can introduce projection remapping
if (toJoin.length === 1 && this.measureFilters.length === 0 && this.measures.filter(m => m.expression).length === 0) {
return `${toJoin[0].replace(/^SELECT/, `SELECT ${this.topLimit()}`)} ${this.orderBy()}${this.groupByDimensionLimit()}`;
}

Expand Down
Expand Up @@ -123,6 +123,12 @@ describe('SQL Generation', () => {
subQuery: true
},
checkinsRolling: {
sql: \`\${visitor_checkins.visitorCheckinsRolling}\`,
type: \`number\`,
subQuery: true
},
checkinsWithPropagation: {
sql: \`\${visitor_checkins.visitor_checkins_count}\`,
type: \`number\`,
Expand Down Expand Up @@ -180,6 +186,14 @@ describe('SQL Generation', () => {
visitor_checkins_count: {
type: 'count'
},
visitorCheckinsRolling: {
type: 'count',
rollingWindow: {
trailing: 'unbounded'
}
},
revenue_per_checkin: {
type: 'number',
sql: \`\${visitors.visitor_revenue} / \${visitor_checkins_count}\`
Expand Down Expand Up @@ -963,6 +977,54 @@ describe('SQL Generation', () => {
});
});

it('subquery rolling', async () => {
await compiler.compile();

const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: [
'visitors.visitor_count'
],
dimensions: [
'visitors.checkinsRolling'
],
timeDimensions: [{
dimension: 'visitors.created_at',
granularity: 'day',
dateRange: ['2017-01-01', '2017-01-30']
}],
timezone: 'America/Los_Angeles',
filters: [],
order: [{
id: 'visitors.checkins'
}]
});

console.log(query.buildSqlAndParams());

return dbRunner.testQuery(query.buildSqlAndParams()).then(res => {
console.log(JSON.stringify(res));
expect(res).toEqual(
[{
visitors__checkins_rolling: '0',
visitors__created_at_day: '2017-01-06T00:00:00.000Z',
visitors__visitor_count: '2'
}, {
visitors__checkins_rolling: '1',
visitors__created_at_day: '2017-01-05T00:00:00.000Z',
visitors__visitor_count: '1'
}, {
visitors__checkins_rolling: '2',
visitors__created_at_day: '2017-01-04T00:00:00.000Z',
visitors__visitor_count: '1'
}, {
visitors__checkins_rolling: '3',
visitors__created_at_day: '2017-01-02T00:00:00.000Z',
visitors__visitor_count: '1'
}]
);
});
});

it('subquery with propagated filters', async () => {
await compiler.compile();

Expand Down

0 comments on commit 6084407

Please sign in to comment.