Skip to content

Commit ec45734

Browse files
authored
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 9c370f4 commit ec45734

File tree

4 files changed

+118
-5
lines changed

4 files changed

+118
-5
lines changed

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

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

34+
CREATE FUNCTION ag_catalog._ag_enforce_edge_uniqueness2(graphid, graphid)
35+
RETURNS bool
36+
LANGUAGE c
37+
STABLE
38+
PARALLEL SAFE
39+
as 'MODULE_PATHNAME';
40+
41+
CREATE FUNCTION ag_catalog._ag_enforce_edge_uniqueness3(graphid, graphid, graphid)
42+
RETURNS bool
43+
LANGUAGE c
44+
STABLE
45+
PARALLEL SAFE
46+
as 'MODULE_PATHNAME';
47+
48+
CREATE FUNCTION ag_catalog._ag_enforce_edge_uniqueness4(graphid, graphid, graphid, graphid)
49+
RETURNS bool
50+
LANGUAGE c
51+
STABLE
52+
PARALLEL SAFE
53+
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
@@ -3276,13 +3276,13 @@ static FuncCall *prevent_duplicate_edges(cypher_parsestate *cpstate,
32763276
{
32773277
List *edges = NIL;
32783278
ListCell *lc;
3279-
List *qualified_function_name;
3280-
String *ag_catalog, *edge_fn;
3279+
List *qualified_function_name = NULL;
3280+
String *ag_catalog;
3281+
String *edge_fn = NULL;
3282+
bool is_vle_edge = false;
3283+
int nentities = list_length(entities);
32813284

32823285
ag_catalog = makeString("ag_catalog");
3283-
edge_fn = makeString("_ag_enforce_edge_uniqueness");
3284-
3285-
qualified_function_name = list_make2(ag_catalog, edge_fn);
32863286

32873287
/* iterate through each entity, collecting the access node for each edge */
32883288
foreach (lc, entities)
@@ -3298,10 +3298,33 @@ static FuncCall *prevent_duplicate_edges(cypher_parsestate *cpstate,
32983298
}
32993299
else if (entity->type == ENT_VLE_EDGE)
33003300
{
3301+
is_vle_edge = true;
33013302
edges = lappend(edges, entity->expr);
33023303
}
33033304
}
33043305

3306+
if (!is_vle_edge && (nentities >= 5 && nentities <= 9))
3307+
{
3308+
if (nentities == 5)
3309+
{
3310+
edge_fn = makeString("_ag_enforce_edge_uniqueness2");
3311+
}
3312+
else if (nentities == 7)
3313+
{
3314+
edge_fn = makeString("_ag_enforce_edge_uniqueness3");
3315+
}
3316+
else
3317+
{
3318+
edge_fn = makeString("_ag_enforce_edge_uniqueness4");
3319+
}
3320+
}
3321+
else
3322+
{
3323+
edge_fn = makeString("_ag_enforce_edge_uniqueness");
3324+
}
3325+
3326+
qualified_function_name = list_make2(ag_catalog, edge_fn);
3327+
33053328
return makeFuncCall(qualified_function_name, edges, COERCE_SQL_SYNTAX, -1);
33063329
}
33073330

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)