Skip to content

Commit 1f23178

Browse files
committed
Start Vertex can be a variable
1 parent 73df6c3 commit 1f23178

5 files changed

Lines changed: 44 additions & 20 deletions

File tree

postgraph--0.1.0.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ CREATE FUNCTION build_vertex(graphid, oid, gtype)
314314
RETURNS vertex
315315
LANGUAGE c
316316
IMMUTABLE
317-
RETURNS NULL ON NULL INPUT
317+
CALLED ON NULL INPUT
318318
PARALLEL SAFE
319319
COST 50
320320
AS 'MODULE_PATHNAME';

regress/expected/cypher_create.out

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ MATCH () RETURN 1;
135135
1
136136
(3 rows)
137137

138+
MATCH (a) RETURN a;
139+
a
140+
--------------------------------------------------------------
141+
{"id": 281474976710657, "label": "", "properties": {}}
142+
{"id": 1125899906842625, "label": "test", "properties": {}}
143+
{"id": 1688849860263937, "label": "test2", "properties": {}}
144+
(3 rows)
145+
138146
CREATE ()-[]->();
139147
WARNING: relcache reference leak: relation "_ag_label_vertex" not closed
140148
WARNING: relcache reference leak: relation "_ag_label_vertex" not closed
@@ -156,14 +164,14 @@ EXPLAIN MATCH ()-[]->() RETURN 1;
156164
EXPLAIN ANALYZE MATCH ()-[]->() RETURN 1;
157165
QUERY PLAN
158166
-----------------------------------------------------------------------------------------------------------------------------------------------
159-
Nested Loop (cost=12.50..50471.30 rows=2520000 width=32) (actual time=0.060..0.237 rows=1 loops=1)
167+
Nested Loop (cost=12.50..50471.30 rows=2520000 width=32) (actual time=0.046..0.207 rows=1 loops=1)
160168
-> Append (cost=0.00..58.80 rows=2520 width=8) (actual time=0.005..0.013 rows=5 loops=1)
161169
-> Seq Scan on _ag_label_vertex _age_default_alias_0_1 (cost=0.00..2.20 rows=120 width=8) (actual time=0.005..0.006 rows=3 loops=1)
162170
-> Seq Scan on test _age_default_alias_0_2 (cost=0.00..22.00 rows=1200 width=8) (actual time=0.001..0.002 rows=1 loops=1)
163171
-> Seq Scan on test2 _age_default_alias_0_3 (cost=0.00..22.00 rows=1200 width=8) (actual time=0.002..0.002 rows=1 loops=1)
164-
-> Function Scan on edge_search _age_default_alias_1 (cost=12.50..22.50 rows=1000 width=0) (actual time=0.043..0.043 rows=0 loops=5)
165-
Planning Time: 0.095 ms
166-
Execution Time: 0.262 ms
172+
-> Function Scan on edge_search _age_default_alias_1 (cost=12.50..22.50 rows=1000 width=0) (actual time=0.037..0.037 rows=0 loops=5)
173+
Planning Time: 0.093 ms
174+
Execution Time: 0.231 ms
167175
(8 rows)
168176

169177
MATCH ()-[]->() RETURN 1;
@@ -185,13 +193,13 @@ SELECT * FROM cypher_create._ag_label_vertex;
185193
SELECT * FROM cypher_create._adj__adj__ag_label_vertex;
186194
id | start_id | end_id | properties
187195
---------------+----------+--------+------------
188-
(0, 41914048) | (1, 2) | (1, 3) |
196+
(0, 25304768) | (1, 2) | (1, 3) |
189197
(1 row)
190198

191199
SELECT * FROM cypher_create._adj__ag_label_vertex;
192200
id | start_id | end_id | properties
193201
---------------+----------+--------+------------
194-
(0, 41914048) | (1, 2) | (1, 3) |
202+
(0, 25304768) | (1, 2) | (1, 3) |
195203
(1 row)
196204

197205
MATCH () RETURN 1;

regress/sql/cypher_create.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ SELECT * FROM cypher_create.test2;
4444

