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
23 changes: 23 additions & 0 deletions .changeset/34465-predicate-update-parent-summary-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@objectstack/objectql': patch
---

fix(objectql)!: a predicate update judges each matched row against the parent summaries the rows judged before it move, as consecutive by-id updates do

Clause-②: no (narrowing)
Metadata-protocol: none

<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export changes shape. What moves is the summary value a rule reads while one predicate update is judged: no stored row, registry key or authored value changes, so there is nothing for a migration to rewrite and no tombstone. -->

**BREAKING** for a predicate update (`update(object, patch, { where, multi: true })`, a flow `update_record` step with `multi: true`) of a child object whose validation rule reads a roll-up `summary` of a parent one hop through a reference (`record.inv.primaries > 0`), or whose field-level `requiredWhen` reads one on `parent` (`parent.line_count >= 3`), or that reads a `formula` or stored computed field of the parent calculated from one (`record.project.remaining`, `remaining = budget - spent`). A bulk create and a bulk update already equal one-by-one writes for a rule reading the summary itself (a bulk create does not yet for a rule reading a parent field calculated from one: its accumulator moves the summary key alone, #34821); this extends that to the update that selects its rows by a filter, calculated parent fields included.

- FROM: the matched rows' related parents and master-detail headers were read once, and every row judged the summary as it stood before the update: "one primary line per invoice" (`record.is_primary == true && record.inv.primaries > 0`) refused the second of two lines made primary one by one, and admitted both made primary by one predicate update.
- TO: each matched row judges the summary as the rows judged before it leave it, for `count`, `sum`, `min`, `max` and `avg`, a row moving to another parent included: the base is read in the database (every child as stored for `count` / `sum` / `avg`, the children outside the matched set for `min` / `max`, one grouped aggregate per summary a rule reads) and the matched rows are folded in memory. A parent field calculated from a folded summary is computed again over the parent with the folded value. The update is refused with `VALIDATION_FAILED` and writes nothing, as it does for any refused row. Only an object with such a rule, on an update matching more than one row, reads the base (and, when a calculated field reads a folded summary, the parents' stored rows once).
- The answer moves both ways. Mostly it narrows: main admitted a row that one-by-one refuses, and it is refused now. It also admits a row main refused when the rows judged before it move the summary toward admitting it, as one by one does. For example, with `parent.line_count >= 3` requiring a field, a row leaving that parent lowers the count the later rows read.

**Not moved.** A rule that reads no summary, a predicate update matching one row, a by-id update, hooks and flow steps (they read the value from before the update), and a rule of the parent itself (it judges the summary once, after the write). Three cases keep reading the stored value, as before, and each one covers that summary for EVERY parent the update reads it for, not only the parent that caused it:
- a summary whose `filter` reads through another reference, as a dotted path (`{ 'account.kind': 'x' }`) or in the nested form (`{ account: { status: 'x' } }`), or that the in-memory filter walker refuses;
- a summary meeting a value that is not a number, in any matched row or in any parent's base;
- a `min` / `max` when any one parent holds more than 5,000 matched rows.

The fix for a caller: update the rows the rule refuses on their own, or narrow the filter so the update matches the rows the rule admits.
15 changes: 15 additions & 0 deletions content/docs/data-modeling/fields.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,21 @@ recomputes after each row, so each row's rules, and the parent's own rules
judging each recompute, read it as one-by-one updates would, for every summary
function, `avg` included. A `parent.` predicate that reads a field that is not a
summary, and is not calculated from one, does not count.
**A predicate update equals one-by-one updates too:** an update that selects
its rows by a filter (`update(object, patch, { where, multi: true })`, a flow
`update_record` step with `multi: true`) judges every matched row before its
one write. A validation rule reading a parent summary one hop through a
reference, a detail object's `requiredWhen` reading it on `parent`, and a rule
reading a formula or stored computed field of the parent calculated from it,
read it as the matched rows judged before leave it, for every summary function,
`avg` included, and a row that moves to another parent leaves the old one and
joins the new one. If any row is refused, none is written. Three cases keep
reading the stored value for every parent the update reads that summary for: a
summary whose `filter` reads through another reference, as a dotted path or in
the nested form `{ account: { status: 'x' } }` (or one the in-memory filter
cannot evaluate), a summary meeting a value that is not a number, and a `min` / `max`
when one parent holds more than 5,000 matched rows. A rule of the parent
itself judges the summary once, after the write.
**Hooks and flow steps read the value from before the batch:** inside one bulk
create or bulk update, a hook or flow step that reads a parent's summary reads
it as stored before the batch, on create and update alike.
Expand Down
20 changes: 19 additions & 1 deletion packages/objectql/src/engine-summaries-and-expansion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,19 @@ const ruleReadsByIndex = new WeakMap<object, Set<string>>();
* (`summary-rule-reads.ts`).
*/
function rollUpReadByARule(host: Pick<SummariesAndExpansionHost, '_registry' | 'summaryIndex'>, descriptors: readonly SummaryDescriptor[]): boolean {
const read = summaryFieldsReadByRulesOf(host);
return descriptors.some((d) => read.has(ruleReadKey(d.parentObject, d.summaryField)));
}

/** The `<object>.<field>` keys a rule reads, cached per built summary index ({@link ruleReadsByIndex}). */
function summaryFieldsReadByRulesOf(host: Pick<SummariesAndExpansionHost, '_registry' | 'summaryIndex'>): Set<string> {
const index = host.summaryIndex;
let read = index ? ruleReadsByIndex.get(index) : undefined;
if (!read) {
read = summaryFieldsReadByRules(host._registry.getAllObjects(), index ? [...index.values()].flat() : []);
if (index) ruleReadsByIndex.set(index, read);
}
return descriptors.some((d) => read.has(ruleReadKey(d.parentObject, d.summaryField)));
return read;
}

/**
Expand Down Expand Up @@ -456,6 +462,18 @@ export class SummariesAndExpansionFacet {
return [...new Set(this.getSummaryDescriptors(childObject).map((desc) => desc.fkField))];
}

/** [#34465] The roll-ups over `childObject` whose summary field a rule reads
* — the same judgement that keeps a recompute from being deferred
* (`summary-rule-reads.ts`, ruling 6928862195 B). A predicate update folds
* exactly these into the values its rules read, so each matched row is
* judged as one-by-one updates judge it. Empty when no rule reads any. */
static summaryDescriptorsReadByRules(this: Pick<SummariesAndExpansionHost, '_registry' | 'getSummaryDescriptors' | 'summaryIndex'>, childObject: string): SummaryDescriptor[] {
const descriptors = this.getSummaryDescriptors(childObject);
if (descriptors.length === 0) return [];
const read = summaryFieldsReadByRulesOf(this);
return descriptors.filter((d) => read.has(ruleReadKey(d.parentObject, d.summaryField)));
}

/** The body of `ObjectQL#getOwnedSummaryDescriptors`; the method's docblock there is its contract. */
static getOwnedSummaryDescriptors(this: Pick<SummariesAndExpansionHost, 'ensureSummaryIndexes' | 'summaryIndexByParent'>, parentObject: string): SummaryDescriptor[] {
this.ensureSummaryIndexes();
Expand Down
Loading
Loading