1
0
mirror of https://github.com/postgres/postgres.git synced 2026-01-27 21:43:08 +03:00

Support negative indexes in split_part().

This provides a handy way to get, say, the last field of the string.
Use of a negative index in this way has precedent in the nearby
left() and right() functions.

The implementation scans the string twice when N < -1, but it seems
likely that N = -1 will be the huge majority of actual use cases,
so I'm not really excited about adding complexity to avoid that.

Nikhil Benesch, reviewed by Jacob Champion; cosmetic tweakage by me

Discussion: https://postgr.es/m/cbb7f861-6162-3a51-9823-97bc3aa0b638@gmail.com
This commit is contained in:
Tom Lane
2020-11-13 13:49:48 -05:00
parent 3bf44303b9
commit ec0294fb2c
4 changed files with 172 additions and 17 deletions

View File

@@ -533,8 +533,24 @@ SELECT replace('yabadoo', 'bad', '') AS "yaoo";
--
-- test split_part
--
select split_part('','@',1) AS "empty string";
select split_part('','@',-1) AS "empty string";
select split_part('joeuser@mydatabase','',1) AS "joeuser@mydatabase";
select split_part('joeuser@mydatabase','',2) AS "empty string";
select split_part('joeuser@mydatabase','',-1) AS "joeuser@mydatabase";
select split_part('joeuser@mydatabase','',-2) AS "empty string";
select split_part('joeuser@mydatabase','@',0) AS "an error";
select split_part('joeuser@mydatabase','@@',1) AS "joeuser@mydatabase";
select split_part('joeuser@mydatabase','@@',2) AS "empty string";
select split_part('joeuser@mydatabase','@',1) AS "joeuser";
select split_part('joeuser@mydatabase','@',2) AS "mydatabase";
@@ -543,6 +559,14 @@ select split_part('joeuser@mydatabase','@',3) AS "empty string";
select split_part('@joeuser@mydatabase@','@',2) AS "joeuser";
select split_part('joeuser@mydatabase','@',-1) AS "mydatabase";
select split_part('joeuser@mydatabase','@',-2) AS "joeuser";
select split_part('joeuser@mydatabase','@',-3) AS "empty string";
select split_part('@joeuser@mydatabase@','@',-2) AS "mydatabase";
--
-- test to_hex
--