Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(psl): retrieve all referential actions based on relationMode #4844

Merged
merged 3 commits into from
Apr 30, 2024

Conversation

Druue
Copy link
Contributor

@Druue Druue commented Apr 29, 2024

The idea was to use referential_actions for retrieving the list of referential actions in the default SQL case (relationMode = "foreignKeys"). Once MongoDB + PlanetScale support was required, we added emulated_referential_actions (only valid when relationMode = "prisma").

We ran into an issue where we were only using referential_actions i.e. only for SQL FKs in prisma-fmt and therefore missing out on completions for referential actions specific to relationMode = "prisma", or in MongoDB's case, we didn't retrieve any.

So this PR renames referential_actions into foreign_key_referential_actions and adds a new referential_actions(&self, relation_mode: RelationMode) -> BitFlags<ReferentialAction> method to help us avoid similar confusion in the future

fixes prisma/language-tools#1676


Context: Slack.

@Druue Druue requested a review from a team as a code owner April 29, 2024 14:42
@Druue Druue requested review from jkomyno and removed request for a team April 29, 2024 14:42
Copy link
Contributor

github-actions bot commented Apr 29, 2024

WASM Query Engine file Size

Engine This PR Base branch Diff
Postgres 2.137MiB 2.137MiB 71.000B
Postgres (gzip) 840.872KiB 840.812KiB 61.000B
Mysql 2.107MiB 2.107MiB 35.000B
Mysql (gzip) 828.040KiB 827.981KiB 60.000B
Sqlite 1.998MiB 1.998MiB 64.000B
Sqlite (gzip) 787.565KiB 787.577KiB -12.000B

@Druue
Copy link
Contributor Author

Druue commented Apr 29, 2024

Somehow we didn't have a test for this in language-tools or in engines so I've added one here using the schema defined in the issue

@Druue Druue added this to the 5.14.0 milestone Apr 29, 2024
Copy link

codspeed-hq bot commented Apr 29, 2024

CodSpeed Performance Report

Merging #4844 will not alter performance

Comparing fix/completions-referential_actions (bd1a681) with main (c559fb9)

Summary

✅ 11 untouched benchmarks

Copy link
Contributor

github-actions bot commented Apr 29, 2024

✅ WASM query-engine performance won't change substantially (0.997x)

Full benchmark report
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/bench?schema=imdb_bench&sslmode=disable" \
node --experimental-wasm-modules query-engine/driver-adapters/executor/dist/bench.mjs
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
cpu: AMD EPYC 7763 64-Core Processor
runtime: node v18.20.2 (x64-linux)

benchmark                   time (avg)             (min … max)       p75       p99      p999
-------------------------------------------------------------- -----------------------------
• movies.findMany() (all - ~50K)
-------------------------------------------------------------- -----------------------------
Web Assembly: Baseline     292 ms/iter       (289 ms … 295 ms)    293 ms    295 ms    295 ms
Web Assembly: Latest       381 ms/iter       (380 ms … 383 ms)    383 ms    383 ms    383 ms
Web Assembly: Current      383 ms/iter       (381 ms … 386 ms)    386 ms    386 ms    386 ms
Node API: Current          202 ms/iter       (198 ms … 205 ms)    204 ms    205 ms    205 ms

summary for movies.findMany() (all - ~50K)
  Web Assembly: Current
   1.9x slower than Node API: Current
   1.31x slower than Web Assembly: Baseline
   1.01x slower than Web Assembly: Latest

• movies.findMany({ take: 2000 })
-------------------------------------------------------------- -----------------------------
Web Assembly: Baseline  11'680 µs/iter (11'499 µs … 12'062 µs) 11'717 µs 12'062 µs 12'062 µs
Web Assembly: Latest    15'607 µs/iter (15'235 µs … 17'590 µs) 15'601 µs 17'590 µs 17'590 µs
Web Assembly: Current   15'604 µs/iter (15'233 µs … 16'905 µs) 15'619 µs 16'905 µs 16'905 µs
Node API: Current        8'188 µs/iter   (7'959 µs … 9'138 µs)  8'269 µs  9'138 µs  9'138 µs

