1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
/*PGR-GNU*****************************************************************
File: degree.sql
Copyright (c) 2022 ~ Celia Virginia Vergara Castillo
mail: vicky at georepublic.de
------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
--v3.4
CREATE FUNCTION pgr_degree(
TEXT, -- Edges SQL
TEXT, -- Vertices SQL
dryrun BOOLEAN DEFAULT false,
OUT node BIGINT,
OUT degree BIGINT
)
RETURNS SETOF RECORD AS
$BODY$
DECLARE
edges_sql TEXT;
vertices_sql TEXT;
has_in_edges BOOLEAN := TRUE;
has_out_edges BOOLEAN := TRUE;
eids TEXT;
query TEXT;
sqlhint TEXT;
BEGIN
BEGIN
edges_sql := _pgr_checkQuery($1);
PERFORM _pgr_checkColumn(edges_sql, 'id', 'ANY-INTEGER', dryrun => $3);
vertices_sql := _pgr_checkQuery($2);
PERFORM _pgr_checkColumn(vertices_sql, 'id', 'ANY-INTEGER', dryrun => $3);
EXCEPTION WHEN OTHERS THEN
GET STACKED DIAGNOSTICS sqlhint = PG_EXCEPTION_HINT;
RAISE EXCEPTION '%', SQLERRM USING HINT = sqlhint, ERRCODE = SQLSTATE;
END;
has_in_edges := _pgr_checkColumn(vertices_sql, 'in_edges', 'ANY-INTEGER[]', true, dryrun => $3);
has_out_edges := _pgr_checkColumn(vertices_sql, 'out_edges', 'ANY-INTEGER[]', true, dryrun => $3);
IF NOT has_in_edges AND NOT has_out_edges THEN
RAISE EXCEPTION 'column "in_edges" does not exist' USING HINT = vertices_sql, ERRCODE = 42703;
END IF;
IF has_in_edges THEN
eids = $$coalesce(in_edges::BIGINT[], '{}'::BIGINT[])$$;
END IF;
IF has_out_edges THEN
IF has_in_edges THEN
eids = E'\n ' || eids
|| E'\n ||\n '
|| $$coalesce(out_edges::BIGINT[], '{}'::BIGINT[])$$;
ELSE
eids = $$coalesce(out_edges::BIGINT[], '{}'::BIGINT[])$$;
END IF;
ELSE
IF NOT has_in_edges THEN
RAISE EXCEPTION 'Missing column'
USING HINT = 'Column "in_edges" and/or "out_edges" is missing in'||E'\n'||vertices_sql;
END IF;
END IF;
query := format($q$
WITH
-- a sub set of edges of the graph goes here
g_edges AS (
$q$ || edges_sql || $q$
),
-- sub set of vertices of the graph goes here
all_vertices AS (
$q$ || vertices_sql || $q$
),
g_vertices AS (
SELECT id,
unnest(%s) AS eid
FROM all_vertices
),
totals AS (
SELECT v.id, count(*)
FROM g_vertices v
JOIN g_edges e ON (v.eid = e.id) GROUP BY v.id
)
SELECT id::BIGINT, count::BIGINT FROM all_vertices JOIN totals USING (id)
$q$, eids);
IF dryrun THEN
RAISE NOTICE '%', query || ';';
ELSE
RETURN QUERY EXECUTE query;
END IF;
END;
$BODY$
LANGUAGE plpgsql VOLATILE STRICT;
COMMENT ON FUNCTION pgr_degree(TEXT, TEXT, BOOLEAN)
IS 'pgr_degree
- Parameters
- Edges SQL with columns: id
- Vertices SQL with columns: id, in_edges, out_edges
- Documentation:
- ${PROJECT_DOC_LINK}/pgr_degree.html
';
--v3.8
CREATE FUNCTION pgr_degree(
TEXT, -- Edges SQL
dryrun BOOLEAN DEFAULT false,
OUT node BIGINT,
OUT degree BIGINT
)
RETURNS SETOF RECORD AS
$BODY$
DECLARE
edges_sql TEXT;
has_id BOOLEAN;
has_source BOOLEAN;
has_target BOOLEAN;
eids TEXT;
query TEXT;
sqlhint TEXT;
BEGIN
-- Verify the data is complete
BEGIN
edges_sql := _pgr_checkQuery($1);
has_id := _pgr_checkColumn(edges_sql, 'id', 'ANY-INTEGER', dryrun => $2);
has_source := _pgr_checkColumn(edges_sql, 'source', 'ANY-INTEGER', dryrun => $2);
has_target := _pgr_checkColumn(edges_sql, 'target', 'ANY-INTEGER', dryrun => $2);
EXCEPTION WHEN OTHERS THEN
GET STACKED DIAGNOSTICS sqlhint = PG_EXCEPTION_HINT;
RAISE EXCEPTION '%', SQLERRM USING HINT = sqlhint, ERRCODE = SQLSTATE;
END;
query := format($q$
WITH
-- a sub set of edges of the graph goes here
g_edges AS (
%1$s
),
-- sub set of vertices of the graph goes here
g_vertices AS (
SELECT source, id FROM g_edges
UNION ALL
SELECT target, id FROM g_edges
),
totals AS (
SELECT source AS node, count(*) AS degree
FROM g_vertices
GROUP BY node
)
SELECT node::BIGINT, degree::BIGINT
FROM totals
$q$, edges_sql);
IF dryrun THEN
RAISE NOTICE '%', query || ';';
ELSE
RETURN QUERY EXECUTE query;
END IF;
END;
$BODY$
LANGUAGE plpgsql VOLATILE STRICT;
COMMENT ON FUNCTION pgr_degree(TEXT, BOOLEAN)
IS 'pgr_degree
- Parameters
- Edges SQL with columns: id
- Documentation:
- ${PROJECT_DOC_LINK}/pgr_degree.html
';
|