fix(mssql): place table lock hint inside nested join parentheses - #12765
fix(mssql): place table lock hint inside nested join parentheses#12765lazerg wants to merge 2 commits into
Conversation
Code Review by Qodo
1. Test returns inside loop
|
01db026 to
5102231
Compare
5102231 to
cd4e0bd
Compare
…n in nested join lock test
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 |
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