Skip to content

Commit 57fee23

Browse files
committed
Implement CREATE GRAPH
First draft of create graph logic.
1 parent fe5ba88 commit 57fee23

File tree

15 files changed

+131
-60
lines changed

15 files changed

+131
-60
lines changed

Makefile

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -86,42 +86,7 @@ EXTENSION = postgraph
8686

8787
DATA = postgraph--0.1.0.sql
8888

89-
# sorted in dependency order
90-
REGRESS = graphid \
91-
gtype \
92-
network \
93-
temporal \
94-
range \
95-
vertex \
96-
edge \
97-
variable_edge \
98-
traversal \
99-
catalog \
100-
cypher \
101-
lists\
102-
expr \
103-
vector \
104-
cypher_create \
105-
cypher_match \
106-
cypher_unwind \
107-
cypher_set \
108-
cypher_remove \
109-
cypher_delete \
110-
cypher_with \
111-
cypher_vle \
112-
cypher_union \
113-
cypher_merge \
114-
cypher_call \
115-
order_by \
116-
regex \
117-
tsearch \
118-
aggregation \
119-
traversal_functions \
120-
variable_edge_functions \
121-
geometric \
122-
postgis \
123-
index \
124-
drop
89+
REGRESS = new_cypher
12590

12691
srcdir=`pwd`
12792
POSTGIS_DIR ?= postgis_dir

regress/expected/new_cypher.out

Whitespace-only changes.

regress/sql/new_cypher.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
LOAD 'postgraph';
2+
3+
CREATE GRAPH new_cypher;
4+
5+
CREATE (n);

sql/postgraph.sql.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ AS 'MODULE_PATHNAME';
9999
--
100100
-- utility functions
101101
--
102-
CREATE FUNCTION create_graph(graph_name name)
102+
CREATE FUNCTION create_graph(graph_name text)
103103
RETURNS void
104104
LANGUAGE c
105105
AS 'MODULE_PATHNAME';

src/backend/commands/graph_commands.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "parser/parser.h"
3838
#include "utils/rel.h"
3939
#include "utils/relcache.h"
40+
#include "utils/builtins.h"
4041

4142
#include "catalog/ag_graph.h"
4243
#include "catalog/ag_label.h"
@@ -49,7 +50,7 @@
4950
*/
5051
#define gen_graph_namespace_name(graph_name) (graph_name)
5152

52-
static Oid create_schema_for_graph(const Name graph_name);
53+
static Oid create_schema_for_graph(const char *graph_name);
5354
static void drop_schema_for_graph(char *graph_name_str, const bool cascade);
5455
static void remove_schema(Node *schema_name, DropBehavior behavior);
5556
static void rename_graph(const Name graph_name, const Name new_name);
@@ -105,41 +106,42 @@ Datum create_graph(PG_FUNCTION_ARGS)
105106
Oid nsp_id;
106107

107108
if (PG_ARGISNULL(0))
108-
{
109109
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
110110
errmsg("graph name must not be NULL")));
111-
}
111+
112+
/*
112113
graph_name = PG_GETARG_NAME(0);
113114
114-
graph_name_str = NameStr(*graph_name);
115+
graph_name_str = NameStr(*graph_name);*/
116+
graph_name_str = TextDatumGetCString(PG_GETARG_DATUM(0));
115117
if (graph_exists(graph_name_str))
116-
{
117118
ereport(ERROR,
118119
(errcode(ERRCODE_UNDEFINED_SCHEMA),
119120
errmsg("graph \"%s\" already exists", graph_name_str)));
120-
}
121121

122-
nsp_id = create_schema_for_graph(graph_name);
122+
nsp_id = create_schema_for_graph(graph_name_str);
123123

124-
insert_graph(graph_name, nsp_id);
124+
insert_graph(graph_name_str, nsp_id);
125125

126126
//Increment the Command counter before create the generic labels.
127127
CommandCounterIncrement();
128128

129129
//Create the default label tables
130-
graph = graph_name->data;
130+
graph = graph_name_str;//graph_name->data;
131131
create_label(graph, AG_DEFAULT_LABEL_VERTEX, LABEL_TYPE_VERTEX, NIL);
132132
create_label(graph, AG_DEFAULT_LABEL_EDGE, LABEL_TYPE_EDGE, NIL);
133133

