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

Optmize vertex and edge builder functions #1252

Merged
merged 1 commit into from
Oct 17, 2023
Merged
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ OBJS = src/backend/age.o \
src/backend/utils/adt/agtype_ops.o \
src/backend/utils/adt/agtype_parser.o \
src/backend/utils/adt/agtype_util.o \
src/backend/utils/adt/agtype_raw.o \
src/backend/utils/adt/age_global_graph.o \
src/backend/utils/adt/age_session_info.o \
src/backend/utils/adt/age_vle.o \
Expand Down
132 changes: 55 additions & 77 deletions src/backend/utils/adt/agtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "utils/agtype.h"
#include "utils/agtype_parser.h"
#include "utils/ag_float8_supp.h"
#include "utils/agtype_raw.h"
#include "catalog/ag_graph.h"
#include "catalog/ag_label.h"
#include "utils/graphid.h"
Expand Down Expand Up @@ -2181,74 +2182,64 @@ PG_FUNCTION_INFO_V1(_agtype_build_vertex);
*/
Datum _agtype_build_vertex(PG_FUNCTION_ARGS)
{
agtype_in_state result;
graphid id;
char *label;
agtype *properties;
agtype_build_state *bstate;
agtype *rawscalar;
agtype *vertex;

memset(&result, 0, sizeof(agtype_in_state));

result.res = push_agtype_value(&result.parse_state, WAGT_BEGIN_OBJECT,
NULL);

/* process graphid */
result.res = push_agtype_value(&result.parse_state, WAGT_KEY,
string_to_agtype_value("id"));

/* handles null */
if (fcinfo->args[0].isnull)
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("_agtype_build_vertex() graphid cannot be NULL")));
}

id = AG_GETARG_GRAPHID(0);
result.res = push_agtype_value(&result.parse_state, WAGT_VALUE,
integer_to_agtype_value(id));

/* process label */
result.res = push_agtype_value(&result.parse_state, WAGT_KEY,
string_to_agtype_value("label"));

if (fcinfo->args[1].isnull)
{
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("_agtype_build_vertex() label cannot be NULL")));
}

result.res =
push_agtype_value(&result.parse_state, WAGT_VALUE,
string_to_agtype_value(PG_GETARG_CSTRING(1)));

/* process properties */
result.res = push_agtype_value(&result.parse_state, WAGT_KEY,
string_to_agtype_value("properties"));
id = AG_GETARG_GRAPHID(0);
label = PG_GETARG_CSTRING(1);

//if the properties object is null, push an empty object
if (fcinfo->args[2].isnull)
{
result.res = push_agtype_value(&result.parse_state, WAGT_BEGIN_OBJECT,
NULL);
result.res = push_agtype_value(&result.parse_state, WAGT_END_OBJECT,
NULL);
agtype_build_state *bstate = init_agtype_build_state(0, AGT_FOBJECT);
properties = build_agtype(bstate);
pfree_agtype_build_state(bstate);
}
else
{
agtype *properties = AG_GET_ARG_AGTYPE_P(2);
properties = AG_GET_ARG_AGTYPE_P(2);

if (!AGT_ROOT_IS_OBJECT(properties))
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("_agtype_build_vertex() properties argument must be an object")));
}

add_agtype((Datum)properties, false, &result, AGTYPEOID, false);
}

result.res = push_agtype_value(&result.parse_state, WAGT_END_OBJECT, NULL);
bstate = init_agtype_build_state(3, AGT_FOBJECT);
write_string(bstate, "id");
write_string(bstate, "label");
write_string(bstate, "properties");
write_graphid(bstate, id);
write_string(bstate, label);
write_container(bstate, properties);
vertex = build_agtype(bstate);
pfree_agtype_build_state(bstate);

result.res->type = AGTV_VERTEX;
bstate = init_agtype_build_state(1, AGT_FARRAY | AGT_FSCALAR);
write_extended(bstate, vertex, AGT_HEADER_VERTEX);
rawscalar = build_agtype(bstate);
pfree_agtype_build_state(bstate);

PG_RETURN_POINTER(agtype_value_to_agtype(result.res));
PG_RETURN_POINTER(rawscalar);
}

