Skip to content
Merged
22 changes: 22 additions & 0 deletions .changeset/29486-string-stored-filter-comparand.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@objectstack/spec': minor
'@objectstack/objectql': patch
---

feat(objectql, spec): a number or boolean comparand on a text- or option-family field compares as its string, the form the write door stores (#29486)

Clause-②: yes (widening) — a filter that compares a text-family field (`text`, `textarea`, `email`, `url`, `phone`, `password`, `markdown`, `html`, `richtext`, `code`, `signature`, `qrcode`) or an option-family field (`select`, `radio`, `multiselect`, `checkboxes`, `tags`) with a number or a boolean now matches the rows the write door stored for that value; before, it matched none on every driver, with no error.
Metadata-protocol: semantic
Skills-docs: `content/docs/deployment/cli.mdx` (`os migrate text-scalars`, `os migrate option-codes`) now says a filter on the number reads as the string. No `skills/**` page teaches how a filter reads a comparand on a text or option field.

Maintainer ruling A on #29486: a number or boolean comparand on a text- or option-family field compares as `String(value)`, the form the write side stores since #29241 (text) and #29562 (options).

**What answers differently.**

- `{ code: 42 }`, `{ code: { $in: [42, true] } }` and `{ stage: 3 }` against a `text` / `select` field written `42` / `3`: FROM no row (InMemoryDriver and SQLite measured) TO the rows written `42` or `"42"` (`3` or `"3"`). `$ne` / `$nin` FROM every row TO every row but those. `true` / `false` read as `"true"` / `"false"`, and a `bigint` within ±2^53 as the number it names.
- A range is now a STRING range. `$gt` / `$gte` / `$lt` / `$lte` bounds and `$between` members are narrowed like any comparand, so a range on a text or option field compares strings in the column's order: `{ code: { $gt: 9 } }` does not match `"10"` (`"10"` < `"9"` as strings), and `{ code: { $between: [1, 10] } }` does not match `"5"` (`"5"` > `"10"`). Before, InMemoryDriver and MongoDB matched no row for a number bound (they do not order a number against a string), and SQLite compared by the column's affinity: measured, `{ $gt: 9 }` matched `"alpha"` and `{ $gt: 4, $lt: 5 }` matched `"5"` and `"42"`, where it now matches `"42"` alone. To range over numbers, declare the field as a number.
- Every engine verb that collects a filter (`find`, `findOne`, `count`, `aggregate`, `update`, `delete`), both spellings of `where` (the object form and the `FilterArray` sugar), the per-aggregation `filter` and `having`, and every REST door that reaches them. The comparand is narrowed at the engine's field-aware filter seam, beside the number and boolean doors, so every driver receives the string.
- Unchanged: a string comparand, `null` (the null test, never `"null"`), a `Date`, the text operators (`$contains: 42` is still refused), and every other field type. A scalar comparison on a multi-value option field is still refused by the drivers ("use `$contains`"); only the comparand they are handed changes.
- Rows written before #29241 / #29562 and not yet migrated: a raw `42` on driver-memory with persistence matched `{ code: 42 }` and no longer does, as `{ code: "42" }` never did. Run `os migrate text-scalars --apply` and `os migrate option-codes --apply` (tracked in #29643).

**In-process APIs.** `@objectstack/spec/data` exports `stringStoredComparandDoorVerdict`, `stringStoredComparandFieldVerdict`, `STRING_STORED_COMPARAND_DOOR_FAMILIES`, `STRING_STORED_COMPARAND_DOOR_SCALAR_OPERATORS`, `STRING_STORED_COMPARAND_DOOR_LIST_OPERATORS`, the type `StringStoredComparandDoorFieldMeta` / `StringStoredComparandDoorVerdict`, and the cross-driver conformance table `FILTER_STRING_STORED_COMPARAND_CASES` with its fixture (`FILTER_STRING_STORED_COMPARAND_FIELDS`, `FILTER_STRING_STORED_COMPARAND_ROWS`, `StringStoredComparandRow`, `StringStoredComparandCase`). All additive.
12 changes: 7 additions & 5 deletions content/docs/deployment/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1733,8 +1733,9 @@ by: `3` as `"3"`, `true` as `"true"`. Rows written before that rule keep the
form the database stored: the raw number or boolean on driver-memory with
persistence, the text `"3.0"`, `"1.0"` or `"0.0"` on SQLite, and raw elements
inside a multi-value array on every driver. A filter on `"3"` does not find
those rows, a read hands back the old form, and a unique field admits a new
`"3"` beside an old `"3.0"`.
those rows, nor does a filter on `3`, which a filter reads as `"3"` (a number or
boolean compared with an option field compares as its string); a read hands
back the old form, and a unique field admits a new `"3"` beside an old `"3.0"`.

```bash
os migrate option-codes # Dry run: counts per object and field, writes no rows
Expand All @@ -1759,9 +1760,10 @@ A number or a boolean written to a text field (`text`, `textarea`, `email`,
`signature`, `qrcode`) is stored as its string: `42` as `"42"`, `true` as
`"true"`. Rows written before that rule keep the form the database stored: the
raw number or boolean on driver-memory with persistence, the text `"42.0"`,
`"1.0"` or `"0.0"` on SQLite. A filter on `"42"` does not find those rows, a
read hands back the old form, and a unique field admits a new `"42"` beside an
old `42` or `"42.0"`.
`"1.0"` or `"0.0"` on SQLite. A filter on `"42"` does not find those rows, nor
does a filter on `42`, which a filter reads as `"42"` (a number or boolean
compared with a text field compares as its string); a read hands back the old
form, and a unique field admits a new `"42"` beside an old `42` or `"42.0"`.

```bash
os migrate text-scalars # Dry run: counts per object and field, writes no rows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import { describe, it, expect, beforeAll } from 'vitest';
import {
FILTER_COMPARAND_TYPE_CASES,
FILTER_COMPARAND_TYPE_ROWS,
FILTER_STRING_STORED_COMPARAND_CASES,
FILTER_STRING_STORED_COMPARAND_FIELDS,
FILTER_STRING_STORED_COMPARAND_ROWS,
parseFilterAST,
type FilterCondition,
} from '@objectstack/spec/data';
Expand Down Expand Up @@ -101,3 +104,45 @@ describe('[#7872] InMemoryDriver.find — comparand-type conformance (behind the
).rejects.toThrow(/BigInt/);
});
});

