Skip to content

Commit fe974fb

Browse files
committed
fix(studio): keep a declared percent or currency under an unproven formula verdict
The field inspector cleared a declared `percent` return type when the edited expression proved no type, which is the verdict of a field-over-field quotient such as `record.spent / record.budget`. A declared `percent` or `currency` now survives every verdict that proves no other type (`number`, or none); a verdict that proves text, a boolean or a date still stamps. The returnType jsdoc and describe say so, and the changeset names the forms, not the inspector, as the place that offers `percent`. Claude-Session: https://claude.ai/code/session_01Utp6CajiB6BSQKWowy6YLB Co-authored-by: Claude <noreply@anthropic.com>
1 parent 03a17ea commit fe974fb

4 files changed

Lines changed: 60 additions & 9 deletions

File tree

‎.changeset/35047-formula-percent-return.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ A ratio formula (budget used, gross margin) can now declare `returnType: 'percen
1515
- **The value.** A `percent` formula result is read exactly as a `percent` field reads its stored value: a fraction (`0.25` shows `25%`, so the expression divides and does not multiply by 100), unless the formula declares a `max` above 1, then percentage points (`percentScaleOf`).
1616
- **Its places.** `scale` counts the decimal places of the displayed percentage, as on a `percent` field (absent: 0). The engine rounds a fraction result to `scale + 2` places and a whole percentage to `scale`, so `scale: 1` keeps 0.417 (41.7%) instead of rounding it to 0.4.
1717
- **An existing `percent` field computed from its `expression` rounds the same way.** The engine shares one rounding step for computed values, so a stored `percent` field that computes from an `expression` now keeps `scale + 2` places for a fraction (and `scale` for a whole percentage). Before, its computed fraction was rounded to `scale` itself: at `scale: 1`, 0.417 was stored as 0.4 and read 40.0%; it is now stored 0.417 and reads 41.7%. The change applies to the next computed write; rows written earlier are not rewritten.
18-
- **Its face.** `percentDisplay` applies to a `percent` formula too (bar or plain). The formula renderer formats the result with the percent formatter, and the Studio field inspector offers `percent` as a return type.
18+
- **Its face.** `percentDisplay` applies to a `percent` formula too (bar or plain). The formula renderer formats the result with the percent formatter, and the field and object forms offer `percent` as a return type. Editing the expression in the Studio field inspector keeps a declared `percent` (or `currency`) unless the new expression proves text, a boolean or a date.
1919
- **Declared, never inferred.** No CEL type proves a quotient is a percentage, so authoring keeps a declared `percent` where it would otherwise stamp `number`. Readers that switch on `returnType` (display-name eligibility, filter operator and comparand typing) treat `percent` as a number.
2020

2121
The enum only gains a member: every formula that parsed before parses the same, and nothing stored needs migrating.

‎packages/spec/src/data/field.zod.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,7 +2372,8 @@ export const FieldSchema = lazySchema(() => {
23722372
* Salesforce/Airtable carry a formula's result type). Lets consumers — dataset
23732373
* measures, display formatting, validation — read a declared type instead of
23742374
* re-parsing the expression. Authoring stamps it from the inferred CEL type;
2375-
* absent when the type can't be proven (an ambiguous/`dyn` expression).
2375+
* absent when the type can't be proven (an ambiguous/`dyn` expression),
2376+
* except that a declared `percent` or `currency` stays (below).
23762377
*
23772378
* `currency` (#22727): the result is an amount of money. It carries its
23782379
* currency exactly the way a `currency` field does — through the formula's
@@ -2392,10 +2393,14 @@ export const FieldSchema = lazySchema(() => {
23922393
* (absent ⇒ 0), so the engine rounds a fraction to `scale + 2` places and a
23932394
* whole percentage to `scale`. It is DECLARED, never inferred: no CEL type
23942395
* proves a quotient is a percentage, so authoring keeps a declared `percent`
2395-
* where it would stamp `number`.
2396+
* under every verdict that proves no other type — where it would stamp
2397+
* `number`, and where the type is unproven (a field-over-field quotient such
2398+
* as `record.spent / record.budget` types `dyn`), which would otherwise
2399+
* clear it. A declared `currency` is kept the same way. A verdict that
2400+
* proves `text`, `boolean` or `date` still stamps.
23962401
*/
23972402
returnType: z.enum(['number', 'text', 'boolean', 'date', 'currency', 'percent']).optional()
2398-
.describe('Inferred value type of a formula field (number/text/boolean/date/currency/percent). `currency`: an amount of money, in the currency the formula\'s own `currencyConfig` declares, as a currency field declares it (no `currencyConfig`: dynamic, the tenant default currency). `percent`: a ratio, read and displayed as a `percent` field reads its stored value — a fraction (0.25 shows 25%, so the expression divides and does not multiply by 100) unless the formula declares a `max` above 1, then percentage points; its `scale` is the decimal places of the displayed percentage (absent: 0), and its `percentDisplay` picks the bar or the plain face. Declared by the author, never inferred.'),
2403+
.describe('Value type of a formula field (number/text/boolean/date/currency/percent). Authoring stamps `number`, `text`, `boolean` or `date` from the expression\'s inferred type and leaves it absent when none is proven; `currency` and `percent` are declared by the author, and a verdict that proves no other type (`number`, or none) keeps them. `currency`: an amount of money, in the currency the formula\'s own `currencyConfig` declares, as a currency field declares it (no `currencyConfig`: dynamic, the tenant default currency). `percent`: a ratio, read and displayed as a `percent` field reads its stored value — a fraction (0.25 shows 25%, so the expression divides and does not multiply by 100) unless the formula declares a `max` above 1, then percentage points; its `scale` is the decimal places of the displayed percentage (absent: 0), and its `percentDisplay` picks the bar or the plain face.'),
23992404
summaryOperations: strictObject({
24002405
surface: 'this roll-up summary',
24012406
history: FIELD_HISTORY,

‎ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.returnTypeHeld-27485.test.tsx‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,17 @@ function stubEngine() {
5656
? { ok: false, errors: [{ message: 'Parse error: unexpected end of input' }], warnings: [] }
5757
: { ok: true, errors: [], warnings: [] };
5858
},
59-
introspectScope: () => ({ fields: ['est_hours'], roots: ['record'], functions: ['has'] }),
59+
introspectScope: () => ({ fields: ['est_hours', 'budget'], roots: ['record'], functions: ['has'] }),
6060
// A broken source proves no type, as the engine answers it.
6161
inferExpressionType: (input: unknown) => {
6262
const src = typeof input === 'string' ? input : '';
6363
if (BROKEN.test(src)) return 'unknown' as const;
6464
if (src.includes("'")) return 'text' as const;
6565
if (src.includes('+')) return 'unknown' as const;
66+
// A field-over-field operator types `dyn`, as the engine answers it
67+
// (formula-return.ts): the quotient's type is unproven.
68+
if (/record\.\w+\s*\/\s*record\./.test(src)) return 'unknown' as const;
69+
if (src.includes('>')) return 'boolean' as const;
6670
return 'number' as const;
6771
},
6872
}),
@@ -152,3 +156,43 @@ describe('ObjectFieldInspector — a declared percent survives a number verdict
152156
expect(latest().returnType).toBe('number');
153157
});
154158
});
159+
160+
describe('ObjectFieldInspector — a declared percent or currency survives an unproven verdict (objectstack#35047)', () => {
161+
const RATIO = 'record.est_hours / record.budget';
162+
163+
it("the spec's ratio shape: a field-over-field quotient proves no type and keeps `percent`", async () => {
164+
const { box, latest } = renderFormula('percent');
165+
fireEvent.change(box, { target: { value: RATIO } });
166+
await waitFor(() => expect(latest().expression).toBe(RATIO), { timeout: 3000 });
167+
await settle();
168+
expect(latest().returnType).toBe('percent');
169+
});
170+
171+
it('the same quotient keeps a declared `currency`', async () => {
172+
const { box, latest } = renderFormula('currency');
173+
fireEvent.change(box, { target: { value: RATIO } });
174+
await waitFor(() => expect(latest().expression).toBe(RATIO), { timeout: 3000 });
175+
await settle();
176+
expect(latest().returnType).toBe('currency');
177+
});
178+
179+
it('a `number` verdict keeps a declared `currency` too', async () => {
180+
const { box, latest } = renderFormula('currency');
181+
fireEvent.change(box, { target: { value: 'record.est_hours / 40' } });
182+
await waitFor(() => expect(latest().expression).toBe('record.est_hours / 40'), { timeout: 3000 });
183+
await settle();
184+
expect(latest().returnType).toBe('currency');
185+
});
186+
187+
it('a verdict that proves another type still stamps over a declared `currency`', async () => {
188+
const { box, latest } = renderFormula('currency');
189+
fireEvent.change(box, { target: { value: 'record.est_hours > 40' } });
190+
await waitFor(() => expect(latest().returnType).toBe('boolean'), { timeout: 3000 });
191+
});
192+
193+
it('CONTROL — the same quotient still clears a declared `number`, as before', async () => {
194+
const { box, latest } = renderFormula();
195+
fireEvent.change(box, { target: { value: RATIO } });
196+
await waitFor(() => expect(latest().returnType).toBeUndefined(), { timeout: 3000 });
197+
});
198+
});

‎ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.tsx‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,10 +1101,12 @@ export function ObjectFieldInspector({
11011101
// Spec: `returnType` carries only a PROVEN concrete type —
11021102
// an ambiguous (`unknown`) or unavailable verdict clears it.
11031103
const next = now.inferred && now.inferred !== 'unknown' ? now.inferred : undefined;
1104-
// objectstack#35047 — `percent` is DECLARED, never inferred: no CEL type
1105-
// proves a quotient is a percentage, so a source the engine proves a
1106-
// `number` keeps the author's `percent`. Any other verdict still stamps.
1107-
if (def.returnType === 'percent' && next === 'number') return;
1104+
// objectstack#35047 — `percent` and `currency` are DECLARED: no CEL type
1105+
// proves a quotient is a percentage or a number is money, so a declared
1106+
// one survives every verdict that proves no OTHER type — `number`, and an
1107+
// unproven one alike (a field-over-field quotient types `dyn`). A verdict
1108+
// that proves text, boolean or date still stamps.
1109+
if ((def.returnType === 'percent' || def.returnType === 'currency') && (next === undefined || next === 'number')) return;
11081110
if (def.returnType !== next) patchDef({ returnType: next });
11091111
};
11101112

0 commit comments

Comments
 (0)