Skip to content

Commit 1bbd895

Browse files
committed
fix(transform): route schema-qualified argument types in identity casts
1 parent c02bc96 commit 1bbd895

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

‎packages/transform/__tests__/identity-casts.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ describe('object-identity casts', () => {
3131
.toContain(`CAST('my_schema.fn(uuid, text)' AS regprocedure)`);
3232
});
3333

34+
it('routes a schema-qualified argument type', () => {
35+
expect(
36+
run(`SELECT assert_function('my-schema.fn("other-schema".row_t)'::regprocedure);`)
37+
).toContain(`CAST('my_schema.fn("other_schema".row_t)' AS regprocedure)`);
38+
});
39+
40+
it('routes an argument type under an unqualified function name', () => {
41+
expect(
42+
run(`SELECT assert_function('fn("other-schema".row_t)'::regprocedure);`)
43+
).toContain(`CAST('fn("other_schema".row_t)' AS regprocedure)`);
44+
});
45+
3446
it('routes ::regproc', () => {
3547
expect(run(`SELECT assert_trigger('my-schema.users'::regclass, 'stamps', 'other-schema.tg'::regproc);`))
3648
.toContain(`CAST('other_schema.tg' AS regproc)`);

‎packages/transform/src/transform.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -375,20 +375,24 @@ export function transformIdentityCastLiteral(
375375
return QuoteUtils.quoteIdentifier(newName);
376376
}
377377

378-
const qualified = QUALIFIED_IDENTITY_RE.exec(sval);
379-
if (!qualified) return sval;
378+
// `regprocedure` and `regoperator` carry an argument list after the name, and
379+
// an argument type can itself be schema-qualified
380+
// (`fn("my-schema".row_type)`), so the list is routed as ordinary schema refs.
381+
const argsAt = sval.indexOf('(');
382+
const head = argsAt === -1 ? sval : sval.slice(0, argsAt);
383+
const args =
384+
argsAt === -1 ? '' : transformSchemaRefsInString(sval.slice(argsAt), router, result);
385+
386+
const qualified = QUALIFIED_IDENTITY_RE.exec(head);
387+
if (!qualified) return `${head}${args}`;
380388
const schemaName = unquoteIdentifier(qualified[1]);
381-
const remainder = qualified[2];
382-
// `regprocedure` and `regoperator` carry an argument list after the name.
383-
const argsAt = remainder.indexOf('(');
384-
const objName = unquoteIdentifier((argsAt === -1 ? remainder : remainder.slice(0, argsAt)).trim());
385-
const args = argsAt === -1 ? '' : remainder.slice(argsAt);
389+
const objName = unquoteIdentifier(qualified[2].trim());
386390

387391
const target = router.resolveObject(schemaName, objName, ns);
388-
if (!target) return sval;
392+
if (!target) return `${head}${args}`;
389393
const rebound = target.name !== undefined && target.name !== objName;
390394
const requalified = target.schema === null || (!!target.schema && target.schema !== schemaName);
391-
if (!rebound && !requalified) return sval;
395+
if (!rebound && !requalified) return `${head}${args}`;
392396

393397
result.schemasFound.add(schemaName);
394398
const nameToken = QuoteUtils.quoteIdentifier(rebound ? target.name! : objName);

0 commit comments

Comments
 (0)