Repository navigation
Add SQLCMD mode auto-detection for query execution - #2848
Draft
Benjin Dubishar (Benjin) wants to merge 2 commits into
Draft
Benjin Dubishar (Benjin) wants to merge 2 commits into
Benjin Dubishar (Benjin) wants to merge 2 commits into
Conversation
Adds an optional autoDetectSqlCmdMode flag to execute requests. When SQLCMD mode isn't otherwise enabled, STS inspects the text being executed and runs that execution in SQLCMD mode if it contains SQLCMD syntax. The execute response now reports isSqlCmdMode for the run.
3 of 5 tasks
…esent SQLCMD commands are only recognized at a colon that starts a line, and variable references always contain $(. Checking for these first avoids a full lex for scripts that only contain mid-line colons (time literals, comments, :: calls).
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds optional auto-detection of SQLCMD syntax for query execution. When enabled, STS checks the text being executed and runs that execution in SQLCMD mode if it contains SQLCMD syntax. Otherwise it runs normally. The decision applies only to that execution run.
Contract
ExecuteRequestParamsBase.AutoDetectSqlCmdMode(bool?, JSONautoDetectSqlCmdMode). It applies toquery/executeDocumentSelection,query/executeStringandquery/executedocumentstatement.isSqlCmdModeis true, or if it is omitted and the query execution settings enable SQLCMD mode.isSqlCmdModeis explicitlyfalse, STS inspects the text being executed (document, selection or statement) whenautoDetectSqlCmdModeis true.ExecuteRequestResult.IsSqlCmdMode(bool, JSONisSqlCmdMode) reports whether the execution actually runs in SQLCMD mode, whether it was requested, enabled in settings or auto-detected. Clients that ignore the field are unaffected.Detection (
SqlCmdSyntaxDetector.ContainsSqlCmdSyntax, in ManagedBatchParser)Lexer::setvar,:r,:on error,:connect,:!!,:exit,:out, and the other recognized commands. Because it reuses the lexer, commands inside string literals or comments,::fn_...andGO [n]are not detected.$(identifier)variable references are detected in code,[...]and"...", but not inside single-quoted string literals or comments.:or the script contains$(. The lexer only recognizes commands at that position, so results are unchanged, but scripts whose colons are all mid-line (time literals, comments,::calls) skip the full lex.Performance
Detection cost compared with the batch parse
Queryalready does (BatchParserWrapper.GetBatches). Overhead is relative to the parse that actually runs: regular mode for a/b, SQLCMD mode for c/d. Release build, Apple M4 Pro, .NET 10; median of 11–101 warmed-up runs.:or$(:only, not SQLCMD:setvaron line 1:setvaronly at the end$(var)only at the endThe 1 KB scripts for b, c and d are about 2 KB, because one generated block is bigger than 1 KB. Case c was measured before the final reorder of the pre-check, so line-1 SQLCMD should now cost less than shown. Without the pre-check, case b cost 62–82% (43.6 ms at 5 MB). Case d still needs a full lex, but only for scripts that really are SQLCMD and are only run in SQLCMD mode because of
autoDetectSqlCmdMode. The parse itself takes about 12–14 ms per MB.Client counterpart: vscode-mssql adds a new
autovalue formssql.query.defaultSqlCmdMode. It sends theautoDetectSqlCmdModerequest flag and reads theisSqlCmdModeresult field.Code Changes Checklist
dotnet test)Validation:
dotnet test --filter "FullyQualifiedName~SqlCmdSyntaxDetectorTests|FullyQualifiedName~QueryExecution.Execution.ServiceIntegrationTests"intest/Microsoft.SqlTools.ServiceLayer.UnitTests: passed (75/75)dotnet test --no-build --filter "FullyQualifiedName~ServiceLayer.UnitTests.QueryExecution|FullyQualifiedName~EditData"intest/Microsoft.SqlTools.ServiceLayer.UnitTests: passed (753/753), run on the first commitdotnet testsuite, integration tests, and end-to-end testing with vscode-mssql.Reviewers: Please read our reviewer guidelines