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 1qWHRe-002jdN-7I for pgsql-hackers@arkaria.postgresql.org; Wed, 16 Aug 2023 14:24:22 +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 1qWHRb-007CUH-Sn for pgsql-hackers@arkaria.postgresql.org; Wed, 16 Aug 2023 14:24:20 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qWHRb-007CU9-Hs for pgsql-hackers@lists.postgresql.org; Wed, 16 Aug 2023 14:24:19 +0000 Received: from mail-oa1-x2a.google.com ([2001:4860:4864:20::2a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qWHRX-000UC6-MF for pgsql-hackers@postgresql.org; Wed, 16 Aug 2023 14:24:18 +0000 Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-1c5451a1fbaso602424fac.1 for ; Wed, 16 Aug 2023 07:24:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692195853; x=1692800653; 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=C5IdBcxXWqpp2BpvRRDnOSNc81ohvpLOyq+lVmwFlKA=; b=i3tSVbMG7rf8bT8111q0kdAcFzEqldhyf1qw9YdZDP9SWh1bE7nEiGPyUdLJBYi4IN QhvnI8y1x4qGQIrC49Wc9wG9nUSO+9ndULGGSvrr7gUXSmlp66NrMLkEPS6MPF/6S+KD tDEwOnkBDsLY4r13ilqGvMLzoPhkRAEhn8Zh+rstNhsIwdqss0kbgnRoYWYUonueASF1 E5YmqdEV1dMBr5VmRnm63TrEkIebPg3WiQn8S6PcS/kivds4P1Y8Bp7pdw8m3qyhzbn3 Yg46FcsZ64Fc1hCD3DMPwf4hGDCO5JeyitvJF8FGUeiRfdFa9IiyEgZZ2JIF7zyHtlAN CP2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692195853; x=1692800653; 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=C5IdBcxXWqpp2BpvRRDnOSNc81ohvpLOyq+lVmwFlKA=; b=AoyeRqABz77TmmnSL5unXdbxrQ+lf1ydyIRZMC+QomLoDrntDxIrWaMc+ja475lY1s AvBILacAF8M0GYvriEWN0J2AdNRJeWefV6I5DjYawVrRIVvr3QeBLjjw55Cu44zy9UTi ovXZID59+BD5sfc7+FUFSNYHGknEdwo1YZCdpaB77/GVPRWsNGRjuZ6U2NUlP84OeEws 200ypc3nzT4YeFO2hxJjn4+e6NivhcTsjub7mFEpIICq5k9yTSsuEOUw1lHYbGprl1pb CuIKGJYBRyWR5vSYV3gUc2vAYTQZswa8YRSj6O3zEK8uAIDMkNLk7JDIe7fBtOu2ovzC Hd1w== X-Gm-Message-State: AOJu0Yzvb3C0G1rh1Uv0IlXK6U65+yv+jRMQREkF48Hk6CFJEuvNJNjB 5obTJ75i6Xki2zm2SpOF8sc= X-Google-Smtp-Source: AGHT+IGcBT1P8L45d5vXfXRlgsjWZoJsXgC7Y6+ss9+YMRe7ZRfSaIQRNwgg6jBLRqmRYftz1cWkQQ== X-Received: by 2002:a05:6870:b28a:b0:1bf:4f42:de91 with SMTP id c10-20020a056870b28a00b001bf4f42de91mr2319630oao.33.1692195853626; Wed, 16 Aug 2023 07:24:13 -0700 (PDT) Received: from nathanxps13 ([2600:1700:48f8:340f:4f4c:44bb:6856:3082]) by smtp.gmail.com with ESMTPSA id t14-20020a05683022ee00b006b881a6518esm6113206otc.10.2023.08.16.07.24.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Aug 2023 07:24:12 -0700 (PDT) Date: Wed, 16 Aug 2023 07:24:10 -0700 From: Nathan Bossart To: John Naylor Cc: Kirk Wolak , Peter Eisentraut , Eric Radman , pgsql-hackers@postgresql.org Subject: Re: [PATCH] Add function to_oct Message-ID: <20230816142410.GA2651383@nathanxps13> References: <5c260f97-d8c8-4a68-7498-022e550c6b10@enterprisedb.com> <20230725232426.GA2992317@nathanxps13> <20230726001656.GC2992317@nathanxps13> <20230726032917.GA3151386@nathanxps13> <20230815041129.GB2294364@nathanxps13> <20230815171715.GA2312399@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="7JfCtLOvnd9MIVvH" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --7JfCtLOvnd9MIVvH Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Aug 16, 2023 at 10:35:27AM +0700, John Naylor wrote: > ``` > *ptr = '\0'; > > do > ``` > > to > > ``` > *ptr = '\0'; > do > ``` Oh, I misunderstood. I thought you meant that there might be a whitespace change on that line, not the surrounding ones. This is fixed in v6. > Now I'm struggling to understand why each and every instance has its own > nominal buffer, passed down to the implementation. All we care about is the > result -- is there some reason not to confine the buffer declaration to the > general implementation? We can do that if we use a static variable, which is what I've done in v6. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --7JfCtLOvnd9MIVvH Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v6-0001-add-to_binary-and-to_oct.patch" From dce8c16d55f1fe91747be45abe155b5d7d1f4274 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 25 Jul 2023 16:09:01 -0700 Subject: [PATCH v6 1/1] add to_binary() and to_oct() --- doc/src/sgml/func.sgml | 42 +++++++++++++ src/backend/utils/adt/varlena.c | 90 ++++++++++++++++++++------- src/include/catalog/pg_proc.dat | 12 ++++ src/test/regress/expected/strings.out | 26 +++++++- src/test/regress/sql/strings.sql | 9 ++- 5 files changed, 153 insertions(+), 26 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index be2f54c914..23b5ac7457 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -3737,6 +3737,48 @@ repeat('Pg', 4) PgPgPgPg + + + + to_binary + + to_binary ( integer ) + text + + + to_binary ( bigint ) + text + + + Converts the number to its equivalent binary representation. + + + to_binary(2147483647) + 1111111111111111111111111111111 + + + + + + + to_oct + + to_oct ( integer ) + text + + + to_oct ( bigint ) + text + + + Converts the number to its equivalent octal representation. + + + to_oct(2147483647) + 17777777777 + + + diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index b1ec5c32ce..2330f86c09 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -4919,53 +4919,97 @@ 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 char * +convert_to_base(uint64 value, int base) { - uint32 value = (uint32) PG_GETARG_INT32(0); + /* + * We size the buffer for to_binary's longest possible return value, + * including the terminating '\0'. + */ + static char buf[sizeof(uint64) * BITS_PER_BYTE + 1]; char *ptr; const char *digits = "0123456789abcdef"; - char buf[32]; /* bigger than needed, but reasonable */ + + Assert(base > 1); + Assert(base <= 16); ptr = buf + sizeof(buf) - 1; *ptr = '\0'; 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 ptr; } /* - * Convert an int64 to a string containing a base 16 (hex) representation of + * Convert an integer to a string containing a base-2 (binary) representation + * of the number. + */ +Datum +to_binary32(PG_FUNCTION_ARGS) +{ + uint64 value = (uint64) PG_GETARG_INT32(0); + char *result = convert_to_base(value, 2); + + PG_RETURN_TEXT_P(cstring_to_text(result)); +} +Datum +to_binary64(PG_FUNCTION_ARGS) +{ + uint64 value = (uint64) PG_GETARG_INT64(0); + char *result = convert_to_base(value, 2); + + PG_RETURN_TEXT_P(cstring_to_text(result)); +} + +/* + * 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 = (uint64) PG_GETARG_INT32(0); + char *result = convert_to_base(value, 8); + + PG_RETURN_TEXT_P(cstring_to_text(result)); +} +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 */ + char *result = convert_to_base(value, 8); - ptr = buf + sizeof(buf) - 1; - *ptr = '\0'; + PG_RETURN_TEXT_P(cstring_to_text(result)); +} - 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 = (uint64) PG_GETARG_INT32(0); + char *result = convert_to_base(value, 16); + + PG_RETURN_TEXT_P(cstring_to_text(result)); +} +Datum +to_hex64(PG_FUNCTION_ARGS) +{ + uint64 value = (uint64) PG_GETARG_INT64(0); + char *result = convert_to_base(value, 16); - PG_RETURN_TEXT_P(cstring_to_text(ptr)); + PG_RETURN_TEXT_P(cstring_to_text(result)); } /* diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 6996073989..e046d91c38 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..28c81ea6e4 100644 --- a/src/test/regress/expected/strings.out +++ b/src/test/regress/expected/strings.out @@ -2129,8 +2129,32 @@ select split_part('@joeuser@mydatabase@','@',-2) AS "mydatabase"; (1 row) -- --- test to_hex +-- test to_binary, to_oct, and to_hex -- +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(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(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..f7e35bfb41 100644 --- a/src/test/regress/sql/strings.sql +++ b/src/test/regress/sql/strings.sql @@ -685,10 +685,15 @@ 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(256*256*256 - 1) AS "111111111111111111111111"; +select to_binary(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "11111111111111111111111111111111"; + +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(256*256*256 - 1) AS "ffffff"; select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff"; -- -- 2.25.1 --7JfCtLOvnd9MIVvH--