4545
MATCH () RETURN 1;
4646

47+
MATCH (a) RETURN a;
48+
4749
CREATE ()-[]->();
4850

4951
EXPLAIN MATCH ()-[]->() RETURN 1;

src/backend/parser/cypher_clause.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,10 +1182,10 @@ static void transform_match_pattern(cypher_parsestate *cpstate, Query *query, Li
11821182
if (i % 2 == 1) {
11831183
cypher_node *node = lfirst(path_cell);
11841184
prev_node = node;
1185+
bool has_variable = false;
1186+
11851187
if (node->name)
1186-
ereport(ERROR,
1187-
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1188-
errmsg("MATCH variable names are not supported")));
1188+
has_variable = true;
11891189
else
11901190
node->name = get_next_default_alias(cpstate);
11911191

@@ -1260,11 +1260,17 @@ static void transform_match_pattern(cypher_parsestate *cpstate, Query *query, Li
12601260

12611261
//resno = pstate->p_next_resno++;
12621262
// XXX: End LTree Code here
1263+
expr = (Expr *)make_vertex_expr(cpstate, pnsi);
1264+
// make target entry and add it
1265+
resno = pstate->p_next_resno++;
1266+
te = makeTargetEntry(expr, resno, node->name, false);
1267+
1268+
query->targetList = lappend(query->targetList, te);
12631269

12641270
//
12651271
} else {
12661272

1267-
if (node->props) {
1273+
if (node->props || has_variable) {
12681274
int sublevels_up =1;
12691275
Node * id_field = scanNSItemForColumn(cpstate, edge_pnsi, 0, "endid", -1);
12701276

@@ -1277,12 +1283,6 @@ static void transform_match_pattern(cypher_parsestate *cpstate, Query *query, Li
12771283
}
12781284
//edge_pnsi = pnsi;
12791285
}
1280-
//expr = (Expr *)make_vertex_expr(cpstate, pnsi);
1281-
// make target entry and add it
1282-
// pstate->p_next_resno++;
1283-
//te = makeTargetEntry(expr, resno, node->name, false);
1284-
1285-
//query->targetList = lappend(query->targetList, te);
12861286

12871287
// id field
12881288
Node *id = scanNSItemForColumn(pstate, pnsi, 0, AG_VERTEX_COLNAME_ID, -1);

src/backend/utils/adt/vertex.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,13 @@ Datum
101101
build_vertex(PG_FUNCTION_ARGS) {
102102
graphid id = AG_GETARG_GRAPHID(0);
103103
Oid graph_oid = PG_GETARG_OID(1);
104-
gtype *properties = AG_GET_ARG_GTYPE_P(2);
104+
gtype *properties = NULL;
105+
if (!PG_ARGISNULL(2)) {
106+
properties = AG_GET_ARG_GTYPE_P(2);
105107

106-
if (!AGT_ROOT_IS_OBJECT(properties))
107-
PG_RETURN_NULL();
108+
if (!AGT_ROOT_IS_OBJECT(properties))
109+
PG_RETURN_NULL();
110+
}
108111

109112
AG_RETURN_VERTEX(create_vertex(id, graph_oid, properties));
110113
}
@@ -124,6 +127,17 @@ create_vertex(graphid id, Oid graph_oid, gtype *properties) {
124127
append_to_buffer(&buffer, (char *)&graph_oid, sizeof(Oid));
125128

126129
// properties
130+
if (properties == NULL) {
131+
gtype_in_state result;
132+
133+
memset(&result, 0, sizeof(gtype_in_state));
134+
135+
push_gtype_value(&result.parse_state, WGT_BEGIN_OBJECT, NULL);
136+
result.res = push_gtype_value(&result.parse_state, WGT_END_OBJECT, NULL);
137+
138+
properties = gtype_value_to_gtype(result.res);
139+
}
140+
127141
append_to_buffer(&buffer, properties, VARSIZE(properties));
128142

129143
vertex *v = (vertex *)buffer.data;

0 commit comments

Comments
 (0)