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 1qWetT-003xpA-VM for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Aug 2023 15:26:40 +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 1qWetR-001RI5-Lt for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Aug 2023 15:26:37 +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 1qWetR-001RHl-B2 for pgsql-hackers@lists.postgresql.org; Thu, 17 Aug 2023 15:26:37 +0000 Received: from mail-yw1-x112a.google.com ([2607:f8b0:4864:20::112a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qWetN-000gmD-N0 for pgsql-hackers@postgresql.org; Thu, 17 Aug 2023 15:26:36 +0000 Received: by mail-yw1-x112a.google.com with SMTP id 00721157ae682-579de633419so75655197b3.3 for ; Thu, 17 Aug 2023 08:26:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692285991; x=1692890791; 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=mmzaol0CbcRa3/vBOPvMGSDQ0dakOWBcdA3v0co35/4=; b=K9ncWe81Xcjd8/QXcN7p8qg4FWb6UzvNApBYPRkga5xmh2K/jWY8X5yNjG7lV9YPEe iBDQMOu17I3vyNpUzITpR3/+CuwXvQ7dd5y9k+aUhTiWIr2ROu8kR29q5orzZyw8NjaR hvNrcf/ROu/Jpke5+kBpSTNuoXgHsCl2vmxm1MIIlmZFRwoBW59C4smd5OG9x6NL4Cr2 44o7z+G/RX3nA5V406zy4zX+QNQGcNey6GfpOCYIkT4cmgjwNPSmPzz9L4Q1+Q/JiCJr m4ZbP7+rlLZNS7jrgt2RyDhP43DRiRGKvm8atVxJVRFxRRupXfLRGBxhBDt/eZgRq3Bx tYrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692285991; x=1692890791; 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=mmzaol0CbcRa3/vBOPvMGSDQ0dakOWBcdA3v0co35/4=; b=DIE81BYYK8yACqqg9+Uq2+UOG9CjnbJGtBJch/ySNu9y0Sk7pnN5tPVXm2MBTVUupm l4zXmw6dnimYw5JCDbsNO2elOWD/yPgG0WZV61eHVqvV7cG4Kn3+KZud4ZDnJxcd7alo E2Nm2ZEFfA38mvOV6PRXjXXTkD/eL+M14d3CsF6fJQttsLTn6Zfkx5B7Co7lST6ERIiB NjNra14g5horTf2dEzw9FPbPvyAhhihJOv/c7aHtg5JG65CfMIDiBl++B9xyW40uSMqt xDCXnsgiynRdMeY97Unlto3C0/HsP5SHs7S9ZVUloaPeRhVcXce+Kl5n070U+ow2KSgk jSgg== X-Gm-Message-State: AOJu0YzqoOapCZALRQqrk7Vazcj+/De8RpP143nkEXwQCrZZfK1VD9OG ci9+hbqhbMTBbq5A+OQE++s= X-Google-Smtp-Source: AGHT+IG3q7N4sC+MfS0PTRwowWfjkDQmLmTvg+CzzyAQsieL9eHZdmmluGwlRlJD59P41TRtoHkBXg== X-Received: by 2002:a0d:e502:0:b0:55a:574f:327c with SMTP id o2-20020a0de502000000b0055a574f327cmr4808706ywe.13.1692285991370; Thu, 17 Aug 2023 08:26:31 -0700 (PDT) Received: from nathanxps13 ([2600:1700:48f8:340f:3dfe:d9d7:e7f3:524e]) by smtp.gmail.com with ESMTPSA id o16-20020a0dcc10000000b00577139f85dfsm743081ywd.22.2023.08.17.08.26.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Aug 2023 08:26:30 -0700 (PDT) Date: Thu, 17 Aug 2023 08:26:28 -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: <20230817152628.GA3148583@nathanxps13> References: <20230725232426.GA2992317@nathanxps13> <20230726001656.GC2992317@nathanxps13> <20230726032917.GA3151386@nathanxps13> <20230815041129.GB2294364@nathanxps13> <20230815171715.GA2312399@nathanxps13> <20230816142410.GA2651383@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="mP3DRpeJDSE+ciuQ" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --mP3DRpeJDSE+ciuQ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Aug 17, 2023 at 12:35:54PM +0700, John Naylor wrote: > That makes it a lexically-scoped global variable, which we don't need > either. Can we have the internal function allocate on the stack, then > call cstring_to_text() on that, returning the text result? That does its > own palloc. > > Or maybe better, save the starting pointer, compute the length at the end, > and call cstring_to_text_with_len()? (It seems we wouldn't need > the nul-terminator then, either.) Works for me. I did it that way in v7. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --mP3DRpeJDSE+ciuQ Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v7-0001-add-to_binary-and-to_oct.patch" From b7eccfe08150ec90ca4b48fe69c2b37453c633b2 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 25 Jul 2023 16:09:01 -0700 Subject: [PATCH v7 1/1] add to_binary() and to_oct() --- doc/src/sgml/func.sgml | 42 ++++++++++++ src/backend/utils/adt/varlena.c | 92 +++++++++++++++++++-------- 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(+), 28 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..58c0cbcdd8 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -4919,53 +4919,95 @@ 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. + * We size the buffer for to_binary's longest possible return value. */ -Datum -to_hex32(PG_FUNCTION_ARGS) +#define CONVERT_TO_BASE_BUFSIZE (sizeof(uint64) * BITS_PER_BYTE) + +/* + * Workhorse for to_binary, to_oct, and to_hex. Note that base must be > 1 and + * <= 16. + */ +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 */ + char buf[CONVERT_TO_BASE_BUFSIZE]; + int len; + + Assert(base > 1); + Assert(base <= 16); - ptr = buf + sizeof(buf) - 1; - *ptr = '\0'; + ptr = buf + sizeof(buf); 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)); + len = CONVERT_TO_BASE_BUFSIZE - (ptr - buf); + return cstring_to_text_with_len(ptr, len); +} + +#undef CONVERT_TO_BASE_BUFSIZE + +/* + * 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); + + 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 = (uint64) 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 = (uint64) 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 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 --mP3DRpeJDSE+ciuQ--