Skip to content

Commit e8d81f2

Browse files
authored
fix(schema-compiler): Add missing “quarter” time unit to granularity definitions (#8708)
* fix(schema-compiler): Add missing “quarter” time unit to granularity definitions * add tests for quarters in granularities * fix(schema-compiler): allow custom granularities with 1 week interval definition * more tests
1 parent 84c176c commit e8d81f2

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

packages/cubejs-schema-compiler/src/compiler/CubeValidator.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ const everyCronTimeZone = Joi.string().custom((value, helper) => {
8282
}
8383
});
8484

85-
const GranularityInterval = Joi.string().pattern(/^\d+\s+(second|minute|hour|day|week|month|year)s?(\s\d+\s+(second|minute|hour|day|week|month|year)s?){0,7}$/, 'granularity interval');
85+
const GranularityInterval = Joi.string().pattern(/^\d+\s+(second|minute|hour|day|week|month|quarter|year)s?(\s\d+\s+(second|minute|hour|day|week|month|quarter|year)s?){0,7}$/, 'granularity interval');
8686
// Do not allow negative intervals for granularities, while offsets could be negative
87-
const GranularityOffset = Joi.string().pattern(/^-?(\d+\s+)(second|minute|hour|day|week|month|year)s?(\s-?\d+\s+(second|minute|hour|day|week|month|year)s?){0,7}$/, 'granularity offset');
87+
const GranularityOffset = Joi.string().pattern(/^-?(\d+\s+)(second|minute|hour|day|week|month|quarter|year)s?(\s-?\d+\s+(second|minute|hour|day|week|month|quarter|year)s?){0,7}$/, 'granularity offset');
8888

8989
const BaseDimensionWithoutSubQuery = {
9090
aliases: Joi.array().items(Joi.string()),
@@ -140,6 +140,8 @@ const BaseDimensionWithoutSubQuery = {
140140
month: () => 12 % v === 0,
141141
// Only quarters divisible by a year with no remainder are valid
142142
quarter: () => 4 % v === 0,
143+
// Only 1 week is valid
144+
week: () => v === 1,
143145
// Only 1 day is valid
144146
day: () => v === 1,
145147
// Only hours divisible by a day with no remainder are valid

packages/cubejs-schema-compiler/test/unit/cube-validator.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,43 @@ describe('Cube Validation', () => {
745745
error: (message: any, _e: any) => {
746746
console.log(message);
747747
expect(message).toContain('"dimensions.createdAt" does not match any of the allowed types');
748+
expect(message).toContain('Arbitrary intervals cannot be used without origin point specified');
749+
}
750+
} as any);
751+
752+
expect(validationResult.error).toBeTruthy();
753+
}
754+
755+
{
756+
const cube = newCube({
757+
half_year: {
758+
interval: '3 quarters',
759+
}
760+
});
761+
762+
const validationResult = cubeValidator.validate(cube, {
763+
error: (message: any, _e: any) => {
764+
console.log(message);
765+
expect(message).toContain('"dimensions.createdAt" does not match any of the allowed types');
766+
expect(message).toContain('Arbitrary intervals cannot be used without origin point specified');
767+
}
768+
} as any);
769+
770+
expect(validationResult.error).toBeTruthy();
771+
}
772+
773+
{
774+
const cube = newCube({
775+
half_year: {
776+
interval: '3 weeks',
777+
}
778+
});
779+
780+
const validationResult = cubeValidator.validate(cube, {
781+
error: (message: any, _e: any) => {
782+
console.log(message);
783+
expect(message).toContain('"dimensions.createdAt" does not match any of the allowed types');
784+
expect(message).toContain('Arbitrary intervals cannot be used without origin point specified');
748785
}
749786
} as any);
750787

@@ -854,6 +891,18 @@ describe('Cube Validation', () => {
854891
expect(validationResult.error).toBeFalsy();
855892
}
856893

894+
{
895+
const cube = newCube({
896+
half_year: {
897+
interval: '2 quarters',
898+
origin: '2024-04',
899+
}
900+
});
901+
902+
const validationResult = cubeValidator.validate(cube, new ConsoleErrorReporter());
903+
expect(validationResult.error).toBeFalsy();
904+
}
905+
857906
{
858907
const cube = newCube({
859908
half_year: {

0 commit comments

Comments
 (0)