Skip to content

Commit

Permalink
[FIX] pivot: do not allow date dimension without granularity
Browse files Browse the repository at this point in the history
When a date dimension is used in a pivot view, the granularity must be
specified. Before this commit, it automatically defaulted to
'month_number'. With the experience of the odoo pivot, it's not a good
idea to have a default granularity, as it's not easily migrable. In
addition, supported a date dimension without granularity leads to
lots of "if-else" in the code to handle this.

So, we prefer to avoid this situation and raise an error when a date
dimension is used without a granularity.

closes #4499

Task: 0
Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
  • Loading branch information
pro-odoo committed Jun 20, 2024
1 parent 4004ad2 commit 0f68783
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion demo/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2524,7 +2524,7 @@ export const demoData = {
1: {
type: "SPREADSHEET",
columns: [{ name: "Stage" }],
rows: [{ name: "Created on", order: "asc" }],
rows: [{ name: "Created on", granularity: "month_number", order: "asc" }],
measures: [{ name: "Expected Revenue", aggregator: "count" }],
name: "My pivot",
dataSet: { sheetId: "pivot", zone: { top: 0, bottom: 21, left: 0, right: 8 } },
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/pivot/pivot_runtime_definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ function createMeasure(fields: PivotFields, measure: PivotCoreMeasure): PivotMea
function createPivotDimension(fields: PivotFields, dimension: PivotCoreDimension): PivotDimension {
const field = fields[dimension.name];
const type = field?.type ?? "integer";
const granularity =
field && isDateField(field) ? dimension.granularity ?? "month_number" : undefined;
const granularity = field && isDateField(field) ? dimension.granularity : undefined;

return {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/pivot/spreadsheet_pivot/date_spreadsheet_pivot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { PivotDimension } from "../../../types/pivot";
import { FieldValue } from "./data_entry_spreadsheet_pivot";

export function createDate(dimension: PivotDimension, value: FieldValue["value"], locale: Locale) {
const granularity = dimension.granularity || "month_number";
if (!(granularity in MAP_VALUE_DIMENSION_DATE)) {
const granularity = dimension.granularity;
if (!granularity || !(granularity in MAP_VALUE_DIMENSION_DATE)) {
throw new Error(`Unknown date granularity: ${granularity}`);
}
if (value === null) {
Expand Down
4 changes: 2 additions & 2 deletions tests/pivots/spreadsheet_pivot/spreadsheet_pivot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ describe("Spreadsheet Pivot", () => {
]);

updatePivot(model, "1", {
columns: [{ name: "Created on", order: "asc" }],
columns: [{ name: "Created on", granularity: "month_number", order: "asc" }],
});
expect(getEvaluatedGrid(model, "B26:F26")).toEqual([
["February", "March", "April", "Total", ""],
Expand Down Expand Up @@ -218,7 +218,7 @@ describe("Spreadsheet Pivot", () => {
]);

updatePivot(model, "1", {
rows: [{ name: "Created on", order: "asc" }],
rows: [{ name: "Created on", granularity: "month_number", order: "asc" }],
});
expect(getEvaluatedGrid(model, "A28:A32")).toEqual([
["February"],
Expand Down

0 comments on commit 0f68783

Please sign in to comment.