Skip to content

fix(postgres): preserve quoted "NULL" elements when decoding text arrays - #4488

Open
MinJaeRun wants to merge 3 commits into
transact-rs:mainfrom
MinJaeRun:fix/postgres-text-array-quoted-null
Open

MinJaeRun wants to merge 3 commits into
transact-rs:mainfrom
MinJaeRun:fix/postgres-text-array-quoted-null

Conversation

@MinJaeRun

Copy link
Copy Markdown

Does your PR solve an issue?

closes #4452

Is this a breaking change?

No. This is a bug fix for silent data loss when decoding PostgreSQL text arrays containing the literal string "NULL".

Description

In PostgreSQL's text array serialization format, an unquoted NULL element represents SQL NULL, whereas a quoted "NULL" element represents the literal string "NULL":

SELECT ARRAY['NULL', NULL, 'null']::text[]; -- serializes as: {"NULL",NULL,null}

Previously, sqlx-postgres/src/types/array.rs unquoted the element text before checking if value == "NULL", erroneously decoding the string "NULL" into None (SQL NULL) instead of Some("NULL").

This patch tracks whether the parsed element was enclosed in quotes (was_quoted) so that only unquoted NULL tokens are mapped to None.

Includes unit tests verifying both Vec<Option<String>> and Vec<String> in sqlx-postgres/src/types/array.rs and integration coverage in tests/postgres/types.rs.

In PostgreSQL's text array serialization format, an unquoted `NULL` element represents SQL NULL, whereas a quoted `"NULL"` element represents the literal four-character string `"NULL"`.

Previously, the text array decoder unquoted the element text before checking `if value == "NULL"`, erroneously decoding string `"NULL"` into `None` (SQL NULL) and causing silent data loss.

This patch tracks whether the parsed element was surrounded by quotes (`was_quoted`) so that only unquoted `NULL` tokens are treated as SQL NULL.

Closes transact-rs#4452

@abonander abonander left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

cargo fmt needs to be run, otherwise LGTM.

@MinJaeRun

Copy link
Copy Markdown
Author

Ran cargo fmt across the modified files as requested! Ready for re-review. Thanks @abonander!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Postgres text arrays: a quoted "NULL" element is decoded as SQL NULL

2 participants