Repository navigation
fix(mssql): place table lock hint inside nested join parentheses - #12765
Conversation
Code Review by Qodo
1.
|
01db026 to
5102231
Compare
5102231 to
cd4e0bd
Compare
commit: |
|
DO NOT MERGE (yet) Need to check if it works for other databases. This might be an SqlServer syntax, but for other databases the place where the hint was added before this PR could have been correct. |
|
I checked this across all dialects. That call site only emits To make sure I did not miss a path, I generated the SQL for a two-level nested join with all five lock modes, on the old code and on the new code, for 10 driver types: mssql, mysql, mariadb, postgres, cockroachdb, oracle, sap, better-sqlite3, aurora-mysql, spanner. That is 50 queries per side. The two sets are identical except for the three MSSQL lock modes. Samples that the patch does not change: postgres, ... INNER JOIN ("category" "categories" INNER JOIN "category_images_image" "categories_images" ON ... INNER JOIN "image" "images" ON ...) ON ... FOR UPDATEmysql, ... INNER JOIN (`category` `categories` INNER JOIN `category_images_image` `categories_images` ON ... INNER JOIN `image` `images` ON ...) ON ... LOCK IN SHARE MODEoracle and sap put MSSQL, ... INNER JOIN ("category" "categories" INNER JOIN "category_images_image" "categories_images" WITH (NOLOCK) ON ... INNER JOIN "image" "images" WITH (NOLOCK) ON ...) WITH (NOLOCK) ON ...after: ... INNER JOIN ("category" "categories" WITH (NOLOCK) INNER JOIN "category_images_image" "categories_images" WITH (NOLOCK) ON ... INNER JOIN "image" "images" WITH (NOLOCK) ON ...) ON ...The old form drops the hint from The non-MSSQL results above come from generated SQL only, so no server ran them. The MSSQL case ran on a real SQL Server container: the new test fails on |
|
Hey @alumni, I checked the lock hint change across dialects. createTableLockExpression() only returns a non-empty string for mssql, so other databases aren't affected. Does that address the DO NOT MERGE concern? |
|
This repository was recently restructured into a pnpm workspace, and the If your branch has diverged enough that a full rebase isn't worth it, Sorry for the disruption, and thanks for your patience. |
be4b77a to
a22a38e
Compare
|
Rebased onto the new packages layout, head is a22a38e. The rename replayed cleanly, but #12793 landed in the same window and rewrote That change also made my test stop catching the bug. Inner joins no longer nest, so the old two-inner-join query is already flat and correct on master. The broken SQL now appears only when the outer join is a LEFT join, so the test parent is now On current master: LEFT JOIN ("category" "categories" INNER JOIN ... ) WITH (NOLOCK) ON ...With the fix: LEFT JOIN ("category" "categories" WITH (NOLOCK) INNER JOIN ... ) ON ...I ran it against a SQL Server container. The test fails on master and passes with the patch, and the suite is 3648 passing, 0 failing. I also re-ran the cross-dialect check on the rebased code. Only the three MSSQL lock modes change, the other nine driver types generate identical SQL. |
|
Hey @alumni, please take a look when you have time |
Fixes #12764
Description of change
On SQL Server, a relation chain two levels deep combined with
setLock()produced invalid T-SQL. The table hint landed after the closing parenthesis of the nested join, where SQL Server does not allow one, and the inner table was left without a hint at all:The query then failed with
Incorrect syntax near the keyword 'with'. Nesting the joins in parentheses came in with #11137, butbuildJoinClausestill appended the hint afterpostfix, so once a join had children the hint drifted outside the parentheses instead of staying on its own table reference. Moving the call in front ofchildJoinsputs the hint straight after the alias it belongs to, which is where the flat (depth 1) case already put it:Verified with a new test in
test/functional/query-builder/locking, which fails onmasterand passes here. The full suite runs green against SQL Server 2025.Pull-Request Checklist
masterbranchFixes #12764tests/**.test.ts)docs/docs/**.md) N/A, bug fix with no documented behavior change