summary for movies.findMany({ take: 2000 })
  Web Assembly: Current
   1.91x slower than Node API: Current
   1.34x slower than Web Assembly: Baseline
   1x faster than Web Assembly: Latest

• movies.findMany({ where: {...}, take: 2000 })
-------------------------------------------------------------- -----------------------------
Web Assembly: Baseline   1'902 µs/iter   (1'757 µs … 3'047 µs)  1'926 µs  2'716 µs  3'047 µs
Web Assembly: Latest     2'431 µs/iter   (2'329 µs … 3'745 µs)  2'416 µs  2'973 µs  3'745 µs
Web Assembly: Current    2'432 µs/iter   (2'346 µs … 3'085 µs)  2'425 µs  2'965 µs  3'085 µs
Node API: Current        1'444 µs/iter   (1'353 µs … 1'856 µs)  1'443 µs  1'772 µs  1'856 µs

summary for movies.findMany({ where: {...}, take: 2000 })
  Web Assembly: Current
   1.68x slower than Node API: Current
   1.28x slower than Web Assembly: Baseline
   1x faster than Web Assembly: Latest

• movies.findMany({ include: { cast: true } take: 2000 }) (m2m)
-------------------------------------------------------------- -----------------------------
Web Assembly: Baseline     560 ms/iter       (552 ms … 575 ms)    562 ms    575 ms    575 ms
Web Assembly: Latest       768 ms/iter       (763 ms … 784 ms)    767 ms    784 ms    784 ms
Web Assembly: Current      764 ms/iter       (759 ms … 780 ms)    766 ms    780 ms    780 ms
Node API: Current          475 ms/iter       (465 ms … 485 ms)    484 ms    485 ms    485 ms

summary for movies.findMany({ include: { cast: true } take: 2000 }) (m2m)
  Web Assembly: Current
   1.61x slower than Node API: Current
   1.36x slower than Web Assembly: Baseline
   1x faster than Web Assembly: Latest

• movies.findMany({ where: {...}, include: { cast: true } take: 2000 }) (m2m)
-------------------------------------------------------------- -----------------------------
Web Assembly: Baseline  78'506 µs/iter (77'274 µs … 79'557 µs) 79'530 µs 79'557 µs 79'557 µs
Web Assembly: Latest       108 ms/iter       (107 ms … 108 ms)    108 ms    108 ms    108 ms
Web Assembly: Current      108 ms/iter       (107 ms … 108 ms)    108 ms    108 ms    108 ms
Node API: Current       62'715 µs/iter (61'820 µs … 64'063 µs) 63'813 µs 64'063 µs 64'063 µs

summary for movies.findMany({ where: {...}, include: { cast: true } take: 2000 }) (m2m)
  Web Assembly: Current
   1.72x slower than Node API: Current
   1.37x slower than Web Assembly: Baseline
   1x faster than Web Assembly: Latest

• movies.findMany({ take: 2000, include: { cast: { include: { person: true } } } })
-------------------------------------------------------------- -----------------------------
Web Assembly: Baseline     983 ms/iter       (979 ms … 991 ms)    990 ms    991 ms    991 ms
Web Assembly: Latest     1'281 ms/iter   (1'274 ms … 1'298 ms)  1'289 ms  1'298 ms  1'298 ms
Web Assembly: Current    1'274 ms/iter   (1'266 ms … 1'290 ms)  1'277 ms  1'290 ms  1'290 ms
Node API: Current          892 ms/iter       (844 ms … 917 ms)    916 ms    917 ms    917 ms

