Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id F08C1B5DBE0 for ; Thu, 26 May 2011 20:47:23 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 92664-10 for ; Thu, 26 May 2011 23:47:16 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from www.postnewspapers.com.au (www.postnewspapers.com.au [173.230.139.50]) by mail.postgresql.org (Postfix) with ESMTP id 9C6A2B5D832 for ; Thu, 26 May 2011 20:47:16 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by www.postnewspapers.com.au (Postfix) with ESMTP id 93C0E882; Thu, 26 May 2011 19:47:16 -0400 (EDT) X-Virus-Scanned: amavisd-new at postnewspapers.com.au.com Received: from www.postnewspapers.com.au ([127.0.0.1]) by localhost (www.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id iohbHeNZV1JV; Thu, 26 May 2011 19:47:16 -0400 (EDT) Received: from mail.postnewspapers.com.au (ppp171-128.static.internode.on.net [150.101.171.128]) by www.postnewspapers.com.au (Postfix) with ESMTP id 6A3BA2D; Thu, 26 May 2011 19:47:15 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 36133AE67F; Fri, 27 May 2011 07:47:14 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (mail.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id liDbXAtjsETK; Fri, 27 May 2011 07:47:09 +0800 (WST) Received: from [10.1.1.5] (203-206-49-57.dyn.iinet.net.au [203.206.49.57]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id 4ABF2AE2A5; Fri, 27 May 2011 07:47:09 +0800 (WST) Message-ID: <4DDEE67C.1080100@postnewspapers.com.au> Date: Fri, 27 May 2011 07:47:08 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110428 Fedora/3.1.10-1.fc14 Thunderbird/3.1.10 MIME-Version: 1.0 To: Marco Spiga , pgsql-bugs@postgresql.org Subject: Re: BUG #6015: to_hex and negative integer References: <201105081807.p48I7fgA087261@wwwmaster.postgresql.org> <4DC78CB6.9080101@postnewspapers.com.au> <20110526152103.GA2677@alice.it> In-Reply-To: <20110526152103.GA2677@alice.it> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201105/194 X-Sequence-Number: 30333 On 05/26/2011 11:21 PM, Marco Spiga wrote: > Thanks Craig for your reply. > > I have read wikipedia link and what you have tell me is TRUE. > > But if I give these select: > megaradius=# SELECT to_hex(-42); > to_hex > ---------- > ffffffd6 > (1 riga) > > megaradius=# SELECT to_hex(4294967254); > to_hex > ---------- > ffffffd6 > (1 riga) Ha! Great example. What's happening is that 4294967254 cannot be represented as a 32-bit signed integer, so it's being extended to a 64-bit integer. As a result, you're comparing a signed int32 to a signed int64 using a bitwise comparison without extending the int32 to int64, which is incorrect. select pg_typeof(-42); pg_typeof ----------- integer (1 row) select pg_typeof(4294967254); pg_typeof ----------- bigint (1 row) When converting 4294967254 to a hex representation, to_hex omits leading zeroes; it's really 0x00000000ffffffd6 . This is fine so long as you retain information about the format the original number was in - in particular whether it was a 32-bit or 64-bit value. It's also fine if you're only ever dealing with unsigned or positive values. Alas, to_hex _does_ deal with negative values, and by discarding leading zeroes it discards information about the format of the original number. In this particular case, 4294967254 doesn't fit into a signed 32 bit int, but it DOES fit into an unsigned 32-bit int, so it's only eight hex digits. The result is *really* (int64)0x00000000ffffffd6, but the omission of leading zeroes is creating confusion. Personally, I think it's incorrect for to_hex to drop leading zeroes if it also supports negative numbers. It should do one or the other, or it should extend all values up to 64-bits before conversion so there's no confusion with negative numbers. Unfortunately I doubt any of those options can be implemented for backward compat reasons, but adding to_hex_32 and to_hex_64 that take only one width would help without breaking BC. You can work around this yourself by ensuring that you NEVER call to_hex with a 32-bit integer. Always extend to a 64-bit value to eliminate confusion. SELECT to_hex('4294967254'::bigint); to_hex ---------- ffffffd6 (1 row) SELECT to_hex('-42'::bigint); to_hex ------------------ ffffffffffffffd6 You can create a simple wrapper to do this: CREATE OR REPLACE FUNCTION to_hex_64 (smallint) RETURNS text AS $$ SELECT to_hex($1::bigint); $$ LANGUAGE 'sql'; CREATE OR REPLACE FUNCTION to_hex_64 (integer) RETURNS text AS $$ SELECT to_hex($1::bigint); $$ LANGUAGE 'sql'; CREATE OR REPLACE FUNCTION to_hex_64 (bigint) RETURNS text AS $$ SELECT to_hex($1::bigint); $$ LANGUAGE 'sql'; Alternately, you can implement a to_hex variant that replaces the dropped leading zeroes by using pg_typeof(...) to decide whether the argument is a 32-bit or 64-bit integer and padding the result of to_hex appropriately. That way there's no confusion. > SELECT 'true' AS test WHERE (SELECT to_hex(-42)) = (SELECT to_hex(4294967254)); > test SELECT (SELECT to_hex(-42)) = (SELECT to_hex(4294967254)); ?column? ---------- t SELECT (SELECT to_hex('-42'::bigint)) = (SELECT to_hex('4294967254'::bigint)); ?column? -------- f -- Craig Ringer