Re: PATCH: jsonpath string methods: lower, upper, initcap, l/r/btrim, replace, split_part
David E. Wheeler <david@justatheory.com>
From: "David E. Wheeler" <david@justatheory.com>
To: jian he <jian.universality@gmail.com>
Cc: Chao Li <li.evan.chao@gmail.com>, Álvaro Herrera <alvherre@kurilemu.de>, Florents Tselai <florents.tselai@gmail.com>, Tom Lane <tgl@sss.pgh.pa.us>, Peter Eisentraut <peter@eisentraut.org>, Robert Haas <robertmhaas@gmail.com>, Alexander Korotkov <aekorotkov@gmail.com>, pgsql-hackers <pgsql-hackers@lists.postgresql.org>, Andrew Dunstan <andrew@dunslane.net>
Date: 2026-01-04T20:51:41Z
Lists: pgsql-hackers
Attachments
- v16-0001-Rename-jsonpath-method-arg-tokens.patch (application/octet-stream) patch v16-0001
- v16-0002-Add-additional-jsonpath-string-methods.patch (application/octet-stream) patch v16-0002
On Dec 3, 2025, at 22:56, jian he <jian.universality@gmail.com> wrote:
> seems no deparse regress tests, like:
> create view vj as select jsonb_path_query('" hello "', '$.ltrim(" ")') as a;
> \sv vj
>
> that mean the changes in printJsonPathItem are not tested?
I’m afraid I don’t understand. All of the new tests in src/test/regress/sql/jsonpath.sql exercise the printJsonPathItem changes.
> + /* Create the appropriate jb value to return */
> + switch (jsp->type)
> + {
> + /* Cases for functions that return text */
> + case jpiStrReplace:
> comments indentation should align with the word "case"?
Make sense to me; I thought this was indented by pgindent, but I just re-ran it and it didn’t complain.
> pnstrdup, CStringGetTextDatum copied twice for the same contend?
> I think you can just
> ``
> text *tmp = cstring_to_text_with_len(jb->val.string.val, jb->val.string.len);
> Datum str = PointerGetDatum(tmp)
> ``
> In the first main switch block, there's no need to call
> ``CStringGetTextDatum(tmp)``
> because str is already a Datum. We can simply use str directly.
Very close reading, appreciated. I removed tmp altogether, creating this line:
```c
str = PointerGetDatum(cstring_to_text_with_len(jb->val.string.val, jb->val.string.len));
```
And replacing the use of `CStringGetTextDatum(tmp)` in both the `jpiStrReplace` and the `jpiStrSplitPart` cases.
> I noticed that almost all of them use DEFAULT_COLLATION_OID,
> but jpiStrSplitPart uses C_COLLATION_OID.
Right, fixed.
On Dec 3, 2025, at 23:22, jian he <jian.universality@gmail.com> wrote:
> seems no tests for the changes in jspIsMutableWalker too.
> we can make some simple dummy tests like:
>
> create table tjs(a jsonb);
> create index on tjs(jsonb_path_match(a, '$.ltrim(" ")'));
Added to the existing `create index` tests to exercise the new functions.
On Dec 7, 2025, at 06:21, jian he <jian.universality@gmail.com> wrote:
> the return value should be
> "def"
Don’t know how that slipped by us; thank you! Fixed by changing the index number to 3.
> The actual signature:
> <replaceable>characters</replaceable> part is optional, but we didn't
> use square brackets
> to indicate it's optional.
Right, fixed.
> similarly:
> string . rtrim([ characters ]) → string
> change to
> ```
> string . rtrim() → string
> string . rtrim(characters ) → string
> ```
>
> string . btrim([ characters ]) → string
> change to
> ```
> string . btrim() → string
> string . btrim([ characters ]) → string
> ``
> would improve the readability, I think.
I can see that, but the syntax here was borrowed from the existing trim functions, which use the [brackets] for the optional args[1]:
<function>rtrim</function> ( <parameter>string</parameter> <type>text</type>
<optional>, <parameter>characters</parameter> <type>text</type> </optional> )
<returnvalue>text</returnvalue>
Updated and rebased patch attached.
Best,
David
[1]: https://github.com/postgres/postgres/blob/ac94ce8/doc/src/sgml/func/func-string.sgml#L383
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Add additional jsonpath string methods
- bd4f879a9cdd 19 (unreleased) landed
-
Rename jsonpath method arg tokens
- a35c9d524ed0 19 (unreleased) landed
-
Fix transient memory leakage in jsonpath evaluation.
- 5a2043bf7131 19 (unreleased) cited
-
Make jsonpath .string() be immutable for datetimes.
- cb599b9ddfcc 18.0 cited