summary for movies.findMany({ take: 2000, include: { cast: { include: { person: true } } } })
  Web Assembly: Current
   1.43x slower than Node API: Current
   1.3x slower than Web Assembly: Baseline
   1.01x faster than Web Assembly: Latest

• movie.findMany({ where: { ... }, take: 2000, include: { cast: { include: { person: true } } } })
-------------------------------------------------------------- -----------------------------
Web Assembly: Baseline     141 ms/iter       (140 ms … 142 ms)    142 ms    142 ms    142 ms
Web Assembly: Latest       184 ms/iter       (183 ms … 187 ms)    186 ms    187 ms    187 ms
Web Assembly: Current      181 ms/iter       (178 ms … 184 ms)    183 ms    184 ms    184 ms
Node API: Current          112 ms/iter       (109 ms … 117 ms)    117 ms    117 ms    117 ms

summary for movie.findMany({ where: { ... }, take: 2000, include: { cast: { include: { person: true } } } })
  Web Assembly: Current
   1.61x slower than Node API: Current
   1.28x slower than Web Assembly: Baseline
   1.02x faster than Web Assembly: Latest

• movie.findMany({ where: { reviews: { author: { ... } }, take: 100 }) (to-many -> to-one)
-------------------------------------------------------------- -----------------------------
Web Assembly: Baseline     875 µs/iter     (814 µs … 1'469 µs)    874 µs  1'360 µs  1'469 µs
Web Assembly: Latest     1'205 µs/iter   (1'139 µs … 1'870 µs)  1'205 µs  1'649 µs  1'870 µs
Web Assembly: Current    1'234 µs/iter   (1'149 µs … 2'066 µs)  1'222 µs  1'820 µs  2'066 µs
Node API: Current          803 µs/iter     (722 µs … 1'176 µs)    817 µs    932 µs  1'176 µs

summary for movie.findMany({ where: { reviews: { author: { ... } }, take: 100 }) (to-many -> to-one)
  Web Assembly: Current
   1.54x slower than Node API: Current
   1.41x slower than Web Assembly: Baseline
   1.02x slower than Web Assembly: Latest

• movie.findMany({ where: { cast: { person: { ... } }, take: 100 }) (m2m -> to-one)
-------------------------------------------------------------- -----------------------------
Web Assembly: Baseline     878 µs/iter     (823 µs … 1'606 µs)    872 µs  1'415 µs  1'606 µs
Web Assembly: Latest     1'219 µs/iter   (1'145 µs … 1'848 µs)  1'220 µs  1'661 µs  1'848 µs
Web Assembly: Current    1'200 µs/iter   (1'151 µs … 1'468 µs)  1'214 µs  1'359 µs  1'468 µs
Node API: Current          793 µs/iter     (724 µs … 1'185 µs)    816 µs    897 µs  1'185 µs

summary for movie.findMany({ where: { cast: { person: { ... } }, take: 100 }) (m2m -> to-one)
  Web Assembly: Current
   1.51x slower than Node API: Current
   1.37x slower than Web Assembly: Baseline
   1.02x faster than Web Assembly: Latest

After changes in bd1a681

@Druue
Copy link
Contributor Author

Druue commented Apr 30, 2024

Test Results:

MySQL w/ FKs: Cascade, Restrict, NoAction, SetNull, SetDefault;
MySQL w/ Prisma: Cascade, Restrict, NoAction, SetNull;
MongoDB w/ Prisma: Cascade, Restrict, NoAction, SetNull;

Which lines up with the following table from our Referential Actions Docs
image

Copy link
Contributor

@jkomyno jkomyno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the change requests. Looks good!

@Druue Druue force-pushed the fix/completions-referential_actions branch from 80843b2 to bd1a681 Compare April 30, 2024 14:30
@Druue Druue merged commit 612a7f7 into main Apr 30, 2024
207 checks passed
@Druue Druue deleted the fix/completions-referential_actions branch April 30, 2024 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing code autocomplete for referential actions with mongodb
3 participants