Skip to content

Commit

Permalink
Merge branch 'main' into 67-shortest-path-bounded
Browse files Browse the repository at this point in the history
  • Loading branch information
SiberiaWolfP committed Feb 11, 2024
2 parents cfe0649 + 6241607 commit 1f21138
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[submodule "duckdb-pgq"]
path = duckdb-pgq
url = [email protected]:cwida/duckdb-pgq.git
branch = master
branch = main
[submodule "duckdb"]
path = duckdb
url = [email protected]:duckdb/duckdb.git
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This repository is currently a research project and a work in progress. Feel fre
## Getting started
To get started, first clone this repository:
```sh
git clone --recurse-submodules https://github.com/cwida/duckpgq-extension.git
git clone --recurse-submodules git@github.com:cwida/duckpgq-extension.git
```
Note that `--recurse-submodules` will ensure the correct version of DuckDB is pulled allowing you to get started right away.

Expand Down
2 changes: 1 addition & 1 deletion duckdb
2 changes: 1 addition & 1 deletion duckdb-pgq
Submodule duckdb-pgq updated 0 files
4 changes: 2 additions & 2 deletions duckpgq/include/duckpgq_extension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ ParserExtensionPlanResult duckpgq_plan(ParserExtensionInfo *info,
ClientContext &,
unique_ptr<ParserExtensionParseData>);

ParserExtensionPlanResult duckpgq_handle_statement(unique_ptr<SQLStatement> &statement);

ParserExtensionPlanResult
duckpgq_handle_statement(unique_ptr<SQLStatement> &statement);

