Fix: normalize empty strings consistently across MySQL, PostgreSQL, and Snowflake - #4
Open
vmatt wants to merge 2 commits into
Open
Fix: normalize empty strings consistently across MySQL, PostgreSQL, and Snowflake#4vmatt wants to merge 2 commits into
vmatt wants to merge 2 commits into
Conversation
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.
Problem
Cross-database diffs produced spurious differences on string columns where one side stored
''(empty string) and the other storedNULL.MySQL and PostgreSQL treat empty string and NULL as distinct values. Snowflake can store genuine
''but also sometimes storesNULLfor the same logical "no value" — depending on how data was loaded. When reladiff hashed rows,''andNULLproduced different hashes, causing false diffs.Example false diff on a
text/varcharcolumn:Root cause
normalize_value_by_typedispatchesStringTypecolumns tonormalize_text(), which defaulted to a plainto_string()cast in all dialects. No normalization was applied to empty strings before hashing.Fix
Added a
normalize_texthook toAbstractMixin_NormalizeValue(insqeleton/abcs/mixins.py) with a passthrough default. MySQL, PostgreSQL, and Snowflake all override it to emitNULLIF(<value>, ''), so empty strings are treated as NULL before hashing.sqeleton/databases/mysql.py):NULLIF(cast({value} as char), '')sqeleton/databases/postgresql.py):NULLIF({value}::varchar, '')sqeleton/databases/snowflake.py):NULLIF(cast({value} as string), '')Oracle is intentionally excluded — it auto-converts
''toNULLat storage time, so Oracle already behaves consistently and needs no override.This makes all four cases match correctly across any pair of these databases:
''NULLNULL/NULLNULL''NULL/NULL''''NULL/NULLNULLNULLNULL/NULLFiles changed
sqeleton/abcs/mixins.py— addednormalize_text()hook +StringTypedispatch innormalize_value_by_type()sqeleton/databases/mysql.py—normalize_textoverride:NULLIF(cast(... as char), '')sqeleton/databases/postgresql.py—normalize_textoverride:NULLIF(... ::varchar, '')sqeleton/databases/snowflake.py—normalize_textoverride:NULLIF(cast(... as string), '')