Skip to content

fix(driver-sql): by-id writes, aggregate() and distinct() honour external.columnMap - #31349

Merged
objectstack-fleet[bot] merged 4 commits into
mainfrom
claude/project-thread-on4xyn
Nov 8, 2026
Merged

objectstack-fleet[bot] merged 4 commits into
mainfrom
claude/project-thread-on4xyn

Conversation

@objectstack-fleet

Copy link
Copy Markdown
Contributor

Requested by 田騏 · project thread

Fixes #31340

Clause-②: no
Metadata-protocol: none

维护者速读

  • 改了什么:外部对象用 external.columnMap 把主键映射到远端列(如 CUST_NO: 'id')时,update()、delete()、bulkDelete() 现在按映射后的列匹配行,不再写死 WHERE "id" = ?;aggregate()(分组、日期分桶、聚合字段)和 distinct() 也改为按远端列取值,输出键和展示仍用本地字段名。
  • 为什么改:远端表恰好另有一个叫 id 的普通列时,旧语句在 SQLite / PostgreSQL / MySQL 上按错误的列匹配:更新返回空,删除会删掉没人指定的行,而且不报错。聚合和去重则直接报「列不存在」。
  • 风险与回滚:没有 columnMap 的对象和轮转分片,生成的 SQL 与之前完全一致。只改 packages/drivers/driver-sql,两个 patch changeset。回滚就是还原这个 PR。
  • 先看:sql-driver-sequence-semantics.ts 的 update / delete,sql-driver-bulk-operations.ts 的 bulkDelete;sql-driver-aggregation.ts 的 columnOf;两个新测试文件。
  • 两分钟验证:pnpm --filter @objectstack/driver-sql exec vitest run src/sql-driver-external-columnmap-writes.test.ts src/sql-driver-external-columnmap-aggregate.test.ts,预期 13 个用例全部通过。
  • 需要你做的:无,席位落地。upsert 同样没走映射,已另立 driver-sql: upsert() ignores external.columnMap for its conflict target and read-back — a mapped key refuses with "no such column: id", or answers a different row than the one it wrote #31342,不在本 PR。

What changes

Two defects, one commit and one patch changeset each. Both fixes were first written on the Oracle federation card's branch; this PR lands them alone off main, with nothing Oracle-specific (no spec change, no dialect arm).

By-id writes. update() (the statement and its read-back), delete() and bulkDelete() resolve the key column through remoteColumn(object, 'id', 'id'), the same mapping create()'s read-back and the write payload (applyWriteColumnMap) already use. Rotation shards are never federated and keep id.

Analytic reads. aggregate() spells every identifier (GROUP BY, the structured group key, the date-bucket expression, the aggregand) as the remote column, and distinct() selects and reads back the remote column. Output keys, presentation (readPresentationKind, presentReadValue) and the per-field registries stay keyed by the FIELD. buildDateBucketExpr gains an optional column parameter: the field still drives temporalFieldKind and the SQLite repair, and the bindings name the column.

An object without a columnMap compiles the statement it compiled before.

Why

ADR-0015 federation maps a local field to a remote column; find(), count() and every WHERE honoured that, these five doors did not. The silent half: a remote table with its own unmapped id column, where the unmapped WHERE "id" = ? matched the surrogate, so an update answered null and a delete removed a row nobody named.

Verification

Measured on this branch (sqlite, the in-tree driver), before the merge of main at 0ceca39 and re-run on the merge head:

  • New pins red with the fix reverted, green with it applied:
    • sql-driver-external-columnmap-writes.test.ts: 5 of 5 fail with sql-driver-sequence-semantics.ts and sql-driver-bulk-operations.ts restored to origin/main; 5 pass with the fix.
    • sql-driver-external-columnmap-aggregate.test.ts: 8 of 8 fail with sql-driver-aggregation.ts, sql-driver-distinct.ts and sql-driver-date-bucket.ts restored to origin/main; 8 pass with the fix.
  • pnpm --filter @objectstack/driver-sql test: 240 files passed, 12 skipped; 3880 tests passed, 218 skipped (the live PG and MySQL cells, which run in Temporal Conformance (live PG + MySQL) on main).
  • pnpm --filter @objectstack/driver-sql run typecheck: exit 0.
  • eslint over the eight changed source and test files: exit 0.
  • dispatch-gates --commands: 68 derived; all 68 run, every one exit 0; dispatch-gates --ran: 68 accounted, 0 NOT-MEASURED, 0 UNRUN.

