Skip to content

Commit

Permalink
Bind copy and planner
Browse files Browse the repository at this point in the history
  • Loading branch information
rfdavid committed Jul 4, 2023
1 parent b028346 commit 7447b35
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,6 @@ elseif (${BUILD_BENCHMARK})
add_subdirectory(test/test_helper)
endif()
add_subdirectory(tools)

# REMOVE ME
add_subdirectory(examples/cpp)
17 changes: 12 additions & 5 deletions examples/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
using namespace kuzu::main;

int main() {
auto database = std::make_unique<Database>("" /* fill db path */);
auto database = std::make_unique<Database>("/tmp/puffer_fish" /* fill db path */);
auto connection = std::make_unique<Connection>(database.get());

// Create schema.

connection->query("CREATE NODE TABLE Person(name STRING, age INT64, PRIMARY KEY(name));");
// Create nodes.
connection->query("CREATE (:Person {name: 'Alice', age: 25});");
connection->query("CREATE (:Person {name: 'Bob', age: 30});");

connection->query("COPY Person TO '/tmp/out.csv';");

// Create schema.
//connection->query("CREATE NODE TABLE Person(name STRING, age INT64, PRIMARY KEY(name));");
// Create nodes.
//connection->query("CREATE (:Person {name: 'Alice', age: 25});");
//connection->query("CREATE (:Person {name: 'Bob', age: 30});");

// Execute a simple query.
auto result = connection->query("MATCH (a:Person) RETURN a.name AS NAME, a.age AS AGE;");
//auto result = connection->query("MATCH (a:Person) RETURN a.name AS NAME, a.age AS AGE;");
// Print query result.
std::cout << result->toString();
//std::cout << result->toString();
}
7 changes: 5 additions & 2 deletions src/binder/bind/bind_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ std::unique_ptr<BoundStatement> Binder::bindCopyClause(const Statement& statemen
auto& copyCSV = (Copy&)statement;
auto catalogContent = catalog.getReadOnlyVersion();
auto tableName = copyCSV.getTableName();
auto copyDirection = copyCSV.getDirection();
auto copyDirection = copyCSV.getCopyDirection();
validateTableExist(catalog, tableName);
auto tableID = catalogContent->getTableID(tableName);
auto csvReaderConfig = bindParsingOptions(copyCSV.getParsingOptions());
Expand Down Expand Up @@ -43,7 +43,10 @@ std::unique_ptr<BoundStatement> Binder::bindCopyClause(const Statement& statemen
tableSchema->tableName));
}
}
return std::make_unique<BoundCopy>(

std::shared_ptr<NodeExpression> node = createQueryNode(InternalKeyword::ANONYMOUS, std::vector<common::table_id_t>{tableID});

return std::make_unique<BoundCopy>(node,
CopyDescription(copyDirection, boundFilePaths, csvReaderConfig, actualFileType), tableID,
tableName);
}
Expand Down
7 changes: 4 additions & 3 deletions src/include/binder/copy/bound_copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ namespace binder {

class BoundCopy : public BoundStatement {
public:
BoundCopy(
BoundCopy(std::shared_ptr<NodeExpression> nodeExpression,
common::CopyDescription copyDescription, common::table_id_t tableID, std::string tableName)
: BoundStatement{common::StatementType::COPY,
BoundStatementResult::createSingleStringColumnResult()},
nodeExpression{std::move(nodeExpression)},
copyDescription{std::move(copyDescription)}, tableID{tableID}, tableName{std::move(
tableName)} {}

inline std::shared_ptr<NodeOrRelExpression> getNodeOrRelExpression() const { return nodeOrRelExpression; }
inline std::shared_ptr<NodeExpression> getNodeExpression() const { return nodeExpression; }

inline common::CopyDescription getCopyDescription() const { return copyDescription; }

Expand All @@ -35,7 +36,7 @@ class BoundCopy : public BoundStatement {
std::string tableName;

// CopyTo only
std::shared_ptr<NodeOrRelExpression> nodeOrRelExpression;
std::shared_ptr<NodeExpression> nodeExpression;
expression_vector expressionsToCopy;
};

Expand Down
2 changes: 1 addition & 1 deletion src/include/parser/copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Copy : public Statement {
return &parsingOptions;
}
inline common::CopyDescription::FileType getFileType() const { return fileType; }
inline common::CopyDescription::CopyDirection getDirection() const { return copyDirection; }
inline common::CopyDescription::CopyDirection getCopyDirection() const { return copyDirection; }

private:
common::CopyDescription::CopyDirection copyDirection;
Expand Down
2 changes: 2 additions & 0 deletions src/include/planner/join_order_enumerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class JoinOrderEnumeratorContext;
* filter push down
*/
class JoinOrderEnumerator {
friend class Planner;

public:
JoinOrderEnumerator(const catalog::Catalog& catalog, QueryPlanner* queryPlanner)
: catalog{catalog},
Expand Down
10 changes: 6 additions & 4 deletions src/include/planner/planner.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ class Planner {
const storage::NodesStatisticsAndDeletedIDs& nodesStatistics,
const storage::RelsStatistics& relsStatistics, const BoundStatement& statement);

static std::unique_ptr<LogicalPlan> planCopy(
const catalog::Catalog& catalog, const BoundStatement& statement);
static std::unique_ptr<LogicalPlan> planCopy(const catalog::Catalog& catalog,
const storage::NodesStatisticsAndDeletedIDs& nodesStatistics, const storage::RelsStatistics& relsStatistics,
const BoundStatement& statement);

static std::unique_ptr<LogicalPlan> planCopyTo(
const catalog::Catalog& catalog, const BoundStatement& statement);
static std::unique_ptr<LogicalPlan> planCopyTo(const catalog::Catalog& catalog,
const storage::NodesStatisticsAndDeletedIDs& nodesStatistics, const storage::RelsStatistics& relsStatistics,
const BoundStatement& statement);

static std::unique_ptr<LogicalPlan> planCopyFrom(
const catalog::Catalog& catalog, const BoundStatement& statement);
Expand Down
30 changes: 22 additions & 8 deletions src/planner/planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include "planner/logical_plan/logical_operator/logical_rename_table.h"
#include "planner/logical_plan/logical_operator/logical_standalone_call.h"

// TODO: will be reviewed
#include "planner/join_order_enumerator.h"
#include "planner/logical_plan/logical_operator/logical_scan_node.h"

using namespace kuzu::catalog;
using namespace kuzu::common;
Expand All @@ -48,7 +50,7 @@ std::unique_ptr<LogicalPlan> Planner::getBestPlan(const Catalog& catalog,
plan = planCreateRelTable(statement);
} break;
case StatementType::COPY: {
plan = planCopy(catalog, statement);
plan = planCopy(catalog, nodesStatistics, relsStatistics, statement);
} break;
case StatementType::DROP_TABLE: {
plan = planDropTable(statement);
Expand Down Expand Up @@ -166,14 +168,15 @@ std::unique_ptr<LogicalPlan> Planner::planRenameProperty(const BoundStatement& s
return plan;
}

std::unique_ptr<LogicalPlan> Planner::planCopy(
const catalog::Catalog& catalog, const BoundStatement& statement) {
std::unique_ptr<LogicalPlan> Planner::planCopy(const Catalog& catalog,
const NodesStatisticsAndDeletedIDs& nodesStatistics, const RelsStatistics& relsStatistics,
const BoundStatement& statement) {
auto& boundCopyStatement = (BoundCopy&)statement;
if (boundCopyStatement.getCopyDescription().copyDirection ==
CopyDescription::CopyDirection::FROM) {
return planCopyFrom(catalog, statement);
} else {
return planCopyTo(catalog, statement);
return planCopyTo(catalog, nodesStatistics, relsStatistics, statement);
}
}

Expand All @@ -200,13 +203,24 @@ std::unique_ptr<LogicalPlan> Planner::planCopyFrom(
return plan;
}

std::unique_ptr<LogicalPlan> Planner::planCopyTo(
const catalog::Catalog& catalog, const BoundStatement& statement) {
std::unique_ptr<LogicalPlan> Planner::planCopyTo(const Catalog& catalog,
const NodesStatisticsAndDeletedIDs& nodesStatistics, const RelsStatistics& relsStatistics,
const BoundStatement& statement) {
auto plan = std::make_unique<LogicalPlan>();
auto& boundCopyStatement = (BoundCopy&)statement;

auto nodeOrRelExpression = boundCopyStatement.getNodeOrRelExpression();
appendScanNodeID(nodeOrRelExpression, plan);
auto planner = QueryPlanner(catalog, nodesStatistics, relsStatistics);
JoinOrderEnumerator joinOrderEnumerator(catalog, &planner);

auto nodeExpression = boundCopyStatement.getNodeExpression();


/* auto scan = make_shared<LogicalScanNode>(nodeExpression);
scan->computeFactorizedSchema();
plan->setLastOperator(std::move(scan));
*/

joinOrderEnumerator.appendScanNodeID(nodeExpression, *plan);

auto copyTo = make_shared<LogicalCopyTo>(boundCopyStatement.getCopyDescription(),
boundCopyStatement.getTableID(), boundCopyStatement.getTableName(),
Expand Down

0 comments on commit 7447b35

Please sign in to comment.