Skip to content

Commit

Permalink
Add cider core including expressions and plan nodes (facebookincubator#3
Browse files Browse the repository at this point in the history
)
  • Loading branch information
yma11 authored and Cheng Xu committed Sep 27, 2021
1 parent efcc413 commit ff8494b
Show file tree
Hide file tree
Showing 7 changed files with 401 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,4 @@ include_directories(.)
install(FILES velox/type/Type.h DESTINATION "include/velox")

add_subdirectory(velox)
add_subdirectory(cider)
12 changes: 12 additions & 0 deletions cider/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
add_subdirectory(core)
13 changes: 13 additions & 0 deletions cider/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
add_library(cider_core CiderPlanNode.cpp)
include_directories(.)
172 changes: 172 additions & 0 deletions cider/core/CiderExpressions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "velox/cider/core/CiderRowExpr.h"

using json = nlohmann::json;

namespace intel::cider::core{
namespace {
std::string matchType(std::string type) {
if (type == "int" || type == "integer" || type == "Int") {
return "Integer";
}
if (type == "double") {
return "decimal";
}
return type;
}

int getTypePrecision(std::string type) {
if (type == "int" || type == "integer" || type == "Int") {
return 10;
}
if (type == "double") {
return 15;
}
return -1;
}

std::string matchOperator(std::string originOp) {
if(originOp == "EQUAL") {
return "=";
}
if(originOp == "GREATER_THAN") {
return ">=";
}
if(originOp == "LESS_THAN") {
return "<";
}
if(originOp == "MODULUS") {
return "MOD";
}
if(originOp == "DIVIDE") {
return "/";
}
return originOp;
}
}

class CiderConstantExpr : public CiderRowExpr {
public:
CiderConstantExpr(std::string value, std::string type)
: type_{move(type)}, value_{move(value)} {}

std::string value() const{
return value_;
}

std::string type() const{
return type_;
}

json toCiderJSON() const override {
json j = json::object();
if (type_ == "varchar") {
j.push_back({"literal", value_});
j.push_back({"type", "CHAR"});
j.push_back({"target_type", "CHAR"});
j.push_back({"scale", -2147483648});
j.push_back({"precision", value_.length()});
j.push_back({"type_scale", -2147483648});
j.push_back({"type_precision", value_.length()});
}
if (type_ == "bigint" || type_ == "integer") {
j.push_back({"literal", std::stoi(value_)});
j.push_back({"type", "DECIMAL"});
j.push_back({"target_type", matchType(type_)});
j.push_back({"scale", 0});
j.push_back({"precision", value_.length()});
j.push_back({"type_scale", 0});
j.push_back({"type_precision", getTypePrecision(type_)});
}
if (type_ == "double") {
int precision = value_.length() - 1;
int scale = precision - value_.find(".");
int pos = value_.find(".");
std::string val = value_;
val = val.erase(pos, 1);
j.push_back({"literal", std::stoi(val)});
j.push_back({"type", "DECIMAL"});
j.push_back({"target_type", matchType(type_)});
j.push_back({"scale", scale});
j.push_back({"precision", precision});
j.push_back({"type_scale", scale});
j.push_back({"type_precision", precision});
}
if (type_ == "bool") {
j.push_back({"literal", value_});
j.push_back({"type", "BOOLEAN"});
j.push_back({"target_type", "BOOLEAN"});
j.push_back({"scale", -2147483648});
j.push_back({"precision", 1});
j.push_back({"type_scale", -2147483648});
j.push_back({"type_precision", 1});
}

return j;
}

private:
const std::string type_;
const std::string value_;
};

class CiderVariableExpr : public CiderRowExpr {
public:
CiderVariableExpr(std::string name, std::string type, int index)
: name_{move(name)}, type_{move(type)}, index_(index) {}

json toCiderJSON() const override {
json j = json::object();
j.push_back({"input", std::to_string(index_)});
return j;
}

private:
const std::string name_;
const std::string type_;
const int index_;
};

// row expression that describes a predicate, such as "field_a > 2"
class CiderCallExpr : public CiderRowExpr {
public:
CiderCallExpr(std::string op, std::string type, std::vector<std::shared_ptr<const CiderRowExpr>>& expressions)
: op_{move(op)}, type_{move(type)}, expressions_{move(expressions)} {}

json toCiderJSON() const override {
json opNode = json::object();
opNode.push_back({"op", matchOperator(op_)});
json exprNodes = json::array();
for (auto expr : expressions_) {
json exprNode = expr->toCiderJSON();
exprNodes.push_back(exprNode);
}
opNode.push_back({"operands", exprNodes});
json typeNode = json::object();
std::string typeForChange = type_;
std::transform(typeForChange.begin(), typeForChange.end(), typeForChange.begin(), ::toupper);
typeNode.push_back({"type",typeForChange});
typeNode.push_back({"nullable", true});
opNode.push_back({"type", typeNode});
return opNode;
}

private:
const std::string op_;
const std::string type_;
const std::vector<std::shared_ptr<const CiderRowExpr>> expressions_;
};
}
25 changes: 25 additions & 0 deletions cider/core/CiderPlanNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "velox/cider/core/CiderPlanNode.h"

