pramsey / pgsql-arraymath

Functions and operators for element-by-element math and logic on arrays

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status

pgsql-arraymath

An extension for element-by-element operations on PostgreSQL arrays with a integer, float or numeric data type.

Enabling in a database

CREATE EXTENSION arraymath;

The operators are all the usual ones, but prefixed by @ to indicate their element-by-element nature.

  • @= element-by-element equality, returns boolean[]
  • @< element-by-element less than, returns boolean[]
  • @> element-by-element greater than, returns boolean[]
  • @<= element-by-element less than or equals, returns boolean[]
  • @>= element-by-element greater than or equals, returns boolean[]
  • @+ element-by-element addition
  • @- element-by-element subtraction
  • @* element-by-element multiplication
  • @/ element-by-element division

The functions are prefixed by array_.

  • array_sum(anyarray) sums up all the elements
  • array_avg(anyarray) returns float average of all elements
  • array_min(anyarray) returns minimum of all elements
  • array_max(anyarray) returns maximum of all elements
  • array_median(anyarray) returns the median of all elements
  • array_sort(anyarray) sorts the array from smallest to largest
  • array_rsort(anyarray) sorts the array from largest to smallest

Array versus Constant

If you apply the operators with an array on one side and a constant on the other, the constant will be applied to all the elements of the array. For example:

SELECT ARRAY[1,2,3,4] @< 4;
{t,t,t,f}
SELECT ARRAY[3.4,5.6,7.6] @* 8.1;
{27.54,45.36,61.56}

Array versus Array

If you apply the operators with an array on both sides, the operator will be applied to each element pairing in turn, returning an array as long as the larger of the two inputs. Where the shorter array runs out of elements, the process will simply move back to the start of the array. For example:

    SELECT ARRAY[1,2] @+ ARRAY[3,4];
    
      {4,6}
      
    SELECT ARRAY[1,2,3,4,5,6] @* ARRAY[1,2];
    
      {1,4,3,8,5,12}
      
    SELECT ARRAY[1,1,1,1] @< ARRAY[0,2];
    
      {f,t,f,t}

    SELECT ARRAY[1,2,3] @= ARRAY[3,2,1];

      {f,t,f}

Array Functions

The extension includes a few utility functions that work to summarize or manipulate an array directly without unnesting.

SELECT array_sort(ARRAY[9,1,8,2,7,3,6,4,5]);

  {1,2,3,4,5,6,7,8,9}

SELECT array_sort(ARRAY[9,1,8,2,7,3,6,4,5], reverse => true);

  {9,8,7,6,5,4,3,2,1}

SELECT array_sum(ARRAY[1,2,3,4,5,6,7,8,9]);

  45

SELECT array_avg(ARRAY[1,2,3,4,5,6,7,8,9]);

  5

SELECT array_min(ARRAY[1,2,3,4,5,6,7,8,9]);

  1

SELECT array_max(ARRAY[1,2,3,4,5,6,7,8,9]);

  9

SELECT array_median(ARRAY[1,2,3,4,5,6,7,8,9]);

  5

As far as possible, the functions preserve the data type of the original input. For the median and mean, the return type is float8.

SELECT pg_typeof(array_min(ARRAY[1,2,3,4,5,6,7,8,9]));

  integer

About

Functions and operators for element-by-element math and logic on arrays

License:MIT License


Languages

Language:C 99.0%Language:Makefile 1.0%