Skip to content

Commit

Permalink
Merge pull request #203 from cwida/41-allow-more-concise-syntax-when-…
Browse files Browse the repository at this point in the history
…creating-property-graph

41 allow more concise syntax when creating property graph
  • Loading branch information
Dtenwolde authored Sep 6, 2024
2 parents e1a145f + 179b0bf commit c55e59b
Show file tree
Hide file tree
Showing 6 changed files with 9,904 additions and 9,678 deletions.
32 changes: 20 additions & 12 deletions src/parser/transform/statement/transform_create_property_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,32 @@ Transformer::TransformPropertyGraphTable(duckdb_libpgquery::PGPropertyGraphTable
pg_table->destination_reference = possible_dst_alias->second;
}

for (auto &src_key = graph_table->src_pk->head; src_key != nullptr; src_key = lnext(src_key)) {
auto key = reinterpret_cast<duckdb_libpgquery::PGValue *>(src_key->data.ptr_value);
pg_table->source_pk.emplace_back(key->val.str);
if (graph_table->src_pk) {
for (auto &src_key = graph_table->src_pk->head; src_key != nullptr; src_key = lnext(src_key)) {
auto key = reinterpret_cast<duckdb_libpgquery::PGValue *>(src_key->data.ptr_value);
pg_table->source_pk.emplace_back(key->val.str);
}
}

for (auto &dst_key = graph_table->dst_pk->head; dst_key != nullptr; dst_key = lnext(dst_key)) {
auto key = reinterpret_cast<duckdb_libpgquery::PGValue *>(dst_key->data.ptr_value);
pg_table->destination_pk.emplace_back(key->val.str);
if (graph_table->dst_pk) {
for (auto &dst_key = graph_table->dst_pk->head; dst_key != nullptr; dst_key = lnext(dst_key)) {
auto key = reinterpret_cast<duckdb_libpgquery::PGValue *>(dst_key->data.ptr_value);
pg_table->destination_pk.emplace_back(key->val.str);
}
}

for (auto &src_key = graph_table->src_fk->head; src_key != nullptr; src_key = lnext(src_key)) {
auto key = reinterpret_cast<duckdb_libpgquery::PGValue *>(src_key->data.ptr_value);
pg_table->source_fk.emplace_back(key->val.str);
if (graph_table->src_fk) {
for (auto &src_key = graph_table->src_fk->head; src_key != nullptr; src_key = lnext(src_key)) {
auto key = reinterpret_cast<duckdb_libpgquery::PGValue *>(src_key->data.ptr_value);
pg_table->source_fk.emplace_back(key->val.str);
}
}

for (auto &dst_key = graph_table->dst_fk->head; dst_key != nullptr; dst_key = lnext(dst_key)) {
auto key = reinterpret_cast<duckdb_libpgquery::PGValue *>(dst_key->data.ptr_value);
pg_table->destination_fk.emplace_back(key->val.str);
if (graph_table->dst_fk) {
for (auto &dst_key = graph_table->dst_fk->head; dst_key != nullptr; dst_key = lnext(dst_key)) {
auto key = reinterpret_cast<duckdb_libpgquery::PGValue *>(dst_key->data.ptr_value);
pg_table->destination_fk.emplace_back(key->val.str);
}
}
}
return pg_table;
Expand Down
52 changes: 34 additions & 18 deletions third_party/libpg_query/grammar/statements/pgq.y
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,26 @@ KeySpecification:
'(' name_list ')' { $$ = $2; }
;

KeyDefinition:
KEY KeySpecification { $$ = $2; }
;

KeyReference:
KeyDefinition REFERENCES { $$ = $1; }
KEY KeySpecification REFERENCES qualified_name '(' name_list ')'
{
/* Case where both KEY (id) and REFERENCES (id) are provided */
PGKeyReference *key_ref = makeNode(PGKeyReference);
key_ref->key_columns = $2;
key_ref->ref_table = $4;
key_ref->ref_columns = $6;
$$ = (PGNode *) key_ref;
}
|
qualified_name
{
/* Case where neither KEY (id) nor REFERENCES (id) are provided */
PGKeyReference *key_ref = makeNode(PGKeyReference);
key_ref->key_columns = NULL;
key_ref->ref_table = $1;
key_ref->ref_columns = NULL;
$$ = (PGNode *) key_ref;
}
;

LabelList:
Expand Down Expand Up @@ -187,23 +201,25 @@ EdgeTableDefinitionList:

EdgeTableDefinition:
QualifiednameOptionalAs
SOURCE KeyReference qualified_name KeySpecification
DESTINATION KeyReference qualified_name KeySpecification
SOURCE KeyReference
DESTINATION KeyReference
PropertiesClause LabelOptional Discriminator
{
PGPropertyGraphTable *n = (PGPropertyGraphTable*) $12;
PGPropertyGraphTable *n = (PGPropertyGraphTable*) $8;
n->table = $1;
n->is_vertex_table = false;
n->src_fk = $3;
n->src_name = $4;
n->src_pk = $5;
n->dst_fk = $7;
n->dst_name = $8;
n->dst_pk = $9;
n->properties = $10;
/* Xth label in list is set iff discriminator Xth-bit==1 */
if (n->labels) n->labels = lappend(n->labels,makeString($11));
else n->labels = list_make1(makeString($11));
PGKeyReference *src_key_ref = (PGKeyReference *) $3;
n->src_fk = src_key_ref->key_columns;
n->src_name = src_key_ref->ref_table;
n->src_pk = src_key_ref->ref_columns;
PGKeyReference *dst_key_ref = (PGKeyReference *) $5;
n->dst_fk = dst_key_ref->key_columns;
n->dst_name = dst_key_ref->ref_table;
n->dst_pk = dst_key_ref->ref_columns;
n->properties = $6;
/* Handle labels and discriminator as before */
if (n->labels) n->labels = lappend(n->labels, makeString($7));
else n->labels = list_make1(makeString($7));
$$ = (PGNode *) n;
}
;
Expand Down
3 changes: 1 addition & 2 deletions third_party/libpg_query/grammar/types/pgq.yh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
%type <list> VertexTableDefinitionList
%type <list> LabelList
%type <list> KeySpecification
%type <list> KeyDefinition
%type <list> KeyReference
%type <node> KeyReference
%type <node> Discriminator
%type <node> VertexTableDefinition
%type <list> EdgeTableDefinitionList
Expand Down
1 change: 1 addition & 0 deletions third_party/libpg_query/include/nodes/nodes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ typedef enum PGNodeTag {
T_PGPathInfo, /* SQL/PGQ extension */
T_PGLabelTest, /* SQL/PGQ extension */
T_PGPathElement,/* SQL/PGQ extension */
T_PGKeyReference, /* SQL/PGQ extension */

/*
* TAGS FOR REPLICATION GRAMMAR PARSE NODES (replnodes.h)
Expand Down
7 changes: 7 additions & 0 deletions third_party/libpg_query/include/nodes/parsenodes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2203,6 +2203,13 @@ typedef struct PGCreatePropertyGraphStmt {
PGOnCreateConflict onconflict; /* what to do on create conflict */
} PGCreatePropertyGraphStmt;

typedef struct PGKeyReference {
PGNodeTag type;
PGList *key_columns; /* List of key column names (optional) */
PGRangeVar *ref_table; /* The referenced table (mandatory) */
PGList *ref_columns; /* List of referenced columns (optional) */
} PGKeyReference;

typedef struct PGPropertyGraphTable {
PGNodeTag type;

Expand Down
Loading

0 comments on commit c55e59b

Please sign in to comment.