Skip to content

Commit 8f5d538

Browse files
jrgemignaniMuhammadTahaNaveed
authored andcommitted
Add fast functions for checking edge uniqueness (#2227)
Added fast functions for checking edge uniqueness. This will help improve performance for MATCH queries with paths longer than 3 but less than 11. The normal edge uniqueness function will deal with any path 11 and over. modified: age--1.6.0--y.y.y.sql modified: sql/agtype_graphid.sql modified: src/backend/parser/cypher_clause.c modified: src/backend/utils/adt/age_vle.c
1 parent 6a85d93 commit 8f5d538

4 files changed

Lines changed: 118 additions & 5 deletions

File tree

age--1.6.0--y.y.y.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,23 @@
3333
--* file. We need to keep the order of these changes.
3434
--* REMOVE ALL LINES ABOVE, and this one, that start with --*
3535

36+
CREATE FUNCTION ag_catalog._ag_enforce_edge_uniqueness2(graphid, graphid)
37+
RETURNS bool
38+
LANGUAGE c
39+
STABLE
40+
PARALLEL SAFE
41+
as 'MODULE_PATHNAME';
42+
43+
CREATE FUNCTION ag_catalog._ag_enforce_edge_uniqueness3(graphid, graphid, graphid)
44+
RETURNS bool
45+
LANGUAGE c
46+
STABLE
47+
PARALLEL SAFE
48+
as 'MODULE_PATHNAME';
49+
50+
CREATE FUNCTION ag_catalog._ag_enforce_edge_uniqueness4(graphid, graphid, graphid, graphid)
51+
RETURNS bool
52+
LANGUAGE c
53+
STABLE
54+
PARALLEL SAFE
55+
as 'MODULE_PATHNAME';

sql/agtype_graphid.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ CALLED ON NULL INPUT
7777
PARALLEL SAFE
7878
AS 'MODULE_PATHNAME';
7979

80+
CREATE FUNCTION ag_catalog._ag_enforce_edge_uniqueness2(graphid, graphid)
81+
RETURNS bool
82+
LANGUAGE c
83+
STABLE
84+
PARALLEL SAFE
85+
as 'MODULE_PATHNAME';
86+
87+
CREATE FUNCTION ag_catalog._ag_enforce_edge_uniqueness3(graphid, graphid, graphid)
88+
RETURNS bool
89+
LANGUAGE c
90+
STABLE
91+
PARALLEL SAFE
92+
as 'MODULE_PATHNAME';
93+
94+
CREATE FUNCTION ag_catalog._ag_enforce_edge_uniqueness4(graphid, graphid, graphid, graphid)
95+
RETURNS bool
96+
LANGUAGE c
97+
STABLE
98+
PARALLEL SAFE
99+
as 'MODULE_PATHNAME';
100+
80101
CREATE FUNCTION ag_catalog._ag_enforce_edge_uniqueness(VARIADIC "any")
81102
RETURNS bool
82103
LANGUAGE c

src/backend/parser/cypher_clause.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3271,13 +3271,13 @@ static FuncCall *prevent_duplicate_edges(cypher_parsestate *cpstate,
32713271
{
32723272
List *edges = NIL;
32733273
ListCell *lc;
3274-
List *qualified_function_name;
3275-
String *ag_catalog, *edge_fn;
3274+
List *qualified_function_name = NULL;
3275+
String *ag_catalog;
3276+
String *edge_fn = NULL;
3277+
bool is_vle_edge = false;
3278+
int nentities = list_length(entities);
32763279

32773280
ag_catalog = makeString("ag_catalog");
3278-
edge_fn = makeString("_ag_enforce_edge_uniqueness");
3279-
3280-
qualified_function_name = list_make2(ag_catalog, edge_fn);
32813281

32823282
/* iterate through each entity, collecting the access node for each edge */
32833283
foreach (lc, entities)
@@ -3293,10 +3293,33 @@ static FuncCall *prevent_duplicate_edges(cypher_parsestate *cpstate,
32933293
}
32943294
else if (entity->type == ENT_VLE_EDGE)
32953295
{
3296+
is_vle_edge = true;
32963297
edges = lappend(edges, entity->expr);
32973298
}
32983299
}
32993300

3301+
if (!is_vle_edge && (nentities >= 5 && nentities <= 9))
3302+
{
3303+
if (nentities == 5)
3304+
{
3305+
edge_fn = makeString("_ag_enforce_edge_uniqueness2");
3306+
}
3307+
else if (nentities == 7)
3308+
{
3309+
edge_fn = makeString("_ag_enforce_edge_uniqueness3");
3310+
}
3311+
else
3312+
{
3313+
edge_fn = makeString("_ag_enforce_edge_uniqueness4");
3314+
}
3315+
}
3316+
else
3317+
{
3318+
edge_fn = makeString("_ag_enforce_edge_uniqueness");
3319+
}
3320+
3321+
qualified_function_name = list_make2(ag_catalog, edge_fn);
3322+
33003323
return makeFuncCall(qualified_function_name, edges, COERCE_SQL_SYNTAX, -1);
33013324
}
33023325

src/backend/utils/adt/age_vle.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,6 +2427,55 @@ Datum age_build_vle_match_edge(PG_FUNCTION_ARGS)
24272427
PG_RETURN_POINTER(agtype_value_to_agtype(result.res));
24282428
}
24292429

