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: 2 additions & 2 deletions packages/cubejs-schema-compiler/src/adapter/BaseFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export class BaseFilter extends BaseDimension {
return this.query.afterOrOnDateFilter(column, after);
}

public formatFromDate(date: string) {
public formatFromDate(date: string): string {
if (date) {
if (this.query.timestampPrecision() === 3) {
if (date.match(dateTimeLocalMsRegex)) {
Expand Down Expand Up @@ -425,7 +425,7 @@ export class BaseFilter extends BaseDimension {
return this.query.inDbTimeZone(this.formatFromDate(date));
}

public formatToDate(date: string) {
public formatToDate(date: string): string {
if (date) {
if (this.query.timestampPrecision() === 3) {
if (date.match(dateTimeLocalMsRegex)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { DimensionDefinition, SegmentDefinition } from '../compiler/CubeEvaluato
import { Granularity } from './Granularity';

export class BaseTimeDimension extends BaseFilter {
public readonly dateRange: any;
public readonly dateRange: [string, string];

public readonly granularityObj: Granularity | undefined;

Expand Down Expand Up @@ -44,26 +44,23 @@ export class BaseTimeDimension extends BaseFilter {
}

public selectColumns() {
const context = this.query.safeEvaluateSymbolContext();
if (!context.granularityOverride && !this.granularityObj) {
if (!this.granularityObj) {
return null;
}

return super.selectColumns();
}

public hasNoRemapping() {
const context = this.query.safeEvaluateSymbolContext();
if (!context.granularityOverride && !this.granularityObj) {
if (!this.granularityObj) {
return false;
}

return super.hasNoRemapping();
}

public aliasName() {
const context = this.query.safeEvaluateSymbolContext();
if (!context.granularityOverride && !this.granularityObj) {
if (!this.granularityObj) {
return null;
}

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

const fullName = `${this.dimension}.${actualGranularity}`;
if (this.query.options.memberToAlias && this.query.options.memberToAlias[fullName]) {
if (this.query.options.memberToAlias?.[fullName]) {
return this.query.options.memberToAlias[fullName];
}

Expand Down Expand Up @@ -110,7 +107,7 @@ export class BaseTimeDimension extends BaseFilter {
granularity: granularityName
}) : this.granularityObj;

if ((context.renderedReference || {})[path]) {
if (context.renderedReference?.[path]) {
return context.renderedReference[path];
}

Expand Down Expand Up @@ -158,7 +155,7 @@ export class BaseTimeDimension extends BaseFilter {
return super.filterParams();
}

protected dateFromFormattedValue: any | null = null;
protected dateFromFormattedValue: string | null = null;

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

protected dateToFormattedValue: any | null = null;
protected dateToFormattedValue: string | null = null;

public dateToFormatted() {
if (!this.dateToFormattedValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
getEnv,
} from '@cubejs-backend/shared';
import { PostgresQuery } from '../../../src/adapter/PostgresQuery';
import { prepareYamlCompiler } from '../../unit/PrepareCompiler';
import { dbRunner } from './PostgresDBRunner';

Expand Down Expand Up @@ -189,38 +188,37 @@ views:

`);

async function runQueryTest(q, expectedResult) {
if (!getEnv('nativeSqlPlanner')) {
return;
}
await compiler.compile();
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, q);

console.log(query.buildSqlAndParams());

const res = await dbRunner.testQuery(query.buildSqlAndParams());
console.log(JSON.stringify(res));

expect(res).toEqual(
expectedResult
);
}

it('multi stage over sub query', async () => runQueryTest({
measures: ['orders.revenue', 'orders.revenue_1_y_ago', 'orders.cagr_1_y'],
timeDimensions: [
{
dimension: 'orders.date',
granularity: 'year'
}
],
timezone: 'UTC'
}, [

{ orders__date_year: '2023-01-01T00:00:00.000Z',
if (getEnv('nativeSqlPlanner')) {
it('multi stage over sub query', async () => dbRunner.runQueryTest({
measures: ['orders.revenue', 'orders.revenue_1_y_ago', 'orders.cagr_1_y'],
timeDimensions: [
{
dimension: 'orders.date',
granularity: 'year'
}
],
timezone: 'UTC'
}, [{
orders__date_year: '2023-01-01T00:00:00.000Z',
orders__revenue: '15',
orders__revenue_1_y_ago: '5',
orders__cagr_1_y: '2.0000000000000000' },
{ orders__date_year: '2024-01-01T00:00:00.000Z', orders__revenue: '30', orders__revenue_1_y_ago: '15', orders__cagr_1_y: '1.0000000000000000' },
{ orders__date_year: '2025-01-01T00:00:00.000Z', orders__revenue: '5', orders__revenue_1_y_ago: '30', orders__cagr_1_y: '-0.83333333333333333333' }]));
orders__cagr_1_y: '2.0000000000000000'
},
{
orders__date_year: '2024-01-01T00:00:00.000Z',
orders__revenue: '30',
orders__revenue_1_y_ago: '15',
orders__cagr_1_y: '1.0000000000000000'
},
{
orders__date_year: '2025-01-01T00:00:00.000Z',
orders__revenue: '5',
orders__revenue_1_y_ago: '30',
orders__cagr_1_y: '-0.83333333333333333333'
}],
{ joinGraph, cubeEvaluator, compiler }));
} else {
// This test is working only in tesseract
test.skip('multi stage over sub query', () => { expect(1).toBe(1); });
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,66 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
}
]));

it('rolling window with same td with and without granularity', async () => runQueryTest({
measures: [
'visitors.countRollingWeekToDate'
],
timeDimensions: [
{
dimension: 'visitors.created_at',
granularity: 'day',
dateRange: ['2017-01-01', '2017-01-10']
},
{
dimension: 'visitors.created_at',
dateRange: ['2017-01-01', '2017-01-10']
}
],
order: [{
id: 'visitors.created_at'
}],
timezone: 'America/Los_Angeles'
}, [{
visitors__count_rolling_week_to_date: null,
visitors__created_at_day: '2017-01-01T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '1',
visitors__created_at_day: '2017-01-02T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '1',
visitors__created_at_day: '2017-01-03T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '2',
visitors__created_at_day: '2017-01-04T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '3',
visitors__created_at_day: '2017-01-05T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '5',
visitors__created_at_day: '2017-01-06T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '5',
visitors__created_at_day: '2017-01-07T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '5',
visitors__created_at_day: '2017-01-08T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: null,
visitors__created_at_day: '2017-01-09T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: null,
visitors__created_at_day: '2017-01-10T00:00:00.000Z',
}]));

it('two rolling windows with two time dimension granularities', async () => runQueryTest({
measures: [
'visitors.countRollingUnbounded',
Expand Down
Loading