Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

biginteger array functions

Are there any available functions for work with big integers?

I've found a module intarray, but functions from this module only work with integer, not bigint.

I miss a function for removing an item from an array. Something like the implementation of a "minus" operator in mentioned module:

int[] - int (remove entries matching right argument from array)

like image 888
Radek Simko Avatar asked Oct 29 '25 16:10

Radek Simko


1 Answers

Postgres 9.3 or newer

With the added array_remove(anyarray, anyelement), the task is simple now:

test=> SELECT array_remove('{1,3,4,3}'::bigint[], 3);
 array_remove 
--------------
 {1,4}

Postgres 9.2 or older

Create a custom function. This one is reasonably fast:

CREATE OR REPLACE FUNCTION arr_subtract(int8[], int8[])
  RETURNS int8[]
  LANGUAGE sql IMMUTABLE AS
$func$
SELECT ARRAY(
   SELECT a
   FROM   unnest($1) WITH ORDINALITY x(a, ord)
   WHERE  a <> ALL ($2)
   ORDER  BY ord
   );
$func$;

Call:

SELECT arr_subtract('{3,5,6,7,8,9}':: int8[], '{3,4,8}'::int8[]);

Result:

{5,6,7,9}

Allows to remove multiple values at once.
Keeps the original order of the array.
Does not work for NULL values.

Related:

  • PostgreSQL unnest() with element number
  • Exclude matched array elements
like image 198
Erwin Brandstetter Avatar answered Oct 31 '25 05:10

Erwin Brandstetter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!