| From: | 민영재 <dudwo990413(at)gmail(dot)com> |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Cc: | theshallow27(at)gmail(dot)com |
| Subject: | Re: BUG #19736: `JSON_OBJECT ... ABSENT ON NULL` accepts invalid keys when the value is NULL |
| Date: | 2026-10-11 10:26:02 |
| Message-ID: | CAP=tSTAi4iECc=5Cr0zYxQU=E-zQyQF7wrVF2MGQ=R7LmiMwkw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs pgsql-hackers |
Hi,
I reproduced both examples on master at 9e7d152488f1.
In json_build_object_worker(), the ABSENT ON NULL skip happens before
NULL-key validation. Both json and jsonb paths also skip the scalar-key
checks when omitting an entry without a uniqueness check.
The attached patch moves the json NULL-key check before the skip and
validates the key type before omitting an entry in either output path.
It avoids calling output or cast functions for omitted keys and preserves
the existing uniqueness checks.
Regression tests cover NULL, array, composite, json and jsonb keys with
NULL values. The new tests fail without the fix; with the patch, make
check passes all 239 tests on macOS/arm64 with assertions enabled.
Comments are welcome.
Thanks,
Yeongjae Min
On Fri, 02 Oct 2026 22:35:57 +0000, PG Bug reporting form
<noreply(at)postgresql(dot)org> wrote:
> The following bug has been logged on the website:
>
> Bug reference: 19736
> Logged by: Shallow
> Email address: theshallow27(at)gmail(dot)com
> PostgreSQL version: 18.6
> Operating system: Linux
> Description:
>
> When `ABSENT ON NULL` removes a pair because its value is SQL NULL,
> `JSON_OBJECT` also skips validation of that pair's key. The documented
> restrictions say that a key cannot be NULL and must be a scalar expression
> without a cast to JSON. Those restrictions should still apply when the value
> pair will be omitted.
>
> **Reproduction:**
>
> ```sql
> SELECT JSON_OBJECT((NULL::text) VALUE NULL::text
> ABSENT ON NULL RETURNING json);
>
> SELECT JSON_OBJECT((ARRAY[1,2]) VALUE NULL::text
> ABSENT ON NULL RETURNING json);
> ```
>
> **Actual result:** Both statements return `{}`.
>
> **Expected result:** Both statements should reject the invalid key, as the
> key
> restrictions apply regardless of whether the value is omitted.
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Validate-JSON-object-keys-before-omitting-null-values.patch | application/octet-stream | 8.4 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | expl0it3r | 2026-10-11 16:44:05 | Static libpq and ecpglib clash with PThreads4W on Windows |
| Previous Message | Chinmay Kanchi | 2026-10-11 02:04:42 | GIN (JSONPath) checks equality only on the first node of each operand |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Richard Guo | 2026-10-11 12:41:03 | Re: Wrong results from an antijoin |
| Previous Message | Ayush Tiwari | 2026-10-11 09:54:27 | [PATCH] Clear walsender reader pointers after cleanup |