mirror of
https://github.com/postgres/postgres.git
synced 2025-08-28 18:48:04 +03:00
Allow casting between bytea and integer types.
This allows smallint, integer, and bigint values to be cast to and from bytea. The bytea value is the two's complement representation of the integer, with the most significant byte first. For example: 1234::bytea -> \x000004d2 (-1234)::bytea -> \xfffffb2e Author: Aleksander Alekseev <aleksander@timescale.com> Reviewed-by: Joel Jacobson <joel@compiler.org> Reviewed-by: Yugo Nagata <nagata@sraoss.co.jp> Reviewed-by: Peter Eisentraut <peter@eisentraut.org> Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com> Discussion: https://postgr.es/m/CAJ7c6TPtOp6%2BkFX5QX3fH1SVr7v65uHr-7yEJ%3DGMGQi5uhGtcA%40mail.gmail.com
This commit is contained in:
@@ -5035,6 +5035,23 @@ SELECT format('Testing %3$s, %2$s, %s', 'one', 'two', 'three');
|
||||
</variablelist>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
In addition, it is possible to cast integral values to and from type
|
||||
<type>bytea</type>. Casting an integer to <type>bytea</type> produces
|
||||
2, 4, or 8 bytes, depending on the width of the integer type. The result
|
||||
is the two's complement representation of the integer, with the most
|
||||
significant byte first. Some examples:
|
||||
<programlisting>
|
||||
1234::smallint::bytea <lineannotation>\x04d2</lineannotation>
|
||||
cast(1234 as bytea) <lineannotation>\x000004d2</lineannotation>
|
||||
cast(-1234 as bytea) <lineannotation>\xfffffb2e</lineannotation>
|
||||
'\x8000'::bytea::smallint <lineannotation>-32768</lineannotation>
|
||||
'\x8000'::bytea::integer <lineannotation>32768</lineannotation>
|
||||
</programlisting>
|
||||
Casting a <type>bytea</type> to an integer will raise an error if the
|
||||
length of the <type>bytea</type> exceeds the width of the integer type.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
See also the aggregate function <function>string_agg</function> in
|
||||
<xref linkend="functions-aggregate"/> and the large object functions
|
||||
|
Reference in New Issue
Block a user