From 1ad78945246be1182e9c7badc6c190fffd0587f0 Mon Sep 17 00:00:00 2001 From: Caio Tarifa Date: Sat, 7 Nov 2026 11:46:11 -0300 Subject: [PATCH 1/2] fix(orm): don't transform select subqueries twice in QueryNameMapper --- .../orm/src/client/executor/name-mapper.ts | 8 +++++- tests/regression/test/issue-2913.test.ts | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/regression/test/issue-2913.test.ts diff --git a/packages/orm/src/client/executor/name-mapper.ts b/packages/orm/src/client/executor/name-mapper.ts index f26374275..787a11454 100644 --- a/packages/orm/src/client/executor/name-mapper.ts +++ b/packages/orm/src/client/executor/name-mapper.ts @@ -140,7 +140,13 @@ export class QueryNameMapper extends OperationNodeTransformer { })) : undefined; const selections = this.processSelectQuerySelections(node); - const baseResult = super.transformSelectQuery(node, queryId); + + // "from", "joins" and "selections" are already transformed above, don't transform + // them again, otherwise nested subqueries are processed exponentially many times + const baseResult = super.transformSelectQuery( + { ...node, from: undefined, joins: undefined, selections: undefined }, + queryId, + ); return { ...baseResult, diff --git a/tests/regression/test/issue-2913.test.ts b/tests/regression/test/issue-2913.test.ts new file mode 100644 index 000000000..e1707a419 --- /dev/null +++ b/tests/regression/test/issue-2913.test.ts @@ -0,0 +1,28 @@ +import { createPolicyTestClient } from '@zenstackhq/testtools'; +import { describe, expect, it } from 'vitest'; + +describe('Regression for issue 2913', () => { + it('policies chaining check() through mapped models do not take exponential time to compile', async () => { + // M12 -> M11 -> ... -> M0, each model readable only if its parent is + const depth = 12; + const schema = Array.from( + { length: depth + 1 }, + (_, i) => ` +model M${i} { + id Int @id + ${i > 0 ? `parentId Int\n parent M${i - 1} @relation(fields: [parentId], references: [id])` : ''} + ${i < depth ? `children M${i + 1}[]` : ''} + + @@allow('create', true) + @@allow('read', ${i > 0 ? 'check(parent)' : 'true'}) + @@map('m${i}') +}`, + ).join('\n'); + + const db: any = await createPolicyTestClient(schema); + + const start = performance.now(); + await expect(db[`m${depth}`].findMany()).resolves.toEqual([]); + expect(performance.now() - start).toBeLessThan(1000); + }); +}); From dcec56889634269dfe6f681e5ab354fee70529f3 Mon Sep 17 00:00:00 2001 From: Caio Tarifa Date: Sat, 7 Nov 2026 12:03:24 -0300 Subject: [PATCH 2/2] test: check policy results and mapped fields in issue 2913 regression --- tests/regression/test/issue-2913.test.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/regression/test/issue-2913.test.ts b/tests/regression/test/issue-2913.test.ts index e1707a419..2727f7d0a 100644 --- a/tests/regression/test/issue-2913.test.ts +++ b/tests/regression/test/issue-2913.test.ts @@ -10,19 +10,32 @@ describe('Regression for issue 2913', () => { (_, i) => ` model M${i} { id Int @id - ${i > 0 ? `parentId Int\n parent M${i - 1} @relation(fields: [parentId], references: [id])` : ''} + ${i > 0 ? `parentId Int @map('parent_id')\n parent M${i - 1} @relation(fields: [parentId], references: [id])` : `visible Boolean @map('is_visible')`} ${i < depth ? `children M${i + 1}[]` : ''} @@allow('create', true) - @@allow('read', ${i > 0 ? 'check(parent)' : 'true'}) + @@allow('read', ${i > 0 ? 'check(parent)' : 'visible'}) @@map('m${i}') }`, ).join('\n'); const db: any = await createPolicyTestClient(schema); + // two chains, only the first one has a visible root + for (let i = 0; i <= depth; i++) { + await db.$unuseAll()[`m${i}`].createMany({ + data: [1, 2].map((id) => (i === 0 ? { id, visible: id === 1 } : { id, parentId: id })), + }); + } + + // warm up the connection so that the timing below is about building the query + await expect(db.m0.findMany()).resolves.toEqual([{ id: 1, visible: true }]); + const start = performance.now(); - await expect(db[`m${depth}`].findMany()).resolves.toEqual([]); - expect(performance.now() - start).toBeLessThan(1000); + const leaves = await db[`m${depth}`].findMany(); + const elapsed = performance.now() - start; + + expect(leaves).toEqual([{ id: 1, parentId: 1 }]); + expect(elapsed).toBeLessThan(1000); }); });