namespace intel::cider::core {
namespace {
static const std::vector<std::shared_ptr<const CiderPlanNode>> EMPTY_SOURCES;
}

const std::vector<std::shared_ptr<const CiderPlanNode>>& CiderTableScanNode::sources()
const {
return EMPTY_SOURCES;
}
}
153 changes: 153 additions & 0 deletions cider/core/CiderPlanNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "string.h"
#include "velox/cider/core/CiderRowExpr.h"
#include "nlohmann/json.hpp"
#include "nlohmann/fifo_map.hpp"
using json = nlohmann::json;
template<class K, class V, class dummy_compare, class A>
using tmp_fifo_map = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>;
using tmp_json = nlohmann::basic_json<tmp_fifo_map>;

namespace intel::cider::core{
typedef std::string PlanNodeId;
class CiderPlanNode {
public:
explicit CiderPlanNode(const PlanNodeId& id) : id_{id} {}

virtual ~CiderPlanNode() {}

const PlanNodeId& id() const {
return id_;
}

virtual const std::vector<std::shared_ptr<const CiderPlanNode>>& sources() const = 0;

virtual json toCiderJSON(std::string ciderPlanId) const = 0;

virtual std::string name() const = 0;

private:
const PlanNodeId id_;
};

class CiderProjectNode : public CiderPlanNode {
public:
CiderProjectNode(
const PlanNodeId& id,
std::vector<std::string> assignmentKeys,
std::vector<std::shared_ptr<const CiderRowExpr>> assignmentExprs,
std::shared_ptr<const CiderPlanNode> source )
: CiderPlanNode(id), assignmentKeys_{assignmentKeys},
assignmentExprs_(assignmentExprs), sources_{source} {}

json toCiderJSON(std::string ciderPlanId) const override {
json projectNode = json::object();
projectNode.push_back({"id", ciderPlanId});
projectNode.push_back({"relOp", name()});
tmp_json fieldNodes = tmp_json::array();
tmp_json fieldExprs = tmp_json::array();
for (int i=0; i < assignmentKeys_.size(); i++) {
fieldNodes.push_back({assignmentKeys_.at(i)});
fieldExprs.push_back({assignmentExprs_.at(i)->toCiderJSON()});
}
projectNode.push_back({"fields", fieldNodes});
projectNode.push_back({"exprs", fieldExprs});

return projectNode;
}

std::string name() const override {
return "LogicalProject";
}

const std::vector<std::shared_ptr<const CiderPlanNode>>& sources() const override {
return sources_;
}

private:
const std::vector<std::string> assignmentKeys_;
const std::vector<std::shared_ptr<const CiderRowExpr>> assignmentExprs_;
const std::vector<std::shared_ptr<const CiderPlanNode>> sources_;
};

class CiderFilterNode : public CiderPlanNode {
public:
CiderFilterNode(
const PlanNodeId& id,
std::shared_ptr<const CiderRowExpr> filter,
std::shared_ptr<const CiderPlanNode> source)
: CiderPlanNode(id), filter_(filter), sources_{source} {}

json toCiderJSON(std::string ciderPlanId) const override {
json filterNode = json::object();
filterNode.push_back({"id", ciderPlanId});
filterNode.push_back({"relOp", name()});
filterNode.push_back({"condition", filter_->toCiderJSON()});
return filterNode;
}

std::string name() const override {
return "LogicalFilter";
}

const std::vector<std::shared_ptr<const CiderPlanNode>>& sources() const override {
return sources_;
}

private:
const std::shared_ptr<const CiderRowExpr> filter_;
const std::vector<std::shared_ptr<const CiderPlanNode>> sources_;
};

class CiderTableScanNode : public CiderPlanNode {
public:
CiderTableScanNode(
const PlanNodeId& id,
std::string tableName,
std::string schemaName,
std::vector<std::string> fieldNames)
: CiderPlanNode(id), tableName_(tableName),
schemaName_(schemaName), fieldNames_(fieldNames) {};

std::string name() const override {
return "LogicalTableScan";
}

json toCiderJSON(std::string ciderPlanId) const override {
json scanNode = json::object();
scanNode.push_back({"id", ciderPlanId});
scanNode.push_back({"name", name()});

tmp_json fieldJsonNames = tmp_json::array();
for (std::string fieldName : fieldNames_) {
fieldJsonNames.push_back(fieldName);
}
scanNode.push_back({"fieldNames", fieldJsonNames});
json tableInfoNode = json::object();
tableInfoNode.push_back({schemaName_, tableName_});
scanNode.push_back({"table", tableInfoNode});
json inputNode = json::array();
scanNode.push_back({"input", inputNode});
return scanNode;
}

const std::vector<std::shared_ptr<const CiderPlanNode>>& sources() const override;
private:
const std::string tableName_;
const std::string schemaName_;
const std::vector<std::string> fieldNames_;
};
}
Loading

0 comments on commit ff8494b

Please sign in to comment.