From d31f71f354facddb9a49619886d2b65746b172a3 Mon Sep 17 00:00:00 2001 From: Rafsun Masud Date: Mon, 8 Jul 2024 14:59:29 -0700 Subject: [PATCH] Add graph_exists function (#1958) This function returns if a graph exists. --- age--1.5.0--y.y.y.sql | 4 ++ regress/expected/catalog.out | 64 +++++++++++++++++++++++++++ regress/sql/catalog.sql | 29 +++++++++++- sql/age_agtype.sql | 4 ++ src/backend/commands/graph_commands.c | 29 +++++++++++- 5 files changed, 127 insertions(+), 3 deletions(-) diff --git a/age--1.5.0--y.y.y.sql b/age--1.5.0--y.y.y.sql index ef6a7727e..9461e3bf3 100644 --- a/age--1.5.0--y.y.y.sql +++ b/age--1.5.0--y.y.y.sql @@ -106,3 +106,7 @@ CREATE FUNCTION ag_catalog.age_graph_stats(agtype) PARALLEL SAFE AS 'MODULE_PATHNAME'; +CREATE FUNCTION ag_catalog.graph_exists(graph_name name) + RETURNS agtype + LANGUAGE c + AS 'MODULE_PATHNAME', 'age_graph_exists'; diff --git a/regress/expected/catalog.out b/regress/expected/catalog.out index bc7b9434a..d06a0ce67 100644 --- a/regress/expected/catalog.out +++ b/regress/expected/catalog.out @@ -393,6 +393,70 @@ SELECT create_vlabel(NULL, NULL); ERROR: graph name must not be NULL SELECT create_elabel(NULL, NULL); ERROR: graph name must not be NULL +-- age_graph_exists() +CREATE FUNCTION raise_notice(graph_name TEXT) +RETURNS void AS $$ +DECLARE + res BOOLEAN; +BEGIN + -- this tests whether graph_exists works with IF-ELSE. + SELECT graph_exists('graph1') INTO res; + IF res THEN + RAISE NOTICE 'graph exists'; + ELSE + RAISE NOTICE 'graph does not exist'; + END IF; +END $$ LANGUAGE plpgsql; +SELECT graph_exists('graph1'); + graph_exists +-------------- + false +(1 row) + +SELECT create_graph('graph1'); +NOTICE: graph "graph1" has been created + create_graph +-------------- + +(1 row) + +SELECT graph_exists('graph1'); + graph_exists +-------------- + true +(1 row) + +SELECT raise_notice('graph1'); +NOTICE: graph exists + raise_notice +-------------- + +(1 row) + +SELECT drop_graph('graph1', true); +NOTICE: drop cascades to 2 other objects +DETAIL: drop cascades to table graph1._ag_label_vertex +drop cascades to table graph1._ag_label_edge +NOTICE: graph "graph1" has been dropped + drop_graph +------------ + +(1 row) + +SELECT graph_exists('graph1'); + graph_exists +-------------- + false +(1 row) + +SELECT raise_notice('graph1'); +NOTICE: graph does not exist + raise_notice +-------------- + +(1 row) + +DROP FUNCTION raise_notice(TEXT); -- dropping the graph SELECT drop_graph('graph', true); NOTICE: drop cascades to 2 other objects diff --git a/regress/sql/catalog.sql b/regress/sql/catalog.sql index 96c63e47a..85fc4e8ab 100644 --- a/regress/sql/catalog.sql +++ b/regress/sql/catalog.sql @@ -166,7 +166,32 @@ SELECT create_elabel(NULL, 'r'); SELECT create_vlabel(NULL, NULL); SELECT create_elabel(NULL, NULL); +-- age_graph_exists() +CREATE FUNCTION raise_notice(graph_name TEXT) +RETURNS void AS $$ +DECLARE + res BOOLEAN; +BEGIN + -- this tests whether graph_exists works with IF-ELSE. + SELECT graph_exists('graph1') INTO res; + IF res THEN + RAISE NOTICE 'graph exists'; + ELSE + RAISE NOTICE 'graph does not exist'; + END IF; +END $$ LANGUAGE plpgsql; + +SELECT graph_exists('graph1'); + +SELECT create_graph('graph1'); +SELECT graph_exists('graph1'); +SELECT raise_notice('graph1'); + +SELECT drop_graph('graph1', true); +SELECT graph_exists('graph1'); +SELECT raise_notice('graph1'); + +DROP FUNCTION raise_notice(TEXT); + -- dropping the graph SELECT drop_graph('graph', true); - - diff --git a/sql/age_agtype.sql b/sql/age_agtype.sql index 944508dd0..a6932cb1f 100644 --- a/sql/age_agtype.sql +++ b/sql/age_agtype.sql @@ -1060,3 +1060,7 @@ CREATE OPERATOR CLASS graphid_ops_hash OPERATOR 1 =, FUNCTION 1 ag_catalog.graphid_hash_cmp(graphid); +CREATE FUNCTION ag_catalog.graph_exists(graph_name name) + RETURNS agtype + LANGUAGE c + AS 'MODULE_PATHNAME', 'age_graph_exists'; diff --git a/src/backend/commands/graph_commands.c b/src/backend/commands/graph_commands.c index df16ad152..802025c81 100644 --- a/src/backend/commands/graph_commands.c +++ b/src/backend/commands/graph_commands.c @@ -109,7 +109,34 @@ Datum create_graph(PG_FUNCTION_ARGS) (errmsg("graph \"%s\" has been created", NameStr(*graph_name)))); /* according to postgres specification of c-language functions if function returns void this is the syntax */ - PG_RETURN_VOID(); + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(age_graph_exists); + +Datum age_graph_exists(PG_FUNCTION_ARGS) +{ + Name graph_name; + char *graph_name_str; + + /* if no argument is passed with the function, graph name cannot be null */ + if (PG_ARGISNULL(0)) + { + ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("graph name can not be NULL"))); + } + + graph_name = PG_GETARG_NAME(0); + graph_name_str = NameStr(*graph_name); + + if (graph_exists(graph_name_str)) + { + return boolean_to_agtype(true); + } + else + { + return boolean_to_agtype(false); + } } static Oid create_schema_for_graph(const Name graph_name)