6 #ifndef TURI_SFRAME_QUERY_OPTIMIZATION_APPEND_TRANSFORMS_H_ 7 #define TURI_SFRAME_QUERY_OPTIMIZATION_APPEND_TRANSFORMS_H_ 9 #include <core/storage/query_engine/planning/optimizations/optimization_transforms.hpp> 10 #include <core/storage/query_engine/planning/optimization_engine.hpp> 11 #include <core/storage/query_engine/operators/all_operators.hpp> 12 #include <core/storage/query_engine/planning/optimization_node_info.hpp> 13 #include <core/storage/query_engine/operators/operator_properties.hpp> 14 #include <core/data/flexible_type/flexible_type.hpp> 19 namespace query_eval {
21 class opt_append_transform :
public opt_transform {
23 return (t == planner_node_type::APPEND_NODE);
31 std::string description() {
return "append(source, source) -> source"; }
33 std::pair<bool, sframe> try_sframe_append(cnode_info_ptr n) {
35 for (
const auto& input: n->inputs) {
36 if (input->type != planner_node_type::SFRAME_SOURCE_NODE)
return {
false, new_sf};
37 auto begin = input->p(
"begin_index");
38 auto end = input->p(
"end_index");
40 const auto& sf = input->any_p<
sframe>(
"sframe");
42 if (begin == 0 && end == sf.size()) {
49 new_sf = new_sf.
append(sf);
52 return {
false, new_sf};
57 new_sf = n->inputs[0]->any_p<
sframe>(
"sframe");
60 return {
true, new_sf};
63 std::pair<bool, sarray<flexible_type> > try_sarray_append(cnode_info_ptr n) {
65 for (
const auto& input: n->inputs) {
66 if (input->type != planner_node_type::SARRAY_SOURCE_NODE)
return {
false, new_sa};
67 auto begin = input->p(
"begin_index");
68 auto end = input->p(
"end_index");
70 auto sa_ptr = input->any_p<std::shared_ptr<sarray<flexible_type> > >(
"sarray");
72 const auto& sa = *sa_ptr;
74 if (begin == 0 && end == sa.size()) {
76 new_sa = new_sa.
append(sa);
78 return {
false, new_sa};
82 if(new_sa.
size() == 0) {
83 new_sa = *(n->inputs[0]->any_p<std::shared_ptr<sarray<flexible_type> > >(
"sarray"));
86 return {
true, new_sa};
92 ASSERT_NE(n->inputs.size(), 0);
95 if(! ((n->inputs[0]->type == planner_node_type::SFRAME_SOURCE_NODE
96 || n->inputs[0]->type == planner_node_type::SARRAY_SOURCE_NODE)
98 (n->inputs[1]->type == planner_node_type::SFRAME_SOURCE_NODE
99 || n->inputs[1]->type == planner_node_type::SARRAY_SOURCE_NODE))) {
105 auto sframe_append_result = try_sframe_append(n);
107 if (sframe_append_result.first) {
108 auto& new_sf = sframe_append_result.second;
110 pnode_ptr new_pnode = op_sframe_source::make_planner_node(new_sf,
113 opt_manager->replace_node(n, new_pnode);
118 auto sarray_append_result = try_sarray_append(n);
119 if (sarray_append_result.first) {
120 auto& new_sa = sarray_append_result.second;
125 opt_manager->replace_node(n, new_pnode);
137 std::string description() {
return "append(source, empty_source) -> source"; }
141 if(n->inputs[1]->length() == 0) {
142 opt_manager->replace_node(n, n->inputs[0]->pnode);
146 if(n->inputs[0]->length() == 0) {
147 opt_manager->replace_node(n, n->inputs[1]->pnode);
void set_column_name(size_t column_id, const std::string &name)
size_t num_rows() const
Returns the length of each sarray.
sarray append(const sarray &other) const
std::shared_ptr< planner_node > pnode_ptr
A handy typedef.
size_t num_columns() const
Returns the number of columns in the SFrame. Does not throw.
sframe append(const sframe &other) const