Skip to content
Merged
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
4 changes: 4 additions & 0 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,10 @@ export class BaseQuery {
return evaluateSql === '*' ? '1' : evaluateSql;
}
if (this.ungrouped) {
if ((this.safeEvaluateSymbolContext().ungroupedAliasesForCumulative || {})[measurePath]) {
evaluateSql = this.safeEvaluateSymbolContext().ungroupedAliasesForCumulative[measurePath];
}

if (symbol.type === 'count' || symbol.type === 'countDistinct' || symbol.type === 'countDistinctApprox') {
const sql = this.caseWhenStatement([{ sql: `(${evaluateSql}) IS NOT NULL`, label: '1' }]);
return evaluateSql === '*' ? '1' : sql;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ describe('SQL Generation', () => {
\${FILTER_PARAMS.visitor_checkins.created_at.filter('created_at')} AND
\${FILTER_GROUP(FILTER_PARAMS.visitor_checkins.created_at.filter("(created_at - INTERVAL '3 DAY')"), FILTER_PARAMS.visitor_checkins.source.filter('source'))}
\`,
sql_alias: \`vc\`,

rewriteQueries: true,

Expand Down Expand Up @@ -568,28 +569,28 @@ describe('SQL Generation', () => {
visitors__created_at_day: '2017-01-02T00:00:00.000Z',
visitors__visitor_revenue: '100',
visitors__visitor_count: '1',
visitor_checkins__visitor_checkins_count: '3',
vc__visitor_checkins_count: '3',
visitors__per_visitor_revenue: '100'
},
{
visitors__created_at_day: '2017-01-04T00:00:00.000Z',
visitors__visitor_revenue: '200',
visitors__visitor_count: '1',
visitor_checkins__visitor_checkins_count: '2',
vc__visitor_checkins_count: '2',
visitors__per_visitor_revenue: '200'
},
{
visitors__created_at_day: '2017-01-05T00:00:00.000Z',
visitors__visitor_revenue: null,
visitors__visitor_count: '1',
visitor_checkins__visitor_checkins_count: '1',
vc__visitor_checkins_count: '1',
visitors__per_visitor_revenue: null
},
{
visitors__created_at_day: '2017-01-06T00:00:00.000Z',
visitors__visitor_revenue: null,
visitors__visitor_count: '2',
visitor_checkins__visitor_checkins_count: '0',
vc__visitor_checkins_count: '0',
visitors__per_visitor_revenue: null
}
]
Expand Down Expand Up @@ -627,7 +628,7 @@ describe('SQL Generation', () => {
}, [{
visitors__visitor_revenue: '300',
visitors__visitor_count: '5',
visitor_checkins__visitor_checkins_count: '6',
vc__visitor_checkins_count: '6',
visitors__per_visitor_revenue: '60'
}]));

Expand Down Expand Up @@ -748,22 +749,22 @@ describe('SQL Generation', () => {
{
visitors__created_at_day: '2017-01-02T00:00:00.000Z',
visitors__revenue_rolling: null,
visitor_checkins__visitor_checkins_count: '3'
vc__visitor_checkins_count: '3'
},
{
visitors__created_at_day: '2017-01-04T00:00:00.000Z',
visitors__revenue_rolling: '100',
visitor_checkins__visitor_checkins_count: '2'
vc__visitor_checkins_count: '2'
},
{
visitors__created_at_day: '2017-01-05T00:00:00.000Z',
visitors__revenue_rolling: '200',
visitor_checkins__visitor_checkins_count: '1'
vc__visitor_checkins_count: '1'
},
{
visitors__created_at_day: '2017-01-06T00:00:00.000Z',
visitors__revenue_rolling: '500',
visitor_checkins__visitor_checkins_count: '0'
vc__visitor_checkins_count: '0'
}
]));

Expand Down Expand Up @@ -979,7 +980,7 @@ describe('SQL Generation', () => {
return dbRunner.testQuery(query.buildSqlAndParams()).then(res => {
console.log(JSON.stringify(res));
expect(res).toEqual(
[{ visitor_checkins__revenue_per_checkin: '50' }]
[{ vc__revenue_per_checkin: '50' }]
);
});
});
Expand All @@ -1000,7 +1001,7 @@ describe('SQL Generation', () => {
return dbRunner.testQuery(query.buildSqlAndParams()).then(res => {
console.log(JSON.stringify(res));
expect(res).toEqual(
[{ visitor_checkins__google_sourced_checkins: '1' }]
[{ vc__google_sourced_checkins: '1' }]
);
});
});
Expand All @@ -1024,7 +1025,7 @@ describe('SQL Generation', () => {
return dbRunner.testQuery(query.buildSqlAndParams()).then(res => {
console.log(JSON.stringify(res));
expect(res).toEqual(
[{ visitor_checkins__google_sourced_checkins: '1' }]
[{ vc__google_sourced_checkins: '1' }]
);
});
});
Expand Down Expand Up @@ -1435,19 +1436,78 @@ describe('SQL Generation', () => {
}]
}, [
{
visitor_checkins__cards_count: '0',
vc__cards_count: '0',
visitors__visitor_revenue: '300'
},
{
visitor_checkins__cards_count: '1',
vc__cards_count: '1',
visitors__visitor_revenue: '100'
},
{
visitor_checkins__cards_count: null,
vc__cards_count: null,
visitors__visitor_revenue: null
}
]));

it('ungrouped cumulative query', async () => {
await compiler.compile();

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

console.log(query.buildSqlAndParams());

return dbRunner.testQuery(query.buildSqlAndParams()).then(res => {
console.log(JSON.stringify(res));
expect(res).toEqual(
[
{
vc__id: 3,
vc__created_at_day: '2017-01-04T00:00:00.000Z',
vc__visitor_checkins_count: 1,
vc__visitor_checkins_rolling: 1
},
{
vc__id: 4,
vc__created_at_day: '2017-01-04T00:00:00.000Z',
vc__visitor_checkins_count: 1,
vc__visitor_checkins_rolling: 1
},
{
vc__id: 5,
vc__created_at_day: '2017-01-04T00:00:00.000Z',
vc__visitor_checkins_count: 1,
vc__visitor_checkins_rolling: 1
},
{
vc__id: 6,
vc__created_at_day: '2017-01-05T00:00:00.000Z',
vc__visitor_checkins_count: 1,
vc__visitor_checkins_rolling: 1
}
]
);
});
});

it('join rollup pre-aggregation', async () => {
await compiler.compile();

Expand Down Expand Up @@ -1490,7 +1550,7 @@ describe('SQL Generation', () => {
expect(res).toEqual(
[
{
visitor_checkins__source: 'google',
vc__source: 'google',
visitors__created_at_day: '2017-01-02T00:00:00.000Z',
visitors__per_visitor_revenue: '100'
}
Expand Down Expand Up @@ -1537,7 +1597,7 @@ describe('SQL Generation', () => {
console.log(JSON.stringify(res));
expect(res).toEqual(
[{
visitor_checkins__source: 'google',
vc__source: 'google',
visitors__created_at_day: '2017-01-02T00:00:00.000Z',
visitors__visitor_revenue: '100'
}]
Expand All @@ -1564,7 +1624,7 @@ describe('SQL Generation', () => {
return dbRunner.testQuery(query.buildSqlAndParams()).then(res => {
console.log(JSON.stringify(res));
expect(res).toEqual(
[{ visitor_checkins__revenue_per_checkin: '60' }]
[{ vc__revenue_per_checkin: '60' }]
);
});
});
Expand All @@ -1590,7 +1650,7 @@ describe('SQL Generation', () => {
return dbRunner.testQuery(query.buildSqlAndParams()).then(res => {
console.log(JSON.stringify(res));
expect(res).toEqual(
[{ visitor_checkins__revenue_per_checkin: '50' }]
[{ vc__revenue_per_checkin: '50' }]
);
});
});
Expand Down Expand Up @@ -1635,12 +1695,12 @@ describe('SQL Generation', () => {
ungrouped: true,
allowUngroupedWithoutPrimaryKey: true,
}, [
{ visitor_checkins__created_at_day: '2017-01-02T00:00:00.000Z', visitor_checkins__google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-03T00:00:00.000Z', visitor_checkins__google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-05T00:00:00.000Z', visitor_checkins__google_sourced_checkins: 1 },
{ vc__created_at_day: '2017-01-02T00:00:00.000Z', vc__google_sourced_checkins: null },
{ vc__created_at_day: '2017-01-03T00:00:00.000Z', vc__google_sourced_checkins: null },
{ vc__created_at_day: '2017-01-04T00:00:00.000Z', vc__google_sourced_checkins: null },
{ vc__created_at_day: '2017-01-04T00:00:00.000Z', vc__google_sourced_checkins: null },
{ vc__created_at_day: '2017-01-04T00:00:00.000Z', vc__google_sourced_checkins: null },
{ vc__created_at_day: '2017-01-05T00:00:00.000Z', vc__google_sourced_checkins: 1 },
]));

it('ungrouped filtered distinct count', () => runQueryTest({
Expand All @@ -1659,12 +1719,12 @@ describe('SQL Generation', () => {
ungrouped: true,
allowUngroupedWithoutPrimaryKey: true,
}, [
{ visitor_checkins__created_at_day: '2017-01-02T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-03T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: null },
{ visitor_checkins__created_at_day: '2017-01-05T00:00:00.000Z', visitor_checkins__unique_google_sourced_checkins: 1 },
{ vc__created_at_day: '2017-01-02T00:00:00.000Z', vc__unique_google_sourced_checkins: null },
{ vc__created_at_day: '2017-01-03T00:00:00.000Z', vc__unique_google_sourced_checkins: null },
{ vc__created_at_day: '2017-01-04T00:00:00.000Z', vc__unique_google_sourced_checkins: null },
{ vc__created_at_day: '2017-01-04T00:00:00.000Z', vc__unique_google_sourced_checkins: null },
{ vc__created_at_day: '2017-01-04T00:00:00.000Z', vc__unique_google_sourced_checkins: null },
{ vc__created_at_day: '2017-01-05T00:00:00.000Z', vc__unique_google_sourced_checkins: 1 },
]));

it('ungrouped ratio measure', () => runQueryTest({
Expand All @@ -1683,12 +1743,12 @@ describe('SQL Generation', () => {
ungrouped: true,
allowUngroupedWithoutPrimaryKey: true,
}, [
{ visitor_checkins__created_at_day: '2017-01-02T00:00:00.000Z', visitor_checkins__unique_sources_per_checking: 1 },
{ visitor_checkins__created_at_day: '2017-01-03T00:00:00.000Z', visitor_checkins__unique_sources_per_checking: 1 },
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__unique_sources_per_checking: 1 },
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__unique_sources_per_checking: 1 },
{ visitor_checkins__created_at_day: '2017-01-04T00:00:00.000Z', visitor_checkins__unique_sources_per_checking: 1 },
{ visitor_checkins__created_at_day: '2017-01-05T00:00:00.000Z', visitor_checkins__unique_sources_per_checking: 1 },
{ vc__created_at_day: '2017-01-02T00:00:00.000Z', vc__unique_sources_per_checking: 1 },
{ vc__created_at_day: '2017-01-03T00:00:00.000Z', vc__unique_sources_per_checking: 1 },
{ vc__created_at_day: '2017-01-04T00:00:00.000Z', vc__unique_sources_per_checking: 1 },
{ vc__created_at_day: '2017-01-04T00:00:00.000Z', vc__unique_sources_per_checking: 1 },
{ vc__created_at_day: '2017-01-04T00:00:00.000Z', vc__unique_sources_per_checking: 1 },
{ vc__created_at_day: '2017-01-05T00:00:00.000Z', vc__unique_sources_per_checking: 1 },
]));

it('builds geo dimension', () => runQueryTest({
Expand Down Expand Up @@ -2116,7 +2176,7 @@ describe('SQL Generation', () => {
}],
order: []
}, [
{ visitor_checkins__visitor_checkins_count: '4' }
{ vc__visitor_checkins_count: '4' }
])
);

Expand Down Expand Up @@ -2150,7 +2210,7 @@ describe('SQL Generation', () => {
}],
order: []
}, [
{ visitor_checkins__visitor_checkins_count: '4' }
{ vc__visitor_checkins_count: '4' }
])
);

Expand All @@ -2168,7 +2228,7 @@ describe('SQL Generation', () => {
}],
order: []
}, [
{ visitor_checkins__visitor_checkins_count: '4' }
{ vc__visitor_checkins_count: '4' }
])
);

Expand Down Expand Up @@ -2204,7 +2264,7 @@ describe('SQL Generation', () => {
}],
order: []
}, [
{ visitor_checkins__visitor_checkins_count: '1' }
{ vc__visitor_checkins_count: '1' }
])
);

Expand Down Expand Up @@ -2244,7 +2304,7 @@ describe('SQL Generation', () => {
}],
order: []
}, [
{ visitor_checkins__visitor_checkins_count: '1' }
{ vc__visitor_checkins_count: '1' }
])
);

Expand Down Expand Up @@ -2711,11 +2771,11 @@ describe('SQL Generation', () => {
[{
visitors__percentage_of_total: 9,
visitors__revenue: '100',
visitor_checkins__source: 'google'
vc__source: 'google'
}, {
visitors__percentage_of_total: 91,
visitors__revenue: '1000',
visitor_checkins__source: null
vc__source: null
}]
));

Expand Down
Loading