Datum make_vertex(Datum id, Datum label, Datum properties)
Expand All @@ -2263,18 +2254,13 @@ PG_FUNCTION_INFO_V1(_agtype_build_edge);
*/
Datum _agtype_build_edge(PG_FUNCTION_ARGS)
{
agtype_in_state result;
agtype_build_state *bstate;
agtype *edge, *rawscalar;
graphid id, start_id, end_id;

memset(&result, 0, sizeof(agtype_in_state));

result.res = push_agtype_value(&result.parse_state, WAGT_BEGIN_OBJECT,
NULL);
char *label;
agtype *properties;

/* process graph id */
result.res = push_agtype_value(&result.parse_state, WAGT_KEY,
string_to_agtype_value("id"));

if (fcinfo->args[0].isnull)
{
ereport(ERROR,
Expand All @@ -2283,27 +2269,17 @@ Datum _agtype_build_edge(PG_FUNCTION_ARGS)
}

id = AG_GETARG_GRAPHID(0);
result.res = push_agtype_value(&result.parse_state, WAGT_VALUE,
integer_to_agtype_value(id));

/* process label */
result.res = push_agtype_value(&result.parse_state, WAGT_KEY,
string_to_agtype_value("label"));

if (fcinfo->args[3].isnull)
{
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("_agtype_build_vertex() label cannot be NULL")));
}

result.res =
push_agtype_value(&result.parse_state, WAGT_VALUE,
string_to_agtype_value(PG_GETARG_CSTRING(3)));
label = PG_GETARG_CSTRING(3);

/* process end_id */
result.res = push_agtype_value(&result.parse_state, WAGT_KEY,
string_to_agtype_value("end_id"));

if (fcinfo->args[2].isnull)
{
ereport(ERROR,
Expand All @@ -2312,13 +2288,8 @@ Datum _agtype_build_edge(PG_FUNCTION_ARGS)
}

end_id = AG_GETARG_GRAPHID(2);
result.res = push_agtype_value(&result.parse_state, WAGT_VALUE,
integer_to_agtype_value(end_id));

/* process start_id */
result.res = push_agtype_value(&result.parse_state, WAGT_KEY,
string_to_agtype_value("start_id"));

if (fcinfo->args[1].isnull)
{
ereport(ERROR,
Expand All @@ -2327,40 +2298,47 @@ Datum _agtype_build_edge(PG_FUNCTION_ARGS)
}

start_id = AG_GETARG_GRAPHID(1);
result.res = push_agtype_value(&result.parse_state, WAGT_VALUE,
integer_to_agtype_value(start_id));

/* process properties */
result.res = push_agtype_value(&result.parse_state, WAGT_KEY,
string_to_agtype_value("properties"));

/* if the properties object is null, push an empty object */
if (fcinfo->args[4].isnull)
{
result.res = push_agtype_value(&result.parse_state, WAGT_BEGIN_OBJECT,
NULL);
result.res = push_agtype_value(&result.parse_state, WAGT_END_OBJECT,
NULL);
agtype_build_state *bstate = init_agtype_build_state(0, AGT_FOBJECT);
properties = build_agtype(bstate);
pfree_agtype_build_state(bstate);
}
else
{
agtype *properties = AG_GET_ARG_AGTYPE_P(4);
properties = AG_GET_ARG_AGTYPE_P(4);

if (!AGT_ROOT_IS_OBJECT(properties))
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("_agtype_build_edge() properties argument must be an object")));
}

add_agtype((Datum)properties, false, &result, AGTYPEOID, false);
}

result.res = push_agtype_value(&result.parse_state, WAGT_END_OBJECT, NULL);

result.res->type = AGTV_EDGE;

PG_RETURN_POINTER(agtype_value_to_agtype(result.res));
bstate = init_agtype_build_state(5, AGT_FOBJECT);
write_string(bstate, "id");
write_string(bstate, "label");
write_string(bstate, "end_id");
write_string(bstate, "start_id");
write_string(bstate, "properties");
write_graphid(bstate, id);
write_string(bstate, label);
write_graphid(bstate, end_id);
write_graphid(bstate, start_id);
write_container(bstate, properties);
edge = build_agtype(bstate);
pfree_agtype_build_state(bstate);

bstate = init_agtype_build_state(1, AGT_FARRAY | AGT_FSCALAR);
write_extended(bstate, edge, AGT_HEADER_EDGE);
rawscalar = build_agtype(bstate);
pfree_agtype_build_state(bstate);
PG_RETURN_POINTER(rawscalar);
}

Datum make_edge(Datum id, Datum startid, Datum endid, Datum label,
Expand Down
Loading