2017-03-22 23:38:43 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
#include "database/graph_db_accessor.hpp"
|
|
|
|
#include "query/context.hpp"
|
|
|
|
#include "query/frontend/ast/cypher_main_visitor.hpp"
|
|
|
|
#include "query/frontend/opencypher/parser.hpp"
|
|
|
|
#include "query/frontend/semantic/symbol_generator.hpp"
|
2017-04-13 16:01:16 +08:00
|
|
|
#include "query/interpret/frame.hpp"
|
|
|
|
#include "query/plan/planner.hpp"
|
2017-03-22 23:38:43 +08:00
|
|
|
|
|
|
|
namespace query {
|
|
|
|
|
|
|
|
template <typename Stream>
|
|
|
|
void Interpret(const std::string &query, GraphDbAccessor &db_accessor,
|
|
|
|
Stream &stream) {
|
|
|
|
clock_t start_time = clock();
|
|
|
|
|
|
|
|
Config config;
|
|
|
|
Context ctx(config, db_accessor);
|
|
|
|
std::map<std::string, TypedValue> summary;
|
|
|
|
|
|
|
|
// query -> AST
|
2017-04-13 16:01:16 +08:00
|
|
|
frontend::opencypher::Parser parser(query);
|
2017-03-22 23:38:43 +08:00
|
|
|
auto low_level_tree = parser.tree();
|
|
|
|
|
|
|
|
// AST -> high level tree
|
2017-03-27 19:09:14 +08:00
|
|
|
frontend::CypherMainVisitor visitor(ctx);
|
2017-03-22 23:38:43 +08:00
|
|
|
visitor.visit(low_level_tree);
|
|
|
|
auto high_level_tree = visitor.query();
|
|
|
|
|
|
|
|
// symbol table fill
|
|
|
|
SymbolTable symbol_table;
|
|
|
|
SymbolGenerator symbol_generator(symbol_table);
|
|
|
|
high_level_tree->Accept(symbol_generator);
|
|
|
|
|
|
|
|
// high level tree -> logical plan
|
2017-03-27 19:09:14 +08:00
|
|
|
auto logical_plan = plan::MakeLogicalPlan(*high_level_tree, symbol_table);
|
2017-03-22 23:38:43 +08:00
|
|
|
|
|
|
|
// generate frame based on symbol table max_position
|
|
|
|
Frame frame(symbol_table.max_position());
|
|
|
|
|
2017-03-28 18:42:04 +08:00
|
|
|
std::vector<std::string> header;
|
2017-03-27 19:09:14 +08:00
|
|
|
if (auto produce = dynamic_cast<plan::Produce *>(logical_plan.get())) {
|
2017-03-22 23:38:43 +08:00
|
|
|
// top level node in the operator tree is a produce (return)
|
|
|
|
// so stream out results
|
|
|
|
|
|
|
|
// generate header
|
|
|
|
for (auto named_expression : produce->named_expressions())
|
|
|
|
header.push_back(named_expression->name_);
|
|
|
|
stream.Header(header);
|
|
|
|
|
|
|
|
// collect the symbols from the return clause
|
|
|
|
std::vector<Symbol> symbols;
|
|
|
|
for (auto named_expression : produce->named_expressions())
|
|
|
|
symbols.emplace_back(symbol_table[*named_expression]);
|
|
|
|
|
|
|
|
// stream out results
|
|
|
|
auto cursor = produce->MakeCursor(db_accessor);
|
|
|
|
while (cursor->Pull(frame, symbol_table)) {
|
|
|
|
std::vector<TypedValue> values;
|
|
|
|
for (auto &symbol : symbols) values.emplace_back(frame[symbol]);
|
|
|
|
stream.Result(values);
|
|
|
|
}
|
2017-03-27 19:09:14 +08:00
|
|
|
} else if (dynamic_cast<plan::CreateNode *>(logical_plan.get()) ||
|
|
|
|
dynamic_cast<plan::CreateExpand *>(logical_plan.get()) ||
|
2017-04-03 20:56:16 +08:00
|
|
|
dynamic_cast<plan::SetProperty *>(logical_plan.get()) ||
|
|
|
|
dynamic_cast<plan::SetProperties *>(logical_plan.get()) ||
|
|
|
|
dynamic_cast<plan::SetLabels *>(logical_plan.get()) ||
|
|
|
|
dynamic_cast<plan::RemoveProperty *>(logical_plan.get()) ||
|
|
|
|
dynamic_cast<plan::RemoveLabels *>(logical_plan.get()) ||
|
2017-04-06 00:01:20 +08:00
|
|
|
dynamic_cast<plan::Delete *>(logical_plan.get())) {
|
2017-03-28 18:42:04 +08:00
|
|
|
stream.Header(header);
|
2017-03-27 19:09:14 +08:00
|
|
|
auto cursor = logical_plan.get()->MakeCursor(db_accessor);
|
2017-04-03 20:56:16 +08:00
|
|
|
while (cursor->Pull(frame, symbol_table)) continue;
|
|
|
|
} else {
|
|
|
|
throw QueryRuntimeException("Unknown top level LogicalOp");
|
2017-03-22 23:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
clock_t end_time = clock();
|
|
|
|
double time_second = double(end_time - start_time) / CLOCKS_PER_SEC;
|
|
|
|
summary["query_time_sec"] = TypedValue(time_second);
|
2017-03-27 19:09:14 +08:00
|
|
|
// TODO set summary['type'] based on transaction metadata
|
|
|
|
// the type can't be determined based only on top level LogicalOp
|
|
|
|
// (for example MATCH DELETE RETURN will have Produce as it's top)
|
|
|
|
// for now always se "rw" because something must be set, but it doesn't
|
|
|
|
// have to be correct (for Bolt clients)
|
|
|
|
summary["type"] = "rw";
|
2017-03-22 23:38:43 +08:00
|
|
|
stream.Summary(summary);
|
|
|
|
}
|
|
|
|
}
|