Skip to content

Commit c7c74d4

Browse files
committed
no-mistakes(review): Fix MySQL derived divide parentheses; reword CHANGELOG
1 parent 36f7f2f commit c7c74d4

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ here.**
1111
## [Unreleased]
1212

1313
**New metamodel vocabulary: `measure.derived` and its `@expr`.** It is additive: nothing that
14-
loaded before stops loading. If this ships after 1.3.0, the release moves `metamodelVersion`.
14+
loaded before stops loading. This vocabulary ships in 1.3.0 with `metamodelVersion` 1.3 if it merges before the 1.3.0 publish; if it lands after, the next release moves `metamodelVersion`.
1515

1616
### Added
1717

‎server/typescript/packages/codegen-ts/src/projection/report-ddl-emit.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ function derived(t: ReportDerivedTerm, d: ReportDialect, factRef: string | undef
134134
let sql: string;
135135
if (t.fn === DERIVED_FN_DIVIDE) {
136136
// Inside CAST(...) the dividend needs no parentheses of its own; MySQL has no cast.
137-
const real = realOf(derived(t.left, d, factRef), d);
138-
const dividend = real.startsWith("CAST(") ? real : derived(t.left, d, factRef, false);
137+
const dividend = d === "mysql" ? derived(t.left, d, factRef, false) : realOf(derived(t.left, d, factRef), d);
139138
const divisor =
140139
t.right.kind === "value" && t.right.value !== 0
141140
? derived(t.right, d, factRef, false)

‎server/typescript/packages/codegen-ts/test/projection/report-derived-measures.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,41 @@ describe("FR-044 R13: derived measures in a report view", () => {
8181
expect(sql).toContain("(m.`weekly_capacity_hours` * 52) / 12 AS `capacity_hours`");
8282
});
8383

84+
test("mysql keeps the parentheses around a dividend that is a difference of integral sums", async () => {
85+
const doc = JSON.parse(readFileSync(FIXTURE, "utf8")) as Json;
86+
const children = (doc["metadata.root"] as { children: Json[] }).children;
87+
const entity = children.find((c) => (c["object.entity"] as Json | undefined)?.name === "TimeEntry")!;
88+
const ec = (entity["object.entity"] as { children: Json[] }).children;
89+
const hours = ec.findIndex((c) => (c["field.decimal"] as Json | undefined)?.name === "hours");
90+
ec[hours] = { "field.long": { name: "hours", "@required": true } };
91+
ec.push(
92+
{ "measure.aggregate": { name: "allHours", "@agg": "sum", "@of": "TimeEntry.hours" } },
93+
{ "measure.aggregate": { name: "billedHours", "@agg": "sum", "@of": "TimeEntry.hours", "@filter": { billable: true } } },
94+
);
95+
ec.push({
96+
"measure.derived": {
97+
name: "hoursGapPerBudget",
98+
"@expr": {
99+
fn: "divide",
100+
args: [{ fn: "subtract", args: [{ field: "allHours" }, { field: "billedHours" }] }, { field: "projectBudget" }],
101+
},
102+
},
103+
});
104+
children.push({
105+
"object.report": {
106+
name: "HoursGap",
107+
"@from": "TimeEntry",
108+
"@dimensions": ["projectId", "projectBudget"],
109+
"@measures": ["allHours", "billedHours", "hoursGapPerBudget"],
110+
children: [{ "source.rdb": { "@kind": "view", "@view": "v_hoursgap" } }],
111+
},
112+
});
113+
const { root, errors } = await new MetaDataLoader().load([new InMemoryStringSource(JSON.stringify(doc))]);
114+
expect(errors).toEqual([]);
115+
const sql = buildReportViews(root, { dialect: "mysql" }).find((v) => v.name === "v_hoursgap")?.sql ?? "";
116+
expect(sql).toMatch(/\(CAST\(.* AS SIGNED\) - CAST\(.* AS SIGNED\)\) \/ NULLIF\(p\.`budget`, 0\) AS `hours_gap_per_budget`/);
117+
});
118+
84119
test("Cube: a derived measure over measures is a number member reading them", async () => {
85120
const model = buildCubeModel(await load(true), { dialect: "postgres", columnNamingStrategy: "snake_case" });
86121
const cube = model.cubes.find((c) => c.name.toLowerCase().includes("timeentr"))!;

0 commit comments

Comments
 (0)