Skip to content

fix(query-orchestrator): Correct local date parsing for partition start/end queries #9543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions packages/cubejs-api-gateway/src/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import structuredClone from '@ungap/structured-clone';
import {
getEnv,
getRealType,
parseLocalDate,
parseUtcIntoLocalDate,
QueryAlias,
} from '@cubejs-backend/shared';
import {
Expand Down Expand Up @@ -921,8 +921,8 @@ class ApiGateway {
// It's expected that selector.dateRange is provided in local time (without timezone)
// At the same time it is ok to get timestamps with `Z` (in UTC).
if (selector.dateRange) {
const start = parseLocalDate([{ val: selector.dateRange[0] }], 'UTC');
const end = parseLocalDate([{ val: selector.dateRange[1] }], 'UTC');
const start = parseUtcIntoLocalDate([{ val: selector.dateRange[0] }], 'UTC');
const end = parseUtcIntoLocalDate([{ val: selector.dateRange[1] }], 'UTC');
if (!start || !end) {
throw new UserError(`Cannot parse selector date range ${selector.dateRange}`);
}
Expand Down
9 changes: 5 additions & 4 deletions packages/cubejs-backend-shared/src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export const utcToLocalTimeZone = (timezone: string, timestampFormat: string, ti
return moment.tz(timestamp, 'UTC').tz(timezone).format(timestampFormat);
};

export const parseLocalDate = (data: { [key: string]: string }[] | null | undefined, timezone: string, timestampFormat: string = 'YYYY-MM-DDTHH:mm:ss.SSS'): string | null => {
export const parseUtcIntoLocalDate = (data: { [key: string]: string }[] | null | undefined, timezone: string, timestampFormat: string = 'YYYY-MM-DDTHH:mm:ss.SSS'): string | null => {
if (!data) {
return null;
}
Expand All @@ -278,11 +278,12 @@ export const parseLocalDate = (data: { [key: string]: string }[] | null | undefi
let parsedMoment;

if (value.includes('Z') || /([+-]\d{2}:?\d{2})$/.test(value.trim())) {
// We have timezone info
// We have timezone info encoded in the value string
parsedMoment = moment(value, formats, true);
} else {
// If no tz info - use provided timezone
parsedMoment = moment.tz(value, formats, true, timezone);
// If no tz info - use UTC as cube expects data source connection to be in UTC timezone
// and so date functions (e.g. `now()`) would return timestamps in UTC.
parsedMoment = moment.tz(value, formats, true, 'UTC');
}

if (!parsedMoment.isValid()) {
Expand Down
30 changes: 15 additions & 15 deletions packages/cubejs-backend-shared/test/time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
timeSeries,
isPredefinedGranularity,
timeSeriesFromCustomInterval,
parseLocalDate,
parseUtcIntoLocalDate,
utcToLocalTimeZone,
addSecondsToLocalTimestamp,
reformatInIsoLocal,
Expand Down Expand Up @@ -264,38 +264,38 @@ describe('extractDate', () => {
const timezone = 'Europe/Kiev';

it('should return null if data is empty', () => {
expect(parseLocalDate(null, timezone)).toBeNull();
expect(parseLocalDate(undefined, timezone)).toBeNull();
expect(parseLocalDate([], timezone)).toBeNull();
expect(parseUtcIntoLocalDate(null, timezone)).toBeNull();
expect(parseUtcIntoLocalDate(undefined, timezone)).toBeNull();
expect(parseUtcIntoLocalDate([], timezone)).toBeNull();
});

it('should return null if no valid date is found in data', () => {
expect(parseLocalDate([{}], timezone)).toBeNull();
expect(parseLocalDate([{ someKey: 'invalid date' }], timezone)).toBeNull();
expect(parseUtcIntoLocalDate([{}], timezone)).toBeNull();
expect(parseUtcIntoLocalDate([{ someKey: 'invalid date' }], timezone)).toBeNull();
});

it('should throw an error for unknown timezone', () => {
const input = [{ date: '2025-02-28T12:00:00+03:00' }];
expect(() => parseLocalDate(input, 'Invalid/Timezone'))
expect(() => parseUtcIntoLocalDate(input, 'Invalid/Timezone'))
.toThrowError('Unknown timezone: Invalid/Timezone');
});

it('should parse a date with UTC timezone', () => {
const input = [{ date: '2025-02-28T12:00:00Z' }];
const result = parseLocalDate(input, timezone);
const result = parseUtcIntoLocalDate(input, timezone);
expect(result).toBe('2025-02-28T14:00:00.000');
});

it('should parse a date with an offset timezone', () => {
const input = [{ date: '2025-02-28T12:00:00+03:00' }];
const result = parseLocalDate(input, timezone);
const result = parseUtcIntoLocalDate(input, timezone);
expect(result).toBe('2025-02-28T11:00:00.000');
});

it('should parse a date without timezone as UTC', () => {
const input = [{ date: '2025-02-28 12:00:00' }];
const result = parseLocalDate(input, timezone);
expect(result).toBe('2025-02-28T12:00:00.000');
const result = parseUtcIntoLocalDate(input, timezone);
expect(result).toBe('2025-02-28T14:00:00.000');
});

it('should handle multiple formats', () => {
Expand All @@ -304,10 +304,10 @@ describe('extractDate', () => {
const input3 = [{ date: '2025-02-28T12:00:00Z' }];
const input4 = [{ date: '2025-02-28T12:00:00+03:00' }];

expect(parseLocalDate(input1, timezone)).toBe('2025-02-28T12:00:00.000');
expect(parseLocalDate(input2, timezone)).toBe('2025-02-28T12:00:00.000');
expect(parseLocalDate(input3, timezone)).toBe('2025-02-28T14:00:00.000');
expect(parseLocalDate(input4, timezone)).toBe('2025-02-28T11:00:00.000');
expect(parseUtcIntoLocalDate(input1, timezone)).toBe('2025-02-28T14:00:00.000');
expect(parseUtcIntoLocalDate(input2, timezone)).toBe('2025-02-28T14:00:00.000');
expect(parseUtcIntoLocalDate(input3, timezone)).toBe('2025-02-28T14:00:00.000');
expect(parseUtcIntoLocalDate(input4, timezone)).toBe('2025-02-28T11:00:00.000');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
utcToLocalTimeZone,
timeSeries,
localTimestampToUtc,
parseLocalDate,
parseUtcIntoLocalDate,
} from '@cubejs-backend/shared';
import { InlineTable, TableStructure } from '@cubejs-backend/base-driver';
import { DriverFactory } from './DriverFactory';
Expand Down Expand Up @@ -526,7 +526,7 @@ export class PreAggregationPartitionRangeLoader {
}

public static extractDate(data: any, timezone: string, timestampFormat: string = DEFAULT_TS_FORMAT): string {
return parseLocalDate(data, timezone, timestampFormat);
return parseUtcIntoLocalDate(data, timezone, timestampFormat);
}

public static readonly FROM_PARTITION_RANGE = FROM_PARTITION_RANGE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ export class PreAggregations {
public async getVersionEntries(preAggregations: PreAggregationDescription[], requestId): Promise<VersionEntry[][]> {
const loadCacheByDataSource = {};

const getLoadCacheByDataSource = (dataSource = 'default', preAggregationSchema) => {
const getLoadCacheByDataSource = (preAggregationSchema, dataSource = 'default') => {
if (!loadCacheByDataSource[`${dataSource}_${preAggregationSchema}`]) {
loadCacheByDataSource[`${dataSource}_${preAggregationSchema}`] =
new PreAggregationLoadCache(
Expand All @@ -740,9 +740,9 @@ export class PreAggregations {
preAggregations.map(
async preAggregation => {
const { dataSource, preAggregationsSchema } = preAggregation;
const cacheKey = getLoadCacheByDataSource(dataSource, preAggregationsSchema).tablesCachePrefixKey(preAggregation);
const cacheKey = getLoadCacheByDataSource(preAggregationsSchema, dataSource).tablesCachePrefixKey(preAggregation);
if (!firstByCacheKey[cacheKey]) {
firstByCacheKey[cacheKey] = getLoadCacheByDataSource(dataSource, preAggregationsSchema).getVersionEntries(preAggregation);
firstByCacheKey[cacheKey] = getLoadCacheByDataSource(preAggregationsSchema, dataSource).getVersionEntries(preAggregation);
const res = await firstByCacheKey[cacheKey];
return res.versionEntries;
}
Expand Down
Loading