Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions data_diff/databases/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Float,
Decimal,
Integer,
JSON,
Text,
TemporalType,
FractionalType,
Expand Down Expand Up @@ -68,6 +69,8 @@ class Dialect(BaseDialect):
"tinytext": Text,
# Boolean
"boolean": Boolean,
# JSON
"json": JSON,
}

def quote(self, s: str) -> str:
Expand Down
7 changes: 7 additions & 0 deletions data_diff/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,13 @@ def diff_int_dynamic_color_template(diff_value: int) -> str:


def _jsons_equiv(a: str, b: str):
# Treat Python None (DB null) as the JSON null literal so that a NULL on
# the MySQL side matches a 'null' string produced by TO_JSON_STRING(NULL)
# on the BigQuery side (or any other DB that serializes NULL as 'null').
if a is None:
a = "null"
if b is None:
b = "null"
Comment on lines +519 to +522
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

After normalizing None values to the string "null", it is more efficient to check for string equality before proceeding to parse the JSON. This avoids the overhead of two json.loads() calls when comparing a database NULL (which becomes "null") with a JSON "null" literal, which is the primary use case for this change. Additionally, since this function now explicitly handles None values, the type hints in the function signature should ideally be updated to Optional[str] to reflect this.

    if a is None:
        a = "null"
    if b is None:
        b = "null"
    if a == b:
        return True

try:
return json.loads(a) == json.loads(b)
except (ValueError, TypeError, json.decoder.JSONDecodeError): # not valid jsons
Expand Down
Loading