Skip to content

Commit ff8494b

Browse files
yma11Cheng Xu
authored and
Cheng Xu
committed
Add cider core including expressions and plan nodes (facebookincubator#3)
1 parent efcc413 commit ff8494b

File tree

7 files changed

+401
-0
lines changed

7 files changed

+401
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,4 @@ include_directories(.)
191191
install(FILES velox/type/Type.h DESTINATION "include/velox")
192192

193193
add_subdirectory(velox)
194+
add_subdirectory(cider)

cider/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
add_subdirectory(core)

cider/core/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
add_library(cider_core CiderPlanNode.cpp)
13+
include_directories(.)

cider/core/CiderExpressions.h

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
#pragma once
15+
#include "velox/cider/core/CiderRowExpr.h"
16+
17+
using json = nlohmann::json;
18+
19+
namespace intel::cider::core{
20+
namespace {
21+
std::string matchType(std::string type) {
22+
if (type == "int" || type == "integer" || type == "Int") {
23+
return "Integer";
24+
}
25+
if (type == "double") {
26+
return "decimal";
27+
}
28+
return type;
29+
}
30+
31+
int getTypePrecision(std::string type) {
32+
if (type == "int" || type == "integer" || type == "Int") {
33+
return 10;
34+
}
35+
if (type == "double") {
36+
return 15;
37+
}
38+
return -1;
39+
}
40+
41+
std::string matchOperator(std::string originOp) {
42+
if(originOp == "EQUAL") {
43+
return "=";
44+
}
45+
if(originOp == "GREATER_THAN") {
46+
return ">=";
47+
}
48+
if(originOp == "LESS_THAN") {
49+
return "<";
50+
}
51+
if(originOp == "MODULUS") {
52+
return "MOD";
53+
}
54+
if(originOp == "DIVIDE") {
55+
return "/";
56+
}
57+
return originOp;
58+
}
59+
}
60+
61+
class CiderConstantExpr : public CiderRowExpr {
62+
public:
63+
CiderConstantExpr(std::string value, std::string type)
64+
: type_{move(type)}, value_{move(value)} {}
65+
66+
std::string value() const{
67+
return value_;
68+
}
69+
70+
std::string type() const{
71+
return type_;
72+
}
73+
74+
json toCiderJSON() const override {
75+
json j = json::object();
76+
if (type_ == "varchar") {
77+
j.push_back({"literal", value_});
78+
j.push_back({"type", "CHAR"});
79+
j.push_back({"target_type", "CHAR"});
80+
j.push_back({"scale", -2147483648});
81+
j.push_back({"precision", value_.length()});
82+
j.push_back({"type_scale", -2147483648});
83+
j.push_back({"type_precision", value_.length()});
84+
}
85+
if (type_ == "bigint" || type_ == "integer") {
86+
j.push_back({"literal", std::stoi(value_)});
87+
j.push_back({"type", "DECIMAL"});
88+
j.push_back({"target_type", matchType(type_)});
89+
j.push_back({"scale", 0});
90+
j.push_back({"precision", value_.length()});
91+
j.push_back({"type_scale", 0});
92+
j.push_back({"type_precision", getTypePrecision(type_)});
93+
}
94+
if (type_ == "double") {
95+
int precision = value_.length() - 1;
96+
int scale = precision - value_.find(".");
97+
int pos = value_.find(".");
98+
std::string val = value_;
99+
val = val.erase(pos, 1);
100+
j.push_back({"literal", std::stoi(val)});
101+
j.push_back({"type", "DECIMAL"});
102+
j.push_back({"target_type", matchType(type_)});
103+
j.push_back({"scale", scale});
104+
j.push_back({"precision", precision});
105+
j.push_back({"type_scale", scale});
106+
j.push_back({"type_precision", precision});
107+
}
108+
if (type_ == "bool") {
109+
j.push_back({"literal", value_});
110+
j.push_back({"type", "BOOLEAN"});
111+
j.push_back({"target_type", "BOOLEAN"});
112+
j.push_back({"scale", -2147483648});
113+
j.push_back({"precision", 1});
114+
j.push_back({"type_scale", -2147483648});
115+
j.push_back({"type_precision", 1});
116+
}
117+
118+
return j;
119+
}
120+
121+
private:
122+
const std::string type_;
123+
const std::string value_;
124+
};
125+
126+
class CiderVariableExpr : public CiderRowExpr {
127+
public:
128+
CiderVariableExpr(std::string name, std::string type, int index)
129+
: name_{move(name)}, type_{move(type)}, index_(index) {}
130+
131+
json toCiderJSON() const override {
132+
json j = json::object();
133+
j.push_back({"input", std::to_string(index_)});
134+
return j;
135+
}
136+
137+
private:
138+
const std::string name_;
139+
const std::string type_;
140+
const int index_;
141+
};
142+
143+
// row expression that describes a predicate, such as "field_a > 2"
144+
class CiderCallExpr : public CiderRowExpr {
145+
public:
146+
CiderCallExpr(std::string op, std::string type, std::vector<std::shared_ptr<const CiderRowExpr>>& expressions)
147+
: op_{move(op)}, type_{move(type)}, expressions_{move(expressions)} {}
148+
149+
json toCiderJSON() const override {
150+
json opNode = json::object();
151+
opNode.push_back({"op", matchOperator(op_)});
152+
json exprNodes = json::array();
153+
for (auto expr : expressions_) {
154+
json exprNode = expr->toCiderJSON();
155+
exprNodes.push_back(exprNode);
156+
}
157+
opNode.push_back({"operands", exprNodes});
158+
json typeNode = json::object();
159+
std::string typeForChange = type_;
160+
std::transform(typeForChange.begin(), typeForChange.end(), typeForChange.begin(), ::toupper);
161+
typeNode.push_back({"type",typeForChange});
162+
typeNode.push_back({"nullable", true});
163+
opNode.push_back({"type", typeNode});
164+
return opNode;
165+
}
166+
167+
private:
168+
const std::string op_;
169+
const std::string type_;
170+
const std::vector<std::shared_ptr<const CiderRowExpr>> expressions_;
171+
};
172+
}

cider/core/CiderPlanNode.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
#include "velox/cider/core/CiderPlanNode.h"
15+
16+
namespace intel::cider::core {
17+
namespace {
18+
static const std::vector<std::shared_ptr<const CiderPlanNode>> EMPTY_SOURCES;
19+
}
20+
21+
const std::vector<std::shared_ptr<const CiderPlanNode>>& CiderTableScanNode::sources()
22+
const {
23+
return EMPTY_SOURCES;
24+
}
25+
}

cider/core/CiderPlanNode.h

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
#pragma once
15+
#include "string.h"
16+
#include "velox/cider/core/CiderRowExpr.h"
17+
#include "nlohmann/json.hpp"
18+
#include "nlohmann/fifo_map.hpp"
19+
using json = nlohmann::json;
20+
template<class K, class V, class dummy_compare, class A>
21+
using tmp_fifo_map = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>;
22+
using tmp_json = nlohmann::basic_json<tmp_fifo_map>;
23+
24+
namespace intel::cider::core{
25+
typedef std::string PlanNodeId;
26+
class CiderPlanNode {
27+
public:
28+
explicit CiderPlanNode(const PlanNodeId& id) : id_{id} {}
29+
30+
virtual ~CiderPlanNode() {}
31+
32+
const PlanNodeId& id() const {
33+
return id_;
34+
}
35+
36+
virtual const std::vector<std::shared_ptr<const CiderPlanNode>>& sources() const = 0;
37+
38+
virtual json toCiderJSON(std::string ciderPlanId) const = 0;
39+
40+
virtual std::string name() const = 0;
41+
42+
private:
43+
const PlanNodeId id_;
44+
};
45+
46+
class CiderProjectNode : public CiderPlanNode {
47+
public:
48+
CiderProjectNode(
49+
const PlanNodeId& id,
50+
std::vector<std::string> assignmentKeys,
51+
std::vector<std::shared_ptr<const CiderRowExpr>> assignmentExprs,
52+
std::shared_ptr<const CiderPlanNode> source )
53+
: CiderPlanNode(id), assignmentKeys_{assignmentKeys},
54+
assignmentExprs_(assignmentExprs), sources_{source} {}
55+
56+
json toCiderJSON(std::string ciderPlanId) const override {
57+
json projectNode = json::object();
58+
projectNode.push_back({"id", ciderPlanId});
59+
projectNode.push_back({"relOp", name()});
60+
tmp_json fieldNodes = tmp_json::array();
61+
tmp_json fieldExprs = tmp_json::array();
62+
for (int i=0; i < assignmentKeys_.size(); i++) {
63+
fieldNodes.push_back({assignmentKeys_.at(i)});
64+
fieldExprs.push_back({assignmentExprs_.at(i)->toCiderJSON()});
65+
}
66+
projectNode.push_back({"fields", fieldNodes});
67+
projectNode.push_back({"exprs", fieldExprs});
68+
69+
return projectNode;
70+
}
71+
72+
std::string name() const override {
73+
return "LogicalProject";
74+
}
75+
76+
const std::vector<std::shared_ptr<const CiderPlanNode>>& sources() const override {
77+
return sources_;
78+
}
79+
80+
private:
81+
const std::vector<std::string> assignmentKeys_;
82+
const std::vector<std::shared_ptr<const CiderRowExpr>> assignmentExprs_;
83+
const std::vector<std::shared_ptr<const CiderPlanNode>> sources_;
84+
};
85+
86+
class CiderFilterNode : public CiderPlanNode {
87+
public:
88+
CiderFilterNode(
89+
const PlanNodeId& id,
90+
std::shared_ptr<const CiderRowExpr> filter,
91+
std::shared_ptr<const CiderPlanNode> source)
92+
: CiderPlanNode(id), filter_(filter), sources_{source} {}
93+
94+
json toCiderJSON(std::string ciderPlanId) const override {
95+
json filterNode = json::object();
96+
filterNode.push_back({"id", ciderPlanId});
97+
filterNode.push_back({"relOp", name()});
98+
filterNode.push_back({"condition", filter_->toCiderJSON()});
99+
return filterNode;
100+
}
101+
102+
std::string name() const override {
103+
return "LogicalFilter";
104+
}
105+
106+
const std::vector<std::shared_ptr<const CiderPlanNode>>& sources() const override {
107+
return sources_;
108+
}
109+
110+
private:
111+
const std::shared_ptr<const CiderRowExpr> filter_;
112+
const std::vector<std::shared_ptr<const CiderPlanNode>> sources_;
113+
};
114+
115+
class CiderTableScanNode : public CiderPlanNode {
116+
public:
117+
CiderTableScanNode(
118+
const PlanNodeId& id,
119+
std::string tableName,
120+
std::string schemaName,
121+
std::vector<std::string> fieldNames)
122+
: CiderPlanNode(id), tableName_(tableName),
123+
schemaName_(schemaName), fieldNames_(fieldNames) {};
124+
125+
std::string name() const override {
126+
return "LogicalTableScan";
127+
}
128+
129+
json toCiderJSON(std::string ciderPlanId) const override {
130+
json scanNode = json::object();
131+
scanNode.push_back({"id", ciderPlanId});
132+
scanNode.push_back({"name", name()});
133+
134+
tmp_json fieldJsonNames = tmp_json::array();
135+
for (std::string fieldName : fieldNames_) {
136+
fieldJsonNames.push_back(fieldName);
137+
}
138+
scanNode.push_back({"fieldNames", fieldJsonNames});
139+
json tableInfoNode = json::object();
140+
tableInfoNode.push_back({schemaName_, tableName_});
141+
scanNode.push_back({"table", tableInfoNode});
142+
json inputNode = json::array();
143+
scanNode.push_back({"input", inputNode});
144+
return scanNode;
145+
}
146+
147+
const std::vector<std::shared_ptr<const CiderPlanNode>>& sources() const override;
148+
private:
149+
const std::string tableName_;
150+
const std::string schemaName_;
151+
const std::vector<std::string> fieldNames_;
152+
};
153+
}

0 commit comments

Comments
 (0)