Out of scope, measured and filed: upsert() on a columnMap-ed object uses the unmapped id as its conflict target and read-back key (#31342).

Human Minimum

  • 1. Schemas start as Zod; packages/spec holds no runtime logic. (packages/spec not touched.)
  • 2. Every name I added uses the casing for its kind.
  • 3. No formatter ran; pnpm lint passes. (eslint over the changed files, exit 0.)
  • 4. One issue, one branch off an up-to-date main, one pull request.
  • 5. A change that publishes carries a changeset, and both declaration lines are filled in here and in the changeset. (@objectstack/driver-sql patch, two changesets.)
  • 6. packages/spec touched: its generated artifacts are regenerated and committed. (Not touched.)
  • 7. No hand edits to content/docs/references/, packages/*/CHANGELOG.md or content/docs/releases/.
  • 8. Title, body and comments are in English.
  • The Before Submitting list in CONTRIBUTING.md is done.

Generated by Claude Code

🤖 Generated with Claude Code

… on every dialect

GROUP BY, the date-bucket expression, the aggregand and SELECT DISTINCT
named the field verbatim, so a columnMap-ed federated object was asked for
a column its remote table does not have. Identifiers are now the remote
column; output keys, presentation and the per-field registries stay the
field. buildDateBucketExpr gains a column parameter: the registries are
read by the field, the statement names the column.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017ihL2euiyuLXizr6FW32R3
…hrough external.columnMap

The by-id doors wrote WHERE "id" = ? whatever the map said: a failure
on a remote table without an id column, and a match on the wrong column
on one that has its own. They now match the key's mapped column, as the
insert read-back already did.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017ihL2euiyuLXizr6FW32R3
@github-actions github-actions Bot added size/m documentation Improvements or additions to documentation tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface labels Nov 8, 2026
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

📓 Docs Drift Check

This PR changes 1 package(s): @objectstack/driver-sql, touching 37 documentable anchor(s).

5 hand-written doc(s) NAME something this change touched and may need an implementation-accuracy re-verification:

  • content/docs/data-modeling/external-datasources.mdx (via remoteColumn (symbol, a field of class SqlDriver; a field of interface AggregationHost; a field of interface BulkOperationsHost; a field of interface DistinctHost), remoteColumn (literal, a string literal in SequenceSemanticsFacet; a string literal in aggregate; a string literal in bulkDelete; a string literal in distinct; a string literal in remoteColumn; a string literal in update))
  • content/docs/data-modeling/queries.mdx (via applyTenantScope (literal, a string literal in SequenceSemanticsFacet; a string literal in aggregate; a string literal in bulkDelete; a string literal in distinct; a string literal in update))
  • content/docs/data-modeling/virtual-objects.mdx (via bulkDelete (symbol, a method of class BulkOperationsFacet))
  • content/docs/frontend/data-source.mdx (via bulkDelete (symbol, a method of class BulkOperationsFacet))
  • content/docs/permissions/tenant-audit-census.mdx (via auditMissingTenant (literal, a string literal in SequenceSemanticsFacet; a string literal in bulkDelete; a string literal in update))

⛔ 1 release-owned page(s) also name something this change touched. These are read-only:

  • content/docs/releases/v17/17-1.mdx (via applyTenantScope (literal, a string literal in SequenceSemanticsFacet; a string literal in aggregate; a string literal in bulkDelete; a string literal in distinct; a string literal in update))

content/docs/releases/ is RELEASE-OWNED (AGENTS.md "Documentation Guardrails"): release
notes are written centrally at release time, and a code PR that edits them is the exact PR
that guardrail exists to stop. They are still audited — read-only. If one of them is actually
wrong, file an issue or open a dedicated docs-only PR; do not edit it here.

What this run could not see
  • 4 name(s) were too generic to anchor anything (single lowercase words)
  • the SDK route bridge reached 63 of 226 client-bound route-ledger rows — the other 163 have no registrar path: tail to select them, so pages documenting THEIR client methods cannot appear above, on this or any run. Of those 163: 0 are remediable by widening that discovery convention (an in-repo file declares the path; the convention did not scan it); 59 are structural — on a ledger where NOT ONE row is declared in-repo, so no discovery change reaches them at any price; 104 are undecided (no in-repo declaration, on a ledger that has other in-repo registrars — absence and an unreadable spelling are not distinguishable here). The rows themselves: node scripts/docs-audit/affected-docs.mjs --bridge-coverage
  • a page that states a rule by its inputs shares no identifier with the emitter that implements the rule, so an emitter-only diff cannot list it — not on this run and not on any run. Measured on fix(driver-sql): emit varchar(maxLength) for a text field a declared index keys on #11430: content/docs/protocol/objectql/types.mdx documents the text-family column mapping by the ObjectQL type names it maps FROM (text / textarea / html) while the diff changed createColumn; it went unlisted, and it was the page that diff falsified, in four places. No shared token exists to detect this on, so a rule your change carries has to be re-read by hand in the pages that restate it.
  • a key NAME is not a key, so the hand re-read the line above prescribes can land on the wrong schema. The same spelling is authorable on one governed type and a [REMOVED] tombstone on another for each of active, aria, joins, objects, template, tools and version (censused on [finding] tools is a key on BOTH AgentSchema (tombstoned, dead) and SkillSchema (live, cloud-attested), so a name-based search attributes skill examples to the agent key — it produced a false stop-the-line alarm on PR #19059 #19093 over the liveness ledger's governed types, top-level keys); nothing in a search result distinguishes the two, so a grep hit on a LIVE example reads as evidence about the DEAD key. Measured on fix(spec): the agent.tools liveness row says dead — it claimed live on a key the schema tombstoned #19059: content/docs/ai/agents.mdx was reported as contradicting the agent.tools tombstone over its tools: example at :161, which is inside the defineSkill({ block opened at :155 — the page was already correct. Settle ownership by PARSING the value against both schemas, never by the name: that literal PASSES SkillSchema, and as an AgentSchema it FAILS at tools with the tombstone prescription. ⛔ These names are not the whole class — a key retired through a .strict() guidance map leaves no tombstone in the walked shape and none of them here (tool.category, live as AIToolDefinition.category).

Coarse fallback — 13 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): node scripts/docs-audit/affected-docs.mjs --json dc7f5c4b4aeb2cb401c68f37f65de19ee352c92a → packageMentionDocs.

Which tree this was computed on

This run read content/docs from d5c62026e218e0a386f2cf1b64c1bd6040c7bc05 — the merge of head 05ecc9792248cad6861a306a3de9946d671b9a68 into base dc7f5c4b4aeb2cb401c68f37f65de19ee352c92a, which is what actions/checkout gives a pull_request run. Not the PR head.

A worktree cut from an older main holds a different content/docs, so re-deriving there can legitimately return a different list — that is a different tree, not a wrong row. To answer on the same tree:

# while this PR is open — GitHub drops the merge commit once it closes
git fetch origin d5c62026e218e0a386f2cf1b64c1bd6040c7bc05 && git checkout d5c62026e218e0a386f2cf1b64c1bd6040c7bc05
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin dc7f5c4b4aeb2cb401c68f37f65de19ee352c92a 05ecc9792248cad6861a306a3de9946d671b9a68 && git checkout -B drift-repro dc7f5c4b4aeb2cb401c68f37f65de19ee352c92a && git merge --no-ff 05ecc9792248cad6861a306a3de9946d671b9a68

node scripts/docs-audit/affected-docs.mjs --json dc7f5c4b4aeb2cb401c68f37f65de19ee352c92a

⚠️ That checkout carried uncommitted changes, so the commit above does not fully identify what was read.

Advisory only, and a precision-first one (#9192): a page is listed because it names a
symbol, wire route or SDK method this diff touched — not because it mentions a changed
package. Each row says which anchor put it there, so a wrong row is reportable rather than
merely annoying. To re-verify, run the docs-accuracy-audit workflow scoped to these files:
node scripts/docs-audit/affected-docs.mjs dc7f5c4b4aeb2cb401c68f37f65de19ee352c92a → pass the list as
args.docs, on the commit named under Which tree this was computed on.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

epic:#27261#1 · marchtian · session_017ihL2c5z3q4h8inrn5kwqV
ACCEPT · PR #31349 @ 05ecc979 · 2026-11-08T03:29Z

维护者速读

  • 改了什么: driver-sql 有两类地方以前没有按外部对象的 columnMap 换成远端列名:
    1. 按主键写入的地方:update()(写入和回读)、delete()、bulkDelete()。现在主键列通过 remoteColumn 解析,和 create() 回读、find()、WHERE 一致。
    2. 聚合和取值的地方:aggregate()(分组、聚合字段、日期分桶)和 distinct()。现在按远端列名生成 SQL,返回结果仍然按字段名作键。
  • 为什么改: 主键映射到别的列时,如果远端表恰好也有一列叫 id,以前的更新和删除会按错误的列匹配,可能改错或删错行,而且不报错。聚合和取值则会直接报「列不存在」。这两个问题是 driver-sql: Oracle as a federation dialect — an external datasource over Oracle Database reads (and, double opt-in, writes) live, with type-compat and the predicate emitters modelling oracle #31328(Oracle)开发时发现的,不依赖 Oracle,所以单独先落地。
  • 风险与回滚: 没有映射的对象不受影响,remoteColumn 在没有映射时返回原字段名。轮转分片对象不是联邦对象,也不受影响。不改 spec(Clause-②: no)。还原即可回滚。
  • 需要你做的: 无,席位落地。

席位意见:ACCEPT。无契约面:Clause-②: no,不碰 packages/spec/src/**,也不是 governed 路径。按 contract-review.md,这种情况的复核记录是 CI 加席位自读,本条即席位自读。无 must-fix。

席位核对

  1. Shape: +327/−22 across 10 files. Write key in sql-driver-sequence-semantics.ts (update, delete) and sql-driver-bulk-operations.ts (bulkDelete). Read side in sql-driver-aggregation.ts, sql-driver-distinct.ts and sql-driver-date-bucket.ts (a column parameter, defaulting to the field). Host wiring in sql-driver.ts. Two sqlite pin files, and one patch changeset per defect naming SQLite, PostgreSQL and MySQL. No Oracle code; it was ported off the driver-sql: Oracle as a federation dialect — an external datasource over Oracle Database reads (and, double opt-in, writes) live, with type-compat and the predicate emitters modelling oracle #31328 branch.
  2. Premise measured: on main, the by-id doors build .where('id', id) / .whereIn('id', ids). Red/green by the card thread: with the fix reverted, 5/5 write pins and 8/8 aggregate/distinct pins go red; on the merge head, 13/13 are green (os-dev-report 6716712598).
  3. Gate check-runs on 05ecc979: 37 runs at ACCEPT time, 10 success, 13 skipped, 14 in progress, none failed. Automerge waits for the required ones. Local: 68 derived gates, all exit 0. Full driver-sql suite: 3880 passed, 218 skipped (live PG/MySQL cells, no URL).
  4. Seat read:
    • Every registry lookup (readPresentationKind, aggregandColumnClassOf, temporalFieldKind) and every output key stays the FIELD. Only identifiers move to the column.
    • The string group key is aliased back to the field when the column differs. A structured key aliases only when the output key differs from the column.
    • The date bucket recurses with the field and swaps identifier bindings equal to the field. Today every bucket arm binds only identifiers, so that holds. Nit: if an arm ever binds a value literal equal to a field name, that literal would be swapped. Not blocking.
  5. Out of scope, routed: upsert's conflict target and read-back also miss the map (measured, two cases), and so does the paged-read tie-breaker. Both are filed as driver-sql: upsert() ignores external.columnMap for its conflict target and read-back — a mapped key refuses with "no such column: id", or answers a different row than the one it wrote #31342, which comes next in the same thread.
  6. Landing: not governed (check-governed-merges --pr 31349; 349 lines ≤ 3000). The seat lands it through the relay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/m tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface

Projects

None yet

2 participants