134134
ereport(NOTICE,
135-
(errmsg("graph \"%s\" has been created", NameStr(*graph_name))));
135+
(errmsg("graph \"%s\" has been created", graph_name_str)));
136136

137+
PopActiveSnapshot();
138+
137139
PG_RETURN_VOID();
138140
}
139141

140-
static Oid create_schema_for_graph(const Name graph_name)
142+
static Oid create_schema_for_graph(const char * graph_name)
141143
{
142-
char *graph_name_str = NameStr(*graph_name);
144+
char *graph_name_str = graph_name;
143145
CreateSchemaStmt *schema_stmt;
144146
CreateSeqStmt *seq_stmt;
145147
TypeName *integer;

src/backend/nodes/ag_nodes.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ const char *node_names[] = {
6262
"cypher_update_item",
6363
"cypher_delete_information",
6464
"cypher_delete_item",
65-
"cypher_merge_information"
65+
"cypher_merge_information",
66+
"cypher_create_graph"
6667
};
6768

6869
/*
@@ -127,7 +128,8 @@ const ExtensibleNodeMethods node_methods[] = {
127128
DEFINE_NODE_METHODS_EXTENDED(cypher_update_item),
128129
DEFINE_NODE_METHODS_EXTENDED(cypher_delete_information),
129130
DEFINE_NODE_METHODS_EXTENDED(cypher_delete_item),
130-
DEFINE_NODE_METHODS_EXTENDED(cypher_merge_information)
131+
DEFINE_NODE_METHODS_EXTENDED(cypher_merge_information),
132+
DEFINE_NODE_METHODS(cypher_create_graph),
131133
};
132134

133135
static bool equal_ag_node(const ExtensibleNode *a, const ExtensibleNode *b)

src/backend/nodes/cypher_outfuncs.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,14 @@ void out_cypher_merge_information(StringInfo str, const ExtensibleNode *node)
427427
WRITE_NODE_FIELD(path);
428428
}
429429

430+
// serialization function for the cypher_node ExtensibleNode.
431+
void out_cypher_create_graph(StringInfo str, const ExtensibleNode *node)
432+
{
433+
DEFINE_AG_NODE(cypher_create_graph);
434+
435+
WRITE_STRING_FIELD(graph_name);
436+
}
437+
430438
/*
431439
* Copied from Postgres
432440
*

src/backend/nodes/cypher_readfuncs.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,11 @@ void read_cypher_merge_information(struct ExtensibleNode *node)
299299
READ_INT_FIELD(merge_function_attr);
300300
READ_NODE_FIELD(path);
301301
}
302+
303+
304+
void read_cypher_create_graph(struct ExtensibleNode *node)
305+
{
306+
READ_LOCALS(cypher_create_graph);
307+
308+
READ_STRING_FIELD(graph_name);
309+
}

src/backend/parser/cypher_analyze.c

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ makeRawStmt(Node *stmt, int stmt_location)
103103
static List *cypher_parse(char *string){
104104
List *raw_parsetree_list;
105105

106-
TRACE_POSTGRESQL_QUERY_PARSE_START(string);
106+
//TRACE_POSTGRESQL_QUERY_PARSE_START(string);
107107

108-
if (log_parser_stats)
109-
ResetUsage();
108+
//if (log_parser_stats)
109+
//ResetUsage();
110110
/*
111111
raw_parsetree_list = raw_parser(string, RAW_PARSE_DEFAULT);
112112
@@ -148,7 +148,7 @@ static List *cypher_parse(char *string){
148148
* here.
149149
*/
150150

151-
TRACE_POSTGRESQL_QUERY_PARSE_DONE(string);
151+
//TRACE_POSTGRESQL_QUERY_PARSE_DONE(string);
152152

153153
return raw_parsetree_list;
154154
}
@@ -162,6 +162,39 @@ void parse_analyze_fini(void){
162162
parse_analyze_hook = NULL;
163163
}
164164

