Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qXkJY-0071aX-G2 for pgsql-hackers@arkaria.postgresql.org; Sun, 20 Aug 2023 15:26:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qXkJW-005tv0-UX for pgsql-hackers@arkaria.postgresql.org; Sun, 20 Aug 2023 15:26:03 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qXkJW-005tuG-GL for pgsql-hackers@lists.postgresql.org; Sun, 20 Aug 2023 15:26:02 +0000 Received: from mail-qk1-x72f.google.com ([2607:f8b0:4864:20::72f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qXkJO-001AzC-QL for pgsql-hackers@postgresql.org; Sun, 20 Aug 2023 15:26:01 +0000 Received: by mail-qk1-x72f.google.com with SMTP id af79cd13be357-76d84238f01so179018385a.2 for ; Sun, 20 Aug 2023 08:25:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692545154; x=1693149954; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=t0+Ll5G6QxYOOuHzwXAOxH00jeg6bNBvbRU29K7Hlpk=; b=WA9xE7YMT+4hVkhBJyYUZy4UjWIL24/mDrNEeW6LbrPylRqtby1BxYMjStMvOR1rUU BTUae5eQmZFy0PSZtmOJGogaSRyhLCG1ILDqfM9GDRyfmWWx9uwgc3PHeDKGw6QqrSG1 XhRKd1UdnRWL2WciuxNGWnGjLNQOcOr+1IPVnULWhfLOTsYMB5pMUKjOEWUJXsEnhABc vYYHO97mJG1OT5nEM0UVfeFmGNFDm2pXLwHNEHC7iTjcazYvrZ/YSoLYh17UtWHXsghs ddrhEnNRbvqwpf1ywSMeXyoucpP1KP7Rhy63VVDCl7kkafYe4x5g1mXF7ydfc8IVDwfd xXhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692545154; x=1693149954; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=t0+Ll5G6QxYOOuHzwXAOxH00jeg6bNBvbRU29K7Hlpk=; b=Mxoms+lRWPa4spP6w//bpHm6861A22OxMqP6IbDiPEyIjzwnh2JkBTw1piUXSTpxiV muxNyoYje9ca54rH28fuhr5uVJToinatSs5dtJz+iEuf7duhtvt+P1DRGO34jVdidVgC 4Q/3QmfE75PeXeh6NEGd1h+QEtgT/TAouVFbTR1yV0shQaMloFFWzqQEmPe0kr084x6V 6VpkIh6gIObW9h7LYPtCo8CKBUeAUqQ7RUqeDgzhx+NYhtMqjLFx/Q9sCRFwHSV9B3uu 87QB3Ax0ZExy+r6MUO8ptzw8memRsRKU2yOFGzazzRjNteWTGuZpm3Kgj1y3xktF/Xa6 FaWA== X-Gm-Message-State: AOJu0YxNSfCcyG6PPgdYGiC6aR8FgplwqINX+P20wM6RENSj3xc1IPuU 8dstZGRT3W2VWO55IW7AiDI= X-Google-Smtp-Source: AGHT+IGjOeMrzFinzYSlzSGV0va0us0Q/jOTL+OfWrW9JABkr58WDmYDQUKoLxUfe7JN0zMSh5lRKg== X-Received: by 2002:a05:620a:a52:b0:768:4333:c584 with SMTP id j18-20020a05620a0a5200b007684333c584mr5038621qka.77.1692545153912; Sun, 20 Aug 2023 08:25:53 -0700 (PDT) Received: from nathanxps13 (rrcs-70-62-147-148.central.biz.rr.com. [70.62.147.148]) by smtp.gmail.com with ESMTPSA id a5-20020a05620a124500b0076d9e298928sm873077qkl.66.2023.08.20.08.25.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Aug 2023 08:25:53 -0700 (PDT) Date: Sun, 20 Aug 2023 08:25:51 -0700 From: Nathan Bossart To: Dean Rasheed Cc: John Naylor , Kirk Wolak , Peter Eisentraut , Eric Radman , pgsql-hackers@postgresql.org Subject: Re: [PATCH] Add function to_oct Message-ID: <20230820152551.GB290672@nathanxps13> References: <20230726001656.GC2992317@nathanxps13> <20230726032917.GA3151386@nathanxps13> <20230815041129.GB2294364@nathanxps13> <20230815171715.GA2312399@nathanxps13> <20230816142410.GA2651383@nathanxps13> <20230817152628.GA3148583@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="ibTvN161/egqYuK8" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --ibTvN161/egqYuK8 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, Aug 19, 2023 at 08:35:46AM +0100, Dean Rasheed wrote: > I note that there are no tests for negative inputs. I added some in v8. > Doing a quick test, shows that this changes the current behaviour, > because all inputs are now treated as 64-bit: > > HEAD: > > select to_hex((-1234)::int); > to_hex > ---------- > fffffb2e > > With patch: > > select to_hex((-1234)::int); > to_hex > ------------------ > fffffffffffffb2e Good catch. In v8, I fixed this by first casting the input to uint32 for the 32-bit versions of the functions. This prevents the conversion to uint64 from setting the rest of the bits. AFAICT this behavior is pretty well defined in the standard. > The way that negative inputs are handled really should be documented, > or at least it should include a couple of examples. I used your suggestion and noted that the output is the two's complement representation [0]. [0] https://postgr.es/m/CAEZATCVbkL1ynqpsKiTDpch34%3DSCr5nnau%3DnfNmiy2nM3SJHtw%40mail.gmail.com -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --ibTvN161/egqYuK8 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v8-0001-add-to_binary-and-to_oct.patch" From a03c06d0b26ccbd49bda55c2efab565ab9fa90db Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 25 Jul 2023 16:09:01 -0700 Subject: [PATCH v8 1/1] add to_binary() and to_oct() --- doc/src/sgml/func.sgml | 47 ++++++++++++++- src/backend/utils/adt/varlena.c | 86 +++++++++++++++++++-------- src/include/catalog/pg_proc.dat | 12 ++++ src/test/regress/expected/strings.out | 62 ++++++++++++++++++- src/test/regress/sql/strings.sql | 15 ++++- 5 files changed, 192 insertions(+), 30 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index be2f54c914..835c08905e 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -3737,6 +3737,50 @@ repeat('Pg', 4) PgPgPgPg + + + + to_binary + + to_binary ( integer ) + text + + + to_binary ( bigint ) + text + + + Converts the number to its equivalent two's complement binary + representation. + + + to_binary(2147483647) + 1111111111111111111111111111111 + + + + + + + to_oct + + to_oct ( integer ) + text + + + to_oct ( bigint ) + text + + + Converts the number to its equivalent two's complement octal + representation. + + + to_oct(2147483647) + 17777777777 + + + @@ -3750,7 +3794,8 @@ repeat('Pg', 4) PgPgPgPg text - Converts the number to its equivalent hexadecimal representation. + Converts the number to its equivalent two's complement hexadecimal + representation. to_hex(2147483647) diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index b1ec5c32ce..cac3577480 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -4919,53 +4919,87 @@ array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, return result; } -#define HEXBASE 16 /* - * Convert an int32 to a string containing a base 16 (hex) representation of - * the number. + * Workhorse for to_binary, to_oct, and to_hex. Note that base must be > 1 and + * <= 16. */ -Datum -to_hex32(PG_FUNCTION_ARGS) +static inline text * +convert_to_base(uint64 value, int base) { - uint32 value = (uint32) PG_GETARG_INT32(0); - char *ptr; const char *digits = "0123456789abcdef"; - char buf[32]; /* bigger than needed, but reasonable */ - ptr = buf + sizeof(buf) - 1; - *ptr = '\0'; + /* We size the buffer for to_binary's longest possible return value. */ + char buf[sizeof(uint64) * BITS_PER_BYTE]; + char *const end = buf + sizeof(buf); + char *ptr = end; + + Assert(base > 1); + Assert(base <= 16); do { - *--ptr = digits[value % HEXBASE]; - value /= HEXBASE; + *--ptr = digits[value % base]; + value /= base; } while (ptr > buf && value); - PG_RETURN_TEXT_P(cstring_to_text(ptr)); + return cstring_to_text_with_len(ptr, end - ptr); +} + +/* + * Convert an integer to a string containing a base-2 (binary) representation + * of the number. + */ +Datum +to_binary32(PG_FUNCTION_ARGS) +{ + uint64 value = (uint32) PG_GETARG_INT32(0); + + PG_RETURN_TEXT_P(convert_to_base(value, 2)); +} +Datum +to_binary64(PG_FUNCTION_ARGS) +{ + uint64 value = (uint64) PG_GETARG_INT64(0); + + PG_RETURN_TEXT_P(convert_to_base(value, 2)); } /* - * Convert an int64 to a string containing a base 16 (hex) representation of + * Convert an integer to a string containing a base-8 (oct) representation of * the number. */ Datum -to_hex64(PG_FUNCTION_ARGS) +to_oct32(PG_FUNCTION_ARGS) +{ + uint64 value = (uint32) PG_GETARG_INT32(0); + + PG_RETURN_TEXT_P(convert_to_base(value, 8)); +} +Datum +to_oct64(PG_FUNCTION_ARGS) { uint64 value = (uint64) PG_GETARG_INT64(0); - char *ptr; - const char *digits = "0123456789abcdef"; - char buf[32]; /* bigger than needed, but reasonable */ - ptr = buf + sizeof(buf) - 1; - *ptr = '\0'; + PG_RETURN_TEXT_P(convert_to_base(value, 8)); +} - do - { - *--ptr = digits[value % HEXBASE]; - value /= HEXBASE; - } while (ptr > buf && value); +/* + * Convert an integer to a string containing a base-16 (hex) representation of + * the number. + */ +Datum +to_hex32(PG_FUNCTION_ARGS) +{ + uint64 value = (uint32) PG_GETARG_INT32(0); + + PG_RETURN_TEXT_P(convert_to_base(value, 16)); +} +Datum +to_hex64(PG_FUNCTION_ARGS) +{ + uint64 value = (uint64) PG_GETARG_INT64(0); - PG_RETURN_TEXT_P(cstring_to_text(ptr)); + PG_RETURN_TEXT_P(convert_to_base(value, 16)); } /* diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 12fac15ceb..b653351605 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -3707,6 +3707,18 @@ { oid => '2768', descr => 'split string by pattern', proname => 'regexp_split_to_array', prorettype => '_text', proargtypes => 'text text text', prosrc => 'regexp_split_to_array' }, +{ oid => '9030', descr => 'convert int4 number to binary', + proname => 'to_binary', prorettype => 'text', proargtypes => 'int4', + prosrc => 'to_binary32' }, +{ oid => '9031', descr => 'convert int8 number to binary', + proname => 'to_binary', prorettype => 'text', proargtypes => 'int8', + prosrc => 'to_binary64' }, +{ oid => '9032', descr => 'convert int4 number to oct', + proname => 'to_oct', prorettype => 'text', proargtypes => 'int4', + prosrc => 'to_oct32' }, +{ oid => '9033', descr => 'convert int8 number to oct', + proname => 'to_oct', prorettype => 'text', proargtypes => 'int8', + prosrc => 'to_oct64' }, { oid => '2089', descr => 'convert int4 number to hex', proname => 'to_hex', prorettype => 'text', proargtypes => 'int4', prosrc => 'to_hex32' }, diff --git a/src/test/regress/expected/strings.out b/src/test/regress/expected/strings.out index 62698569e1..d24e6fa0cd 100644 --- a/src/test/regress/expected/strings.out +++ b/src/test/regress/expected/strings.out @@ -2129,8 +2129,68 @@ select split_part('@joeuser@mydatabase@','@',-2) AS "mydatabase"; (1 row) -- --- test to_hex +-- test to_binary, to_oct, and to_hex -- +select to_binary(-1234) AS "11111111111111111111101100101110"; + 11111111111111111111101100101110 +---------------------------------- + 11111111111111111111101100101110 +(1 row) + +select to_binary(-1234::bigint); + to_binary +------------------------------------------------------------------ + 1111111111111111111111111111111111111111111111111111101100101110 +(1 row) + +select to_binary(256*256*256 - 1) AS "111111111111111111111111"; + 111111111111111111111111 +-------------------------- + 111111111111111111111111 +(1 row) + +select to_binary(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "11111111111111111111111111111111"; + 11111111111111111111111111111111 +---------------------------------- + 11111111111111111111111111111111 +(1 row) + +select to_oct(-1234) AS "37777775456"; + 37777775456 +------------- + 37777775456 +(1 row) + +select to_oct(-1234::bigint) AS "1777777777777777775456"; + 1777777777777777775456 +------------------------ + 1777777777777777775456 +(1 row) + +select to_oct(256*256*256 - 1) AS "77777777"; + 77777777 +---------- + 77777777 +(1 row) + +select to_oct(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "37777777777"; + 37777777777 +------------- + 37777777777 +(1 row) + +select to_hex(-1234) AS "fffffb2e"; + fffffb2e +---------- + fffffb2e +(1 row) + +select to_hex(-1234::bigint) AS "fffffffffffffb2e"; + fffffffffffffb2e +------------------ + fffffffffffffb2e +(1 row) + select to_hex(256*256*256 - 1) AS "ffffff"; ffffff -------- diff --git a/src/test/regress/sql/strings.sql b/src/test/regress/sql/strings.sql index ca32f6bba5..437713fdc6 100644 --- a/src/test/regress/sql/strings.sql +++ b/src/test/regress/sql/strings.sql @@ -685,10 +685,21 @@ select split_part('joeuser@mydatabase','@',-3) AS "empty string"; select split_part('@joeuser@mydatabase@','@',-2) AS "mydatabase"; -- --- test to_hex +-- test to_binary, to_oct, and to_hex -- -select to_hex(256*256*256 - 1) AS "ffffff"; +select to_binary(-1234) AS "11111111111111111111101100101110"; +select to_binary(-1234::bigint); +select to_binary(256*256*256 - 1) AS "111111111111111111111111"; +select to_binary(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "11111111111111111111111111111111"; + +select to_oct(-1234) AS "37777775456"; +select to_oct(-1234::bigint) AS "1777777777777777775456"; +select to_oct(256*256*256 - 1) AS "77777777"; +select to_oct(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "37777777777"; +select to_hex(-1234) AS "fffffb2e"; +select to_hex(-1234::bigint) AS "fffffffffffffb2e"; +select to_hex(256*256*256 - 1) AS "ffffff"; select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff"; -- -- 2.25.1 --ibTvN161/egqYuK8--