/**
* [#29486] `FILTER_STRING_STORED_COMPARAND_CASES`, the driver's half: a number
* or boolean comparand on a text- or option-family field reaches mingo already
* narrowed to the string the write door stored (the engine's declared-type
* door, pinned in `@objectstack/objectql`), so each case's `stored()` filter
* must select exactly its rows here — and the as-written filter, handed to the
* driver directly, still selects none of them: mingo compares `42` with `"42"`
* strictly, which is the cell the door stands in front of.
*/
describe('[#29486] InMemoryDriver.find — a number or boolean comparand on a string-stored field, as the door hands it', () => {
const STRING_TABLE = 'string_stored_conformance';
let driver: InMemoryDriver;

beforeAll(async () => {
driver = new InMemoryDriver({ persistence: false });
await driver.connect();
await driver.syncSchema(STRING_TABLE, { fields: FILTER_STRING_STORED_COMPARAND_FIELDS as never });
for (const row of FILTER_STRING_STORED_COMPARAND_ROWS) await driver.create(STRING_TABLE, { ...row });
});

const ids = async (where: FilterCondition | undefined): Promise<string[]> => {
const rows = await driver.find(STRING_TABLE, { fields: ['id'], where });
return (rows as Array<Record<string, unknown>>)
.map((r) => String(r.id))
.sort((x, y) => x.localeCompare(y));
};

it('the fixture really is every row', async () => {
expect(await ids(undefined)).toEqual(FILTER_STRING_STORED_COMPARAND_ROWS.map((r) => r.id));
});

for (const c of FILTER_STRING_STORED_COMPARAND_CASES) {
it(c.name, async () => {
expect(await ids(parseFilterAST(c.stored())), c.note).toEqual([...c.expected]);
});
}

it('the strict cell still exists on the direct path — { code: 42 } handed to mingo matches no row', async () => {
expect(await ids(parseFilterAST({ code: 42 }))).toEqual([]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ import { BSON } from 'mongodb';
import {
FILTER_COMPARAND_TYPE_CASES,
FILTER_COMPARAND_TYPE_ROWS,
FILTER_STRING_STORED_COMPARAND_CASES,
FILTER_STRING_STORED_COMPARAND_ROWS,
parseFilterAST,
type ComparandTypeRow,
type StringStoredComparandRow,
type FilterCondition,
} from '@objectstack/spec/data';
import { translateFilter } from './mongodb-filter.js';
Expand Down Expand Up @@ -106,7 +109,7 @@ function matchField(value: unknown, cond: unknown): boolean {
return value === cond;
}

function matchDoc(row: ComparandTypeRow, doc: Record<string, unknown>): boolean {
function matchDoc(row: ComparandTypeRow | StringStoredComparandRow, doc: Record<string, unknown>): boolean {
for (const [key, value] of Object.entries(doc)) {
switch (key) {
case '$and':
Expand Down Expand Up @@ -189,3 +192,24 @@ describe('[#7872] driver-mongodb — comparand-type conformance (server-free, be
.toEqual(['1', '2']);
});
});

/**
* [#29486] `FILTER_STRING_STORED_COMPARAND_CASES`, the driver's half: a number
* or boolean comparand on a text- or option-family field reaches
* `translateFilter` already narrowed to the string the write door stored (the
* engine's declared-type door, pinned in `@objectstack/objectql`), so each
* case's `stored()` filter, translated and wire-round-tripped, must select
* exactly its rows — judged by the same strict reader as the table above.
*/
describe('[#29486] driver-mongodb — a number or boolean comparand on a string-stored field, as the door hands it', () => {
for (const c of FILTER_STRING_STORED_COMPARAND_CASES) {
it(c.name, () => {
const doc = translateFilter(parseFilterAST(c.stored()) as FilterCondition) as Record<string, unknown>;
const wire = BSON.deserialize(BSON.serialize(doc)) as Record<string, unknown>;
const selected = FILTER_STRING_STORED_COMPARAND_ROWS.filter((row) => matchDoc(row, wire))
.map((row) => row.id)
.sort((x, y) => x.localeCompare(y));
expect(selected, c.note).toEqual([...c.expected]);
});
}
});
21 changes: 21 additions & 0 deletions packages/drivers/driver-odata/src/odata-driver.conformance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import {
FILTER_COMPARAND_TYPE_ROWS,
FILTER_LOGIC_CASES,
FILTER_LOGIC_ROWS,
FILTER_STRING_STORED_COMPARAND_CASES,
FILTER_STRING_STORED_COMPARAND_ROWS,
FILTER_TEXT_CASES,
FILTER_TEXT_ROWS,
PAGINATION_ALL_IDS,
Expand Down Expand Up @@ -204,6 +206,25 @@ for (const version of VERSIONS) {
}
});

// [#29486] Through the engine, as written: the engine's declared-type door narrows a number
// or boolean comparand on the text and select fields to the stored string before the
// OData filter is built, so the as-written filter answers the case's rows end to end.
describe('FILTER_STRING_STORED_COMPARAND_CASES', () => {
const setup = () => boot({
version,
collation,
properties: [STRING('id', false), STRING('code'), STRING('stage')],
rows: FILTER_STRING_STORED_COMPARAND_ROWS,
fields: { code: text, stage: { type: 'select' } },
});
for (const c of FILTER_STRING_STORED_COMPARAND_CASES) {
it(c.name, async () => {
const { engine } = await setup();
expect(sortedIds(await engine.find(OBJECT, { where: c.filter(), ...SYSTEM } as never)), c.note).toEqual([...c.expected]);
});
}
});

// Not a shared case-set: the shared filter rows negate text, which the driver never pushes
// down. A negation on a number is pushed down, under `not`, to a service whose parser reads
// OData's precedence (`not` binds tighter than `eq`) and refuses a non-Boolean operand.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ import type { Knex } from 'knex';
import {
FILTER_COMPARAND_TYPE_CASES,
FILTER_COMPARAND_TYPE_ROWS,
FILTER_STRING_STORED_COMPARAND_CASES,
FILTER_STRING_STORED_COMPARAND_ROWS,
parseFilterAST,
type FilterCondition,
} from '@objectstack/spec/data';
Expand Down Expand Up @@ -109,6 +111,55 @@ for (const cell of DIALECT_CELLS) {
continue;
}
declareComparandTypeSweep(cell);
declareStringStoredSweep(cell);
}

/**
* [#29486] `FILTER_STRING_STORED_COMPARAND_CASES`, the driver's half: a number
* or boolean comparand on a text- or option-family field reaches this driver
* already narrowed to the string the write door stored (the engine's
* declared-type door, pinned in `@objectstack/objectql`), so each case's
* `stored()` filter must select exactly its rows here. The columns are the
* ones `syncSchema` gives a `text` and a `select` field: strings.
*/
function declareStringStoredSweep(cell: DialectCell): void {
describe(`[#29486] SqlDriver — a number or boolean comparand on a string-stored field, as the door hands it (${cell.label})`, () => {
const STRING_TABLE = 'os29486_string_stored';
let driver: SqlDriver;
let knexInstance: Knex;

beforeAll(async () => {
driver = new SqlDriver(cell.config());
knexInstance = driver.getKnex();
await knexInstance.schema.dropTableIfExists(STRING_TABLE);
await knexInstance.schema.createTable(STRING_TABLE, (t: Knex.TableBuilder) => {
t.string('id').primary();
t.string('code');
t.string('stage');
});
await knexInstance(STRING_TABLE).insert(FILTER_STRING_STORED_COMPARAND_ROWS.map((r) => ({ ...r })));
});

afterAll(async () => {
await knexInstance?.schema.dropTableIfExists(STRING_TABLE).catch(() => {});
await driver?.disconnect?.();
});

const ids = async (where: FilterCondition | undefined): Promise<string[]> => {
const rows = await driver.find(STRING_TABLE, { fields: ['id'], where });
return rows.map((r: any) => String(r.id)).sort((x, y) => x.localeCompare(y));
};

it('the fixture really is every row', async () => {
expect(await ids(undefined)).toEqual(FILTER_STRING_STORED_COMPARAND_ROWS.map((r) => r.id));
});

for (const c of FILTER_STRING_STORED_COMPARAND_CASES) {
it(c.name, async () => {
expect(await ids(parseFilterAST(c.stored())), c.note).toEqual([...c.expected]);
});
}
});
}

function declareComparandTypeSweep(cell: DialectCell): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import {
FILTER_COMPARAND_TYPE_CASES,
FILTER_COMPARAND_TYPE_ROWS,
FILTER_STRING_STORED_COMPARAND_CASES,
FILTER_STRING_STORED_COMPARAND_ROWS,
parseFilterAST,
type FilterCondition,
} from '@objectstack/spec/data';
Expand Down Expand Up @@ -84,3 +86,46 @@ describe('[#7872] SqliteWasmDriver — comparand-type conformance (behind the do
expect(await ids(undefined)).toEqual(['1', '2']);
});
});

/**
* [#29486] `FILTER_STRING_STORED_COMPARAND_CASES`, the driver's half: a number
* or boolean comparand on a text- or option-family field reaches this driver
* already narrowed to the string the write door stored (the engine's
* declared-type door, pinned in `@objectstack/objectql`), so each case's
* `stored()` filter must select exactly its rows here.
*/
describe('[#29486] SqliteWasmDriver — a number or boolean comparand on a string-stored field, as the door hands it', () => {
const STRING_TABLE = 'string_stored_conformance';
let driver: SqliteWasmDriver;

beforeAll(async () => {
driver = new SqliteWasmDriver({ filename: ':memory:' });
await driver.initObjects([
{ name: STRING_TABLE, fields: { code: { type: 'string' }, stage: { type: 'string' } } },
]);
for (const row of FILTER_STRING_STORED_COMPARAND_ROWS) {
await driver.create(STRING_TABLE, { ...row }, { bypassTenantAudit: true });
}
});

afterAll(async () => {
await driver.disconnect();
});

const ids = async (where: FilterCondition | undefined): Promise<string[]> => {
const rows = await driver.find(STRING_TABLE, { fields: ['id'], where });
return (rows as Array<Record<string, unknown>>)
.map((r) => String(r.id))
.sort((x, y) => x.localeCompare(y));
};

it('the fixture really is every row', async () => {
expect(await ids(undefined)).toEqual(FILTER_STRING_STORED_COMPARAND_ROWS.map((r) => r.id));
});

for (const c of FILTER_STRING_STORED_COMPARAND_CASES) {
it(c.name, async () => {
expect(await ids(parseFilterAST(c.stored())), c.note).toEqual([...c.expected]);
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import {
FILTER_COMPARAND_TYPE_CASES,
FILTER_COMPARAND_TYPE_ROWS,
FILTER_STRING_STORED_COMPARAND_CASES,
FILTER_STRING_STORED_COMPARAND_ROWS,
parseFilterAST,
type FilterCondition,
} from '@objectstack/spec/data';
Expand Down Expand Up @@ -88,3 +90,46 @@ describe('[#7872] TursoDriver — comparand-type conformance (local mode, behind
expect(await ids(undefined)).toEqual(['1', '2']);
});
});

/**
* [#29486] `FILTER_STRING_STORED_COMPARAND_CASES`, the driver's half: a number
* or boolean comparand on a text- or option-family field reaches this driver
* already narrowed to the string the write door stored (the engine's
* declared-type door, pinned in `@objectstack/objectql`), so each case's
* `stored()` filter must select exactly its rows here.
*/
describe('[#29486] TursoDriver — a number or boolean comparand on a string-stored field, as the door hands it', () => {
const STRING_TABLE = 'string_stored_conformance';
let driver: TursoDriver;

beforeAll(async () => {
driver = new TursoDriver({ url: ':memory:' });
await driver.initObjects([
{ name: STRING_TABLE, fields: { code: { type: 'string' }, stage: { type: 'string' } } },
]);
for (const row of FILTER_STRING_STORED_COMPARAND_ROWS) {
await driver.create(STRING_TABLE, { ...row }, { bypassTenantAudit: true });
}
});

afterAll(async () => {
await driver.disconnect();
});

const ids = async (where: FilterCondition | undefined): Promise<string[]> => {
const rows = await driver.find(STRING_TABLE, { fields: ['id'], where });
return (rows as Array<Record<string, unknown>>)
.map((r) => String(r.id))
.sort((x, y) => x.localeCompare(y));
};

it('the fixture really is every row', async () => {
expect(await ids(undefined)).toEqual(FILTER_STRING_STORED_COMPARAND_ROWS.map((r) => r.id));
});

for (const c of FILTER_STRING_STORED_COMPARAND_CASES) {
it(c.name, async () => {
expect(await ids(parseFilterAST(c.stored())), c.note).toEqual([...c.expected]);
});
}
});
Loading
Loading