MySQL Charset NULL Error #27
Merged
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.
MySQL Charset NULL Error - Complete Analysis & Fix
This error occurred in the RiveryIO/go-mysql fork at line 521 in
canal.go.🔍 Root Cause Analysis
What Was Happening
The Application Flow:
GetColumnsCharsets()to load charset information for tablesINFORMATION_SCHEMA.COLUMNStable to get charset dataCHARACTER_SET_NAMEvalues into Go variablesThe Fatal Flaw:
Why It Failed:
CHARACTER_SET_NAMEcolumn can containNULLvaluesdatabase/sqlpackage cannot convertNULLdatabase values directly to Gostringtypesrows.Scan()encounters aNULLvalue, it throws the error: "converting NULL to string is unsupported"The MySQL Data Issue
In MySQL, charset information can be
NULLin these scenarios:BINARY,VARBINARY,BLOBtypes) don't have charsetsINFORMATION_SCHEMAdata1. SQL-Level Protection
Before (vulnerable to NULLs):
After (NULL-safe):
2. Go-Level Protection
Before (crashes on NULL):
After (handles NULL gracefully):
3. Error Handling Improvement
Before (crashes application):
After (graceful error handling):
Handles All Edge Cases
utf8mb4defaultutf8mb4defaultTested and Verified
I created and ran tests that confirm: