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 1qOVDL-0082cE-B9 for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Jul 2023 03:29:27 +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 1qOVDI-001uRa-W5 for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Jul 2023 03:29:24 +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 1qOVDI-001uRR-IU for pgsql-hackers@lists.postgresql.org; Wed, 26 Jul 2023 03:29:24 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qOVDF-001bbK-It for pgsql-hackers@postgresql.org; Wed, 26 Jul 2023 03:29:23 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1bbc06f830aso3737635ad.0 for ; Tue, 25 Jul 2023 20:29:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690342160; x=1690946960; 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=XLmHvCb7jBm45s/VFLuVODdundDC+7VgSnyIpZastzs=; b=He4NhSaVZcHNcgFP/2ZLtXGHnsGaNvrIz0irmCY/t2wuqO/RIaDZWwgPJzXeVSPTXE RNLgZzklxeyGAXR1YFfKN6ag+NzFw1MA2QxRBJvlYWKLB0Deb93g2ZDpVm3ziy4Bo2fr cl8WPaDVcNtZi+SEwIpaZELD90Q6fzPWGAsy2SH8xdC9l/YC6+WeUTnA+nUEgZO0zwIb 6Qy0D7+tznOmZ1q7Kb4Em5EJ4kdxRhHBNBb80K45kdzjLh1orwDlWTdh9Og7UvmIApTf J03J0BG2F5EgDxEgrkKFv37maXkmD3tH+1a1Rlj15Tvgh3RSaZsbaXe4SyTXeIRBZ3x+ nnXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690342160; x=1690946960; 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=XLmHvCb7jBm45s/VFLuVODdundDC+7VgSnyIpZastzs=; b=PtaypJ60zNI3XsIxYu5JiPAFVfW8FsSCDPCodrpJ9d+m/WFzXIp2j1ItYi58VTd31r tiv96swuNth0kUad2Q5XVoxu3RjyqkLe7xzJFqn15hmCU1q+8eMExWUBsSKpCUELRSh5 81Q4ZEL90oON3q5ijBmS9CDZext8Uh+8b4+HUlB/wUB5A0AJrfav+q3rjgC+eUk6wHJj 4ZZuqfT5xTXgQxUFiwO/BC7PHHNYmRCSBhjQB0aGSUHEGAoCuTidnFwB+JAo2o4OmQP6 Wl75/08CpDfLpj7YMm78TgQpCY+jdzDH1mmmUxQYuIdKNackO8DcmEagZDlwcMRdboyc FhFg== X-Gm-Message-State: ABy/qLYxZL9w/VjL1ql/LqaW5k2yX3/ZTl04EVmwficSE89IEvjUX5Q6 tKajNSiv2zj+7nhd/CFgs/c= X-Google-Smtp-Source: APBJJlEhN6XcQ+u87QE3Tlcc6spWODr5SFUMGHBHPsSSAscjh1zQg0mk2FtzyuqXrXHpt6Lae0iTQg== X-Received: by 2002:a17:902:bb81:b0:1b9:d38d:efb1 with SMTP id m1-20020a170902bb8100b001b9d38defb1mr1015792pls.8.1690342160325; Tue, 25 Jul 2023 20:29:20 -0700 (PDT) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id jb19-20020a170903259300b001b5247cac3dsm5496391plb.110.2023.07.25.20.29.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jul 2023 20:29:19 -0700 (PDT) Date: Tue, 25 Jul 2023 20:29:17 -0700 From: Nathan Bossart To: Kirk Wolak Cc: Peter Eisentraut , Eric Radman , pgsql-hackers@postgresql.org Subject: Re: [PATCH] Add function to_oct Message-ID: <20230726032917.GA3151386@nathanxps13> References: <5c260f97-d8c8-4a68-7498-022e550c6b10@enterprisedb.com> <20230725232426.GA2992317@nathanxps13> <20230726001656.GC2992317@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="y0ulUmNC+osPPQO6" Content-Disposition: inline In-Reply-To: <20230726001656.GC2992317@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --y0ulUmNC+osPPQO6 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Jul 25, 2023 at 05:16:56PM -0700, Nathan Bossart wrote: > On Tue, Jul 25, 2023 at 04:24:26PM -0700, Nathan Bossart wrote: >> I started taking a look at this and ended up adding to_binary() and a >> bigint version of to_oct() for completeness. While I was at it, I moved >> the base-conversion logic out to a separate static function that >> to_binary/oct/hex all use. > > Bleh, this patch seems to fail horribly on 32-bit builds. I'll look into > it soon. Here's a new version of the patch with the silly mistakes fixed. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --y0ulUmNC+osPPQO6 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v4-0001-add-to_binary-and-to_oct.patch" From 1a729185b7fb77dbfee10f9a715cef6c3cd7e74b Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 25 Jul 2023 16:09:01 -0700 Subject: [PATCH v4 1/1] add to_binary() and to_oct() --- doc/src/sgml/func.sgml | 42 +++++++++++++ src/backend/utils/adt/varlena.c | 86 +++++++++++++++++++-------- src/include/catalog/pg_proc.dat | 12 ++++ src/test/regress/expected/strings.out | 26 +++++++- src/test/regress/sql/strings.sql | 9 ++- 5 files changed, 148 insertions(+), 27 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index b94827674c..79334ea145 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..b8903f338f 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -165,6 +165,7 @@ static void text_format_string_conversion(StringInfo buf, char conversion, int flags, int width); static void text_format_append_string(StringInfo buf, const char *str, int flags, int width); +static char *convert_to_base(uint64 value, int base); /***************************************************************************** @@ -4919,53 +4920,90 @@ 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. + * Convert an integer to a string containing a base-2 (binary) representation + * of the number. */ Datum -to_hex32(PG_FUNCTION_ARGS) +to_binary32(PG_FUNCTION_ARGS) { - uint32 value = (uint32) PG_GETARG_INT32(0); - char *ptr; - const char *digits = "0123456789abcdef"; - char buf[32]; /* bigger than needed, but reasonable */ + uint64 value = (uint64) PG_GETARG_INT32(0); + char *result = convert_to_base(value, 2); - ptr = buf + sizeof(buf) - 1; - *ptr = '\0'; + 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); - do - { - *--ptr = digits[value % HEXBASE]; - value /= HEXBASE; - } while (ptr > buf && value); + PG_RETURN_TEXT_P(cstring_to_text(result)); +} - PG_RETURN_TEXT_P(cstring_to_text(ptr)); +/* + * Convert an integer to a string containing a base-8 (oct) representation of + * the number. + */ +Datum +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 *result = convert_to_base(value, 8); + + PG_RETURN_TEXT_P(cstring_to_text(result)); } /* - * Convert an int64 to a string containing a base 16 (hex) representation of + * 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 *ptr; + char *result = convert_to_base(value, 16); + + PG_RETURN_TEXT_P(cstring_to_text(result)); +} + +/* + * Workhorse for to_binary, to_oct, and to_hex. Note that base must be either + * 2, 8, or 16. + */ +static char * +convert_to_base(uint64 value, int base) +{ const char *digits = "0123456789abcdef"; - char buf[32]; /* bigger than needed, but reasonable */ + char *buf = palloc(sizeof(uint64) * BITS_PER_BYTE + 1); + char *ptr = buf + (sizeof(uint64) * BITS_PER_BYTE); - ptr = buf + sizeof(buf) - 1; - *ptr = '\0'; + Assert(base == 2 || base == 8 || base == 16); + *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; } /* diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 6996073989..eca2b420e0 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 => '5101', descr => 'convert int4 number to binary', + proname => 'to_binary', prorettype => 'text', proargtypes => 'int4', + prosrc => 'to_binary32' }, +{ oid => '5102', descr => 'convert int8 number to binary', + proname => 'to_binary', prorettype => 'text', proargtypes => 'int8', + prosrc => 'to_binary64' }, +{ oid => '5103', descr => 'convert int4 number to oct', + proname => 'to_oct', prorettype => 'text', proargtypes => 'int4', + prosrc => 'to_oct32' }, +{ oid => '5104', 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 --y0ulUmNC+osPPQO6--