Turi Create  4.0
operator.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_SFRAME_QUERY_ENGINE_OPERATORS_QUERY_OPERATOR_HPP
7 #define TURI_SFRAME_QUERY_ENGINE_OPERATORS_QUERY_OPERATOR_HPP
8 #include <cstddef>
9 #include <string>
10 #include <map>
11 #include <memory>
12 #include <core/util/any.hpp>
13 #include <core/data/flexible_type/flexible_type.hpp>
14 #include <core/storage/query_engine/planning/planner_node.hpp>
15 #include <core/storage/query_engine/operators/operator_properties.hpp>
16 namespace turi {
17 namespace query_eval {
18 
19 class query_context;
20 
21 /**
22  * \ingroup sframe_query_engine
23  * \addtogroup operators Logical Operators
24  * \{
25  */
26 
27 
28 /**
29  * Basic attributes about the operator.
30  */
32  enum attribute {
33  NONE = 0, ///< No attribute
34 
35  LINEAR = 1, /** A linear input operator consumes input sources at the
36  same rate and emit outputs at the same rate*/
37  SUB_LINEAR = 2, /** A sublinear operator consumes input sources at the
38  same rate, but may generate output at a different
39  lower or higher rate */
40  SOURCE = 4, /** A source operator is a direct source from an sframe
41  * or sarray and has no inputs. */
42 
43  LOGICAL_NODE_ONLY = 8, /** A node that never turns into an
44  * executor; it simply is a logical node
45  * only, possibly used in the query
46  * optimizer. */
47 
48  SUPPORTS_SKIPPING = 256, /** If the operator can correctly handle the
49  skip_next_block emit state */
50 
51 
52 
53  };
54 
55  size_t attribute_bitfield; ///< A bitfield of the attribute enum
56  int num_inputs; ///< Number of inputs expected to the operator
57 };
58 
59 /**
60  * The query operator base class.
61  *
62  * All operators must inherit from this class, implementing the
63  * virtual functions described below. The member functions describe
64  * how the class behaves, which in turn describe the capabilities of
65  * the operator and how execution is performed.
66  *
67  * In addition, all of the operators must implement a set of static
68  * functions that describe how they behave. These are:
69  *
70  * name() returns the name of the operator. Used for logging.
71  * static std::string name()
72  *
73  * make_planner_node() A factory function for creating a planner node. Takes
74  * any user defined arguments related to the operator.
75  *
76  * static std::shared_ptr<planner_node> make_planner_node(...).
77  *
78  * from_planner_node() converts the planner node to its operator form.
79  *
80  * static std::shared_ptr<query_operator> from_planner_node(std::shared_ptr<planner_node> pnode)
81  *
82  * infer_type() returns a vector of the output types for each column.
83  *
84  * static std::vector<flex_type_enum> infer_type(std::shared_ptr<planner_node> pnode)
85  *
86  * infer_length() Returns the length if known, and -1 otherwise.
87  *
88  * static int64_t infer_length(std::shared_ptr<planner_node> pnode)
89  *
90  *
91  */
93  public:
94 
95  virtual ~query_operator() = default;
96 
97  virtual planner_node_type type() const = 0;
98 
99  virtual bool coro_running() const = 0;
100 
101  /**
102  * Basic execution attributes about the query.
103  */
105  return planner_node_type_to_attributes(this->type());
106  }
107 
108  /**
109  * Pretty prints the operator including all additional parameters.
110  */
111  inline std::string name() const {
112  return planner_node_type_to_name(this->type());
113  }
114 
115  /**
116  * Pretty prints the operator including all additional parameters.
117  */
118  virtual std::string print() const {
119  return this->name();
120  }
121 
122  /**
123  * Makes a copy of the object.
124  */
125  virtual std::shared_ptr<query_operator> clone() const = 0;
126 
127  /**
128  * Executes a query.
129  */
130  virtual void execute(query_context& context) { ASSERT_TRUE(false); }
131 
132  /** The base case -- the logical-only nodes don't use this.
133  *
134  */
135  static std::shared_ptr<query_operator> from_planner_node(std::shared_ptr<planner_node>) {
136  ASSERT_TRUE(false);
137  return std::shared_ptr<query_operator>();
138  }
139 
140  static std::string repr(std::shared_ptr<planner_node> pnode, pnode_tagger& get_tag) {
141  return planner_node_type_to_name(pnode->operator_type);
142  }
143 
144 };
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 // Generic fall through operator_impl implementation
148 
149 template <planner_node_type PType>
150 class operator_impl : query_operator_attributes {
151 
152  planner_node_type type() const { return planner_node_type::INVALID; }
153 
154  std::shared_ptr<query_operator> clone() const {
155  ASSERT_TRUE(false);
156  return nullptr;
157  }
158 
159  void execute(query_context& context) {
160  ASSERT_TRUE(false);
161  }
162 };
163 
164 
165 /// \}
166 
167 } // namespace query_eval
168 } // namespace turi
169 
170 #endif // TURI_SFRAME_QUERY_ENGINE_OPERATORS_QUERY_OPERATOR_HPP
query_operator_attributes attributes()
Definition: operator.hpp:104
std::string planner_node_type_to_name(planner_node_type type)
int num_inputs
Number of inputs expected to the operator.
Definition: operator.hpp:56
size_t attribute_bitfield
A bitfield of the attribute enum.
Definition: operator.hpp:55
query_operator_attributes planner_node_type_to_attributes(planner_node_type type)
std::string name() const
Definition: operator.hpp:111
#define ASSERT_TRUE(cond)
Definition: assertions.hpp:309
virtual void execute(query_context &context)
Definition: operator.hpp:130
static std::shared_ptr< query_operator > from_planner_node(std::shared_ptr< planner_node >)
Definition: operator.hpp:135
virtual std::string print() const
Definition: operator.hpp:118