Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query compilation framework. #687

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "deps/tugraph-db-browser"]
path = deps/tugraph-db-browser
url = https://github.com/TuGraph-family/tugraph-db-browser.git
[submodule "deps/buildit"]
path = deps/buildit
url = "https://github.com/RTEnzyme/buildit.git"
1 change: 1 addition & 0 deletions deps/buildit
Submodule buildit added at 734725
17 changes: 17 additions & 0 deletions src/BuildCypherLib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ find_package(PythonInterp 3)
find_package(PythonLibs ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} EXACT REQUIRED)
#antlr4-runtime
find_package(antlr4-runtime REQUIRED)
find_package(LLVM REQUIRED CONFIG)
set(ANTRL4_LIBRARY antlr4-runtime.a)

set(TARGET_LGRAPH_CYPHER_LIB lgraph_cypher_lib)
Expand Down Expand Up @@ -56,6 +57,10 @@ set(LGRAPH_CYPHER_SRC # find cypher/ -name "*.cpp" | sort
cypher/execution_plan/ops/op_traversal.cpp
cypher/execution_plan/ops/op_gql_remove.cpp
cypher/execution_plan/scheduler.cpp
cypher/experimental/data_type/field_data.h
cypher/experimental/expressions/cexpr.cpp
cypher/experimental/expressions/kernal/binary.cpp
cypher/experimental/jit/TuJIT.cpp
cypher/filter/filter.cpp
cypher/filter/iterator.cpp
cypher/graph/graph.cpp
Expand Down Expand Up @@ -88,10 +93,22 @@ target_include_directories(${TARGET_LGRAPH_CYPHER_LIB} PUBLIC
${ANTLR4_INCLUDE_DIR}
${CMAKE_CURRENT_LIST_DIR}/cypher)

include_directories(
${CMAKE_SOURCE_DIR}/deps/buildit/include
${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})

target_link_directories(${TARGET_LGRAPH_CYPHER_LIB} PUBLIC
${CMAKE_SOURCE_DIR}/deps/buildit/lib)

target_link_libraries(${TARGET_LGRAPH_CYPHER_LIB} PUBLIC
${ANTRL4_LIBRARY}
geax_isogql
${CMAKE_SOURCE_DIR}/deps/buildit/build/libbuildit.a
lgraph)

target_link_libraries(${TARGET_LGRAPH_CYPHER_LIB} PRIVATE
lgraph_server_lib)

llvm_map_components_to_libnames(llvm_libs Core Support)
target_link_libraries(${TARGET_LGRAPH_CYPHER_LIB} PRIVATE ${llvm_libs})
204 changes: 204 additions & 0 deletions src/cypher/experimental/data_type/field_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@

#pragma once
#include <variant>
#include <unordered_map>
#include <builder/dyn_var.h>
#include <builder/static_var.h>
#include "core/data_type.h"
#include "cypher/cypher_types.h"
#include "cypher/cypher_exception.h"

using builder::static_var;
using builder::dyn_var;
using lgraph::FieldType;

namespace cypher {
namespace compilation {

struct CScalarData {
static constexpr const char* type_name = "CScalarData";
std::variant<
std::monostate, // Represent the null state
dyn_var<short>,
dyn_var<int>,
dyn_var<long>,
dyn_var<float>,
dyn_var<double>
> constant_;

lgraph::FieldType type_;

CScalarData() {
type_ = lgraph_api::FieldType::NUL;
}

CScalarData(CScalarData &&data)
: constant_(std::move(data.constant_)), type_(data.type_) {}

CScalarData(const CScalarData& other)
: constant_(other.constant_), type_(other.type_) {}

CScalarData(const lgraph::FieldData& other) {
type_ = other.type;
switch (other.type) {
case lgraph::FieldType::NUL:
constant_.emplace<std::monostate>();
break;
case lgraph::FieldType::INT64:
constant_.emplace<dyn_var<long>>((long)other.integer());
break;
default:
CYPHER_TODO();
}
}

explicit CScalarData(long integer) {
constant_.emplace<dyn_var<long>>(integer);
type_ = lgraph::FieldType::INT64;
}

explicit CScalarData(const static_var<long> &integer)
: type_(FieldType::INT64) {
constant_ = (dyn_var<long>) integer;
}

explicit CScalarData(const dyn_var<long> &integer)
: constant_(integer), type_(FieldType::INT64) {}

explicit CScalarData(dyn_var<long>&& integer)
: constant_(std::move(integer)), type_(FieldType::INT64) {}

inline dyn_var<long> integer() const {
switch (type_) {
case FieldType::NUL:
case FieldType::BOOL:
throw std::bad_cast();
case FieldType::INT8:
return std::get<dyn_var<short>>(constant_);
case FieldType::INT16:
return std::get<dyn_var<short>>(constant_);
case FieldType::INT32:
return std::get<dyn_var<int>>(constant_);
case FieldType::INT64:
return std::get<dyn_var<long>>(constant_);
case FieldType::FLOAT:
case FieldType::DOUBLE:
case FieldType::DATE:
case FieldType::DATETIME:
case FieldType::STRING:
case FieldType::BLOB:
case FieldType::POINT:
case FieldType::LINESTRING:
case FieldType::POLYGON:
case FieldType::SPATIAL:
case FieldType::FLOAT_VECTOR:
throw std::bad_cast();
}
return dyn_var<long>(0);
}

inline dyn_var<double> real() const {
switch (type_) {
case FieldType::NUL:
case FieldType::BOOL:
case FieldType::INT8:
case FieldType::INT16:
case FieldType::INT32:
case FieldType::INT64:
throw std::bad_cast();
case FieldType::FLOAT:
std::get<dyn_var<float>>(constant_);
case FieldType::DOUBLE:
std::get<dyn_var<double>>(constant_);
case FieldType::DATE:
case FieldType::DATETIME:
case FieldType::STRING:
case FieldType::BLOB:
case FieldType::POINT:
case FieldType::LINESTRING:
case FieldType::POLYGON:
case FieldType::SPATIAL:
case FieldType::FLOAT_VECTOR:
throw std::bad_cast();
}
return dyn_var<double>(0);
}

dyn_var<long> Int64() const {
return std::get<dyn_var<long>>(constant_);
}

inline bool is_integer() const {
return type_ >= FieldType::INT8 && type_ <= FieldType::INT64;
}

inline bool is_real() const {
return type_ == FieldType::DOUBLE || type_ == FieldType::FLOAT;
}

bool is_null() const { return type_ == lgraph::FieldType::NUL; }

bool is_string() const { return type_ == lgraph::FieldType::STRING; }

CScalarData& operator=(CScalarData&& other) noexcept {
if (this != &other) {
constant_ = std::move(other.constant_);
type_ = std::move(other.type_);
}
return *this;
}

CScalarData& operator=(const CScalarData& other) {
if (this != &other) {
constant_ = other.constant_;
type_ = other.type_;
}
return *this;
}

CScalarData operator+(const CScalarData& other) const;
};

struct CFieldData {
enum FieldType { SCALAR, ARRAY, MAP} type;

CScalarData scalar;
std::vector<CFieldData>* array = nullptr;
std::unordered_map<std::string, CFieldData>* map = nullptr;

CFieldData() : type(SCALAR) {}

CFieldData(const CFieldData &data) : type(data.type), scalar(data.scalar) {}

CFieldData(const CScalarData& scalar) : type(SCALAR), scalar(scalar) {}

CFieldData(CScalarData&& scalar) : type(SCALAR), scalar(std::move(scalar)) {}

CFieldData& operator=(const CFieldData& data) {
this->type = data.type;
this->scalar = data.scalar;
return *this;
}

CFieldData& operator=(CFieldData&& data) {
this->type = std::move(data.type);
this->scalar = std::move(data.scalar);
return *this;
}

explicit CFieldData(const static_var<long>& scalar) : type(SCALAR), scalar(scalar) {}

bool is_null() const { return type == SCALAR && scalar.is_null(); }

bool is_string() const { return type == SCALAR && scalar.is_string(); }

bool is_integer() const { return type == SCALAR && scalar.is_integer(); }

bool is_real() const { return type == SCALAR && scalar.is_real(); }

CFieldData operator+(const CFieldData& other) const;

CFieldData operator-(const CFieldData& other) const;
};
} // namespace compilation
} // namespace cypher
80 changes: 80 additions & 0 deletions src/cypher/experimental/data_type/record.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#pragma once

#include <utility>
#include "core/data_type.h"
#include "cypher/cypher_types.h"
#include "cypher/cypher_exception.h"
#include "parser/data_typedef.h"
#include "graph/node.h"
#include "graph/relationship.h"
#include "cypher/resultset/record.h"

#include "experimental/data_type/field_data.h"
using builder::dyn_var;

namespace cypher {

struct SymbolTable;
class RTContext;

namespace compilation {
struct CEntry {
compilation::CFieldData constant_;
cypher::Node* node_ = nullptr;
cypher::Relationship* relationship_ = nullptr;

enum RecordEntryType {
UNKNOWN = 0,
CONSTANT,
NODE,
RELATIONSHIP,
VAR_LEN_RELP,
HEADER, // TODO(anyone) useless?
NODE_SNAPSHOT,
RELP_SNAPSHOT,
} type_;

CEntry() = default;

explicit CEntry(const cypher::Entry& entry) {
switch (entry.type) {
case cypher::Entry::CONSTANT: {
constant_ = CScalarData(entry.constant.scalar);
type_ = CONSTANT;
break;
}
case cypher::Entry::NODE: {
node_ = entry.node;
type_ = NODE;
break;
}
case cypher::Entry::RELATIONSHIP: {
relationship_ = entry.relationship;
type_ = RELATIONSHIP;
break;
}
default:
CYPHER_TODO();
}
}

explicit CEntry(const CFieldData &data) : constant_(data), type_(CONSTANT) {}

explicit CEntry(CFieldData&& data) : constant_(std::move(data)), type_(CONSTANT) {}

explicit CEntry(const CScalarData& scalar) : constant_(scalar), type_(CONSTANT) {}
};

struct CRecord { // Should be derived from cypher::Record
std::vector<CEntry> values;

CRecord() = default;

CRecord(const cypher::Record &record) {
for (auto& entry : record.values) {
values.emplace_back(entry);
}
}
};
} // namespace compilaiton
} // namespace cypher
2 changes: 2 additions & 0 deletions src/cypher/experimental/expressions/cexpr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "cexpr.h"

Loading
Loading