Add reverse(bytea).

This commit introduces a function for reversing the order of the
bytes in binary strings.

Bumps catversion.

Author: Aleksander Alekseev <aleksander@timescale.com>
Discussion: https://postgr.es/m/CAJ7c6TMe0QVRuNssUArbMi0bJJK32%2BzNA3at5m3osrBQ25MHuw%40mail.gmail.com
This commit is contained in:
Nathan Bossart 2025-03-13 11:20:53 -05:00
parent bb25276205
commit 0697b23906
6 changed files with 64 additions and 1 deletions

View File

@ -4660,6 +4660,23 @@ SELECT format('Testing %3$s, %2$s, %s', 'one', 'two', 'three');
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>reverse</primary>
</indexterm>
<function>reverse</function> ( <type>bytea</type> )
<returnvalue>bytea</returnvalue>
</para>
<para>
Reverses the order of the bytes in the binary string.
</para>
<para>
<literal>reverse('\xabcd'::bytea)</literal>
<returnvalue>\xcdab</returnvalue>
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>

View File

@ -3398,6 +3398,27 @@ byteaSetBit(PG_FUNCTION_ARGS)
PG_RETURN_BYTEA_P(res);
}
/*
* Return reversed bytea
*/
Datum
bytea_reverse(PG_FUNCTION_ARGS)
{
bytea *v = PG_GETARG_BYTEA_PP(0);
const char *p = VARDATA_ANY(v);
int len = VARSIZE_ANY_EXHDR(v);
const char *endp = p + len;
bytea *result = palloc(len + VARHDRSZ);
char *dst = (char *) VARDATA(result) + len;
SET_VARSIZE(result, len + VARHDRSZ);
while (p < endp)
*(--dst) = *p++;
PG_RETURN_BYTEA_P(result);
}
/* text_name()
* Converts a text type to a Name type.

View File

@ -57,6 +57,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 202503111
#define CATALOG_VERSION_NO 202503131
#endif

View File

@ -1518,6 +1518,9 @@
{ oid => '6163', descr => 'number of set bits',
proname => 'bit_count', prorettype => 'int8', proargtypes => 'bytea',
prosrc => 'bytea_bit_count' },
{ oid => '8694', descr => 'reverse bytea',
proname => 'reverse', prorettype => 'bytea', proargtypes => 'bytea',
prosrc => 'bytea_reverse' },
{ oid => '725',
proname => 'dist_pl', prorettype => 'float8', proargtypes => 'point line',

View File

@ -236,6 +236,24 @@ SELECT E'De\\678dBeEf'::bytea;
ERROR: invalid input syntax for type bytea
LINE 1: SELECT E'De\\678dBeEf'::bytea;
^
SELECT reverse(''::bytea);
reverse
---------
\x
(1 row)
SELECT reverse('\xaa'::bytea);
reverse
---------
\xaa
(1 row)
SELECT reverse('\xabcd'::bytea);
reverse
---------
\xcdab
(1 row)
SET bytea_output TO escape;
SELECT E'\\xDeAdBeEf'::bytea;
bytea

View File

@ -77,6 +77,10 @@ SELECT E'De\123dBeEf'::bytea;
SELECT E'De\\123dBeEf'::bytea;
SELECT E'De\\678dBeEf'::bytea;
SELECT reverse(''::bytea);
SELECT reverse('\xaa'::bytea);
SELECT reverse('\xabcd'::bytea);
SET bytea_output TO escape;
SELECT E'\\xDeAdBeEf'::bytea;
SELECT E'\\x De Ad Be Ef '::bytea;