Repository navigation
Dynamic SQL Logging, String Functions, SQL Debug Example: small correctness fixes - #957
Merged
Merged
Conversation
…ctness fixes - Dynamic SQL Logging: log physical reads from r.reads (already physical) in both passes instead of reads - logical_reads, which went negative on a warm cache; drop unused variables. - String Functions: strip_characters and strip_characters_cte count characters with DATALENGTH / 2 so trailing spaces are kept; README documents the kept-space behavior and error 6841 for control characters. - SQL Debug Example: print loop advances @block_size so blocks no longer repeat a character; the faked long dynamic SQL is a quoted string literal so the default run executes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M98owc3zzZNEpij3p49Bbe
erikdarlingdata
marked this pull request as ready for review
November 13, 2026 08:44
|
Claude finished @erikdarlingdata's task in 12s —— View job ReviewNo blocking issues found. The changes look correct and match the PR description. I read the diff only and did not run any of it.
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M98owc3zzZNEpij3p49Bbe
|
Claude finished @erikdarlingdata's task in 11s —— View job Review (updated for latest push)No blocking issues found. I read the diff only and did not run any of it.
|
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.
Dynamic SQL Logging: physical reads went negative
What went wrong.
dbo.loggingcomputed physical reads asreads - logical_reads. Insys.dm_exec_requests,readsis already the physical read count, so on a warm cache (logical greater than physical) the loggedphysical_reads_mbwas negative, in both the first pass and the second pass.Fix. Both passes now use
r.reads(the second pass had an unqualifiedreads - logical_readsthat bound tor). The variables@cpu_time,@total_elapsed_time,@reads,@writesand@logical_readswere declared and never used, so they are removed.Proof (SQL2019, scratch db). A batch scans a 20000-row table 5 times (warm cache), calls
dbo.loggingwith@guid_in = NULL, scans 5 more times, then calls it again with the first run hash.String Functions: strip_characters dropped trailing spaces
What went wrong. Both variants sized their number list with
TOP (LEN(...)).LENignores trailing spaces, so trailing spaces in the input were never visited and never returned.strip_characters_cte(N'ab12 ', N'0-9')returnedab, notabplus two spaces. An all-space input returned NULL.Fix. Both variants now use
TOP (DATALENGTH(ISNULL(@string, N'')) / 2). The parameter isnvarchar(4000), so the character count is bytes divided by 2.get_lettersandget_numbershave the sameLENpattern but cannot keep a space, so their output does not change and they are untouched.Proof (SQL2019, scratch db with a Numbers table). Result length in bytes:
ab12+ 2 spaces0-9[ab][ab ]a b1+ 2 spaces0-9[ a b][ a b ]0-9[ ]Empty string and NULL input still return NULL.
README.
String Functions/README.mdnow says that edge spaces stay in the result, and documents error 6841. The functions build the result with FOR XML, so kept text with a control character other than tab, CR or LF fails with 6841 (reproduced withNCHAR(1); tab, CR and LF pass). There is no SQL Server 2016-safe code fix, so this is documentation only. The README also gives the workaround, tested on both variants: add the character to the match expression (N'0-9' + NCHAR(1)returnsab). The plain-english checker reports no hits on the lines I added.SQL Debug Example: print loop repeated characters, default run failed
(a) Print loop. The loop printed
@block_sizecharacters per block but advanced@block_size - 1, so every block repeated the last character of the previous block. It now advances@block_size. I also fixed the comment below the loop:@blockstarts at 1, not zero.Proof: I ran the real procedure
@debug_logic = 1, @execute_sql = 0through pyodbc and summed the PRINT blocks (41 blocks both times):A scratch copy of the loop agrees: the rebuilt text equals the string only after the fix.
(b) Default run.
@execute_sqldefaults to true, but the faked long dynamic SQL wasSELECT x =followed by a bareAAAA...BBBBidentifier, soEXECUTE dbo.DebugExample;failed with Msg 103 (identifier too long). The string is nowSELECT x = '+ 8000 characters +', a quoted string literal that is still about 8000 characters long, so the print loop still has a long string to print.Proof:
EXECUTE dbo.DebugExample;on SQL2019. Before: Msg 103, "The identifier that starts with 'AAAA...' is too long". After: runs to the end, zero errors, returns the 8000-character value.Dropped
Nothing. All three findings still held at b7fba0f.
Test notes
@debug_logic = 1; the server finishes (no lingering request), the client does not. The file also ends with a demoEXEC ... @debug_logic = 1, @debug_performance = 1that I kept out of my install step. The pyodbc capture above avoids the stall.zz_fix_c_1on SQL2019 is dropped.Considered, not added
get_lettersandget_numberstoDATALENGTH: no behavior change (a space is never kept), so no concrete trigger.SUBSTRINGunder the BIN2 collation walks UTF-16 code units, andDATALENGTH / 2counts the same units, so it is consistent.🤖 Generated with Claude Code
https://claude.ai/code/session_01M98owc3zzZNEpij3p49Bbe