diff --git a/duckpgq/src/duckpgq/functions/tablefunctions/create_property_graph.cpp b/duckpgq/src/duckpgq/functions/tablefunctions/create_property_graph.cpp index 979502ce..31c502cb 100644 --- a/duckpgq/src/duckpgq/functions/tablefunctions/create_property_graph.cpp +++ b/duckpgq/src/duckpgq/functions/tablefunctions/create_property_graph.cpp @@ -3,178 +3,157 @@ namespace duckdb { -void CreatePropertyGraphFunction::CheckPropertyGraphTableLabels( - unique_ptr &pg_table, TableCatalogEntry &table) { - if (!pg_table->discriminator.empty()) { - if (!table.ColumnExists(pg_table->discriminator)) { - throw BinderException("Column %s not found in table %s", - pg_table->discriminator, pg_table->table_name); - } - auto &column = table.GetColumn(pg_table->discriminator); - if (!(column.GetType() == LogicalType::BIGINT || - column.GetType() == LogicalType::INTEGER)) { - throw BinderException("The discriminator column %s for table %s should " - "be of type BIGINT or INTEGER", - pg_table->discriminator, pg_table->table_name); - } - } -} - -void CreatePropertyGraphFunction::CheckPropertyGraphTableColumns( - unique_ptr &pg_table, TableCatalogEntry &table) { - if (pg_table->no_columns) { - return; - } - - if (pg_table->all_columns) { - for (auto &except_column : pg_table->except_columns) { - if (!table.ColumnExists(except_column)) { - throw BinderException("Except column %s not found in table %s", - except_column, pg_table->table_name); - } - } - - auto columns_of_table = table.GetColumns().GetColumnNames(); - - std::sort(std::begin(columns_of_table), std::end(columns_of_table)); - std::sort(std::begin(pg_table->except_columns), - std::end(pg_table->except_columns)); - std::set_difference( - columns_of_table.begin(), columns_of_table.end(), - pg_table->except_columns.begin(), pg_table->except_columns.end(), - std::inserter(pg_table->column_names, pg_table->column_names.begin())); - pg_table->column_aliases = pg_table->column_names; - return; - } - - for (auto &column : pg_table->column_names) { - if (!table.ColumnExists(column)) { - throw BinderException("Column %s not found in table %s", column, - pg_table->table_name); - } - } -} - -duckdb::unique_ptr -CreatePropertyGraphFunction::CreatePropertyGraphBind( - ClientContext &context, TableFunctionBindInput &input, - vector &return_types, vector &names) { - names.emplace_back("success"); - return_types.emplace_back(LogicalType::VARCHAR); - auto lookup = context.registered_state.find("duckpgq"); - if (lookup == context.registered_state.end()) { - throw BinderException("Registered DuckPGQ state not found"); - } - auto duckpgq_state = (DuckPGQState *)lookup->second.get(); - auto duckpgq_parse_data = - dynamic_cast(duckpgq_state->parse_data.get()); - - if (!duckpgq_parse_data) { - return {}; - } - auto statement = - dynamic_cast(duckpgq_parse_data->statement.get()); - auto info = dynamic_cast(statement->info.get()); - auto pg_table = - duckpgq_state->registered_property_graphs.find(info->property_graph_name); - - if (pg_table != duckpgq_state->registered_property_graphs.end()) { - throw BinderException("Property graph table with name %s already exists", - info->property_graph_name); - } - - auto &catalog = Catalog::GetCatalog(context, info->catalog); - - case_insensitive_set_t v_table_names; - for (auto &vertex_table : info->vertex_tables) { - auto &table = catalog.GetEntry(context, info->schema, - vertex_table->table_name); - - CheckPropertyGraphTableColumns(vertex_table, table); - CheckPropertyGraphTableLabels(vertex_table, table); - - v_table_names.insert(vertex_table->table_name); - } - - for (auto &edge_table : info->edge_tables) { - auto &table = catalog.GetEntry(context, info->schema, - edge_table->table_name); - - CheckPropertyGraphTableColumns(edge_table, table); - CheckPropertyGraphTableLabels(edge_table, table); - - if (v_table_names.find(edge_table->source_reference) == - v_table_names.end()) { - throw BinderException("Referenced vertex table %s does not exist.", - edge_table->source_reference); - } - - auto &pk_source_table = catalog.GetEntry( - context, info->schema, edge_table->source_reference); - for (auto &pk : edge_table->source_pk) { - if (!pk_source_table.ColumnExists(pk)) { - throw BinderException("Primary key %s does not exist in table %s", pk, - edge_table->source_reference); - } + void CreatePropertyGraphFunction::CheckPropertyGraphTableLabels(unique_ptr &pg_table, + TableCatalogEntry &table) { + if (!pg_table->discriminator.empty()) { + if (!table.ColumnExists(pg_table->discriminator)) { + throw BinderException("Column %s not found in table %s", pg_table->discriminator, pg_table->table_name); + } + auto &column = table.GetColumn(pg_table->discriminator); + if (!(column.GetType() == LogicalType::BIGINT || column.GetType() == LogicalType::INTEGER)) { + throw BinderException("The discriminator column %s for table %s should be of type BIGINT or INTEGER", + pg_table->discriminator, pg_table->table_name); + } + } } - if (v_table_names.find(edge_table->source_reference) == - v_table_names.end()) { - throw BinderException("Referenced vertex table %s does not exist.", - edge_table->source_reference); + void CreatePropertyGraphFunction::CheckPropertyGraphTableColumns(unique_ptr &pg_table, + TableCatalogEntry &table) { + if (pg_table->no_columns) { + return; + } + + if (pg_table->all_columns) { + for (auto &except_column: pg_table->except_columns) { + if (!table.ColumnExists(except_column)) { + throw BinderException("Except column %s not found in table %s", except_column, + pg_table->table_name); + } + } + + auto columns_of_table = table.GetColumns().GetColumnNames(); + + std::sort(std::begin(columns_of_table), std::end(columns_of_table)); + std::sort(std::begin(pg_table->except_columns), std::end(pg_table->except_columns)); + std::set_difference(columns_of_table.begin(), columns_of_table.end(), pg_table->except_columns.begin(), + pg_table->except_columns.end(), + std::inserter(pg_table->column_names, pg_table->column_names.begin())); + pg_table->column_aliases = pg_table->column_names; + return; + } + + for (auto &column: pg_table->column_names) { + if (!table.ColumnExists(column)) { + throw BinderException("Column %s not found in table %s", column, pg_table->table_name); + } + } } - auto &pk_destination_table = catalog.GetEntry( - context, info->schema, edge_table->destination_reference); - for (auto &pk : edge_table->destination_pk) { - if (!pk_destination_table.ColumnExists(pk)) { - throw BinderException("Primary key %s does not exist in table %s", pk, - edge_table->destination_reference); - } + duckdb::unique_ptr + CreatePropertyGraphFunction::CreatePropertyGraphBind(ClientContext &context, TableFunctionBindInput &input, + vector &return_types, vector &names) { + names.emplace_back("success"); + return_types.emplace_back(LogicalType::VARCHAR); + auto lookup = context.registered_state.find("duckpgq"); + if (lookup == context.registered_state.end()) { + throw BinderException("Registered DuckPGQ state not found"); + } + auto duckpgq_state = (DuckPGQState *) lookup->second.get(); + auto duckpgq_parse_data = dynamic_cast(duckpgq_state->parse_data.get()); + + if (!duckpgq_parse_data) { + return {}; + } + auto statement = dynamic_cast(duckpgq_parse_data->statement.get()); + auto info = dynamic_cast(statement->info.get()); + auto pg_table = duckpgq_state->registered_property_graphs.find(info->property_graph_name); + + if (pg_table != duckpgq_state->registered_property_graphs.end()) { + throw BinderException("Property graph table with name %s already exists", info->property_graph_name); + } + + auto &catalog = Catalog::GetCatalog(context, info->catalog); + + case_insensitive_set_t v_table_names; + for (auto &vertex_table: info->vertex_tables) { + auto &table = catalog.GetEntry(context, info->schema, vertex_table->table_name); + + CheckPropertyGraphTableColumns(vertex_table, table); + CheckPropertyGraphTableLabels(vertex_table, table); + + v_table_names.insert(vertex_table->table_name); + } + + for (auto &edge_table: info->edge_tables) { + auto &table = catalog.GetEntry(context, info->schema, edge_table->table_name); + + CheckPropertyGraphTableColumns(edge_table, table); + CheckPropertyGraphTableLabels(edge_table, table); + + if (v_table_names.find(edge_table->source_reference) == v_table_names.end()) { + throw BinderException("Referenced vertex table %s does not exist.", edge_table->source_reference); + } + + auto &pk_source_table = catalog.GetEntry(context, info->schema, + edge_table->source_reference); + for (auto &pk: edge_table->source_pk) { + if (!pk_source_table.ColumnExists(pk)) { + throw BinderException("Primary key %s does not exist in table %s", pk, + edge_table->source_reference); + } + } + + if (v_table_names.find(edge_table->source_reference) == v_table_names.end()) { + throw BinderException("Referenced vertex table %s does not exist.", edge_table->source_reference); + } + + auto &pk_destination_table = + catalog.GetEntry(context, info->schema, edge_table->destination_reference); + + for (auto &pk: edge_table->destination_pk) { + if (!pk_destination_table.ColumnExists(pk)) { + throw BinderException("Primary key %s does not exist in table %s", pk, + edge_table->destination_reference); + } + } + + for (auto &fk: edge_table->source_fk) { + if (!table.ColumnExists(fk)) { + throw BinderException("Foreign key %s does not exist in table %s", fk, edge_table->table_name); + } + } + + for (auto &fk: edge_table->destination_fk) { + if (!table.ColumnExists(fk)) { + throw BinderException("Foreign key %s does not exist in table %s", fk, edge_table->table_name); + } + } + } + return make_uniq(info); } - for (auto &fk : edge_table->source_fk) { - if (!table.ColumnExists(fk)) { - throw BinderException("Foreign key %s does not exist in table %s", fk, - edge_table->table_name); - } + duckdb::unique_ptr + CreatePropertyGraphFunction::CreatePropertyGraphInit(ClientContext &context, + TableFunctionInitInput &input) { + return make_uniq(); } - for (auto &fk : edge_table->destination_fk) { - if (!table.ColumnExists(fk)) { - throw BinderException("Foreign key %s does not exist in table %s", fk, - edge_table->table_name); - } + void CreatePropertyGraphFunction::CreatePropertyGraphFunc(ClientContext &context, TableFunctionInput &data_p, + DataChunk &output) { + auto &bind_data = data_p.bind_data->Cast(); + + auto pg_info = bind_data.create_pg_info; + auto lookup = context.registered_state.find("duckpgq"); + if (lookup == context.registered_state.end()) { + throw BinderException("Registered DuckPGQ state not found"); + } + auto duckpgq_state = (DuckPGQState *) lookup->second.get(); + auto pg_lookup = duckpgq_state->registered_property_graphs.find(pg_info->property_graph_name); + if (pg_lookup == duckpgq_state->registered_property_graphs.end()) { + duckpgq_state->registered_property_graphs[pg_info->property_graph_name] = pg_info->Copy(); + } else { + throw BinderException("A property graph with name %s already exists.", pg_info->property_graph_name); + } } - } - return make_uniq(info); -} - -duckdb::unique_ptr -CreatePropertyGraphFunction::CreatePropertyGraphInit( - ClientContext &context, TableFunctionInitInput &input) { - return make_uniq(); -} - -void CreatePropertyGraphFunction::CreatePropertyGraphFunc( - ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { - auto &bind_data = data_p.bind_data->Cast(); - - auto pg_info = bind_data.create_pg_info; - auto lookup = context.registered_state.find("duckpgq"); - if (lookup == context.registered_state.end()) { - throw BinderException("Registered DuckPGQ state not found"); - } - auto duckpgq_state = (DuckPGQState *)lookup->second.get(); - auto pg_lookup = duckpgq_state->registered_property_graphs.find( - pg_info->property_graph_name); - if (pg_lookup == duckpgq_state->registered_property_graphs.end()) { - duckpgq_state->registered_property_graphs[pg_info->property_graph_name] = - pg_info->Copy(); - } else { - throw BinderException("A property graph with name %s already exists.", - pg_info->property_graph_name); - } -} -}; // namespace duckdb +}; diff --git a/duckpgq/src/duckpgq/functions/tablefunctions/drop_property_graph.cpp b/duckpgq/src/duckpgq/functions/tablefunctions/drop_property_graph.cpp index d02e7503..ce321477 100644 --- a/duckpgq/src/duckpgq/functions/tablefunctions/drop_property_graph.cpp +++ b/duckpgq/src/duckpgq/functions/tablefunctions/drop_property_graph.cpp @@ -2,46 +2,49 @@ #include + namespace duckdb { -duckdb::unique_ptr -DropPropertyGraphFunction::DropPropertyGraphBind( - ClientContext &context, TableFunctionBindInput &, - vector &return_types, vector &names) { - names.emplace_back("success"); - return_types.emplace_back(LogicalType::VARCHAR); - auto lookup = context.registered_state.find("duckpgq"); - if (lookup == context.registered_state.end()) { - throw BinderException("Registered DuckPGQ state not found"); - } - auto duckpgq_state = (DuckPGQState *)lookup->second.get(); - auto duckpgq_parse_data = - dynamic_cast(duckpgq_state->parse_data.get()); - - if (!duckpgq_parse_data) { - return {}; - } - auto statement = - dynamic_cast(duckpgq_parse_data->statement.get()); - auto info = dynamic_cast(statement->info.get()); - return make_uniq(info); -} + duckdb::unique_ptr DropPropertyGraphFunction::DropPropertyGraphBind(ClientContext &context, + TableFunctionBindInput &, + vector &return_types, + vector &names) { + names.emplace_back("success"); + return_types.emplace_back(LogicalType::VARCHAR); + auto lookup = context.registered_state.find("duckpgq"); + if (lookup == context.registered_state.end()) { + throw BinderException("Registered DuckPGQ state not found"); + } + auto duckpgq_state = (DuckPGQState *) lookup->second.get(); + auto duckpgq_parse_data = dynamic_cast(duckpgq_state->parse_data.get()); -duckdb::unique_ptr -DropPropertyGraphFunction::DropPropertyGraphInit(ClientContext &, - TableFunctionInitInput &) { - return make_uniq(); -} + if (!duckpgq_parse_data) { + return {}; + } + auto statement = dynamic_cast(duckpgq_parse_data->statement.get()); + auto info = dynamic_cast(statement->info.get()); + return make_uniq(info); + } + + duckdb::unique_ptr DropPropertyGraphFunction::DropPropertyGraphInit(ClientContext &, + TableFunctionInitInput &) { + return make_uniq(); + } -void DropPropertyGraphFunction::DropPropertyGraphFunc( - ClientContext &context, TableFunctionInput &data_p, DataChunk &) { - auto &bind_data = data_p.bind_data->Cast(); - - auto pg_info = bind_data.drop_pg_info; - auto lookup = context.registered_state.find("duckpgq"); - if (lookup == context.registered_state.end()) { - throw BinderException("Registered DuckPGQ state not found"); - } - auto duckpgq_state = (DuckPGQState *)lookup->second.get(); - duckpgq_state->registered_property_graphs.erase(pg_info->name); + void DropPropertyGraphFunction::DropPropertyGraphFunc(ClientContext &context, TableFunctionInput &data_p, DataChunk &) { + auto &bind_data = data_p.bind_data->Cast(); + + auto pg_info = bind_data.drop_pg_info; + auto lookup = context.registered_state.find("duckpgq"); + if (lookup == context.registered_state.end()) { + throw BinderException("Registered DuckPGQ state not found"); + } + auto duckpgq_state = (DuckPGQState *)lookup->second.get(); + auto registered_pg = duckpgq_state->registered_property_graphs.find(pg_info->name); + if (registered_pg == duckpgq_state->registered_property_graphs.end()) { + throw BinderException("Property graph %s does not exist.", pg_info->name); + } + duckpgq_state->registered_property_graphs.erase(registered_pg); + + } } -} // namespace duckdb + diff --git a/test/sql/basic_match.test b/test/sql/basic_match.test index 8d98abaf..fa18f002 100644 --- a/test/sql/basic_match.test +++ b/test/sql/basic_match.test @@ -179,13 +179,14 @@ FROM GRAPH_TABLE (pg MATCH (a:Person)-[k:Knows]->(b:Person)-[k2:Knows]->(c:Person)-[k3:Knows]->(a:Person) COLUMNS (a.name as a_name, b.name as b_name, c.name as c_name) - ) study; + ) study +ORDER BY study.a_name, study.b_name, study.c_name; ---- -Peter Daniel Tavneet -Peter Daniel Gabor +Daniel Gabor Peter Daniel Tavneet Peter Gabor Peter Daniel -Daniel Gabor Peter +Peter Daniel Gabor +Peter Daniel Tavneet Tavneet Peter Daniel statement error @@ -201,7 +202,7 @@ statement error FROM GRAPH_TABLE (pg MATCH (a)-[k:Knows]->(b:Person) - COLUMNS (a.name as a_name, b.name as b_name) + COLUMNS (a.ncame as a_name, b.name as b_name) ) study; statement error diff --git a/test/sql/drop_property_graph.test b/test/sql/drop_property_graph.test index c15af292..ff9917b0 100644 --- a/test/sql/drop_property_graph.test +++ b/test/sql/drop_property_graph.test @@ -48,6 +48,16 @@ EDGE TABLES ( statement ok -DROP PROPERTY GRAPH pg; +statement error +-DROP PROPERTY GRAPH pg; +---- +Binder Error: Property graph pg does not exist + +statement error +-DROP PROPERTY GRAPH pgdoesntexist; +---- +Binder Error: Property graph pgdoesntexist does not exist + statement error -SELECT study.id FROM GRAPH_TABLE (pg diff --git a/test/sql/shortest_path.test b/test/sql/shortest_path.test index 87c3f77e..bbd1acb7 100644 --- a/test/sql/shortest_path.test +++ b/test/sql/shortest_path.test @@ -55,7 +55,7 @@ query II ---- Daniel VU -# Returns erroreous results https://github.com/cwida/duckpgq-extension/issues/9 +# Returns erroneous results https://github.com/cwida/duckpgq-extension/issues/9 #query II #-FROM GRAPH_TABLE (pg # MATCH