Skip to content

fix(migration/0049): skip NaN sanitize for already-valid JSON values - #71927

Closed
waterWang wants to merge 2 commits into
apache:mainfrom
waterWang:fix-0049-nested-nan-sanitize
Closed

waterWang wants to merge 2 commits into
apache:mainfrom
waterWang:fix-0049-nested-nan-sanitize

Conversation

@waterWang

Copy link
Copy Markdown

Summary

The 0049 migration's NaN sanitizer (regexp_replace) incorrectly matches NaN/Infinity tokens inside JSON string values, injecting an unescaped quote that breaks the outer JSON structure. This causes the migration to abort with INVALID_TEXT_REPRESENTATION on PostgreSQL and equivalent errors on MySQL/SQLite, making the upgrade 2.x → 3.x fail for any XCom value that is a JSON string wrapping a JSON document containing NaN floats.

This happens when:

  • A task pushes already-serialized JSON (e.g. json.dumps(json.dumps({"amount": float("nan")})))
  • A dict value is itself a JSON string ({"report": "{\"amount\": NaN}"})

Both shapes are already valid JSON — the NaN token lives inside a JSON string, not at the JSON syntax level. The sanitizer should not touch them.

Fix

Guard the per-dialect sanitize SQL with the native JSON validity check for each engine:

Dialect Function Available since
PostgreSQL json_valid() PG 14 (Airflow 3 requires PG 14+)
MySQL JSON_VALID() MySQL 5.7+
SQLite json_valid() JSON1 extension (enabled in all modern builds)

Values that are already valid JSON pass through the sanitizer untouched; only values containing bare NaN/Infinity tokens (which are illegal in strict JSON/JSONB) are quoted.

Tests

  • test_sqlite_sanitize_skips_already_valid_json — regression test for both shapes using the SQLite branch (backend-independent).
  • The existing PG/MySQL backend tests in TestPostgresSanitize / TestMysqlSanitize continue to pass (bare NaN/Infinity are still quoted correctly).

Closes #71921

@ColtenOuO ColtenOuO left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

follow the same suggestion in #71880

Also, the issue author mentioned they were already planning to submit a PR for this. It might not be ideal to open a PR over them, so you might want to keep that in mind when picking up issues in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Spam area:db-migrations PRs with DB migration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migration 0049 NaN sanitizer corrupts XCom values whose JSON is nested inside a JSON string, aborting the bytea to JSONB cast

3 participants