From 592ee62c397b20669beee0c9e76affb9bf92cffc Mon Sep 17 00:00:00 2001 From: dtenwolde Date: Wed, 27 Nov 2024 15:37:46 +0100 Subject: [PATCH] Find table by table name now, updated error messages --- .../create_property_graph_info.hpp | 40 ++++++++++++++----- .../transform_create_property_graph.cpp | 6 ++- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/include/duckdb/parser/parsed_data/create_property_graph_info.hpp b/src/include/duckdb/parser/parsed_data/create_property_graph_info.hpp index af1423dc1440..8ca4ee05f556 100644 --- a/src/include/duckdb/parser/parsed_data/create_property_graph_info.hpp +++ b/src/include/duckdb/parser/parsed_data/create_property_graph_info.hpp @@ -99,9 +99,31 @@ struct CreatePropertyGraphInfo : public CreateInfo { return d[len1][len2]; } - shared_ptr GetTable(const string &table_name, bool error_not_found = true, bool is_vertex_table = true) { + shared_ptr 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 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) { @@ -109,7 +131,7 @@ struct CreatePropertyGraphInfo : public CreateInfo { } 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; @@ -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; @@ -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::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."); } diff --git a/src/parser/transform/statement/transform_create_property_graph.cpp b/src/parser/transform/statement/transform_create_property_graph.cpp index e6b695c257cf..d93028ecc6e0 100644 --- a/src/parser/transform/statement/transform_create_property_graph.cpp +++ b/src/parser/transform/statement/transform_create_property_graph.cpp @@ -177,8 +177,10 @@ Transformer::TransformCreatePropertyGraph(duckdb_libpgquery::PGCreatePropertyGra info->label_map[label] = pg_table; } info->label_map[pg_table->main_label] = pg_table; - pg_table->source_pg_table = info->label_map[pg_table->source_reference]; - pg_table->destination_pg_table = info->label_map[pg_table->destination_reference]; + pg_table->source_pg_table = info->GetTableByName(pg_table->source_reference); + D_ASSERT(pg_table->source_pg_table); + pg_table->destination_pg_table = info->GetTableByName(pg_table->destination_reference); + D_ASSERT(pg_table->destination_pg_table); info->edge_tables.push_back(std::move(pg_table)); } }