struct DuckPGQParserExtension : public ParserExtension {
DuckPGQParserExtension() : ParserExtension() {
Expand Down
66 changes: 38 additions & 28 deletions duckpgq/src/duckpgq_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "duckdb/parser/query_node/select_node.hpp"
#include "duckdb/parser/statement/copy_statement.hpp"
#include "duckdb/parser/parsed_data/create_table_info.hpp"
#include "duckdb/parser/tableref/joinref.hpp"

#include "duckdb/parser/statement/extension_statement.hpp"

Expand Down Expand Up @@ -109,32 +110,46 @@ BoundStatement duckpgq_bind(ClientContext &context, Binder &binder,
throw BinderException("Unable to find DuckPGQ Parse Data");
}

ParserExtensionPlanResult duckpgq_handle_statement(SQLStatement *statement, DuckPGQState &duckpgq_state) {
if (statement->type == StatementType::SELECT_STATEMENT) {
auto select_statement = dynamic_cast<SelectStatement *>(statement);
auto select_node = dynamic_cast<SelectNode *>(select_statement->node.get());
auto from_table_function =
dynamic_cast<TableFunctionRef *>(select_node->from_table.get());
void duckpgq_find_match_function(TableRef *table_ref,
DuckPGQState &duckpgq_state) {
if (auto table_function_ref = dynamic_cast<TableFunctionRef *>(table_ref)) {
// Handle TableFunctionRef case
auto function =
dynamic_cast<FunctionExpression *>(from_table_function->function.get());
dynamic_cast<FunctionExpression *>(table_function_ref->function.get());
if (function->function_name == "duckpgq_match") {
duckpgq_state.transform_expression =
std::move(std::move(function->children[0]));
function->children.pop_back();
}
} else if (auto join_ref = dynamic_cast<JoinRef *>(table_ref)) {
// Handle JoinRef case
duckpgq_find_match_function(join_ref->left.get(), duckpgq_state);
duckpgq_find_match_function(join_ref->right.get(), duckpgq_state);
}
}

ParserExtensionPlanResult
duckpgq_handle_statement(SQLStatement *statement, DuckPGQState &duckpgq_state) {
if (statement->type == StatementType::SELECT_STATEMENT) {
const auto select_statement = dynamic_cast<SelectStatement *>(statement);
const auto select_node =
dynamic_cast<SelectNode *>(select_statement->node.get());
duckpgq_find_match_function(select_node->from_table.get(), duckpgq_state);
throw Exception("use duckpgq_bind instead");
}
if (statement->type == StatementType::CREATE_STATEMENT) {
auto &create_statement = statement->Cast<CreateStatement>();
auto create_property_graph = dynamic_cast<CreatePropertyGraphInfo*>(create_statement.info.get());
const auto &create_statement = statement->Cast<CreateStatement>();
const auto create_property_graph =
dynamic_cast<CreatePropertyGraphInfo *>(create_statement.info.get());
if (create_property_graph) {
ParserExtensionPlanResult result;
result.function = CreatePropertyGraphFunction();
result.requires_valid_transaction = true;
result.return_type = StatementReturnType::QUERY_RESULT;
return result;
}
auto create_table = reinterpret_cast<CreateTableInfo*>(create_statement.info.get());
const auto create_table =
reinterpret_cast<CreateTableInfo *>(create_statement.info.get());
duckpgq_handle_statement(create_table->query.get(), duckpgq_state);
}
if (statement->type == StatementType::DROP_STATEMENT) {
Expand All @@ -146,30 +161,27 @@ ParserExtensionPlanResult duckpgq_handle_statement(SQLStatement *statement, Duck
}
if (statement->type == StatementType::EXPLAIN_STATEMENT) {
auto &explain_statement = statement->Cast<ExplainStatement>();
// auto select_statement = dynamic_cast<SelectStatement*>(explain_statement.stmt.get());
// auto select_statement =
// dynamic_cast<SelectStatement*>(explain_statement.stmt.get());
duckpgq_handle_statement(explain_statement.stmt.get(), duckpgq_state);
}
if (statement->type == StatementType::COPY_STATEMENT) {
auto &copy_statement = statement->Cast<CopyStatement>();
auto select_node = dynamic_cast<SelectNode *>(copy_statement.select_statement.get());
auto from_table_function =
dynamic_cast<TableFunctionRef *>(select_node->from_table.get());
auto function =
dynamic_cast<FunctionExpression *>(from_table_function->function.get());
if (function->function_name == "duckpgq_match") {
duckpgq_state.transform_expression =
std::move(std::move(function->children[0]));
function->children.pop_back();
}
const auto &copy_statement = statement->Cast<CopyStatement>();
const auto select_node =
dynamic_cast<SelectNode *>(copy_statement.select_statement.get());
duckpgq_find_match_function(select_node->from_table.get(), duckpgq_state);
throw Exception("use duckpgq_bind instead");
}
if (statement->type == StatementType::INSERT_STATEMENT) {
auto &insert_statement = statement->Cast<InsertStatement>();
duckpgq_handle_statement(insert_statement.select_statement.get(), duckpgq_state);
const auto &insert_statement = statement->Cast<InsertStatement>();
duckpgq_handle_statement(insert_statement.select_statement.get(),
duckpgq_state);
}

// Preferably throw NotImplementedExpection here, but only BinderExceptions are caught properly on MacOS right now
throw BinderException("%s has not been implemented yet for DuckPGQ queries", StatementTypeToString(statement->type));
// Preferably throw NotImplementedExpection here, but only BinderExceptions
// are caught properly on MacOS right now
throw BinderException("%s has not been implemented yet for DuckPGQ queries",
StatementTypeToString(statement->type));
}

ParserExtensionPlanResult
Expand All @@ -194,8 +206,6 @@ duckpgq_plan(ParserExtensionInfo *, ClientContext &context,

auto statement = duckpgq_parse_data->statement.get();
return duckpgq_handle_statement(statement, *duckpgq_state);


}

std::string DuckpgqExtension::Name() { return "duckpgq"; }
Expand Down
73 changes: 73 additions & 0 deletions test/sql/with_statement_duckpgq.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# name: test/sql/sqlpgq/snb.test
# group: [duckpgq]

require duckpgq

statement ok
import database 'duckdb-pgq/data/SNB0.003';

statement ok
-CREATE PROPERTY GRAPH snb_projected
VERTEX TABLES (Message);

query IIIIIII
-WITH message_count AS (
SELECT count(*) as m_count
FROM Message m
WHERE m.creationDate < '2010-05-27 11:16:36.013'
)
SELECT year, isComment,
CASE WHEN m_length < 40 THEN 0
WHEN m_length < 80 THEN 1
WHEN m_length < 160 THEN 2
ELSE 3 END as lengthCategory,
count(*) as messageCount,
avg(m_length) as averageMessageLength,
sum(m_length) as sumMessageLength,
count(*) / mc.m_count as percentageOfMessages
FROM GRAPH_TABLE(snb_projected
MATCH (message:Message where message.creationDate < '2010-05-27 11:16:36.013')
COLUMNS (date_part('year', message.creationDate::TIMESTAMP) as year, message.ImageFile is NULL as isComment, message.length as m_length, message.id)
) tmp, message_count mc
GROUP BY year, isComment, lengthCategory, m_count
ORDER BY year DESC, isComment ASC, lengthCategory ASC;
----
2010 false 0 63 0.0 0 0.9692307692307692
2010 true 2 2 109.0 218 0.03076923076923077


query II
-FROM GRAPH_TABLE (snb_projected
MATCH (m:message)
COLUMNS (m.id)
) tmp, (SELECT id from message limit 1)
LIMIT 10;
----
618475290624 618475290624
343597383683 618475290624
343597383684 618475290624
962072674309 618475290624
962072674310 618475290624
962072674311 618475290624
962072674312 618475290624
962072674313 618475290624
962072674314 618475290624
962072674315 618475290624

query II
-FROM (SELECT id from message limit 1), GRAPH_TABLE (snb_projected
MATCH (m:message)
COLUMNS (m.id)
) tmp
LIMIT 10;
----
618475290624 618475290624
618475290624 343597383683
618475290624 343597383684
618475290624 962072674309
618475290624 962072674310
618475290624 962072674311
618475290624 962072674312
618475290624 962072674313
618475290624 962072674314
618475290624 962072674315

0 comments on commit 1f21138

Please sign in to comment.