Skip to content

Commit 8bf733d

Browse files
committed
fix(objectql): a write never answers null for a column it carried a value for, so a driver that stores it without echoing it leaves it absent (#29538)
The fill now skips a column the insert payload, or the by-id PATCH and then the stored row before it, holds a non-null value for. On driver-sql, driver-memory and driver-mongodb the echo is the stored row, so nothing changes there; on a driver that stores a column without echoing it, the answer no longer claims a null the row does not hold, which a reader that layers the payload under the answer (the record-change flow record) read over the written value. Claude-Session: https://claude.ai/code/session_01JQwxbXRug3R6WZAjtFuAoS Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3597f58 commit 8bf733d

3 files changed

Lines changed: 92 additions & 9 deletions

File tree

‎packages/objectql/src/declared-read-columns.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ export function rowsWithDeclaredColumnsOnly<T>(rows: T[], declared: ReadonlySet<
163163
* - [#29538] a write's answer (the insert result, single or batch, and the
164164
* by-id update readback) names no projection either, so it is answered on
165165
* the whole stored record: the write answer equals the next read of the row.
166+
* Less the columns the write itself carried a value for
167+
* ({@link storedColumnsLeftUnwritten}): those are stored, so a driver that
168+
* did not echo one leaves it absent rather than answered `null`.
166169
*
167170
* ## What stays absent, and why
168171
*
@@ -236,6 +239,38 @@ export function withSelectedColumnsAnswered<T>(row: T, selected: ReadonlySet<str
236239
return (shaped ?? row) as unknown as T;
237240
}
238241

242+
/**
243+
* [#29538] The {@link selectedStoredColumns} a write's answer may fill with
244+
* `null`: those minus every column the write's images say holds a value. A
245+
* write image is what the engine knows the row stores without the driver's
246+
* echo, newest first — the payload it handed the driver and, on an update, the
247+
* stored row it read before the write. The first image that names a column
248+
* decides it: a non-null value IS stored, so a driver that stored it and did
249+
* not echo it leaves it absent from the answer (unknown), never answered
250+
* `null` (a value the row does not hold) — a reader that layers the payload
251+
* under the answer (`{ ...input, ...result }`) would otherwise lose the value
252+
* it wrote. A `null` there (a PATCH clearing a value the prior row held, too)
253+
* is a stored `null` and stays answerable; an `undefined` names nothing. Same
254+
* set reference when nothing is removed.
255+
*/
256+
export function storedColumnsLeftUnwritten(
257+
selected: ReadonlySet<string> | undefined,
258+
...writeImages: unknown[]
259+
): ReadonlySet<string> | undefined {
260+
if (!selected || selected.size === 0) return selected;
261+
const images = writeImages.filter(
262+
(image): image is Record<string, unknown> => !!image && typeof image === 'object' && !Array.isArray(image),
263+
);
264+
let left: Set<string> | undefined;
265+
for (const name of selected) {
266+
const decider = images.find((image) => image[name] !== undefined);
267+
if (decider === undefined || decider[name] === null) continue;
268+
left ??= new Set(selected);
269+
left.delete(name);
270+
}
271+
return left ?? selected;
272+
}
273+
239274
/** {@link withSelectedColumnsAnswered} over a driver's result page. */
240275
export function rowsWithSelectedColumnsAnswered<T>(rows: T[], selected: ReadonlySet<string> | undefined): T[] {
241276
if (!selected || selected.size === 0) return rows;

‎packages/objectql/src/engine.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ import {
392392
rowsWithDeclaredColumnsOnly,
393393
rowsWithSelectedColumnsAnswered,
394394
selectedStoredColumns,
395+
storedColumnsLeftUnwritten,
395396
withDeclaredColumnsOnly,
396397
withSelectedColumnsAnswered,
397398
} from './declared-read-columns.js';
@@ -16693,10 +16694,15 @@ export class ObjectQL implements IObjectQLEngine {
1669316694
// record. Before the formulas, so a formula over an unwritten column
1669416695
// evaluates against `null` as the read's does; inside the verb, so the
1669516696
// security middleware's field mask still deletes an FLS-refused key.
16697+
// A column the row's payload carried a value for is stored, so it is
16698+
// never answered `null`: a driver that did not echo it leaves it
16699+
// absent (`storedColumnsLeftUnwritten`). Rows pair with `liveRows` by
16700+
// index, which the one-row-per-input guard above holds.
1669616701
const insertAnswerColumns = selectedStoredColumns(schemaForValidation, undefined);
1669716702
result = isBatch
16698-
? rowsWithSelectedColumnsAnswered(result as unknown[], insertAnswerColumns)
16699-
: withSelectedColumnsAnswered(result, insertAnswerColumns);
16703+
? (result as unknown[]).map((row, i) =>
16704+
withSelectedColumnsAnswered(row, storedColumnsLeftUnwritten(insertAnswerColumns, liveRows[i])))
16705+
: withSelectedColumnsAnswered(result, storedColumnsLeftUnwritten(insertAnswerColumns, liveRows[0]));
1670016706

1670116707
// Coerce `boolean` fields (SQLite/libsql return 0/1) to real booleans on
1670216708
// the after-hook view so flow trigger conditions (`record.is_escalated
@@ -18367,8 +18373,11 @@ export class ObjectQL implements IObjectQLEngine {
1836718373
// `null` readback (the row left the caller's scope) stays `null`.
1836818374
result = withDeclaredColumnsOnly(result, declaredColumnSet(updateSchema));
1836918375
// [#29538] Same answer on the stored declared columns as the
18370-
// insert result and the next read (#29408).
18371-
result = withSelectedColumnsAnswered(result, selectedStoredColumns(updateSchema, undefined));
18376+
// insert result and the next read (#29408), less those the
18377+
// PATCH or the stored row before it carried a value for.
18378+
result = withSelectedColumnsAnswered(result, storedColumnsLeftUnwritten(
18379+
selectedStoredColumns(updateSchema, undefined), hookContext.input.data, priorRecord,
18380+
));
1837218381
// [#27495] The handle the stored row named before, where this
1837318382
// statement cleared or replaced it.
1837418383
retiredSecrets = retiredSecretSlots(updateSchema, [priorRecord], hookContext.input.data);

‎packages/objectql/src/write-answer-declared-columns.test.ts‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ interface DriverAst {
3434
limit?: number;
3535
}
3636

37-
function makeSparseDriver(name: string) {
37+
function makeSparseDriver(name: string, unechoed: readonly string[] = []) {
3838
const tables = new Map<string, Map<string, Row>>();
3939
const tableFor = (o: string): Map<string, Row> => {
4040
let t = tables.get(o);
@@ -64,6 +64,8 @@ function makeSparseDriver(name: string) {
6464
// A key given `undefined` is not stored, as driver-mongodb's insert doors
6565
// drop it; the echo is the stored row and nothing else.
6666
const stored = (data: Row): Row => Object.fromEntries(Object.entries(data).filter(([, v]) => v !== undefined));
67+
// A driver that stores a column but does not echo it (#1872's shape).
68+
const echo = (row: Row): Row => Object.fromEntries(Object.entries(row).filter(([k]) => !unechoed.includes(k)));
6769
const driver = {
6870
name, version: '0.0.0', supports: {},
6971
async connect(): Promise<void> {},
@@ -76,14 +78,14 @@ function makeSparseDriver(name: string) {
7678
seq += 1;
7779
const row: Row = { ...stored(data), id: (data.id as string | undefined) ?? `new_${seq}` };
7880
tableFor(object).set(String(row.id), row);
79-
return { ...row };
81+
return echo(row);
8082
},
8183
async update(object: string, id: string, data: Row): Promise<Row | null> {
8284
const prior = tableFor(object).get(id);
8385
if (!prior) return null;
8486
const next: Row = { ...prior, ...stored(data), id };
8587
tableFor(object).set(id, next);
86-
return { ...next };
88+
return echo(next);
8789
},
8890
async updateMany(object: string, ast: DriverAst, data: Row): Promise<number> {
8991
const hits = Array.from(tableFor(object).values()).filter((r) => matches(r, ast?.where));
@@ -112,9 +114,9 @@ const ITEM_FIELDS = {
112114
doubled: { type: 'formula', expression: { dialect: 'cel', source: 'record.qty != null ? record.qty * 2 : -1' } },
113115
};
114116

115-
async function boot() {
117+
async function boot(unechoed: readonly string[] = []) {
116118
const engine = new ObjectQL();
117-
const local = makeSparseDriver('memory');
119+
const local = makeSparseDriver('memory', unechoed);
118120
engine.registerDriver(local.driver as never, true);
119121
await engine.init();
120122
engine.registry.registerObject({ name: ITEM, label: 'Item', fields: ITEM_FIELDS } as never, PACKAGE_ID);
@@ -192,3 +194,40 @@ describe('[#29538] a write answers the record the next read answers', () => {
192194
expect(count).toBe(1);
193195
});
194196
});
197+
198+
describe('[#29538] a column the write carried a value for is never answered null', () => {
199+
// The driver stores `note` but leaves it out of its echo. The answer must not
200+
// claim a `null` the row does not hold; a reader layering the payload under
201+
// the answer (trigger-record-change's flow record) keeps the written value.
202+
let h: Booted;
203+
beforeEach(async () => { h = await boot(['note']); });
204+
205+
it('insert: a written, unechoed column stays absent; an unwritten one is null', async () => {
206+
const answer = await h.engine.insert(ITEM, { title: 'w', note: 'written' }) as Row;
207+
expect(h.local.stored(ITEM, String(answer.id))!.note).toBe('written');
208+
expect(own(answer, 'note')).toBe(false);
209+
expect(answer.qty).toBeNull();
210+
});
211+
212+
it('insert (batch): each row is judged by its own payload', async () => {
213+
const [carried, bare] = await h.engine.insert(ITEM, [{ title: 'x', note: 'n1' }, { title: 'y' }]) as Row[];
214+
expect(own(carried, 'note')).toBe(false);
215+
expect(own(bare, 'note')).toBe(true);
216+
expect(bare!.note).toBeNull();
217+
});
218+
219+
it('update by id: a value the prior row stored and the echo dropped stays absent', async () => {
220+
const seeded = await h.engine.insert(ITEM, { title: 's', note: 'kept' }) as Row;
221+
const answer = await h.engine.update(ITEM, { id: seeded.id, title: 's2' }) as Row;
222+
expect(h.local.stored(ITEM, String(seeded.id))!.note).toBe('kept');
223+
expect(own(answer, 'note')).toBe(false);
224+
expect(answer.qty).toBeNull();
225+
});
226+
227+
it('update by id: a PATCH that clears the prior value is answered null', async () => {
228+
const seeded = await h.engine.insert(ITEM, { title: 's', note: 'kept' }) as Row;
229+
const answer = await h.engine.update(ITEM, { id: seeded.id, note: null }) as Row;
230+
expect(own(answer, 'note')).toBe(true);
231+
expect(answer.note).toBeNull();
232+
});
233+
});

0 commit comments

Comments
 (0)