Skip to content

Commit

Permalink
feat: to_date rolling window type syntax (#8399)
Browse files Browse the repository at this point in the history
  • Loading branch information
paveltiunov authored Jun 26, 2024
1 parent 576f7f7 commit eb7755d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/cubejs-schema-compiler/src/adapter/BaseMeasure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ export class BaseMeasure {
return this.query.runningTotalDateJoinCondition();
}
const { rollingWindow } = definition;
if (rollingWindow.type === 'to_date') {
return this.query.rollingWindowToDateJoinCondition(rollingWindow.granularity);
}
// TODO deprecated
if (rollingWindow.type === 'year_to_date' || rollingWindow.type === 'quarter_to_date' || rollingWindow.type === 'month_to_date') {
return this.query.rollingWindowToDateJoinCondition(rollingWindow.type.replace('_to_date', ''));
}
Expand Down
10 changes: 8 additions & 2 deletions packages/cubejs-schema-compiler/src/compiler/CubeValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ const FixedRollingWindow = {
offset: Joi.any().valid('start', 'end')
};

const GranularitySchema = Joi.string().valid('second', 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year').required();

const YearToDate = {
type: Joi.string().valid('year_to_date'),
};
Expand All @@ -128,6 +130,11 @@ const MonthToDate = {
type: Joi.string().valid('month_to_date'),
};

const ToDate = {
type: Joi.string().valid('to_date'),
granularity: GranularitySchema,
};

const BaseMeasure = {
aliases: Joi.array().items(Joi.string()),
format: Joi.any().valid('percent', 'currency', 'number'),
Expand All @@ -149,6 +156,7 @@ const BaseMeasure = {
{ is: 'year_to_date', then: YearToDate },
{ is: 'quarter_to_date', then: QuarterToDate },
{ is: 'month_to_date', then: MonthToDate },
{ is: 'to_date', then: ToDate },
{ is: 'fixed',
then: FixedRollingWindow,
otherwise: FixedRollingWindow
Expand Down Expand Up @@ -277,8 +285,6 @@ const OriginalSqlSchema = condition(
}),
);

const GranularitySchema = Joi.string().valid('second', 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year').required();

const ReferencesFields = ['timeDimensionReference', 'rollupReferences', 'measureReferences', 'dimensionReferences', 'segmentReferences'];
const NonReferencesFields = ['timeDimension', 'timeDimensions', 'rollups', 'measures', 'dimensions', 'segments'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ describe('SQL Generation', () => {
type: 'sum',
sql: 'amount',
rollingWindow: {
type: 'quarter_to_date'
type: 'to_date',
granularity: 'quarter'
}
},
revenue_day_ago: {
Expand Down

0 comments on commit eb7755d

Please sign in to comment.