From 8fe7182867f833af9bdb2704a01ec358633fb5fd Mon Sep 17 00:00:00 2001 From: jian he Date: Wed, 31 Dec 2025 15:24:54 +0800 Subject: [PATCH v10 2/2] Add argument names to the substring functions This change allows substring function to be called using named-argument notation, which can be helpful for readability, particularly for the ones with many arguments. No changes to the citext extension are required, since the citext data type does not define a specialized substring function. commitfest: https://commitfest.postgresql.org/patch/5524 Discussion: https://postgr.es/m/CACJufxHTBkymh06D4mGKNe1YfRNFN+gFBybmygWk=PtMqu00LQ@mail.gmail.com --- doc/src/sgml/func/func-binarystring.sgml | 29 ++++++++-- doc/src/sgml/func/func-bitstring.sgml | 27 +++++++++- doc/src/sgml/func/func-matching.sgml | 28 ++++++---- doc/src/sgml/func/func-string.sgml | 69 +++++++++++++++++++++--- src/backend/catalog/system_functions.sql | 2 +- src/include/catalog/pg_proc.dat | 8 +++ 6 files changed, 140 insertions(+), 23 deletions(-) diff --git a/doc/src/sgml/func/func-binarystring.sgml b/doc/src/sgml/func/func-binarystring.sgml index 58051595126..6f3b6c40a62 100644 --- a/doc/src/sgml/func/func-binarystring.sgml +++ b/doc/src/sgml/func/func-binarystring.sgml @@ -200,11 +200,11 @@ substring - substring ( bytes bytea FROM start integer FOR count integer ) + substring ( source bytea FROM start integer FOR count integer ) bytea - Extracts the substring of bytes starting at + Extracts the substring of source starting at the start'th byte if that is specified, and stopping after count bytes if that is specified. Provide at least one of start @@ -216,6 +216,29 @@ + + + + substring + + substring ( + source bytea, + start integer + , count integer ) + bytea + + + Extracts the substring of source starting at + the start'th byte, + and stopping after count bytes if that is + specified. + + + substring(source=>'\x1234567890'::bytea, start=>3, count=>2) + \x5678 + + + @@ -579,7 +602,7 @@ the start'th byte, and extending for count bytes if that is specified. (Same - as substring(bytes + as substring(source from start for count).) diff --git a/doc/src/sgml/func/func-bitstring.sgml b/doc/src/sgml/func/func-bitstring.sgml index 3f59de464a4..2c85989228c 100644 --- a/doc/src/sgml/func/func-bitstring.sgml +++ b/doc/src/sgml/func/func-bitstring.sgml @@ -279,11 +279,11 @@ substring - substring ( bits bit FROM start integer FOR count integer ) + substring ( source bit FROM start integer FOR count integer ) bit - Extracts the substring of bits starting at + Extracts the substring of source starting at the start'th bit if that is specified, and stopping after count bits if that is specified. Provide at least one of start @@ -295,6 +295,29 @@ + + + + substring + + substring ( + source bit, + start integer + , count integer ) + bit + + + Extracts the substring of source starting at + the start'th bit, + and stopping after count bits if that is + specified. + + + substring(source=>B'110010111111', start=>3, count=>2) + 00 + + + diff --git a/doc/src/sgml/func/func-matching.sgml b/doc/src/sgml/func/func-matching.sgml index f466860ddb0..fb574bb83cb 100644 --- a/doc/src/sgml/func/func-matching.sgml +++ b/doc/src/sgml/func/func-matching.sgml @@ -234,13 +234,13 @@ -string SIMILAR TO pattern ESCAPE escape-character -string NOT SIMILAR TO pattern ESCAPE escape-character +source SIMILAR TO pattern ESCAPE escape-character +source NOT SIMILAR TO pattern ESCAPE escape-character The SIMILAR TO operator returns true or - false depending on whether its pattern matches the given string. + false depending on whether its pattern matches the given string (the source). It is similar to LIKE, except that it interprets the pattern using the SQL standard's definition of a regular expression. SQL regular expressions are a curious cross @@ -369,15 +369,15 @@ regular expression pattern. The function can be written according to standard SQL syntax: -substring(string SIMILAR pattern ESCAPE escape-character) +substring(source SIMILAR pattern ESCAPE escape) or using the now obsolete SQL:1999 syntax: -substring(string FROM pattern FOR escape-character) +substring(source FROM pattern FOR escape) or as a plain three-argument function: -substring(string, pattern, escape-character) +substring(source, pattern, escape) As with SIMILAR TO, the specified pattern must match the entire data string, or else the @@ -581,11 +581,17 @@ substring('foobar' SIMILAR '#"o_b#"%' ESCAPE '#') NULL - The substring function with two parameters, - substring(string from - pattern), provides extraction of a - substring - that matches a POSIX regular expression pattern. It returns null if + The substring function with two parameters provides extraction of a + substring that matches a POSIX regular expression pattern. + The function can be written according to standard SQL syntax: + +substring(source FROM pattern) + + It can also written as a plain two-argument function: + +substring(source, pattern) + + It returns null if there is no match, otherwise the first portion of the text that matched the pattern. But if the pattern contains any parentheses, the portion of the text that matched the first parenthesized subexpression (the diff --git a/doc/src/sgml/func/func-string.sgml b/doc/src/sgml/func/func-string.sgml index 3325ade065a..41035c1e8af 100644 --- a/doc/src/sgml/func/func-string.sgml +++ b/doc/src/sgml/func/func-string.sgml @@ -400,11 +400,11 @@ substring - substring ( string text FROM start integer FOR count integer ) + substring ( source text FROM start integer FOR count integer ) text - Extracts the substring of string starting at + Extracts the substring of source starting at the start'th character if that is specified, and stopping after count characters if that is specified. Provide at least one of start @@ -426,7 +426,7 @@ - substring ( string text FROM pattern text ) + substring ( source text FROM pattern text ) text @@ -441,11 +441,11 @@ - substring ( string text SIMILAR pattern text ESCAPE escape text ) + substring ( source text SIMILAR pattern text ESCAPE escape text ) text - substring ( string text FROM pattern text FOR escape text ) + substring ( source text FROM pattern text FOR escape text ) text @@ -1398,7 +1398,7 @@ the start'th character, and extending for count characters if that is specified. (Same - as substring(string + as substring(source from start for count).) @@ -1412,6 +1412,63 @@ + + + + substring + + substring ( + source text, + start integer + , count integer ) + text + + + Extracts the substring of source starting at the + start'th character, and stopping after + count characters if that is specified. + + + substring(source=>'Thomas', start=>2, count=>3) + hom + + + + + + substring ( + source text, + pattern text, + escape text) + text + + + Extracts the first substring matching SQL regular expression; + see . + + + substring('Thomas', '%#"o_a#"_', '#') + oma + + + + + + substring ( + source text, + pattern text ) + text + + + Extracts the first substring matching POSIX regular expression; + see . + + + substring('Thomas', '...$') + mas + + + diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql index 2d946d6d9e9..f0850f9ef64 100644 --- a/src/backend/catalog/system_functions.sql +++ b/src/backend/catalog/system_functions.sql @@ -42,7 +42,7 @@ CREATE OR REPLACE FUNCTION rpad(text, integer) IMMUTABLE PARALLEL SAFE STRICT COST 1 RETURN rpad($1, $2, ' '); -CREATE OR REPLACE FUNCTION "substring"(text, text, text) +CREATE OR REPLACE FUNCTION "substring"(source text, pattern text, escape text) RETURNS text LANGUAGE sql IMMUTABLE PARALLEL SAFE STRICT COST 1 diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 6c6f31f7043..aed03195012 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -3748,9 +3748,11 @@ { oid => '936', descr => 'extract portion of string', proname => 'substring', prorettype => 'text', proargtypes => 'text int4 int4', + proargnames => '{source, start, count}', prosrc => 'text_substr' }, { oid => '937', descr => 'extract portion of string', proname => 'substring', prorettype => 'text', proargtypes => 'text int4', + proargnames => '{source, start}', prosrc => 'text_substr_no_len' }, { oid => '2087', descr => 'replace all occurrences in string of old_substr with new_substr', @@ -4168,6 +4170,7 @@ prosrc => 'bitcat' }, { oid => '1680', descr => 'extract portion of bitstring', proname => 'substring', prorettype => 'bit', proargtypes => 'bit int4 int4', + proargnames => '{source, start, count}', prosrc => 'bitsubstr' }, { oid => '1681', descr => 'bitstring length', proname => 'length', prorettype => 'int4', proargtypes => 'bit', @@ -4197,6 +4200,7 @@ prosrc => 'bitposition' }, { oid => '1699', descr => 'extract portion of bitstring', proname => 'substring', prorettype => 'bit', proargtypes => 'bit int4', + proargnames => '{source, start}', prosrc => 'bitsubstr_no_len' }, { oid => '3030', descr => 'substitute portion of bitstring', @@ -6302,9 +6306,11 @@ prosrc => 'byteacat' }, { oid => '2012', descr => 'extract portion of string', proname => 'substring', prorettype => 'bytea', + proargnames => '{source, start, count}', proargtypes => 'bytea int4 int4', prosrc => 'bytea_substr' }, { oid => '2013', descr => 'extract portion of string', proname => 'substring', prorettype => 'bytea', proargtypes => 'bytea int4', + proargnames => '{source, start}', prosrc => 'bytea_substr_no_len' }, { oid => '2085', descr => 'extract portion of string', proname => 'substr', prorettype => 'bytea', proargtypes => 'bytea int4 int4', @@ -6504,9 +6510,11 @@ { oid => '2073', descr => 'extract text matching regular expression', proname => 'substring', prorettype => 'text', proargtypes => 'text text', + proargnames => '{source, pattern}', prosrc => 'textregexsubstr' }, { oid => '2074', descr => 'extract text matching SQL regular expression', proname => 'substring', prolang => 'sql', prorettype => 'text', + proargnames => '{source, pattern, escape}', proargtypes => 'text text text', prosrc => 'see system_functions.sql' }, { oid => '2075', descr => 'convert int8 to bitstring', -- 2.34.1