Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p7kmg-0002Di-85 for pgsql-hackers@arkaria.postgresql.org; Tue, 20 Dec 2022 22:08:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p7kme-0003qz-Cd for pgsql-hackers@arkaria.postgresql.org; Tue, 20 Dec 2022 22:08:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p7kmd-0003pt-RD for pgsql-hackers@lists.postgresql.org; Tue, 20 Dec 2022 22:08:24 +0000 Received: from vm3.eradman.com ([45.63.64.45]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p7kmW-0007ri-EX for pgsql-hackers@postgresql.org; Tue, 20 Dec 2022 22:08:20 +0000 Received: from vm3.eradman.com (localhost [127.0.0.1]) by vm3.eradman.com (OpenSMTPD) with ESMTP id aac2cd86 for ; Tue, 20 Dec 2022 17:08:13 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=eradman.com; h=date:from :to:subject:message-id:mime-version:content-type; s=selector1; bh=KCRvsYY1kuR2/zeQ8c4C04v71qc=; b=VokAv9NrRXV5/HnJPE14DrOlTGPY //Kxpo0ZmN0YV3Mjon59AVerB11gi3As2rCBli/WodlWPEO5y+6nZp9CfGw+v3No zpF3cyELj3l5jUCrRG1YMhr/3WiH9VxYhgXP/g9exBqmKSTKf+/vuixErLf7tt9Z 264w1FSH+Fzn6rw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=eradman.com; h=date:from:to :subject:message-id:mime-version:content-type; q=dns; s= selector1; b=SogY7LpaWHirA+hu7iZP6S5vUEoKpc9M/A11RV/BHiIvBVQZCYN oldbyDVHU2M4Lmud8/X5r9z1EA2mdSQRQF5o8HjydgGMWrlQAI6p1F7pM6BM8Tv5 RdFPUE82zGixR0AZzpjq77PMWwr7vanRCjrEaxCLGI3LsZA+ubJ7fY98= Received: from localhost (vm3.eradman.com [local]) by vm3.eradman.com (OpenSMTPD) with ESMTPA id a862d07e for ; Tue, 20 Dec 2022 17:08:13 -0500 (EST) Date: Tue, 20 Dec 2022 17:08:13 -0500 From: Eric Radman To: pgsql-hackers@postgresql.org Subject: [PATCH] Add function to_oct Message-ID: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="DJBEb6F0IJyNw43/" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --DJBEb6F0IJyNw43/ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hello! This patch is a new function based on the implementation of to_hex(int). Since support for octal integer literals was added, to_oct(int) allows octal values to be easily stored and returned in query results. to_oct(0o755) = '755' This is probably most useful for storing file system permissions. -- Eric Radman --DJBEb6F0IJyNw43/ Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="add-function-to_oct.patch" diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 1f63dc6dba..b539e0dc0b 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -3699,6 +3699,23 @@ repeat('Pg', 4) PgPgPgPg + + + + to_oct + + to_oct ( integer ) + text + + + Converts the number to its equivalent octal representation. + + + to_oct(32) + 40 + + + diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 1c52deec55..16929ddeed 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -5229,6 +5229,31 @@ to_hex64(PG_FUNCTION_ARGS) PG_RETURN_TEXT_P(cstring_to_text(ptr)); } +#define OCTBASE 8 +/* + * Convert an int32 to a string containing a base 8 (oct) representation of + * the number. + */ +Datum +to_oct32(PG_FUNCTION_ARGS) +{ + uint32 value = (uint32) PG_GETARG_INT32(0); + char *ptr; + const char *digits = "01234567"; + char buf[32]; /* bigger than needed, but reasonable */ + + ptr = buf + sizeof(buf) - 1; + *ptr = '\0'; + + do + { + *--ptr = digits[value % OCTBASE]; + value /= OCTBASE; + } while (ptr > buf && value); + + PG_RETURN_TEXT_P(cstring_to_text(ptr)); +} + /* * Return the size of a datum, possibly compressed * diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 98d90d9338..824df6fdc8 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -3687,6 +3687,9 @@ { oid => '2090', descr => 'convert int8 number to hex', proname => 'to_hex', prorettype => 'text', proargtypes => 'int8', prosrc => 'to_hex64' }, +{ oid => '2173', descr => 'convert int4 number to oct', + proname => 'to_oct', prorettype => 'text', proargtypes => 'int4', + prosrc => 'to_oct32' }, # for character set encoding support diff --git a/src/test/regress/expected/strings.out b/src/test/regress/expected/strings.out index f028c1f10f..5ebaeb4a80 100644 --- a/src/test/regress/expected/strings.out +++ b/src/test/regress/expected/strings.out @@ -2143,6 +2143,15 @@ select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff" ffffffff (1 row) +-- +-- test to_oct +-- +select to_oct(256*256*256 - 1) AS "77777777"; + 77777777 +---------- + 77777777 +(1 row) + -- -- SHA-2 -- diff --git a/src/test/regress/sql/strings.sql b/src/test/regress/sql/strings.sql index 932f71cbca..c4c40949e7 100644 --- a/src/test/regress/sql/strings.sql +++ b/src/test/regress/sql/strings.sql @@ -691,6 +691,11 @@ select to_hex(256*256*256 - 1) AS "ffffff"; select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff"; +-- +-- test to_oct +-- +select to_oct(256*256*256 - 1) AS "77777777"; + -- -- SHA-2 -- --DJBEb6F0IJyNw43/--