|
| 1 | +--- |
| 2 | +'@objectstack/spec': minor |
| 3 | +'@objectstack/service-analytics': patch |
| 4 | +--- |
| 5 | + |
| 6 | +feat(spec)!: an analytics cube member's `sql` is a column reference — a SQL expression there is refused at parse, and a derived value is declared on an ADR-0021 dataset (#20943) |
| 7 | + |
| 8 | +Clause-②: yes (narrowing) |
| 9 | + |
| 10 | +**BREAKING** — shipped as `minor` under the launch-window convention |
| 11 | +(`check-changeset-no-major` refuses `major` until GA; breaking-ness is carried by |
| 12 | +this banner, the `(narrowing)` arm above and the ADR-0087 disposition below, |
| 13 | +never by the level). |
| 14 | + |
| 15 | +`MetricSchema.sql` and `DimensionSchema.sql` — the `sql` of every member in an |
| 16 | +analytics cube's `measures` and `dimensions` — admit a column reference only: a |
| 17 | +field of the cube's object (`amount`), a relationship path of bare identifiers |
| 18 | +ending in one (`account.amount`, `account.owner.region`), or `'*'` for a count. |
| 19 | +Any other value — a `CASE WHEN …`, an aggregate or a ratio of aggregates, a quoted |
| 20 | +or `$`-prefixed spelling, an empty string — is refused at parse with a |
| 21 | +prescription. This is ADR-0021's "zero raw SQL / zero raw expressions" carried |
| 22 | +from the dataset layer to the cube members it compiles to (maintainer ruling D on |
| 23 | +the card): an expression names no single field, so no platform check can judge |
| 24 | +which fields it reads, and the two analytics strategies never agreed on it — the |
| 25 | +raw-SQL path ran it verbatim and the ObjectQL path refused it. The rule is a |
| 26 | +`pattern` in the published JSON Schema too, so a document validated against |
| 27 | +`json-schema/**` is judged as the parse judges it. |
| 28 | + |
| 29 | +## FROM → TO |
| 30 | + |
| 31 | +A derived value moves to an ADR-0021 dataset over the same object. A conditional |
| 32 | +count or sum is a dataset measure with its own structured `filter`; a ratio, sum, |
| 33 | +difference or product of measures is `derived: { op, of: [...] }` over measures |
| 34 | +named in the same dataset. |
| 35 | + |
| 36 | +``` |
| 37 | +FROM defineCube({ name: 'delivery', sql: 'task', measures: { |
| 38 | + done_rate: { label: 'Done Rate (%)', type: 'number', |
| 39 | + sql: "SUM(CASE WHEN status = 'done' THEN 1 ELSE 0 END) * 100.0 / COUNT(*)" }, |
| 40 | + } }) |
| 41 | + -> parsed; the expression ran verbatim on one strategy and was refused on the other |
| 42 | +TO -> ZodError at measures.done_rate.sql (invalid_format): `measures.<metric>.sql` is a |
| 43 | + column reference: a field of the cube's object (`amount`), a relationship path ending |
| 44 | + in one (`account.amount`), or `'*'` for a count. A SQL expression there was retired … |
| 45 | +
|
| 46 | + defineDataset({ name: 'task_metrics', label: 'Task Metrics', object: 'task', |
| 47 | + dimensions: [/* … */], |
| 48 | + measures: [ |
| 49 | + { name: 'task_count', aggregate: 'count' }, |
| 50 | + { name: 'done_count', aggregate: 'count', filter: { status: 'done' } }, |
| 51 | + { name: 'done_rate', derived: { op: 'ratio', of: ['done_count', 'task_count'] }, format: '0.0%' }, |
| 52 | + ] }) |
| 53 | +``` |
| 54 | + |
| 55 | +**Mind the scale.** A `derived` ratio is a 0–1 fraction. An expression that |
| 56 | +multiplied by 100 returned percentage points; pair the ratio with a `%` numeral |
| 57 | +pattern (the server marks a ratio column's percent scale as a fraction) and |
| 58 | +re-check any consumer that read the old number raw. |
| 59 | + |
| 60 | +**A dimension that bucketed a column with a CASE expression** has no expression |
| 61 | +form in the cube layer or the dataset layer: group by the column itself, or keep |
| 62 | +the bucket as a field of the object and name that field. |
| 63 | + |
| 64 | +**The one-line fix:** parse each cube; every refusal at `…sql` is one member to |
| 65 | +move — replace it with the column it aggregates, or move the derived value to a |
| 66 | +dataset measure as above, and point the dashboards, reports and queries that named |
| 67 | +`<cube>.<member>` at the dataset measure. |
| 68 | + |
| 69 | +**What an author who still writes it sees.** `CubeSchema`, `defineCube()`, |
| 70 | +`defineStack({ analyticsCubes })` (`STACK_SCHEMA_INVALID` / 422) and the |
| 71 | +`analytics_cube` write door refuse the member at its `sql` path with the |
| 72 | +prescription. `tsc` does not: the key's type is still `string`. |
| 73 | + |
| 74 | +## The retirement kit |
| 75 | + |
| 76 | +- **Schema.** `MetricSchema.sql` / `DimensionSchema.sql` carry the pattern and |
| 77 | + their prescriptions (`data/analytics.zod.ts`). A column reference parses |
| 78 | + byte-identically to before. The retired metric `filters` guidance and the |
| 79 | + analytics query's `filters` guidance no longer offer "fold the condition into |
| 80 | + the metric's own `sql` expression" as a live channel; the `metric-filters-removed` |
| 81 | + conversion summary and its D3 entry and step-18 rationale fragment say the same. |
| 82 | +- **ADR-0087.** The D3 entry `cube-member-sql-expression-retired`, with its |
| 83 | + step-18 rationale fragment. No D2 conversion — an expression has no mechanical |
| 84 | + rewrite into a dataset — and no `RETIRED_KEYS_BY_MAJOR` row: no key left the |
| 85 | + shape, so the authorable-surface, api-surface and JSON-schema manifest |
| 86 | + ratchets are unchanged. |
| 87 | +- **Liveness.** The `analytics_cube` ledger rows `measures.sql` and |
| 88 | + `dimensions.sql` stay `live`, re-verified, with the narrowing recorded. |
| 89 | +- **Docs.** The `data/analytics` reference page is regenerated. |
| 90 | +- **Example.** The showcase cube's `done_rate` expression member moves to the |
| 91 | + `showcase_task_metrics` dataset as `done_count` (a count filtered on |
| 92 | + `status: 'done'`) and `done_rate` (`ratio` over `done_count` and `task_count`, |
| 93 | + format `0.0%`). |
| 94 | +- **`@objectstack/service-analytics`** (README only): its query-body section no |
| 95 | + longer tells a reader to fold a per-metric condition into the metric's own |
| 96 | + `sql` expression. The runtime is unchanged: its expression branches remain for |
| 97 | + a cube that reaches the service without meeting the parse, and their deletion |
| 98 | + is a separate change. |
| 99 | + |
| 100 | +## Reach, measured |
| 101 | + |
| 102 | +- This repository: one authored expression member (the showcase `done_rate`), |
| 103 | + moved here. Test fixtures in `@objectstack/service-analytics` that build |
| 104 | + expression members WITHOUT the parse keep exercising the runtime's expression |
| 105 | + branches, unchanged. |
| 106 | +- Out-of-repo authored cubes: NOT MEASURED. |
| 107 | + |
| 108 | +<!-- adr-0087: registered cube-member-sql-expression-retired --> |
0 commit comments