Skip to content

Commit

Permalink
Merge pull request #206 from cwida/issue/160-support-property-graphs-…
Browse files Browse the repository at this point in the history
…on-attached-databases

Allow to create property graph on attached database
  • Loading branch information
Dtenwolde authored Nov 27, 2024
2 parents 6236d0b + cec7f73 commit 0c82f0e
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 97 deletions.
Binary file added data/bluesky/bluesky.duckdb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,39 @@ struct CreatePropertyGraphInfo : public CreateInfo {
return d[len1][len2];
}

shared_ptr<PropertyGraphTable> GetTable(const string &table_name, bool error_not_found = true, bool is_vertex_table = true) {
shared_ptr<PropertyGraphTable> GetTableByName(const string &table_name, bool error_not_found = true, bool is_vertex_table = true) {
if (is_vertex_table) {
// search vertex tables
for (const auto &vertex_table : vertex_tables) {
if (vertex_table->table_name == table_name) {
return vertex_table;
}
}
} else {
// Search edge tables
for (const auto &edge_table : edge_tables) {
if (edge_table->table_name == table_name) {
return edge_table;
}
}
}
if (error_not_found) {
throw Exception(ExceptionType::INVALID, "Table '" + table_name + "' not found in the property graph " + property_graph_name + ".");
}
return nullptr; // Return nullptr if no match is found and error_not_found is false
}

shared_ptr<PropertyGraphTable> GetTableByLabel(const string &label, bool error_not_found = true, bool is_vertex_table = true) {
// First, check if there is an exact match for the table name in label_map
auto table_entry = label_map.find(table_name);
auto table_entry = label_map.find(label);
if (table_entry != label_map.end()) {
// Exact table match found, but verify if it matches the vertex/edge type
if (table_entry->second->is_vertex_table == is_vertex_table) {
return table_entry->second;
}
if (error_not_found) {
throw Exception(ExceptionType::INVALID,
"Exact label '" + table_name + "' found, but it is not a " +
"Exact label '" + label + "' found, but it is not a " +
(is_vertex_table ? "vertex" : "edge") + " table.");
}
return nullptr;
Expand All @@ -126,19 +148,19 @@ struct CreatePropertyGraphInfo : public CreateInfo {
if (pg_table->is_vertex_table != is_vertex_table) {
continue;
}
if (pg_table->table_name == table_name) {
throw Exception(ExceptionType::INVALID, "Table '" + table_name + "' found in the property graph, but does not have the correct label. Did you mean the label '" + pg_table->main_label + "' instead?");
if (pg_table->table_name == label) {
throw Exception(ExceptionType::INVALID, "Table '" + label + "' found in the property graph, but does not have the correct label. Did you mean the label '" + pg_table->main_label + "' instead?");
}

// Use int64_t for the distance calculations
auto distance_main_label = LevenshteinDistance(table_name, pg_table->main_label);
auto distance_main_label = LevenshteinDistance(label, pg_table->main_label);
if (distance_main_label < min_distance) {
min_distance = distance_main_label;
closest_label = pg_table->main_label;
}

for (const auto &sub_label : pg_table->sub_labels) {
auto distance_sub_label = LevenshteinDistance(table_name, sub_label);
auto distance_sub_label = LevenshteinDistance(label, sub_label);
if (distance_sub_label < min_distance) {
min_distance = distance_sub_label;
closest_label = sub_label;
Expand All @@ -150,14 +172,14 @@ struct CreatePropertyGraphInfo : public CreateInfo {
// If a close label match is found, suggest it in the error message
if (min_distance < std::numeric_limits<size_t>::max() && error_not_found) {
throw Exception(ExceptionType::INVALID,
"Label '" + table_name + "' not found. Did you mean the " +
"Label '" + label + "' not found. Did you mean the " +
(is_vertex_table ? "vertex" : "edge") + " label '" + closest_label + "'?");
}

// If no match is found and error_not_found is true, throw an error
if (error_not_found) {
throw Exception(ExceptionType::INVALID,
"Label '" + table_name + "' not found in the property graph for a " +
"Label '" + label + "' not found in the property graph for a " +
(is_vertex_table ? "vertex" : "edge") + " table.");
}

Expand Down
28 changes: 23 additions & 5 deletions src/include/duckdb/parser/property_graph_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include "duckdb/common/vector.hpp"
#include <duckdb/parser/tableref/basetableref.hpp>

namespace duckdb {

Expand All @@ -18,10 +19,13 @@ class PropertyGraphTable {
//! Used for Copy
PropertyGraphTable();
//! Specify both the column and table name
PropertyGraphTable(string table_name, vector<string> column_name, vector<string> label);
PropertyGraphTable(string table_name, vector<string> column_name,
vector<string> label, string catalog_name = "",
string schema = DEFAULT_SCHEMA);
//! Specify both the column and table name with alias
PropertyGraphTable(string table_name, string table_alias, vector<string> column_name, vector<string> label);

PropertyGraphTable(string table_name, string table_alias,
vector<string> column_name, vector<string> label,
string catalog_name = "", string schema = DEFAULT_SCHEMA);
string table_name;
string table_name_alias;

Expand All @@ -34,6 +38,9 @@ class PropertyGraphTable {
vector<string> sub_labels;
string main_label;

string catalog_name;
string schema_name;

//! Associated with the PROPERTIES keyword not mentioned in the creation of table, equalling SELECT * in some sense
bool all_columns = false;

Expand All @@ -50,12 +57,16 @@ class PropertyGraphTable {

string source_reference;

shared_ptr<PropertyGraphTable> source_pg_table;

vector<string> destination_fk;

vector<string> destination_pk;

string destination_reference;

shared_ptr<PropertyGraphTable> destination_pg_table;

public:
string ToString() const;
bool Equals(const PropertyGraphTable *other_p) const;
Expand All @@ -66,11 +77,18 @@ class PropertyGraphTable {

static shared_ptr<PropertyGraphTable> Deserialize(Deserializer &deserializer);

bool hasTableNameAlias() {
bool hasTableNameAlias() const {
return !table_name_alias.empty();
}

string ToLower(const std::string &str);
unique_ptr<BaseTableRef> CreateBaseTableRef(const string &alias = "") const {
auto base_table_ref = make_uniq<BaseTableRef>();
base_table_ref->catalog_name = catalog_name;
base_table_ref->schema_name = schema_name;
base_table_ref->table_name = table_name;
base_table_ref->alias = alias.empty() ? "" : alias;
return base_table_ref;
}

bool IsSourceTable(const string &table_name);
};
Expand Down
Loading

0 comments on commit 0c82f0e

Please sign in to comment.