From cf513eeb5e39d2506efbb75d5369e2c5e0576b57 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 04:55:29 +0000 Subject: [PATCH 1/2] fix(driver-sql): distinct and aggregate group keys present a text boolean as find() does presentReadValue's 'boolean' arm was Boolean(value), so a stored text '0' / 'false' (a TEXT-affinity boolean column, or legacy text a boot has not converged) presented as true in distinct() and aggregate() group keys while find() read it as false. The arm now calls presentStoredBoolean, the function formatOutput applies. Co-authored-by: Claude Claude-Session: https://claude.ai/code/session_01Utp6Ca1jJvsA1dHsPwMiQb --- .changeset/34974-boolean-read-presenter.md | 12 ++ ...river-34974-boolean-read-presenter.test.ts | 145 ++++++++++++++++++ .../src/sql-driver-filter-helpers.ts | 6 +- .../src/sql-driver-sqlite-serialisation.ts | 5 +- 4 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 .changeset/34974-boolean-read-presenter.md create mode 100644 packages/drivers/driver-sql/src/sql-driver-34974-boolean-read-presenter.test.ts diff --git a/.changeset/34974-boolean-read-presenter.md b/.changeset/34974-boolean-read-presenter.md new file mode 100644 index 00000000000..10c14a35464 --- /dev/null +++ b/.changeset/34974-boolean-read-presenter.md @@ -0,0 +1,12 @@ +--- +'@objectstack/driver-sql': patch +--- + +fix(driver-sql): `distinct()` and aggregate group keys read a stored text `'0'` / `'false'` boolean as `false`, as `find()` does + +Clause-②: no +Metadata-protocol: none + +A declared `Field.boolean` / `Field.toggle` column can hold the text `'0'`, `'1'`, `'true'` or `'false'` on SQLite: a column whose type changed to boolean without a rebuild keeps TEXT affinity, and rows a boot has not converged yet still hold the legacy text. Record reads presented those values as their boolean, but `distinct()` and an `aggregate()` group key presented them as `Boolean(value)`, so a stored `'0'` or `'false'` showed as `true`. A group-by over a yes/no field then counted un-ticked rows in a `true` bucket, and `distinct()` could answer `[true]` for a column with no ticked row. + +Both doors now present a boolean through the same function the record read uses: `0`, `'0'` and `'false'` read as `false`; `1`, `'1'` and `'true'` read as `true`. Any other value reads as it did. Postgres is unchanged, since it stores a real boolean. `min` / `max` / `sum` / `avg` over a boolean still answer a number, as before. No metadata key, value or default changes. diff --git a/packages/drivers/driver-sql/src/sql-driver-34974-boolean-read-presenter.test.ts b/packages/drivers/driver-sql/src/sql-driver-34974-boolean-read-presenter.test.ts new file mode 100644 index 00000000000..e3086cd5430 --- /dev/null +++ b/packages/drivers/driver-sql/src/sql-driver-34974-boolean-read-presenter.test.ts @@ -0,0 +1,145 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#34974] `distinct()` and an `aggregate()` group key present a declared + * `Field.boolean` the way `find()` presents it: the stored `0` / `'0'` / + * `'false'` read as `false`, and `1` / `'1'` / `'true'` as `true`. + * + * `presentReadValue`'s `'boolean'` arm was `Boolean(value)`, while the row read + * (`formatOutput`) has read the text spellings as their boolean since #34916. + * On a column with TEXT affinity (one whose type changed to boolean without a + * rebuild) the boot's storage convergence rewrites a legacy `'false'` to `0`, + * which the affinity stores as the text `'0'`, and it leaves a `'0'` alone + * because it already reads as `false` on a row. `Boolean('0')` is `true`, so + * `distinct()` answered `[true]` for a column of un-ticked rows and a group-by + * put every row in a `true` bucket. + * + * Two columns: an INTEGER-form boolean column the driver creates (with the + * pre-#34916 text `'true'` / `'false'` written past the driver, the form a + * boot that has not converged it yet still holds), and a TEXT-affinity column + * after a boot. + */ + +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; +import type { DriverQuery } from '@objectstack/spec/contracts'; +import { SqlDriver } from './sql-driver.js'; + +const INT_TABLE = 'bool_presenter_int_34974'; +const TXT_TABLE = 'bool_presenter_txt_34974'; + +function sqliteDriver(): SqlDriver { + return new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as any); +} + +/** The group-by answer as `[flag, count]` pairs, sorted by the flag's text. */ +async function groupCounts(driver: SqlDriver, table: string, groupBy: DriverQuery['groupBy'], key: string): Promise> { + const rows = (await driver.aggregate(table, { + groupBy, + aggregations: [{ function: 'count', field: 'id', alias: 'n' }], + } as DriverQuery)) as Array>; + return rows + .map((r): [unknown, number] => [r[key], Number(r.n)]) + .sort((a, b) => String(a[0]).localeCompare(String(b[0]))); +} + +describe('[#34974] driver-sql — distinct and aggregate present an INTEGER boolean column as find() does (sqlite)', () => { + let driver: SqlDriver; + + beforeAll(async () => { + driver = sqliteDriver(); + await driver.initObjects([{ name: INT_TABLE, fields: { label: { type: 'string' }, flag: { type: 'boolean' } } }]); + const knex = (driver as any).knex; + // Written past the driver, after the table was created: the convergence + // has not seen these rows, so the text spellings are still text. + await knex(INT_TABLE).insert([ + { id: 'int_one', label: 'int_one', flag: 1 }, + { id: 'text_true', label: 'text_true', flag: 'true' }, + { id: 'text_false', label: 'text_false', flag: 'false' }, + ]); + }); + + afterAll(async () => { + await driver.disconnect(); + }); + + it('the fixture: the column holds the integer 1 and the text true / false', async () => { + const rows = await (driver as any).knex.raw(`select id, typeof(flag) as t from ${INT_TABLE} order by id`); + expect(Object.fromEntries(rows.map((r: any) => [r.id, r.t]))).toEqual({ int_one: 'integer', text_false: 'text', text_true: 'text' }); + }); + + it('find() is the reference: the text false reads as false', async () => { + const rows = (await driver.find(INT_TABLE, { orderBy: [{ field: 'id', order: 'asc' }] } as DriverQuery)) as any[]; + expect(rows.map((r) => [r.id, r.flag])).toEqual([['int_one', true], ['text_false', false], ['text_true', true]]); + }); + + it('distinct() answers false and true, not true alone', async () => { + const values = await driver.distinct(INT_TABLE, 'flag'); + expect([...values].sort()).toEqual([false, true]); + }); + + it('an aggregate group key presents the text false as false', async () => { + // The stored values stay three groups (SQL groups the raw cells); each presents as find() does. + expect(await groupCounts(driver, INT_TABLE, ['flag'], 'flag')).toEqual([[false, 1], [true, 1], [true, 1]]); + }); + + it('an aliased group key presents the same way', async () => { + expect(await groupCounts(driver, INT_TABLE, [{ field: 'flag', alias: 'ticked' }] as any, 'ticked')) + .toEqual([[false, 1], [true, 1], [true, 1]]); + }); +}); + +describe('[#34974] driver-sql — distinct and aggregate present a TEXT-affinity boolean column as find() does (sqlite)', () => { + let driver: SqlDriver; + + beforeAll(async () => { + driver = sqliteDriver(); + const knex = (driver as any).knex; + // A column whose type changed to boolean without a rebuild keeps TEXT + // affinity (a `text` field retyped to `boolean` boots onto the same column). + await knex.raw(`create table ${TXT_TABLE} (id varchar(255) primary key, label varchar(255), flag text)`); + await knex(TXT_TABLE).insert([ + { id: 'canon_zero', label: 'canon_zero', flag: '0' }, + { id: 'legacy_false_a', label: 'legacy_false_a', flag: 'false' }, + { id: 'legacy_false_b', label: 'legacy_false_b', flag: 'false' }, + { id: 'legacy_true', label: 'legacy_true', flag: 'true' }, + ]); + // The boot: the storage convergence rewrites the legacy text to `1` / `0`, + // which this affinity stores as the text '1' / '0'. + await driver.initObjects([{ name: TXT_TABLE, fields: { label: { type: 'string' }, flag: { type: 'boolean' } } }]); + }); + + afterAll(async () => { + await driver.disconnect(); + }); + + it('the fixture: after the boot, every cell is the text 0 or 1', async () => { + const rows = await (driver as any).knex.raw(`select id, flag, typeof(flag) as t from ${TXT_TABLE} order by id`); + expect(rows.map((r: any) => [r.id, r.flag, r.t])).toEqual([ + ['canon_zero', '0', 'text'], + ['legacy_false_a', '0', 'text'], + ['legacy_false_b', '0', 'text'], + ['legacy_true', '1', 'text'], + ]); + }); + + it('find() is the reference: the text 0 reads as false', async () => { + const rows = (await driver.find(TXT_TABLE, { orderBy: [{ field: 'id', order: 'asc' }] } as DriverQuery)) as any[]; + expect(rows.map((r) => [r.id, r.flag])).toEqual([ + ['canon_zero', false], ['legacy_false_a', false], ['legacy_false_b', false], ['legacy_true', true], + ]); + }); + + it('distinct() answers false and true, not true alone', async () => { + const values = await driver.distinct(TXT_TABLE, 'flag'); + expect([...values].sort()).toEqual([false, true]); + }); + + it('an aggregate group key counts three false and one true', async () => { + expect(await groupCounts(driver, TXT_TABLE, ['flag'], 'flag')).toEqual([[false, 3], [true, 1]]); + }); + + it('an aliased group key presents the same way', async () => { + expect(await groupCounts(driver, TXT_TABLE, [{ field: 'flag', alias: 'ticked' }] as any, 'ticked')) + .toEqual([[false, 3], [true, 1]]); + }); +}); diff --git a/packages/drivers/driver-sql/src/sql-driver-filter-helpers.ts b/packages/drivers/driver-sql/src/sql-driver-filter-helpers.ts index 2996a3e6be8..6ec738601be 100644 --- a/packages/drivers/driver-sql/src/sql-driver-filter-helpers.ts +++ b/packages/drivers/driver-sql/src/sql-driver-filter-helpers.ts @@ -45,6 +45,7 @@ import { JSON_COLUMN_INCOMPATIBLE_OPERATORS, findMisplacedReadableIds, presentAs import type { JsonColumnFieldClass } from '@objectstack/core'; import { StorageNameMapping } from '@objectstack/spec/system'; import { emitBalancedGroup } from './sql-driver-balanced-group.js'; +import { presentStoredBoolean } from './sql-driver-sqlite-serialisation.js'; import type { Knex } from 'knex'; import { AUDIT_TIMESTAMP_COLUMNS, CROSS_FIELD_COMPARISON_OPERATORS, SQLITE_TIME_EXPR_REFS, TEXT_OPERATORS, @@ -331,7 +332,10 @@ export class FilterHelpersFacet { // `distinct()` — the divergence the conformance file's §D pins closed. return presentAuditTimestampOutput(value); case 'boolean': - return Boolean(value); + // [#34974] The presenter `formatOutput` applies to a `find()` row, so + // the text `'0'` / `'false'` a TEXT-affinity or unconverted column + // holds presents as `false` here too, never `Boolean('0')`. + return presentStoredBoolean(value); case 'number': // [#20889] The presenter lives in `@objectstack/core`, beside // `AGGREGATE_ANSWER_KIND`, so the analytics native-SQL face presents diff --git a/packages/drivers/driver-sql/src/sql-driver-sqlite-serialisation.ts b/packages/drivers/driver-sql/src/sql-driver-sqlite-serialisation.ts index 7bacb6518a4..6b3e7d5424f 100644 --- a/packages/drivers/driver-sql/src/sql-driver-sqlite-serialisation.ts +++ b/packages/drivers/driver-sql/src/sql-driver-sqlite-serialisation.ts @@ -53,8 +53,11 @@ import { isDateOnInstantColumn, utcMidnightOfDay } from './date-on-instant-colum * that arm admits reached a SQLite column as text (the console's inline grid * sent them), and `Boolean('false')` presented an un-ticked row as `true`. * Any other string keeps the `Boolean(value)` reading it had. + * + * [#34974] `presentReadValue`'s `'boolean'` arm calls this same function, so + * `distinct()` and an `aggregate()` group key read a stored value as `find()` does. */ -function presentStoredBoolean(value: unknown): boolean { +export function presentStoredBoolean(value: unknown): boolean { if (value === 'false' || value === '0') return false; if (value === 'true' || value === '1') return true; return Boolean(value); From c057c311b4f8ed3b8ec8230b0fe6272fa003f756 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 05:03:02 +0000 Subject: [PATCH 2/2] test(driver-sql): the #34973 groupBy pin's before-half reads the text false as false Co-authored-by: Claude Claude-Session: https://claude.ai/code/session_01Utp6Ca1jJvsA1dHsPwMiQb --- .../src/sql-driver-34916-boolean-text-read.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/drivers/driver-sql/src/sql-driver-34916-boolean-text-read.test.ts b/packages/drivers/driver-sql/src/sql-driver-34916-boolean-text-read.test.ts index 74312d0c1c6..11413736a3d 100644 --- a/packages/drivers/driver-sql/src/sql-driver-34916-boolean-text-read.test.ts +++ b/packages/drivers/driver-sql/src/sql-driver-34916-boolean-text-read.test.ts @@ -194,8 +194,9 @@ describe('[#34973] driver-sql — boot converges legacy text booleans to 1 / 0 ( } as DriverQuery)) as Array<{ flag: unknown; n: number }>; return rows.map((r) => [r.flag, Number(r.n)]).sort((a, b) => String(a[0]).localeCompare(String(b[0]))); }; - // Before: four groups, and the text 'false' group presents as true. - expect(await groups()).toEqual([[false, 1], [null, 1], [true, 1], [true, 1], [true, 1]]); + // Before: SQL groups the raw cells apart (five groups); since #34974 each + // presents as find() does, so the text 'false' group reads as false. + expect(await groups()).toEqual([[false, 1], [false, 1], [null, 1], [true, 1], [true, 1]]); await driver.initObjects([CONV_OBJECT]); expect(await groups()).toEqual([[false, 2], [null, 1], [true, 2]]); });