Repository navigation
Plan analysis: mask comments and string literals before the text rules (#4524) - #4540
Merged
Merged
Conversation
erikdarlingdata
marked this pull request as ready for review
September 28, 2026 02:00
This was referenced Sep 28, 2026
Closed
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.
Fixes #4524
Part of #4511
Why
Several plan-analysis rules search the statement text for a hint or a keyword —
MAXDOP 1,RECOMPILE,OPTIMIZE FOR UNKNOWN,NOT IN, and the row-goal cause lookup (TOP/EXISTS/IN/FAST). They searched the raw text, so the same word inside a--comment, a/* */comment, or a string literal counted as real code.-- OPTION (RECOMPILE)in a comment stopped the local-variables rule from warning about unsniffable parameters.NOT INinside a string literal could make the anti-join rule warn about a pattern that was never in the query.What changes
Adds
MaskCommentsAndLiterals, mirroring erikdarlingdata/PerformanceStudio@cc18844: it blanks the contents of string literals and whole comments with spaces (so positions and every other character stay put), handlesN'...', doubled-quote escapes'', nested/* /* */ */comments, and an unterminated comment or literal (masks to the end of the text). Delimited identifiers ([...],"...") are stepped over unchanged.Wired at the five call sites PM has today:
MAXDOP 1(PlanAnalyzer.cs~114)RECOMPILE(~255)IdentifyRowGoalCause(~1086)OPTIMIZE FOR UNKNOWN(~277)NOT INviaHasNotInPattern(~1138)PerformanceStudio's follow-up commit (
dcec793) also applies the helper to a rule 38 (Standard Edition), which PM doesn't have yet — that's #4530, and this PR doesn't touch it.Test plan
New class
Darling.Tests.PlanSync4524Tests:the helper's own scan behavior (through rule 27): a
--comment, a/* */comment, nested/* /* */ */, a plain string literal,N'...'with a doubled'', and an unterminated/*that masks to the end of the text — none of these fire the warning;a real hint survives a dash pair inside a string, a quote inside a bracketed identifier, or a closed comment earlier in the text;
rule 20:
RECOMPILEmentioned only in a--or/* */comment still warns about local variables (it used to suppress the warning); a realOPTION (RECOMPILE)still suppresses it;rule 28:
NOT INinside a string literal does not warn; a realNOT INstill does;rule 3:
MAXDOP 1mentioned only in a comment is not treated as a query-level hint;rule 26: the row-goal cause ignores a keyword sitting inside a trailing comment.
Runtime RED on dev: the new class compiles unchanged against dev (
0ed0c4736), and 11 of its theory cases fail there, across rules 3, 20, 26, 27 and 28.Mutation: putting rule 20 back on the raw text turns both comment cases of
Rule20_RecompileMentionedOnlyInAComment_StillWarnsAboutLocalVariablesred.Run: the plan-analysis test classes (
PlanSync4524Tests,ShowPlanParserCondAndMultiplePlanTests,DarlingActualPlanTests,DarlingAnalysisPipelineTests,DarlingMcpPlanToolsTests,McpPlanAnalysisEnvelopeTests,SerialLoopStoreSizeSourceTests,TsqlConventionGuardTests,DocCommentHygieneTests):Total: 151, Failed: 0, Skipped: 1.Lite.Testsbuilds; its plan-analysis classes run on Windows only.CHANGELOG
SECTION: Fixed
ENTRY: - Plan analysis rules for
MAXDOP 1,RECOMPILE,OPTIMIZE FOR UNKNOWN,NOT IN, and row-goal causes ignore hints and keywords written inside a comment or a string literal ([#4540]) - A hint or keyword sitting in a--//* */comment or inside a quoted string no longer counts as code, so these rules no longer fire on text that isn't actually part of the query, and no longer miss a real hint hidden behind one.REF: [#4540]: #4540