Created
March 14, 2020 12:06
-
-
Save Himstar8/4b7139da38720379957bd12108d8e4bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP FUNCTION IF EXISTS anyarray_uniq(anyarray); | |
CREATE OR REPLACE FUNCTION anyarray_uniq(with_array anyarray) | |
RETURNS anyarray AS | |
$BODY$ | |
DECLARE | |
-- The variable used to track iteration over "with_array". | |
loop_offset integer; | |
-- The array to be returned by this function. | |
return_array with_array%TYPE := '{}'; | |
BEGIN | |
IF with_array IS NULL THEN | |
return NULL; | |
END IF; | |
IF with_array = '{}' THEN | |
return return_array; | |
END IF; | |
-- Iterate over each element in "concat_array". | |
FOR loop_offset IN ARRAY_LOWER(with_array, 1)..ARRAY_UPPER(with_array, 1) LOOP | |
IF with_array[loop_offset] IS NULL THEN | |
IF NOT EXISTS( | |
SELECT 1 | |
FROM UNNEST(return_array) AS s(a) | |
WHERE a IS NULL | |
) THEN | |
return_array = ARRAY_APPEND(return_array, with_array[loop_offset]); | |
END IF; | |
-- When an array contains a NULL value, ANY() returns NULL instead of FALSE... | |
ELSEIF NOT(with_array[loop_offset] = ANY(return_array)) OR NOT(NULL IS DISTINCT FROM (with_array[loop_offset] = ANY(return_array))) THEN | |
return_array = ARRAY_APPEND(return_array, with_array[loop_offset]); | |
END IF; | |
END LOOP; | |
RETURN return_array; | |
END; | |
$BODY$ LANGUAGE plpgsql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment