Skip to content

Commit 521b4f6

Browse files
fix(driver-sql): on Oracle, a composed ReadableIds scope primes its RAW(16) parent's uuid columns (#36084)
Fixes #35960 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:在 Oracle 外部表上,主键为十六字节二进制 uuid 的父对象,其子记录按父记录可见性读取时,现在第一次读就返回应有的记录,取反也只返回其余记录。 - 为什么做:数据权限里“按父记录控制可见性”的子对象在 Oracle 上静默读空,替用 Oracle 外部表的部署解决这个问题。 - 出错会怎样:只影响 Oracle 驱动在读子对象前多读一次父表的列类型;若读取失败则按原来的方式绑定,可直接撤回本次改动。 - 要你做什么:无。 ## What changes On Oracle, a door primes a federated table's `RAW(16)` uuid columns from the catalog before it binds a comparand (`primeOracleRawUuidColumns`). It primed only the door's own object, so a composed `ReadableIds` scope naming a `RAW(16)`-keyed parent bound the parent's ids as text: the child read answered no row and its negation every row, until something read the parent on that driver. - `sql-driver-field-mapping.ts`: `primeOracleRawUuidColumns(object, where?)` now primes the door's object and every object a composed `ReadableIds` leaf in `where` names, at any depth. It reuses the existing walk `collectTextMatchObjects` (the SQL Server GUID prime's), ignoring its text-operator answer, so there is still one walk. - Every Oracle door that primes with a filter in hand passes it: `find`/`findOne` (`findRows`), `count`, `distinct`, `updateMany`, `deleteMany`, `aggregate` (its where plus measure filters, folded once into `primedWhere` for all three primes, and each joined parent's scope), `findWithWindowFunctions`, `analyzeQuery`. - `sql-driver.ts`: the stub takes and forwards `where`; its docblock says what it now primes. - Doors with no filter (`create`, `bulkCreate`, `update`, `delete`, `bulkDelete`) are unchanged. Every other dialect is unchanged (the body returns on `!isOracle` first). Read first, by risk: `sql-driver-field-mapping.ts` (the prime body, the loop over objects), `sql-driver-aggregation.ts` (the `primedWhere` hoist feeds the SQL Server and PostgreSQL primes too), `sql-driver-fixed-read-leaf-conformance.test.ts` (the new describe). ## Tests New live pin in `sql-driver-fixed-read-leaf-conformance.test.ts`, on the Oracle testkit cell (`ORACLE_CELL`, gvenzl/oracle-free 23-slim-faststart): `a composed scope's RAW(16) parent is primed by the child's own door: a fresh driver, the child read first`. Each case builds a fresh `SqlDriver`, and the child read is its first statement. Two forms: the LIST form `{ id: { $in: [U1 upper-case, U2] } }` inside the composed scope, and the share-grant leaf. Ten cases each: find `[c1, c2]`, the negation `[c4]`, count 2, the negation's count 1, distinct, aggregate (where and measure filter), `findWithWindowFunctions`, `analyzeQuery` (statement carries `HEXTORAW(`), `updateMany`, `deleteMany`. The existing nested pin no longer reads the parent first; its comment said the child's door primes the child alone, which is no longer true. <details><summary>Measured on main 8145424 before the fix: 20 of 20 red</summary> ``` Tests 20 failed | 80 skipped (100) AssertionError: expected [] to deeply equal [ 'c1', 'c2' ] AssertionError: expected [ 'c1', 'c2', 'c4' ] to deeply equal [ 'c4' ] AssertionError: expected +0 to be 2 AssertionError: expected 3 to be 1 AssertionError: expected 'select * from "SRIDO_CHILD" where "PA…' to contain 'HEXTORAW(' ``` </details> <details><summary>With the fix, at head 1924389: both Oracle live files green</summary> ``` src/sql-driver-fixed-read-leaf-conformance.test.ts + src/sql-driver-oracle-federation-conformance.test.ts Test Files 2 passed (2) Tests 365 passed | 2 skipped (367) (20 of 20 pin cases pass) os-verify-lock: VERDICT command-exit 0 ``` </details> <details><summary>Ablation: the walk call removed, pin red, restored to HEAD</summary> `node scripts/ablation-replace.mjs` replaced the line `collectTextMatchObjects(where, objects);` in the Oracle prime with a marker (anchor 1 to 0, blob bb3b7d9 to 73e309341315), ran the pin: `Tests 20 failed | 80 skipped (100)`. Restored: blob equals HEAD (bb3b7d9), `git diff HEAD` empty. The test imports the driver from `src/`, so no rebuild is involved. A first attempt was refused by the tool before any test ran (its replacement text already appeared in the anchor, so the count did not rise); it measured nothing. </details> <details><summary>Package suite and typecheck</summary> - `pnpm --filter @objectstack/driver-sql test` (Oracle cell on), at 0c200dd: `Test Files 268 passed | 13 skipped (281)`, `Tests 4686 passed | 258 skipped (4944)`, VERDICT command-exit 0. The later head 1924389 only removes two `as any` casts in the pin. - `pnpm --filter @objectstack/driver-sql typecheck`: exit 0, and `tsc --noEmit` again at 1924389: exit 0. `--listFiles` includes the conformance test file (1 hit). </details> ## Acceptance notes - `findWithWindowFunctions` on a federated Oracle object: a window spec's `orderBy`/`partitionBy` field is not mapped through `external.columnMap` (`ORDER BY "id"` gives ORA-00904 on a table whose column is `ID`), and Oracle refuses `ROW_NUMBER() OVER ()` without an ORDER BY (ORA-30485). The pin orders by the stored column `ID` and says so. Not filed: the door has no producer outside the driver packages (the request surface tombstones `windowFunctions`; `git grep findWithWindowFunctions` outside the drivers hits only CHANGELOG files). - Aggregate's joined parent now primes the objects its own scope composes, too (same walk). That deeper path is not pinned live here: the fixture has no grandparent. ## Verification Two-minute check: with an Oracle Free container running and `OS_TEST_ORACLE_URL=oracle://USER:PASSWORD@127.0.0.1:PORT/FREEPDB1`, run `pnpm --filter @objectstack/driver-sql exec vitest run src/sql-driver-fixed-read-leaf-conformance.test.ts -t 'fresh driver, the child read first'` and expect `Tests 20 passed`. On main the same command answers `20 failed`. <details><summary>Local gates (derived by dispatch-gates --commands)</summary> `node scripts/pm/dispatch-gates.mjs --ran` on the exit-coded record: `72 derived famil(ies) accounted for — 70 run, 2 NOT-MEASURED (2 DERIVED from a recorded exit 3)`. The 70 ran at 0c200dd with exit 0 (`check:query-options-erasure` went red there, +1 test-surface site from two casts in the pin; the casts were removed and it is green at 1924389). Re-run at 1924389, all exit 0: `check:test-source-alias`, `check:cross-package-test-inputs`, `check:nul-bytes`, `check:issue-citations`, `check:doc-authoring`, `check:driver-conformance`, `check-empty-changeset`, `check-changeset-no-major`, `check-adr-0087-registration`. NOT MEASURED: `check:dual-build-cjs-loads` and `check:lean-entry-closure`, reason: PREREQUISITE NOT MET (exit 3). They read the whole repo's built output and only driver-sql's dependency closure was built. CI runs them. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01L9qibwJEh7hMPPvagWM1g7)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 493ad09 commit 521b4f6

