Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add definition of anyarray_uniq function to helpers sql #422

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions sql/helper_functions.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
-- see: https://stackoverflow.com/questions/22677463/how-to-merge-all-integer-arrays-from-all-records-into-single-array-in-postgres/22677955#22677955
-- SOURCE: https://github.com/JDBurnZ/postgresql-anyarray/blob/master/stable/anyarray_uniq.sql
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;

-- SOURCE: https://github.com/JDBurnZ/postgresql-anyarray/blob/master/stable/anyarray_remove_null.sql
DROP FUNCTION IF EXISTS anyarray_remove_null(anyarray);
CREATE OR REPLACE FUNCTION anyarray_remove_null(from_array anyarray)
RETURNS anyarray AS
$BODY$
DECLARE
-- The variable used to track iteration over "from_array".
loop_offset integer;

-- The array to be returned by this function.
return_array from_array%TYPE;
BEGIN
-- Iterate over each element in "from_array".
FOR loop_offset IN ARRAY_LOWER(from_array, 1)..ARRAY_UPPER(from_array, 1) LOOP
IF from_array[loop_offset] IS NOT NULL THEN -- If NULL, will omit from "return_array".
return_array = ARRAY_APPEND(return_array, from_array[loop_offset]);
END IF;
END LOOP;

RETURN return_array;
END;
$BODY$ LANGUAGE plpgsql;

-- HELPFUL LINKS:
-- https://stackoverflow.com/questions/22677463/how-to-merge-all-integer-arrays-from-all-records-into-single-array-in-postgres/22677955#22677955
DROP AGGREGATE IF EXISTS array_cat_agg(anyarray);
CREATE AGGREGATE array_cat_agg(anyarray) (
SFUNC=array_cat,
Expand All @@ -23,7 +86,7 @@ $$ LANGUAGE SQL;




-- HELPFUL LINKS:
-- http://blog.scoutapp.com/articles/2016/07/12/how-to-make-text-searches-in-postgresql-faster-with-trigram-similarity
-- http://www.postgresonline.com/journal/archives/169-Fuzzy-string-matching-with-Trigram-and-Trigraphs.html

Expand Down
28 changes: 3 additions & 25 deletions sql/registrations_with_contacts.sql
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
-- https://github.com/aepyornis/hpd/blob/master/sql/anyarray_remove_null.sql
DROP FUNCTION IF EXISTS anyarray_remove_null(anyarray);
CREATE OR REPLACE FUNCTION anyarray_remove_null(from_array anyarray)
RETURNS anyarray AS
$BODY$
DECLARE
-- The variable used to track iteration over "from_array".
loop_offset integer;

-- The array to be returned by this function.
return_array from_array%TYPE;
BEGIN
-- Iterate over each element in "from_array".
FOR loop_offset IN ARRAY_LOWER(from_array, 1)..ARRAY_UPPER(from_array, 1) LOOP
IF from_array[loop_offset] IS NOT NULL THEN -- If NULL, will omit from "return_array".
return_array = ARRAY_APPEND(return_array, from_array[loop_offset]);
END IF;
END LOOP;

RETURN return_array;
END;
$BODY$ LANGUAGE plpgsql;

DROP TABLE IF EXISTS hpd_registrations_with_contacts;

-- This is mainly used as an easy way to provide contact info on request, not a replacement
-- for cross-table analysis. Hence why the corpnames, businessaddrs, and ownernames are simplified
-- with JSON and such.

DROP TABLE IF EXISTS hpd_registrations_with_contacts;

CREATE TABLE hpd_registrations_with_contacts
as SELECT
registrations.housenumber,
Expand Down
2 changes: 1 addition & 1 deletion who-owns-what.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ api_dependencies:
sql:
# These SQL scripts must be executed in order, as
# some of them depend on others.
- helper_functions.sql
- registrations_with_contacts.sql
- create_bldgs_table.sql
- helper_functions.sql
- search_function.sql
- agg_function.sql
- landlord_contact.sql
Expand Down