Skip to content

Commit c9ebfbc

Browse files
authored
fix(schema-compiler): exclude time dimensions w/o granularities from select list in base queries (#9614)
* typings and linter fixes * fix(schema-compiler): exclude time dimension w/o granularities from select list in base queries * some tests refactoring * add tests
1 parent 84419ae commit c9ebfbc

File tree

4 files changed

+101
-46
lines changed

4 files changed

+101
-46
lines changed

packages/cubejs-schema-compiler/src/adapter/BaseFilter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ export class BaseFilter extends BaseDimension {
389389
return this.query.afterOrOnDateFilter(column, after);
390390
}
391391

392-
public formatFromDate(date: string) {
392+
public formatFromDate(date: string): string {
393393
if (date) {
394394
if (this.query.timestampPrecision() === 3) {
395395
if (date.match(dateTimeLocalMsRegex)) {
@@ -425,7 +425,7 @@ export class BaseFilter extends BaseDimension {
425425
return this.query.inDbTimeZone(this.formatFromDate(date));
426426
}
427427

428-
public formatToDate(date: string) {
428+
public formatToDate(date: string): string {
429429
if (date) {
430430
if (this.query.timestampPrecision() === 3) {
431431
if (date.match(dateTimeLocalMsRegex)) {

packages/cubejs-schema-compiler/src/adapter/BaseTimeDimension.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { DimensionDefinition, SegmentDefinition } from '../compiler/CubeEvaluato
1313
import { Granularity } from './Granularity';
1414

1515
export class BaseTimeDimension extends BaseFilter {
16-
public readonly dateRange: any;
16+
public readonly dateRange: [string, string];
1717

1818
public readonly granularityObj: Granularity | undefined;
1919

@@ -44,26 +44,23 @@ export class BaseTimeDimension extends BaseFilter {
4444
}
4545

4646
public selectColumns() {
47-
const context = this.query.safeEvaluateSymbolContext();
48-
if (!context.granularityOverride && !this.granularityObj) {
47+
if (!this.granularityObj) {
4948
return null;
5049
}
5150

5251
return super.selectColumns();
5352
}
5453

5554
public hasNoRemapping() {
56-
const context = this.query.safeEvaluateSymbolContext();
57-
if (!context.granularityOverride && !this.granularityObj) {
55+
if (!this.granularityObj) {
5856
return false;
5957
}
6058

6159
return super.hasNoRemapping();
6260
}
6361

6462
public aliasName() {
65-
const context = this.query.safeEvaluateSymbolContext();
66-
if (!context.granularityOverride && !this.granularityObj) {
63+
if (!this.granularityObj) {
6764
return null;
6865
}
6966

@@ -74,7 +71,7 @@ export class BaseTimeDimension extends BaseFilter {
7471
const actualGranularity = granularity || this.granularityObj?.granularity || 'day';
7572

7673
const fullName = `${this.dimension}.${actualGranularity}`;
77-
if (this.query.options.memberToAlias && this.query.options.memberToAlias[fullName]) {
74+
if (this.query.options.memberToAlias?.[fullName]) {
7875
return this.query.options.memberToAlias[fullName];
7976
}
8077

@@ -110,7 +107,7 @@ export class BaseTimeDimension extends BaseFilter {
110107
granularity: granularityName
111108
}) : this.granularityObj;
112109

113-
if ((context.renderedReference || {})[path]) {
110+
if (context.renderedReference?.[path]) {
114111
return context.renderedReference[path];
115112
}
116113

@@ -158,7 +155,7 @@ export class BaseTimeDimension extends BaseFilter {
158155
return super.filterParams();
159156
}
160157

161-
protected dateFromFormattedValue: any | null = null;
158+
protected dateFromFormattedValue: string | null = null;
162159

163160
public dateFromFormatted() {
164161
if (!this.dateFromFormattedValue) {
@@ -192,7 +189,7 @@ export class BaseTimeDimension extends BaseFilter {
192189
return this.query.dateTimeCast(this.query.paramAllocator.allocateParam(this.dateRange ? this.dateFromFormatted() : BUILD_RANGE_START_LOCAL));
193190
}
194191

195-
protected dateToFormattedValue: any | null = null;
192+
protected dateToFormattedValue: string | null = null;
196193

197194
public dateToFormatted() {
198195
if (!this.dateToFormattedValue) {

packages/cubejs-schema-compiler/test/integration/postgres/multi-stage.test.ts

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
getEnv,
33
} from '@cubejs-backend/shared';
4-
import { PostgresQuery } from '../../../src/adapter/PostgresQuery';
54
import { prepareYamlCompiler } from '../../unit/PrepareCompiler';
65
import { dbRunner } from './PostgresDBRunner';
76

@@ -189,38 +188,37 @@ views:
189188
190189
`);
191190

192-
async function runQueryTest(q, expectedResult) {
193-
if (!getEnv('nativeSqlPlanner')) {
194-
return;
195-
}
196-
await compiler.compile();
197-
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, q);
198-
199-
console.log(query.buildSqlAndParams());
200-
201-
const res = await dbRunner.testQuery(query.buildSqlAndParams());
202-
console.log(JSON.stringify(res));
203-
204-
expect(res).toEqual(
205-
expectedResult
206-
);
207-
}
208-
209-
it('multi stage over sub query', async () => runQueryTest({
210-
measures: ['orders.revenue', 'orders.revenue_1_y_ago', 'orders.cagr_1_y'],
211-
timeDimensions: [
212-
{
213-
dimension: 'orders.date',
214-
granularity: 'year'
215-
}
216-
],
217-
timezone: 'UTC'
218-
}, [
219-
220-
{ orders__date_year: '2023-01-01T00:00:00.000Z',
191+
if (getEnv('nativeSqlPlanner')) {
192+
it('multi stage over sub query', async () => dbRunner.runQueryTest({
193+
measures: ['orders.revenue', 'orders.revenue_1_y_ago', 'orders.cagr_1_y'],
194+
timeDimensions: [
195+
{
196+
dimension: 'orders.date',
197+
granularity: 'year'
198+
}
199+
],
200+
timezone: 'UTC'
201+
}, [{
202+
orders__date_year: '2023-01-01T00:00:00.000Z',
221203
orders__revenue: '15',
222204
orders__revenue_1_y_ago: '5',
223-
orders__cagr_1_y: '2.0000000000000000' },
224-
{ orders__date_year: '2024-01-01T00:00:00.000Z', orders__revenue: '30', orders__revenue_1_y_ago: '15', orders__cagr_1_y: '1.0000000000000000' },
225-
{ orders__date_year: '2025-01-01T00:00:00.000Z', orders__revenue: '5', orders__revenue_1_y_ago: '30', orders__cagr_1_y: '-0.83333333333333333333' }]));
205+
orders__cagr_1_y: '2.0000000000000000'
206+
},
207+
{
208+
orders__date_year: '2024-01-01T00:00:00.000Z',
209+
orders__revenue: '30',
210+
orders__revenue_1_y_ago: '15',
211+
orders__cagr_1_y: '1.0000000000000000'
212+
},
213+
{
214+
orders__date_year: '2025-01-01T00:00:00.000Z',
215+
orders__revenue: '5',
216+
orders__revenue_1_y_ago: '30',
217+
orders__cagr_1_y: '-0.83333333333333333333'
218+
}],
219+
{ joinGraph, cubeEvaluator, compiler }));
220+
} else {
221+
// This test is working only in tesseract
222+
test.skip('multi stage over sub query', () => { expect(1).toBe(1); });
223+
}
226224
});

packages/cubejs-schema-compiler/test/integration/postgres/sql-generation.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,66 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
11921192
}
11931193
]));
11941194

1195+
it('rolling window with same td with and without granularity', async () => runQueryTest({
1196+
measures: [
1197+
'visitors.countRollingWeekToDate'
1198+
],
1199+
timeDimensions: [
1200+
{
1201+
dimension: 'visitors.created_at',
1202+
granularity: 'day',
1203+
dateRange: ['2017-01-01', '2017-01-10']
1204+
},
1205+
{
1206+
dimension: 'visitors.created_at',
1207+
dateRange: ['2017-01-01', '2017-01-10']
1208+
}
1209+
],
1210+
order: [{
1211+
id: 'visitors.created_at'
1212+
}],
1213+
timezone: 'America/Los_Angeles'
1214+
}, [{
1215+
visitors__count_rolling_week_to_date: null,
1216+
visitors__created_at_day: '2017-01-01T00:00:00.000Z',
1217+
},
1218+
{
1219+
visitors__count_rolling_week_to_date: '1',
1220+
visitors__created_at_day: '2017-01-02T00:00:00.000Z',
1221+
},
1222+
{
1223+
visitors__count_rolling_week_to_date: '1',
1224+
visitors__created_at_day: '2017-01-03T00:00:00.000Z',
1225+
},
1226+
{
1227+
visitors__count_rolling_week_to_date: '2',
1228+
visitors__created_at_day: '2017-01-04T00:00:00.000Z',
1229+
},
1230+
{
1231+
visitors__count_rolling_week_to_date: '3',
1232+
visitors__created_at_day: '2017-01-05T00:00:00.000Z',
1233+
},
1234+
{
1235+
visitors__count_rolling_week_to_date: '5',
1236+
visitors__created_at_day: '2017-01-06T00:00:00.000Z',
1237+
},
1238+
{
1239+
visitors__count_rolling_week_to_date: '5',
1240+
visitors__created_at_day: '2017-01-07T00:00:00.000Z',
1241+
},
1242+
{
1243+
visitors__count_rolling_week_to_date: '5',
1244+
visitors__created_at_day: '2017-01-08T00:00:00.000Z',
1245+
},
1246+
{
1247+
visitors__count_rolling_week_to_date: null,
1248+
visitors__created_at_day: '2017-01-09T00:00:00.000Z',
1249+
},
1250+
{
1251+
visitors__count_rolling_week_to_date: null,
1252+
visitors__created_at_day: '2017-01-10T00:00:00.000Z',
1253+
}]));
1254+
11951255
it('two rolling windows with two time dimension granularities', async () => runQueryTest({
11961256
measures: [
11971257
'visitors.countRollingUnbounded',

0 commit comments

Comments
 (0)