10 files changed

Lines changed: 171 additions & 55 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/driver-sql': patch
3+
---
4+
5+
fix(driver-sql): on Oracle, a read scoped by a parent with a RAW(16) uuid key answers its rows on a driver that has not read the parent yet
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
A child read whose composed `ReadableIds` scope names a federated parent with a `RAW(16)` uuid key, such as the read plugin-security derives for a `controlled_by_parent` object, answered no row on Oracle, and its negation answered every row, with no error. It recovered only once something had read the parent on that driver. The driver reads a federated table's `RAW(16)` columns from the Oracle catalog before it binds a comparand, and it read them for the door's own object only, so the parent's ids bound as text and Oracle compared them with the column's upper-case hex.
11+
12+
Each door that compiles a filter (`find`, `findOne`, `count`, `distinct`, `aggregate`, its measure filters and its joined parents, `updateMany`, `deleteMany`, `findWithWindowFunctions` and `analyzeQuery`) now also reads the `RAW(16)` columns of every object a composed `ReadableIds` scope in that filter names, at any depth. The parent's ids bind through `HEXTORAW`, for a list of ids and for the share-grant subquery alike. Nothing changes for any other dialect.

‎packages/drivers/driver-sql/src/sql-driver-aggregation.ts‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,31 +109,23 @@ export class AggregationFacet {
109109
const measureFilters = (query.aggregations ?? [])
110110
.map((agg) => agg.filter)
111111
.filter((filter): filter is NonNullable<typeof filter> => !!filter && Object.keys(filter).length > 0);
112+
// Every condition this statement compiles, folded into one `where` for the primes below.
113+
const primedWhere = measureFilters.length > 0 ? { $and: [...(query.where ? [query.where] : []), ...measureFilters] } : query.where;
112114
// [#31395] SQL Server: which columns are GUIDs, before a text operator compiles.
113-
if (this.isMssql) {
114-
await this.primeMssqlGuidColumns(
115-
object,
116-
measureFilters.length > 0 ? { $and: [...(query.where ? [query.where] : []), ...measureFilters] } : query.where,
117-
);
118-
}
115+
if (this.isMssql) await this.primeMssqlGuidColumns(object, primedWhere);
119116
// [#31390] On Oracle a federated object's RAW(16) uuid columns are read from
120117
// the remote catalog before a comparand or a written value is bound.
121-
if (this.isOracle) await this.primeOracleRawUuidColumns(object);
118+
if (this.isOracle) await this.primeOracleRawUuidColumns(object, primedWhere);
122119
// [ADR-0155 O2] PostgreSQL: the key type a fixed-read leaf's inner read is cast to.
123120
// The measure filters compile with the same where compiler, so they are
124121
// walked too, folded as the SQL Server prime folds them.
125-
await primeFixedReadLeafKeyTypes(
126-
this,
127-
object,
128-
measureFilters.length > 0 ? { $and: [...(query.where ? [query.where] : []), ...measureFilters] } : query.where,
129-
options,
130-
);
122+
await primeFixedReadLeafKeyTypes(this, object, primedWhere, options);
131123
// …and the same primes for each joined related object, whose scope
132124
// compiles against its own table.
133125
for (const join of relationJoins ?? []) {
134126
if (join.parent === null) continue;
135127
if (this.isMssql) await this.primeMssqlGuidColumns(join.parent.object, join.parent.scope);
136-
if (this.isOracle) await this.primeOracleRawUuidColumns(join.parent.object);
128+
if (this.isOracle) await this.primeOracleRawUuidColumns(join.parent.object, join.parent.scope);
137129
await primeFixedReadLeafKeyTypes(this, join.parent.object, join.parent.scope, options);
138130
}
139131
const builder = this.getBuilder(object, options);

‎packages/drivers/driver-sql/src/sql-driver-bulk-operations.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ export class BulkOperationsFacet {
293293
await primeFixedReadLeafKeyTypes(this, object, query.where, options);
294294
// [#31390] On Oracle a federated object's RAW(16) uuid columns are read from
295295
// the remote catalog before a comparand or a written value is bound.
296-
if (this.isOracle) await this.primeOracleRawUuidColumns(object);
296+
if (this.isOracle) await this.primeOracleRawUuidColumns(object, query.where);
297297
this.auditMissingTenant(object, 'updateMany', options);
298298

299299
// `data ?? {}` keeps the nullish tolerance this door already had on the
@@ -342,7 +342,7 @@ export class BulkOperationsFacet {
342342
await primeFixedReadLeafKeyTypes(this, object, query.where, options);
343343
// [#31390] On Oracle a federated object's RAW(16) uuid columns are read from
344344
// the remote catalog before a comparand or a written value is bound.
345-
if (this.isOracle) await this.primeOracleRawUuidColumns(object);
345+
if (this.isOracle) await this.primeOracleRawUuidColumns(object, query.where);
346346
this.auditMissingTenant(object, 'deleteMany', options);
347347
let total = 0;
348348
for (const target of this.rotationShardsOf(object) ?? [object]) {
@@ -392,7 +392,7 @@ export class BulkOperationsFacet {
392392
await primeFixedReadLeafKeyTypes(this, object, query?.where, options);
393393
// [#31390] On Oracle a federated object's RAW(16) uuid columns are read from
394394
// the remote catalog before a comparand or a written value is bound.
395-
if (this.isOracle) await this.primeOracleRawUuidColumns(object);
395+
if (this.isOracle) await this.primeOracleRawUuidColumns(object, query?.where);
396396
const builder = this.getBuilder(object, options);
397397
this.applyTenantScope(builder, object, options);
398398

‎packages/drivers/driver-sql/src/sql-driver-crud.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export class CrudFacet {
159159
static async findRows(this: Pick<CrudHost, 'applyFilters' | 'applyTenantScope' | 'backendStatementFault' | 'formatOutput' | 'getBuilder' | 'isMssql' | 'isOracle' | 'knex' | 'mapSortField' | 'orderKeysFor' | 'primeMssqlGuidColumns' | 'primeMssqlPagingKey' | 'primeOracleRawUuidColumns' | 'remoteColumn' | 'unresolvableFilterColumnRefusal'>, object: string, query: DriverQuery, options?: DriverOptions, singleRowLookup = false): Promise<any[]> {
160160
// [#31390] On Oracle a federated object's RAW(16) uuid columns are read from
161161
// the remote catalog before a comparand or a written value is bound.
162-
if (this.isOracle) await this.primeOracleRawUuidColumns(object);
162+
if (this.isOracle) await this.primeOracleRawUuidColumns(object, query.where);
163163
// [#31327] On SQL Server a federated object's paging key is read from the
164164
// remote catalog before the ORDER BY is decided (`primeMssqlPagingKey`).
165165
if (this.isMssql) await this.primeMssqlPagingKey(object);

‎packages/drivers/driver-sql/src/sql-driver-distinct.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class DistinctFacet {
6464
await primeFixedReadLeafKeyTypes(this, object, filters, options);
6565
// [#31390] On Oracle a federated object's RAW(16) uuid columns are read from
6666
// the remote catalog before a comparand or a written value is bound.
67-
if (this.isOracle) await this.primeOracleRawUuidColumns(object);
67+
if (this.isOracle) await this.primeOracleRawUuidColumns(object, filters);
6868
const builder = this.getBuilder(object, options);
6969
// The third read door that skipped the chokepoint, and the one #6792 does
7070
// NOT name — the card asserts the opposite ("called at 13 sites — …,

‎packages/drivers/driver-sql/src/sql-driver-field-mapping.ts‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,30 @@ export interface FieldMappingHost {
6666
/** The bodies of the "Field mapping" section of `SqlDriver`: a stateless facet, called by the class's stubs. */
6767
export class FieldMappingFacet {
6868
/** The body of `SqlDriver#primeOracleRawUuidColumns`; the method's docblock there is its contract. */
69-
static async primeOracleRawUuidColumns(this: Pick<FieldMappingHost, 'columnFieldByObject' | 'isOracle' | 'oracleRawUuidFieldsByObject' | 'physicalSchemaByObject' | 'physicalTableByObject' | 'readOracleRawUuidColumns'>, object: string): Promise<void> {
70-
if (!this.isOracle || this.oracleRawUuidFieldsByObject.has(object)) return;
71-
// Only a federated object names a remote table; a platform-created table
72-
// never holds a RAW(16) uuid column, so there is nothing to read.
73-
const remoteTable = this.physicalTableByObject[object];
74-
if (remoteTable === undefined) return;
75-
let columns: string[];
76-
try {
77-
columns = await this.readOracleRawUuidColumns(remoteTable, this.physicalSchemaByObject[object]);
78-
} catch {
79-
// A catalog that cannot be read leaves nothing recorded: every value
80-
// keeps today's text bind, and the next door asks again.
81-
return;
69+
static async primeOracleRawUuidColumns(this: Pick<FieldMappingHost, 'columnFieldByObject' | 'isOracle' | 'oracleRawUuidFieldsByObject' | 'physicalSchemaByObject' | 'physicalTableByObject' | 'readOracleRawUuidColumns'>, object: string, where?: unknown): Promise<void> {
70+
if (!this.isOracle) return;
71+
// The door's own object, and the object of every composed `ReadableIds`
72+
// leaf in its `where`: each leaf's scope compiles against that object's
73+
// own table, so its comparands bind by that table's columns.
74+
const objects = new Set<string>([object]);
75+
collectTextMatchObjects(where, objects);
76+
for (const target of objects) {
77+
if (this.oracleRawUuidFieldsByObject.has(target)) continue;
78+
// Only a federated object names a remote table; a platform-created table
79+
// never holds a RAW(16) uuid column, so there is nothing to read.
80+
const remoteTable = this.physicalTableByObject[target];
81+
if (remoteTable === undefined) continue;
82+
let columns: string[];
83+
try {
84+
columns = await this.readOracleRawUuidColumns(remoteTable, this.physicalSchemaByObject[target]);
85+
} catch {
86+
// A catalog that cannot be read leaves nothing recorded: every value
87+
// keeps today's text bind, and the next door asks again.
88+
continue;
89+
}
90+
const colToField = this.columnFieldByObject[target];
91+
this.oracleRawUuidFieldsByObject.set(target, new Set(columns.map((column) => colToField?.[column] ?? column)));
8292
}
83-
const colToField = this.columnFieldByObject[object];
84-
this.oracleRawUuidFieldsByObject.set(object, new Set(columns.map((column) => colToField?.[column] ?? column)));
8593
}
8694

8795
/** The body of `SqlDriver#readOracleRawUuidColumns`; the method's docblock there is its contract. */
@@ -220,7 +228,9 @@ export class FieldMappingFacet {
220228
* [#31395] Walk a `where` tree for what {@link FieldMappingFacet.primeMssqlGuidColumns}
221229
* needs: whether any key in it is a text operator (`TEXT_OPERATORS`), and the
222230
* object of every composed `ReadableIds` leaf in it, added to `objects` (each
223-
* leaf's subquery compiles its scope against that object's own table). Reads
231+
* leaf's subquery compiles its scope against that object's own table).
232+
* {@link FieldMappingFacet.primeOracleRawUuidColumns} reads the same objects
233+
* and ignores the answer: one walk names the tables a scope compiles against. Reads
224234
* plain objects and arrays only; a `Date`, a binary comparand or any other
225235
* value is a comparand, not a node. Answers whether a text operator was found.
226236
*/

‎packages/drivers/driver-sql/src/sql-driver-fixed-read-leaf-conformance.test.ts‎

Lines changed: 115 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,22 @@ async function oracleAnswer(p: Promise<unknown[]>): Promise<{ rows: string[] } |
944944
}
945945
}
946946

947+
/** Bind the key-type suite's remote tables on `d`: the RAW(16) and NUMBER parents, and the child whose `parent` is a RAW(16) lookup. */
948+
function registerOracleKeyTypeObjects(d: SqlDriver): void {
949+
for (const fx of [ORA_RAW, ORA_NUM]) {
950+
(d as any).registerExternalObject({
951+
name: fx.object,
952+
external: { remoteName: fx.table, columnMap: { ID: 'id', LABEL: 'label' } },
953+
fields: { label: { type: 'text' } },
954+
});
955+
}
956+
(d as any).registerExternalObject({
957+
name: ORA_CHILD.object,
958+
external: { remoteName: ORA_CHILD.table, columnMap: { ID: 'id', PARENT: 'parent', LABEL: 'label' } },
959+
fields: { parent: { type: 'lookup', reference: ORA_RAW.object }, label: { type: 'text' } },
960+
});
961+
}
962+
947963
function declareOracleKeyTypePins(cell: FederationDialectCell): void {
948964
describe(`[ADR-0155 O2] driver-sql — SharedRecordIds on Oracle key types (${cell.label})`, () => {
949965
let driver: SqlDriver;
@@ -975,18 +991,7 @@ function declareOracleKeyTypePins(cell: FederationDialectCell): void {
975991
for (const [id, parent] of [['c1', ORA_U1], ['c2', ORA_U2], ['c4', ORA_U4]] as const) {
976992
await knex.raw(`INSERT INTO "${ORA_CHILD.table}" ("ID", "PARENT", "LABEL") VALUES (?, HEXTORAW(?), 'x')`, [id, hex(parent)]);
977993
}
978-
for (const fx of [ORA_RAW, ORA_NUM]) {
979-
(driver as any).registerExternalObject({
980-
name: fx.object,
981-
external: { remoteName: fx.table, columnMap: { ID: 'id', LABEL: 'label' } },
982-
fields: { label: { type: 'text' } },
983-
});
984-
}
985-
(driver as any).registerExternalObject({
986-
name: ORA_CHILD.object,
987-
external: { remoteName: ORA_CHILD.table, columnMap: { ID: 'id', PARENT: 'parent', LABEL: 'label' } },
988-
fields: { parent: { type: 'lookup', reference: ORA_RAW.object }, label: { type: 'text' } },
989-
});
994+
registerOracleKeyTypeObjects(driver);
990995
for (const c of ORACLE_KEY_CASES) await knex(ORA_GRANTS).insert(grantRows(c));
991996
}, 120_000);
992997

@@ -1044,9 +1049,8 @@ function declareOracleKeyTypePins(cell: FederationDialectCell): void {
10441049
}
10451050

10461051
it('nested under a composed ReadableIds scope: the RAW parent\'s grants are converted as its list is', async () => {
1047-
// The child's door primes the child alone; a read of the parent primes its
1048-
// RAW(16) columns, for the list and the leaf alike.
1049-
await driver.find(ORA_RAW.object, { where: { label: 'none' } as any }, ORA_BYPASS);
1052+
// The child's door primes the parent's RAW(16) columns, for the list and
1053+
// the leaf alike (a fresh driver per door: the describe below).
10501054
const raw = ORACLE_KEY_CASES[0];
10511055
const scoped = (scope: unknown) => seamed({ parent: { $in: ReadableIds.compose(ReadableIds.of(ORA_RAW.object), scope as FilterCondition) } });
10521056
const viaList = sortedIds(await driver.find(ORA_CHILD.object, { where: scoped({ id: { $in: [...raw.grants] } }) }, ORA_BYPASS));
@@ -1055,6 +1059,102 @@ function declareOracleKeyTypePins(cell: FederationDialectCell): void {
10551059
expect(await driver.count(ORA_CHILD.object, { where: scoped({ id: { $in: SharedRecordIds.read(ORA_RAW.object, 'u9') } }) }, ORA_BYPASS)).toBe(2);
10561060
});
10571061

1062+
/**
1063+
* A composed `ReadableIds` scope compiles against its parent's table, so
1064+
* the parent's RAW(16) columns must be known before it binds. The child's
1065+
* own door primes them, on a fresh driver whose first statement is the
1066+
* child read: nothing has read the parent on it. Before, the door primed
1067+
* the child alone, so the parent's comparands bound as text and Oracle
1068+
* compared the RAW column as its upper-case hex text: the read answered no
1069+
* row and its negation every row, with no error (measured on Oracle Free
1070+
* 23ai), until something had read the parent on that driver.
1071+
*/
1072+
describe('a composed scope\'s RAW(16) parent is primed by the child\'s own door: a fresh driver, the child read first', () => {
1073+
const FORMS: readonly { name: string; scope: () => unknown }[] = [
1074+
{ name: 'the list (U1 upper-case, U2)', scope: () => ({ id: { $in: [ORA_U1.toUpperCase(), ORA_U2] } }) },
1075+
{ name: 'the share-grant leaf', scope: () => ({ id: { $in: SharedRecordIds.read(ORA_RAW.object, 'u9') } }) },
1076+
];
1077+
/** `door` as the first statement of a fresh driver: nothing has read the parent on it. */
1078+
async function firstOnFresh<T>(door: (d: SqlDriver) => Promise<T>): Promise<T> {
1079+
const fresh = new SqlDriver(cell.config());
1080+
await fresh.connect();
1081+
registerOracleKeyTypeObjects(fresh);
1082+
try {
1083+
return await door(fresh);
1084+
} finally {
1085+
await fresh.disconnect?.();
1086+
}
1087+
}
1088+
const countOf = (out: unknown[]) => Number((out[0] as any).n ?? (out[0] as any).N);
1089+
1090+
for (const f of FORMS) {
1091+
describe(f.name, () => {
1092+
const scopedRaw = () => ({ parent: { $in: ReadableIds.compose(ReadableIds.of(ORA_RAW.object), f.scope() as FilterCondition) } });
1093+
const where = () => seamed(scopedRaw());
1094+
const negated = () => seamed({ $not: scopedRaw() });
1095+
1096+
it('find answers [c1, c2]', async () => {
1097+
expect(sortedIds(await firstOnFresh((d) => d.find(ORA_CHILD.object, { where: where() }, ORA_BYPASS)))).toEqual(['c1', 'c2']);
1098+
});
1099+
1100+
it('the negation answers [c4], never every row', async () => {
1101+
expect(sortedIds(await firstOnFresh((d) => d.find(ORA_CHILD.object, { where: negated() }, ORA_BYPASS)))).toEqual(['c4']);
1102+
});
1103+
1104+
it('count answers 2', async () => {
1105+
expect(await firstOnFresh((d) => d.count(ORA_CHILD.object, { where: where() }, ORA_BYPASS))).toBe(2);
1106+
});
1107+
1108+
it('count of the negation answers 1', async () => {
1109+
expect(await firstOnFresh((d) => d.count(ORA_CHILD.object, { where: negated() }, ORA_BYPASS))).toBe(1);
1110+
});
1111+
1112+
it('distinct answers [c1, c2]', async () => {
1113+
const out = await firstOnFresh((d) => d.distinct(ORA_CHILD.object, 'id', where(), ORA_BYPASS));
1114+
expect(out.map(String).sort()).toEqual(['c1', 'c2']);
1115+
});
1116+
1117+
it('aggregate answers 2, in its where and in a measure filter', async () => {
1118+
expect(countOf(await firstOnFresh((d) => d.aggregate(ORA_CHILD.object, { where: where(), aggregations: [{ function: 'count', alias: 'n' }] }, ORA_BYPASS)))).toBe(2);
1119+
expect(countOf(await firstOnFresh((d) => d.aggregate(ORA_CHILD.object, { aggregations: [{ function: 'count', alias: 'n', filter: where() }] }, ORA_BYPASS)))).toBe(2);
1120+
});
1121+
1122+
it('findWithWindowFunctions answers [c1, c2]', async () => {
1123+
// Oracle requires an ORDER BY in ROW_NUMBER's window, and a window
1124+
// spec names its column as stored (not through `columnMap`): `ID`.
1125+
const out = await firstOnFresh((d) => d.findWithWindowFunctions(ORA_CHILD.object, { where: where(), windowFunctions: [{ function: 'row_number', alias: 'rn', orderBy: [{ field: 'ID' }] }] }, ORA_BYPASS));
1126+
expect(sortedIds(out)).toEqual(['c1', 'c2']);
1127+
});
1128+
1129+
it('analyzeQuery plans the statement find runs: the parent\'s key is compared through HEXTORAW', async () => {
1130+
const out = await firstOnFresh((d) => d.analyzeQuery(ORA_CHILD.object, { where: where() }, ORA_BYPASS));
1131+
expect(String(out.sql)).toContain('HEXTORAW(');
1132+
});
1133+
1134+
it('updateMany touches [c1, c2]', async () => {
1135+
try {
1136+
expect(await firstOnFresh((d) => d.updateMany(ORA_CHILD.object, { where: where() }, { label: 'touched' }, ORA_BYPASS))).toBe(2);
1137+
expect(sortedIds(await knex(ORA_CHILD.table).where('LABEL', 'touched').select({ id: 'ID' }))).toEqual(['c1', 'c2']);
1138+
} finally {
1139+
await knex(ORA_CHILD.table).update({ LABEL: 'x' });
1140+
}
1141+
});
1142+
1143+
it('deleteMany removes [c1, c2] and leaves [c4]', async () => {
1144+
try {
1145+
expect(await firstOnFresh((d) => d.deleteMany(ORA_CHILD.object, { where: where() }, ORA_BYPASS))).toBe(2);
1146+
expect(sortedIds(await knex(ORA_CHILD.table).select({ id: 'ID' }))).toEqual(['c4']);
1147+
} finally {
1148+
await knex(ORA_CHILD.table).del();
1149+
for (const [id, parent] of [['c1', ORA_U1], ['c2', ORA_U2], ['c4', ORA_U4]] as const) {
1150+
await knex.raw(`INSERT INTO "${ORA_CHILD.table}" ("ID", "PARENT", "LABEL") VALUES (?, HEXTORAW(?), 'x')`, [id, hex(parent)]);
1151+
}
1152+
}
1153+
});
1154+
});
1155+
}
1156+
});
1157+
10581158
it('aggregate agrees with find on a RAW(16) key', async () => {
10591159
const raw = ORACLE_KEY_CASES[0];
10601160
const out = await driver.aggregate(

‎packages/drivers/driver-sql/src/sql-driver-query-plan.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class QueryPlanFacet {
7272
await primeFixedReadLeafKeyTypes(this, object, query.where, options);
7373
// [#31390] On Oracle a federated object's RAW(16) uuid columns are read from
7474
// the remote catalog before a comparand or a written value is bound.
75-
if (this.isOracle) await this.primeOracleRawUuidColumns(object);
75+
if (this.isOracle) await this.primeOracleRawUuidColumns(object, query.where);
7676
const builder = this.getBuilder(object, options);
7777
// A PLAN, not rows — so this is a smaller fix than the one above, and it is
7878
// made on its own merits rather than riding in on that one (#6792). It is

0 commit comments

Comments
 (0)