Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ANY SHORTEST #88

Merged
merged 13 commits into from
Aug 24, 2023
26 changes: 26 additions & 0 deletions src/include/duckdb/common/field_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ class FieldWriter {
}
}

template <class T>
void WriteSerializableList(const vector<shared_ptr<T>> &elements) {
AddField();
Write<uint32_t>(elements.size());
for (idx_t i = 0; i < elements.size(); i++) {
elements[i]->Serialize(*buffer);
}
}

template <class T>
void WriteRegularSerializableList(const vector<T> &elements) {
AddField();
Expand Down Expand Up @@ -361,6 +370,23 @@ class FieldReader {
return result;
}

template <class T, class RETURN_TYPE = shared_ptr<T>, typename... ARGS>
vector<RETURN_TYPE> ReadRequiredSharedSerializableList(ARGS &&... args) {
if (field_count >= max_field_count) {
// field is not there, throw an exception
throw SerializationException("Attempting to read mandatory field, but field is missing");
}
// field is there, read the actual value
AddField();
auto result_count = source.Read<uint32_t>();

vector<RETURN_TYPE> result;
for (idx_t i = 0; i < result_count; i++) {
result.push_back(T::Deserialize(source, std::forward<ARGS>(args)...));
}
return result;
}

template <class T, class RETURN_TYPE = T *, typename... ARGS>
case_insensitive_map_t<RETURN_TYPE> ReadRequiredSerializableMap(ARGS &&... args) {
if (field_count >= max_field_count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,14 @@ struct CreatePropertyGraphInfo : public CreateInfo {
//! Property graph name
string property_graph_name;
//! List of vector tables
vector<unique_ptr<PropertyGraphTable>> vertex_tables;
vector<shared_ptr<PropertyGraphTable>> vertex_tables;

vector<unique_ptr<PropertyGraphTable>> edge_tables;
vector<shared_ptr<PropertyGraphTable>> edge_tables;

//! Dictionary to point label to vector or edge table
case_insensitive_map_t<PropertyGraphTable *> label_map;
case_insensitive_map_t<shared_ptr<PropertyGraphTable>> label_map;

public:
// string ToString() const;
// bool Equals(const BaseExpression *other_p) const;
// void SerializeInternal(Serializer &serializer) const override;
unique_ptr<CreateInfo> Copy() const override;

//! Serializes a blob into a CreatePropertyGraphInfo
Expand Down
4 changes: 1 addition & 3 deletions src/include/duckdb/parser/path_pattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class PathPattern {
bool group = false;
int32_t topk = false;

PathPattern() {

};
PathPattern() = default;

unique_ptr<PathPattern> Copy();

Expand Down
4 changes: 2 additions & 2 deletions src/include/duckdb/parser/property_graph_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ class PropertyGraphTable {
string ToString() const;
bool Equals(const PropertyGraphTable *other_p) const;

unique_ptr<PropertyGraphTable> Copy() const;
shared_ptr<PropertyGraphTable> Copy() const;

void Serialize(Serializer &serializer) const;

static unique_ptr<PropertyGraphTable> Deserialize(Deserializer &deserializer);
static shared_ptr<PropertyGraphTable> Deserialize(Deserializer &deserializer);
};
} // namespace duckdb
6 changes: 6 additions & 0 deletions src/include/duckdb/parser/subpath_element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ class SubPath : public PathReference {
bool single_bind;
int64_t lower, upper;

string path_variable;

// TODO cost_expr, default_value
public:
explicit SubPath(PGQPathReferenceType path_reference_type) : PathReference(path_reference_type) {
path_mode = PGQPathMode::NONE;
single_bind = true;
lower = 1;
upper = 1;
}

string ToString() const override;
Expand Down
2 changes: 1 addition & 1 deletion src/include/duckdb/parser/tableref/matchref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MatchExpression : public ParsedExpression {

string pg_name;
string alias;
vector<unique_ptr<PathPattern>> path_list;
vector<unique_ptr<PathPattern>> path_patterns;

vector<unique_ptr<ParsedExpression>> column_list;

Expand Down
4 changes: 2 additions & 2 deletions src/include/duckdb/parser/transformer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,12 @@ class Transformer {
// SQL/PGQ Property graph transform
//===--------------------------------------------------------------------===//
//! Transform a node/edge table create (SQL/PGQ)
unique_ptr<PropertyGraphTable> TransformPropertyGraphTable(duckdb_libpgquery::PGPropertyGraphTable *node,
shared_ptr<PropertyGraphTable> TransformPropertyGraphTable(duckdb_libpgquery::PGPropertyGraphTable *node,
case_insensitive_set_t &global_label_set);
//! Transform a path pattern (SQL/PGQ)
unique_ptr<PathPattern> TransformPath(duckdb_libpgquery::PGPathPattern *root);
//! Transform a path element (SQL/PGQ)
unique_ptr<PathElement> TransformPathElement(duckdb_libpgquery::PGPathElement *element);
static unique_ptr<PathElement> TransformPathElement(duckdb_libpgquery::PGPathElement *element);
//! Transform a subpath (SQL/PGQ)
unique_ptr<SubPath> TransformSubPathElement(duckdb_libpgquery::PGSubPath *element);

Expand Down
12 changes: 6 additions & 6 deletions src/parser/parsed_data/create_property_graph_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ unique_ptr<CreateInfo> CreatePropertyGraphInfo::Copy() const {
for (auto &vertex_table : vertex_tables) {
auto copied_vertex_table = vertex_table->Copy();
for (auto &label : copied_vertex_table->sub_labels) {
result->label_map[label] = copied_vertex_table.get();
result->label_map[label] = copied_vertex_table;
}
result->label_map[copied_vertex_table->main_label] = copied_vertex_table.get();
result->label_map[copied_vertex_table->main_label] = copied_vertex_table;
result->vertex_tables.push_back(std::move(copied_vertex_table));
}
for (auto &edge_table : edge_tables) {
auto copied_edge_table = edge_table->Copy();
for (auto &label : copied_edge_table->sub_labels) {
result->label_map[label] = copied_edge_table.get();
result->label_map[label] = copied_edge_table;
}
result->label_map[copied_edge_table->main_label] = copied_edge_table.get();
result->label_map[copied_edge_table->main_label] = copied_edge_table;
result->edge_tables.push_back(std::move(copied_edge_table));
}
return std::move(result);
Expand All @@ -47,8 +47,8 @@ unique_ptr<CreateInfo> CreatePropertyGraphInfo::Copy() const {
unique_ptr<CreateInfo> CreatePropertyGraphInfo::Deserialize(FieldReader &reader) {
auto result = make_uniq<CreatePropertyGraphInfo>();
result->property_graph_name = reader.ReadRequired<string>();
result->vertex_tables = reader.ReadRequiredSerializableList<PropertyGraphTable>();
result->edge_tables = reader.ReadRequiredSerializableList<PropertyGraphTable>();
result->vertex_tables = reader.ReadRequiredSharedSerializableList<PropertyGraphTable>();
result->edge_tables = reader.ReadRequiredSharedSerializableList<PropertyGraphTable>();
// result->label_map = reader.ReadRequiredSerializableMap<PropertyGraphTable>();
reader.Finalize();
return std::move(result);
Expand Down
12 changes: 6 additions & 6 deletions src/parser/property_graph_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ void PropertyGraphTable::Serialize(Serializer &serializer) const {
writer.Finalize();
}

unique_ptr<PropertyGraphTable> PropertyGraphTable::Deserialize(Deserializer &deserializer) {
auto pg_table = make_uniq<PropertyGraphTable>();
shared_ptr<PropertyGraphTable> PropertyGraphTable::Deserialize(Deserializer &deserializer) {
auto pg_table = make_shared<PropertyGraphTable>();
FieldReader reader(deserializer);
pg_table->table_name = reader.ReadRequired<string>();
reader.ReadList<string>(pg_table->column_names);
Expand All @@ -254,11 +254,11 @@ unique_ptr<PropertyGraphTable> PropertyGraphTable::Deserialize(Deserializer &des
pg_table->destination_reference = reader.ReadRequired<string>();
}
reader.Finalize();
return pg_table;
return std::move(pg_table);
}

unique_ptr<PropertyGraphTable> PropertyGraphTable::Copy() const {
auto result = make_uniq<PropertyGraphTable>();
shared_ptr<PropertyGraphTable> PropertyGraphTable::Copy() const {
auto result = make_shared<PropertyGraphTable>();

result->table_name = table_name;
for (auto &column_name : column_names) {
Expand Down Expand Up @@ -296,7 +296,7 @@ unique_ptr<PropertyGraphTable> PropertyGraphTable::Copy() const {
for (auto &key : destination_pk) {
result->destination_pk.push_back(key);
}
return result;
return std::move(result);
}

} // namespace duckdb
18 changes: 13 additions & 5 deletions src/parser/subpath_element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ unique_ptr<PathReference> SubPath::Copy() {
result->upper = upper;
result->single_bind = single_bind;

result->path_variable = path_variable;

return std::move(result);
}
bool SubPath::Equals(const PathReference *other_p) const {
Expand Down Expand Up @@ -56,15 +58,19 @@ bool SubPath::Equals(const PathReference *other_p) const {
if (single_bind != other->single_bind) {
return false;
}
if (path_variable != other->path_variable) {
return false;
}
return true;
}
void SubPath::Serialize(FieldWriter &writer) const {
writer.WriteField<PGQPathMode>(path_mode);
writer.WriteSerializableList(path_list);
writer.WriteField<bool>(single_bind);
writer.WriteField<int32_t>(lower);
writer.WriteField<int32_t>(upper);
writer.WriteField<int64_t>(lower);
writer.WriteField<int64_t>(upper);
writer.WriteOptional(where_clause);
writer.WriteString(path_variable);
}

unique_ptr<PathReference> SubPath::Deserialize(FieldReader &reader) {
Expand All @@ -73,13 +79,15 @@ unique_ptr<PathReference> SubPath::Deserialize(FieldReader &reader) {
result->path_mode = reader.ReadRequired<PGQPathMode>();
result->path_list = reader.ReadRequiredSerializableList<PathReference>();
result->single_bind = reader.ReadRequired<bool>();
result->lower = reader.ReadRequired<int32_t>();
result->upper = reader.ReadRequired<int32_t>();
result->lower = reader.ReadRequired<int64_t>();
result->upper = reader.ReadRequired<int64_t>();
result->where_clause = reader.ReadOptional<ParsedExpression>(nullptr);
result->path_variable = reader.ReadRequired<string>();
return std::move(result);
}
string SubPath::ToString() const {
string result = "";
string result;
path_variable.empty() ? result += "" : result += path_variable + " = ";
if (path_list.size() == 1) {
switch (path_list[0]->path_reference_type) {
case PGQPathReferenceType::PATH_ELEMENT: {
Expand Down
20 changes: 10 additions & 10 deletions src/parser/tableref/matchref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ string MatchExpression::ToString() const {
string result = "GRAPH_TABLE (";
result += pg_name + " MATCH";

for (idx_t i = 0; i < path_list.size(); i++) {
for (idx_t i = 0; i < path_patterns.size(); i++) {
(i > 0) ? result += ", " : result;
for (idx_t j = 0; j < path_list[i]->path_elements.size(); j++) {
auto &path_reference = path_list[i]->path_elements[j];
for (idx_t j = 0; j < path_patterns[i]->path_elements.size(); j++) {
auto &path_reference = path_patterns[i]->path_elements[j];
switch (path_reference->path_reference_type) {
case PGQPathReferenceType::PATH_ELEMENT: {
auto path_element = reinterpret_cast<PathElement *>(path_reference.get());
Expand Down Expand Up @@ -64,13 +64,13 @@ bool MatchExpression::Equals(const BaseExpression &other_p) const {
return false;
}

if (path_list.size() != other.path_list.size()) {
if (path_patterns.size() != other.path_patterns.size()) {
return false;
}

// path_list
for (idx_t i = 0; i < path_list.size(); i++) {
if (!path_list[i]->Equals(other.path_list[i].get())) {
for (idx_t i = 0; i < path_patterns.size(); i++) {
if (!path_patterns[i]->Equals(other.path_patterns[i].get())) {
return false;
}
}
Expand Down Expand Up @@ -104,8 +104,8 @@ unique_ptr<ParsedExpression> MatchExpression::Copy() const {
copy->pg_name = pg_name;
copy->alias = alias;

for (auto &path : path_list) {
copy->path_list.push_back(path->Copy());
for (auto &path : path_patterns) {
copy->path_patterns.push_back(path->Copy());
}

for (auto &column : column_list) {
Expand All @@ -120,7 +120,7 @@ unique_ptr<ParsedExpression> MatchExpression::Copy() const {
void MatchExpression::Serialize(FieldWriter &writer) const {
writer.WriteString(pg_name);
writer.WriteString(alias);
writer.WriteSerializableList<PathPattern>(path_list);
writer.WriteSerializableList<PathPattern>(path_patterns);
writer.WriteSerializableList<ParsedExpression>(column_list);
writer.WriteOptional(where_clause);
}
Expand All @@ -129,7 +129,7 @@ unique_ptr<ParsedExpression> MatchExpression::Deserialize(FieldReader &reader) {
auto result = make_uniq<MatchExpression>();
result->pg_name = reader.ReadRequired<string>();
result->alias = reader.ReadRequired<string>();
result->path_list = reader.ReadRequiredSerializableList<PathPattern>();
result->path_patterns = reader.ReadRequiredSerializableList<PathPattern>();
result->column_list = reader.ReadRequiredSerializableList<ParsedExpression>();
result->where_clause = reader.ReadOptional<ParsedExpression>(nullptr);
return std::move(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace duckdb {

unique_ptr<PropertyGraphTable>
shared_ptr<PropertyGraphTable>
Transformer::TransformPropertyGraphTable(duckdb_libpgquery::PGPropertyGraphTable *graph_table,
case_insensitive_set_t &global_label_set) {
vector<string> column_names;
Expand Down Expand Up @@ -59,7 +59,7 @@ Transformer::TransformPropertyGraphTable(duckdb_libpgquery::PGPropertyGraphTable
label_names.emplace_back(label_str);
}

auto pg_table = make_uniq<PropertyGraphTable>(graph_table_name.name, table_name_alias, column_names, label_names);
auto pg_table = make_shared<PropertyGraphTable>(graph_table_name.name, table_name_alias, column_names, label_names);

pg_table->is_vertex_table = graph_table->is_vertex_table;
pg_table->except_columns = std::move(except_list);
Expand Down Expand Up @@ -106,7 +106,7 @@ Transformer::TransformPropertyGraphTable(duckdb_libpgquery::PGPropertyGraphTable
}
}

return pg_table;
return std::move(pg_table);
}

unique_ptr<CreateStatement>
Expand All @@ -128,9 +128,9 @@ Transformer::TransformCreatePropertyGraph(duckdb_libpgquery::PGCreatePropertyGra
auto graph_table = reinterpret_cast<duckdb_libpgquery::PGPropertyGraphTable *>(vertex_table->data.ptr_value);
auto pg_table = TransformPropertyGraphTable(graph_table, global_label_set);
for (auto &label : pg_table->sub_labels) {
info->label_map[label] = pg_table.get();
info->label_map[label] = pg_table;
}
info->label_map[pg_table->main_label] = pg_table.get();
info->label_map[pg_table->main_label] = pg_table;

info->vertex_tables.push_back(std::move(pg_table));
}
Expand All @@ -145,17 +145,17 @@ Transformer::TransformCreatePropertyGraph(duckdb_libpgquery::PGCreatePropertyGra
auto graph_table = reinterpret_cast<duckdb_libpgquery::PGPropertyGraphTable *>(edge_table->data.ptr_value);
auto pg_table = TransformPropertyGraphTable(graph_table, global_label_set);
for (auto &label : pg_table->sub_labels) {
info->label_map[label] = pg_table.get();
info->label_map[label] = pg_table;
}
info->label_map[pg_table->main_label] = pg_table.get();
info->label_map[pg_table->main_label] = pg_table;
info->edge_tables.push_back(std::move(pg_table));
}
}

auto result = make_uniq<CreateStatement>();
result->info = std::move(info);

return result;
return std::move(result);
}

} // namespace duckdb
Loading
Loading