You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
QueryNameMapper transforms nested selects twice per level, so building policy queries is exponential in check() depth #2913
With the policy plugin and @map/@@map, the time spent building a query grows exponentially with how deep check() nests. The generated SQL stays small and runs in milliseconds; the time is spent in QueryNameMapper before the query reaches the database.
Expected: building the query takes time proportional to its size.
Minimal repro: a chain of models where each one is readable only if its parent is.
model M0 {
id Int @id
children M1[]
@@allow('all', true)
@@map('m0')
}
model M1 {
id Int @id
parentId Int
parent M0 @relation(fields: [parentId], references: [id])
children M2[]
@@allow('create', true)
@@allow('read', check(parent))
@@map('m1')
}
// M2 ... Mn follow the same pattern
Time of findMany on the last model, on dev with SQLite:
Depth
2
4
6
8
10
12
dev
7 ms
4 ms
9 ms
90 ms
1,271 ms
20,445 ms
with the fix below
7 ms
2 ms
2 ms
2 ms
2 ms
2 ms
In our app (PostgreSQL, @map on every model) the chains are shorter but wider, e.g. an invoice item whose rules reach the invoice, the receipt, the installment, the document and the policy through check() and collection predicates. There it shows up as:
Operation
Total
Time in the database
findMany
76 ms
3 ms
create rejected by policy
320 ms
3 ms
updateMany rejected by post-update
1,300 ms
16 ms
Environment:
ZenStack version: 3.10.0 (the code below is unchanged on dev)
Database type: PostgreSQL 17 (also reproduces on SQLite)
Node.js version: 24.x
Package manager: pnpm
Additional context
QueryNameMapper.transformSelectQuery (packages/orm/src/client/executor/name-mapper.ts) transforms from, joins and selections itself, then calls:
Kysely's OperationNodeTransformer.transformSelectQuery transforms from, joins and selections again, and the override throws those results away by overwriting them with its own. So every subquery in FROM, JOIN or SELECT is transformed twice, and every subquery inside it four times, and so on. check() nests two such selects per level (select * from (select ... as "$condition" ...) as "$sub"), hence the ~4x per level in the table.
Counting transformSelectQuery calls against the number of select keywords in the final SQL, in our app:
Operation
Selects in SQL
Calls on dev
Calls with the fix
findMany
35
5,308
35
create
72
26,541
72
updateMany
221
111,471
221
Skipping the parts that were already transformed fixes it:
The generated SQL is byte-identical for every query we compared, the three operations above drop to 5 ms, 9 ms and 24 ms, and our policy-heavy test suite went from 26 s to 12.5 s. We're running it as a pnpm patch for now. I'll open a PR with a regression test.
Activity