165+
/*
166+
* Creates the function expression that represents the clause. Adds the
167+
* extensible node that represents the metadata that the clause needs to
168+
* handle the clause in the execution phase.
169+
*/
170+
static FuncExpr *make_clause_create_graph_func_expr(char *graph_name) {
171+
Const *c = makeConst(TEXTOID, -1, InvalidOid, strlen(graph_name), CStringGetTextDatum(graph_name), false, false);
172+
173+
Oid func_oid = get_ag_func_oid("create_graph", 1, TEXTOID);
174+
175+
return makeFuncExpr(func_oid, VOIDOID, list_make1(c), InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);
176+
}
177+
178+
static Query *
179+
cypher_create_graph_utility(ParseState *pstate, const char *graph_name) {
180+
Query *query;
181+
TargetEntry *tle;
182+
FuncExpr *func_expr;
183+
184+
query = makeNode(Query);
185+
query->commandType = CMD_SELECT;
186+
query->targetList = NIL;
187+
188+
func_expr = make_clause_create_graph_func_expr(graph_name);
189+
190+
// Create the target entry
191+
tle = makeTargetEntry((Expr *)func_expr, pstate->p_next_resno++, "create_graph", false);
192+
query->targetList = lappend(query->targetList, tle);
193+
194+
query->rtable = pstate->p_rtable;
195+
query->jointree = makeFromExpr(pstate->p_joinlist, NULL);
196+
return query;
197+
}
165198

166199
/*
167200
* parse_analyze
@@ -193,6 +226,28 @@ cypher_parse_analyze(RawStmt *parseTree, const char *sourceText,
193226
pstate->p_queryEnv = queryEnv;
194227

195228
//query = transformTopLevelStmt(pstate, parseTree);
229+
if (list_length(parseTree->stmt) == 1) {
230+
Node *n = linitial(parseTree->stmt);
231+
232+
if (is_ag_node(n, cypher_create_graph)) {
233+
cypher_create_graph *ccg = n;
234+
//ereport(ERROR, errmsg("Here"));
235+
236+
query = cypher_create_graph_utility(pstate, ccg->graph_name);
237+
238+
query->canSetTag = true;
239+
240+
if (IsQueryIdEnabled())
241+
jstate = JumbleQuery(query, sourceText);
242+
243+
free_parsestate(pstate);
244+
245+
pgstat_report_query_id(query->queryId, false);
246+
PushActiveSnapshot(GetTransactionSnapshot());
247+
return query;
248+
249+
}
250+
}
196251
query = analyze_cypher(parseTree->stmt, pstate, sourceText, 0, NULL, CurrentGraphOid, NULL);
197252

198253
if (IsQueryIdEnabled())

src/backend/parser/cypher_gram.y

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
CALL CASE COALESCE CONTAINS CREATE CUBE CURRENT CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP
9090
DATE DECADE DELETE DESC DESCENDING DETACH DISTINCT
9191
ELSE END_P ENDS EXCEPT EXCLUDE EXISTS EXTRACT
92-
GROUP GROUPS GROUPING
92+
GRAPH GROUP GROUPS GROUPING
9393
FALSE_P FILTER FIRST_P FOLLOWING FROM
9494
HAVING
9595
ILIKE IN INTERSECT INTERVAL IS
@@ -252,8 +252,8 @@ stmt:
252252
*
253253
* Throw syntax error in this case.
254254
*/
255-
if (yychar != YYEOF)
256-
yyerror(&yylloc, scanner, extra, "syntax error");
255+
//if (yychar != YYEOF)
256+
//yyerror(&yylloc, scanner, extra, "syntax error");
257257

258258
extra->result = $1;
259259
}
@@ -385,6 +385,7 @@ yield_item:
385385
semicolon_opt:
386386
/* empty */
387387
| ';'
388+
388389
;
389390

390391
all_or_distinct:
@@ -922,7 +923,6 @@ unwind:
922923
/*
923924
* CREATE clause
924925
*/
925-
926926
create:
927927
CREATE pattern
928928
{
@@ -931,6 +931,15 @@ create:
931931
n = make_ag_node(cypher_create);
932932
n->pattern = $2;
933933

934+
$$ = (Node *)n;
935+
}
936+
| CREATE GRAPH IDENTIFIER
937+
{
938+
939+
cypher_create_graph *n;
940+
n = make_ag_node(cypher_create_graph);
941+
n->graph_name = $3;
942+
934943
$$ = (Node *)n;
935944
}
936945
;

0 commit comments

Comments
 (0)