Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .changeset/35702-boolean-yes-no-counts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@objectstack/spec': minor
'@objectstack/lint': minor
'@object-ui/plugin-grid': patch
'@object-ui/i18n': patch
---

feat(spec): `count_true` / `count_false` column summaries — a boolean column's Yes and No counts in the list footer and group headers

Clause-②: yes (widening)
Metadata-protocol: added

`ListColumn.summary` accepts two more members, `count_true` and `count_false`: the number of rows whose boolean (or toggle) cell is true, and the number whose cell is false. A cell with no value is neither; it stays `count_empty`'s, so `count_true + count_false = count_filled`.

Both are answered on the server with no new aggregation function: booleans aggregate as numbers on every backend, so `count_true` is `SUM(field)` and `count_false` is `COUNT(field) − SUM(field)`, read off the group header row (or the footer's whole-set row) by `deriveColumnSummary`. `COLUMN_SUMMARY_AGGREGATION`'s derived rows now carry `reads`, every node the member reads (`from` is its first); `columnSummaryNodeFunctions(mapping)` is the one reading of a row's nodes, which the group header compiler, the footer's whole-set query and the lint now share. `COLUMN_SUMMARY_FIELD_TYPES` / `isColumnSummaryCompatibleWithFieldType` say the two members apply to `boolean` and `toggle` only.

`@objectstack/lint` refuses `count_true` / `count_false` over any other field type under `list-view-summary-field-type-refused` (an error, so `os validate` and the save door fail): over a number the same `SUM(field)` is a total, and the footer would print it as a count.

`@object-ui/plugin-grid`'s footer menu offers Count of Yes and Count of No first on a boolean or toggle column, and only there; the footer draws them as `Yes 12` / `No 3`, from the whole matched set when the server answers it and from the page, labelled as the page's, when it does not. `@object-ui/i18n` adds the `grid.summary` keys `countTrue`, `countFalse`, `menuCountTrue` and `menuCountFalse` in every pack.
8 changes: 5 additions & 3 deletions content/docs/references/ui/view.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Compound-cell prefix configuration

## ColumnSummary

Aggregation function for the column footer summary — and, on a grouped list view, the per-group header summary (server-side): count (COUNT(*), the group count), count_unique (count_distinct), sum, avg, min, max map onto the query AST's AggregationFunction; count_filled, count_empty, percent_filled, percent_empty derive from one COUNT(field) node (the non-null count) and the group count — count_filled = COUNT(field), count_empty = count − COUNT(field), percent_filled = COUNT(field) / count (0 when count is 0), percent_empty = 1 − percent_filled. Server-side "empty" is null on every face; the footer's client-side reading of empty strings and empty arrays as empty is the renderer's to converge
Aggregation function for the column footer summary — and, on a grouped list view, the per-group header summary (server-side): count (COUNT(*), the group count), count_unique (count_distinct), sum, avg, min, max map onto the query AST's AggregationFunction; count_filled, count_empty, percent_filled, percent_empty derive from one COUNT(field) node (the non-null count) and the group count — count_filled = COUNT(field), count_empty = count − COUNT(field), percent_filled = COUNT(field) / count (0 when count is 0), percent_empty = 1 − percent_filled. count_true and count_false are a boolean column's yes and no counts, boolean fields only — count_true = SUM(field), count_false = COUNT(field) − SUM(field); a null cell is neither. Server-side "empty" is null on every face; the footer's client-side reading of empty strings and empty arrays as empty is the renderer's to converge

### Allowed Values

Expand All @@ -141,6 +141,8 @@ Aggregation function for the column footer summary — and, on a grouped list vi
* `count_unique`
* `percent_empty`
* `percent_filled`
* `count_true`
* `count_false`
* `sum`
* `avg`
* `min`
Expand All @@ -157,7 +159,7 @@ Column footer summary configuration

| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **type** | `Enum<'none' \| 'count' \| 'count_empty' \| 'count_filled' \| 'count_unique' \| 'percent_empty' \| 'percent_filled' \| 'sum' \| 'avg' \| 'min' \| 'max'>` | ✅ | Aggregation function |
| **type** | `Enum<'none' \| 'count' \| 'count_empty' \| 'count_filled' \| 'count_unique' \| 'percent_empty' \| 'percent_filled' \| 'count_true' \| 'count_false' \| 'sum' \| 'avg' \| 'min' \| 'max'>` | ✅ | Aggregation function |
| **field** | `string` | optional | Field to aggregate (defaults to the column field) |


Expand Down Expand Up @@ -725,7 +727,7 @@ List chart view configuration
| **wrap** | `boolean` | optional | Allow text wrapping |
| **type** | `string` | optional | Renderer type override (e.g., "currency", "date") |
| **pinned** | `Enum<'left' \| 'right'>` | optional | Pin/freeze column to left or right side |
| **summary** | `Enum<'none' \| 'count' \| 'count_empty' \| 'count_filled' \| 'count_unique' \| 'percent_empty' \| 'percent_filled' \| 'sum' \| 'avg' \| 'min' \| 'max'> \| { type: Enum<'none' \| 'count' \| 'count_empty' \| 'count_filled' \| 'count_unique' \| …>; field?: string }` | optional | Footer aggregation for this column — the function alone, or `{ type, field }` to aggregate another field |
| **summary** | `Enum<'none' \| 'count' \| 'count_empty' \| 'count_filled' \| 'count_unique' \| 'percent_empty' \| 'percent_filled' \| 'count_true' \| 'count_false' \| 'sum' \| 'avg' \| 'min' \| 'max'> \| { type: Enum<'none' \| 'count' \| 'count_empty' \| 'count_filled' \| 'count_unique' \| …>; field?: string }` | optional | Footer aggregation for this column — the function alone, or `{ type, field }` to aggregate another field |
| **prefix** | `{ field: string; type?: Enum<'badge' \| 'text'> }` | optional | Field rendered inline before this cell value |
| **link** | `boolean` | optional | Functions as the primary navigation link (triggers View navigation) |
| **action** | `string` | optional | Registered Action ID to execute when clicked |
Expand Down
2 changes: 1 addition & 1 deletion content/docs/ui/views.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ columns: [
| `sortable` | `boolean` | Allow column sorting |
| `resizable` | `boolean` | Allow column resize |
| `wrap` | `boolean` | Allow text wrapping |
| `summary` | `enum \| { type, field? }` | Footer aggregation: `none`, `count`, `count_empty`, `count_filled`, `count_unique`, `percent_empty`, `percent_filled`, `sum`, `avg`, `min`, `max`. The object form aggregates a different field than the column's own |
| `summary` | `enum \| { type, field? }` | Footer aggregation: `none`, `count`, `count_empty`, `count_filled`, `count_unique`, `percent_empty`, `percent_filled`, `count_true`, `count_false` (a boolean column's yes / no counts; a cell with no value is neither), `sum`, `avg`, `min`, `max`. The object form aggregates a different field than the column's own |
| `prefix` | `{ field, type?: 'badge' \| 'text' }` | Compound cell — render another field's value inline before this cell's value |
| `align` | `'left' \| 'center' \| 'right'` | Text alignment |
| `link` | `boolean` | Cell functions as the primary navigation link |
Expand Down
6 changes: 6 additions & 0 deletions packages/lint/src/rule-explanations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4283,6 +4283,12 @@ const LIST_VIEW_SUMMARY_FIELD_TYPE_REFUSED_EXPLANATION: RuleExplanation = {
'`dataSource.object`, else `properties.objectName`; its grouped header numbers come from its ' +
'`aggregations` (`grid-aggregation-field-type-refused`), not from the column summaries.',
AGGREGATE_PAIR_ROWS,
'The yes / no counts of a boolean column, `count_true` and `count_false`, lower to `SUM(field)` ' +
'(booleans aggregate as numbers, so the sum is the number of true cells) and `COUNT(field)`. The ' +
'door accepts that sum over a number too, where it is a total rather than a count, so these two ' +
'members are judged by the spec\'s member × field-type table (`COLUMN_SUMMARY_FIELD_TYPES`) and ' +
'refused, under this id, over anything but a `boolean` or `toggle` field. A null cell is neither ' +
'true nor false: it is counted by `count_empty`.',
'The fix is a field of a type the function accepts (`summary: { type, field }` aggregates another ' +
'field), or a summary the field accepts: `count`, or a filled / empty count, takes any field. ' +
'A `formula` field stores no value, whatever its `returnType`, so a total over it has to be ' +
Expand Down
28 changes: 28 additions & 0 deletions packages/lint/src/validate-list-view-field-refs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,34 @@ describe('[#32449] a column summary whose function the aggregate door refuses',
}
});

it('[#35702] refuses the boolean yes / no counts over anything but a boolean — the door would answer a total', () => {
expect(refused(withColumns(
{ field: 'is_locked', summary: 'count_true' },
{ field: 'title', summary: { type: 'count_false', field: 'is_all_day' } },
))).toEqual([]);
for (const [column, words] of [
[{ field: 'estimate', summary: 'count_true' }, '"count_true" over "estimate" (`number`)'],
[{ field: 'is_locked', summary: { type: 'count_false', field: 'title' } }, '"count_false" over "title" (`text`)'],
] as const) {
const findings = withColumns(column);
expect(findings.map((f) => [f.rule, f.path, f.severity])).toEqual([
[LIST_VIEW_SUMMARY_FIELD_TYPE_REFUSED, 'views[0].list.columns[1].summary', 'error'],
]);
expect(findings[0].message).toContain(`summary ${words} is a true / false count`);
expect(findings[0].hint).toMatch(/^Point "count_(true|false)" at a boolean or toggle field/);
}
// Every declared `FieldType`: refused exactly where the spec's member table refuses.
for (const member of ['count_true', 'count_false']) {
for (const type of FieldType.options) {
const objects = [{ name: 'duly_task', label: 'Task', fields: [{ name: 'probe', type, label: 'Probe' }] }];
const findings = refused(validateListViewFieldRefsUnrecorded(stackWith({ data: { provider: 'object', object: 'duly_task' }, columns: [{ field: 'probe', summary: member }] }, objects)));
expect(findings.length, `${member} over ${type}`).toBe(type === 'boolean' || type === 'toggle' ? 0 : 1);
}
}
// Unjudgeable stays unjudged: an unknown field is the existence rule's, a dotted path is skipped.
expect(refused(withColumns({ field: 'title', summary: { type: 'count_true', field: 'owner.name' } }))).toEqual([]);
});

it('skips what it cannot judge: an unknown field (the existence rule\'s), a dotted path, no declared type, a type outside `FieldType`', () => {
expect(withColumns({ field: 'title', summary: { type: 'sum', field: 'nope_field' } }).map((f) => [f.rule, f.path])).toEqual([
[LIST_VIEW_FIELD_UNKNOWN, 'views[0].list.columns[1].summary.field'],
Expand Down
32 changes: 29 additions & 3 deletions packages/lint/src/validate-list-view-field-refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@
* door, and judged by this module's {@link columnSummaryRefusal}, under this
* rule id.
*
* [#35702] The boolean yes / no counts `count_true` / `count_false` ride
* `SUM(field)` (and `COUNT(field)`), which the door ACCEPTS over a number as
* well — where it answers a total, not a count of true cells. So they are
* judged by the spec's member × field-type table instead
* (`COLUMN_SUMMARY_FIELD_TYPES` through `isColumnSummaryCompatibleWithFieldType`,
* the boolean class only), under the same rule id: the footer would print a
* total labelled as a count, with no refusal anywhere to say so.
*
* Not judged: `none`; `count` (fieldless, `COUNT(*)`); the four DERIVED
* members (`count_filled` / `count_empty` / `percent_filled` /
* `percent_empty`), which ride one `COUNT(field)` node and so compare no
Expand Down Expand Up @@ -466,7 +474,13 @@ import {
isAggregateCompatibleWithFieldType,
virtualFieldAggregateRemedy,
} from '@objectstack/spec/data';
import { COLUMN_SUMMARY_AGGREGATION, ListViewSchema, type ColumnSummary } from '@objectstack/spec/ui';
import {
COLUMN_SUMMARY_AGGREGATION,
COLUMN_SUMMARY_FIELD_TYPES,
ListViewSchema,
isColumnSummaryCompatibleWithFieldType,
type ColumnSummary,
} from '@objectstack/spec/ui';

import {
aggregateAcceptsDeclaration,
Expand Down Expand Up @@ -924,16 +938,28 @@ export function columnSummaryRefusal(
const member = typeof summary === 'string' ? summary : isRec(summary) ? summary.type : undefined;
if (typeof member !== 'string' || !Object.prototype.hasOwnProperty.call(COLUMN_SUMMARY_AGGREGATION, member)) return undefined;
const mapping = COLUMN_SUMMARY_AGGREGATION[member as ColumnSummary];
// A member the spec restricts to some field types (the boolean yes / no counts) is judged by that table.
const restricted = Object.prototype.hasOwnProperty.call(COLUMN_SUMMARY_FIELD_TYPES, member);
// `none`, the fieldless `count` and the derived `COUNT(field)` members compare no value.
if (mapping.kind !== 'aggregate' || mapping.fieldless) return undefined;
const fn = mapping.function;
if (!restricted && (mapping.kind !== 'aggregate' || mapping.fieldless)) return undefined;
const field = strName(isRec(summary) && summary.field !== undefined ? summary.field : column.field);
if (!field || field.includes('.')) return undefined;
const verdict = resolveFieldPath(graph, object, field);
if (!verdict || isUnjudgeable(verdict) || verdict.kind !== 'ok') return undefined;
const fieldType = verdict.meta?.type;
if (!isJudgeableAggregateType(fieldType)) return undefined;
const shape = { type: fieldType, multiple: verdict.meta?.multiple === true };
if (restricted) {
if (isColumnSummaryCompatibleWithFieldType(member, fieldType)) return undefined;
return {
message:
`summary "${member}" over "${field}" (${declaredAs(shape)}) is a true / false count, for a ` +
'boolean or toggle field only: here it would print a total as a count',
hint: `Point "${member}" at a boolean or toggle field, or count "${field}" with count_filled or count_empty.`,
};
}
if (mapping.kind !== 'aggregate') return undefined;
const fn = mapping.function;
if (aggregateAcceptsDeclaration(fn, shape)) return undefined;
return {
message:
Expand Down
47 changes: 47 additions & 0 deletions packages/rest/src/list-view-grouping-query-door.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ interface Row {
owner: string;
/** Nullable on purpose — the field the derived `count_filled` pin reads. */
notes: string | null;
/** A nullable boolean — the field the derived `count_true` / `count_false` pin reads. */
approved: boolean | null;
}

function makeRow(unit: string | null, ordinal: number): Row {
Expand All @@ -154,6 +156,8 @@ function makeRow(unit: string | null, ordinal: number): Row {
owner: `owner_${ordinal % 4}`,
// Every fifth row has no note — the server's "empty" (null), never ''.
notes: ordinal % 5 === 0 ? null : `note ${ordinal}`,
// Every fourth row unset, the rest true / false alternating.
approved: ordinal % 4 === 0 ? null : ordinal % 2 === 0,
};
}

Expand Down Expand Up @@ -283,6 +287,7 @@ function objectSchema(name: string) {
amount: { name: 'amount', type: 'number' as const, label: 'Amount' },
owner: { name: 'owner', type: 'text' as const, label: 'Owner' },
notes: { name: 'notes', type: 'text' as const, label: 'Notes' },
approved: { name: 'approved', type: 'boolean' as const, label: 'Approved' },
seq: { name: 'seq', type: 'number' as const, label: 'Stored Order' },
},
};
Expand Down Expand Up @@ -806,6 +811,48 @@ describe('§7 `count_filled` is derived on the header row, and the view filter r
});
});

// ─── §7b The boolean yes / no counts (#35702) ─────────────────────────────────

describe('§7b `count_true` / `count_false` are SUM(field) and COUNT(field) over a boolean, on both faces', () => {
const FLAG_VIEW: ListViewGroupQuerySource = {
grouping: { fields: [{ field: 'business_unit' }] },
columns: [
{ field: 'approved', summary: 'count_true' },
{ field: 'id', summary: { type: 'count_false', field: 'approved' } },
{ field: 'business_unit', summary: { type: 'count_empty', field: 'approved' } },
],
};
const tally = (rows: readonly Row[], value: boolean | null) => rows.filter((r) => r.approved === value).length;

it.each(TIERS)('%s: per group and over the whole set, a null cell counts as neither', async (tier) => {
const query = compileListViewGroupQuery(FLAG_VIEW);
expect(query.aggregations).toEqual([
{ function: 'count', alias: LIST_VIEW_GROUP_COUNT_ALIAS },
{ function: 'sum', field: 'approved', alias: 'sum_approved' },
{ function: 'count', field: 'approved', alias: 'count_approved' },
]);
const headers = await onTier(tier, () => records(OBJECT_CONTIGUOUS, query));
for (const [unit] of UNITS) {
const row = headers.find((h) => h.business_unit === unit) as ListViewGroupHeaderRow;
const rows = CONTIGUOUS.filter((r) => r.business_unit === unit);
expect(deriveColumnSummary(row, 'count_true', 'approved'), `${tier}/${unit} true`).toBe(tally(rows, true));
expect(deriveColumnSummary(row, { type: 'count_false', field: 'approved' }, 'id'), `${tier}/${unit} false`).toBe(tally(rows, false));
expect(deriveColumnSummary(row, { type: 'count_empty', field: 'approved' }, 'business_unit')).toBe(tally(rows, null));
}
// `harbour_office` is one row, ordinal 1: false. Its true count is 0, not blank.
const harbour = headers.find((h) => h.business_unit === 'harbour_office') as ListViewGroupHeaderRow;
expect(deriveColumnSummary(harbour, 'count_true', 'approved')).toBe(0);

// The footer's whole-set question: the same nodes with no `groupBy`.
const whole = await onTier(tier, () => records(OBJECT_CONTIGUOUS, { aggregations: query.aggregations }));
expect(whole).toHaveLength(1);
const [set] = whole as ListViewGroupHeaderRow[];
expect(deriveColumnSummary(set, 'count_true', 'approved')).toBe(tally(CONTIGUOUS, true));
expect(deriveColumnSummary(set, 'count_false', 'approved')).toBe(tally(CONTIGUOUS, false));
expect(tally(CONTIGUOUS, true) + tally(CONTIGUOUS, false) + tally(CONTIGUOUS, null)).toBe(TOTAL_ROWS);
});
});

// ─── §8 Rows inside a group are the existing paged find ──────────────────────

describe('§8 `limit` / `offset` slice INSIDE one group', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/spec/api-surface/ui.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"BulkActionResultSchema (const)",
"CHART_AGGREGATE_COMPARISON_SUFFIX (const)",
"COLUMN_SUMMARY_AGGREGATION (const)",
"COLUMN_SUMMARY_FIELD_TYPES (const)",
"COMPILED_PAGE_KINDS (const)",
"CalendarConfig (type)",
"CalendarConfigSchema (const)",
Expand Down Expand Up @@ -125,6 +126,7 @@
"ColumnSummaryAggregation (type)",
"ColumnSummaryConfig (type)",
"ColumnSummaryConfigSchema (const)",
"ColumnSummaryDerivedSource (type)",
"ColumnSummarySchema (const)",
"ColumnSummaryValue (type)",
"CompileListViewGroupQueryOptions (interface)",
Expand Down Expand Up @@ -556,6 +558,7 @@
"checkPageRequiresKind (function)",
"checkPageSourceCompleteness (function)",
"columnSummaryAlias (function)",
"columnSummaryNodeFunctions (function)",
"compileListViewGroupQuery (function)",
"compileListViewGroupRowsQuery (function)",
"dashboardForm (const)",
Expand All @@ -578,6 +581,7 @@
"hasReservedComponentNamespace (function)",
"isActionParamValuePresent (function)",
"isAggregatedViewContainer (function)",
"isColumnSummaryCompatibleWithFieldType (function)",
"isExpressionBindableTextKey (function)",
"isKnownComponentType (function)",
"isRecordContextBlockType (function)",
Expand Down
Loading
Loading