2430+
PG_FUNCTION_INFO_V1(_ag_enforce_edge_uniqueness2);
2431+
2432+
Datum _ag_enforce_edge_uniqueness2(PG_FUNCTION_ARGS)
2433+
{
2434+
graphid gid1 = AG_GETARG_GRAPHID(0);
2435+
graphid gid2 = AG_GETARG_GRAPHID(1);
2436+
2437+
if (gid1 == gid2)
2438+
{
2439+
PG_RETURN_BOOL(false);
2440+
}
2441+
2442+
PG_RETURN_BOOL(true);
2443+
}
2444+
2445+
PG_FUNCTION_INFO_V1(_ag_enforce_edge_uniqueness3);
2446+
2447+
Datum _ag_enforce_edge_uniqueness3(PG_FUNCTION_ARGS)
2448+
{
2449+
graphid gid1 = AG_GETARG_GRAPHID(0);
2450+
graphid gid2 = AG_GETARG_GRAPHID(1);
2451+
graphid gid3 = AG_GETARG_GRAPHID(2);
2452+
2453+
if (gid1 == gid2 || gid1 == gid3 || gid2 == gid3)
2454+
{
2455+
PG_RETURN_BOOL(false);
2456+
}
2457+
2458+
PG_RETURN_BOOL(true);
2459+
}
2460+
2461+
PG_FUNCTION_INFO_V1(_ag_enforce_edge_uniqueness4);
2462+
2463+
Datum _ag_enforce_edge_uniqueness4(PG_FUNCTION_ARGS)
2464+
{
2465+
graphid gid1 = AG_GETARG_GRAPHID(0);
2466+
graphid gid2 = AG_GETARG_GRAPHID(1);
2467+
graphid gid3 = AG_GETARG_GRAPHID(2);
2468+
graphid gid4 = AG_GETARG_GRAPHID(3);
2469+
2470+
if (gid1 == gid2 || gid1 == gid3 || gid1 == gid4 ||
2471+
gid2 == gid3 || gid2 == gid4 || gid3 == gid4)
2472+
{
2473+
PG_RETURN_BOOL(false);
2474+
}
2475+
2476+
PG_RETURN_BOOL(true);
2477+
}
2478+
24302479
/*
24312480
* This function checks the edges in a MATCH clause to see if they are unique or
24322481
* not. Filters out all the paths where the edge uniques rules are not met.

0 commit comments

Comments
 (0)