Turi Create  4.0
operator_properties.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_INFER_OPERATOR_FIELD_H_
7 #define TURI_SFRAME_QUERY_ENGINE_INFER_OPERATOR_FIELD_H_
8 
9 #include <core/logging/assertions.hpp>
10 #include <memory>
11 #include <vector>
12 #include <string>
13 #include <core/data/flexible_type/flexible_type.hpp>
14 
15 namespace turi { namespace query_eval {
16 
17 struct planner_node;
18 class query_operator;
19 struct query_operator_attributes;
20 
21 /**
22  * An enumeration of all operator types.
23  */
24 enum class planner_node_type : int {
25  CONSTANT_NODE,
26  APPEND_NODE,
27  BINARY_TRANSFORM_NODE,
28  LOGICAL_FILTER_NODE,
29  PROJECT_NODE,
30  RANGE_NODE,
31  SARRAY_SOURCE_NODE,
32  SFRAME_SOURCE_NODE,
33  TRANSFORM_NODE,
34  LAMBDA_TRANSFORM_NODE,
35  GENERALIZED_TRANSFORM_NODE,
36  UNION_NODE,
37  GENERALIZED_UNION_PROJECT_NODE,
38  REDUCE_NODE,
39  TERNARY_OPERATOR,
40 
41  // These are used as logical-node-only types. Do not actually become an operator.
42  IDENTITY_NODE,
43 
44  // used to denote an invalid node type. Must always be last.
45  INVALID
46 };
47 
48 
49 /**
50  * Infers the type schema of a planner node by backtracking its
51  * dependencies.
52  */
53 std::vector<flex_type_enum> infer_planner_node_type(std::shared_ptr<planner_node> pnode);
54 
55 /**
56  * Infers the length of the output of a planner node by backtracking its
57  * dependencies.
58  *
59  * Returns -1 if the length cannot be computed without an actual execution.
60  */
61 int64_t infer_planner_node_length(std::shared_ptr<planner_node> pnode);
62 
63 /**
64  * Infers the number of columns present in the output.
65  */
66 size_t infer_planner_node_num_output_columns(std::shared_ptr<planner_node> pnode);
67 
68 /** Returns the number of nodes in this planning graph, including pnode.
69  */
70 size_t infer_planner_node_num_dependency_nodes(std::shared_ptr<planner_node> pnode);
71 
72 /**
73  * Transforms a planner node into the operator.
74  */
75 std::shared_ptr<query_operator> planner_node_to_operator(std::shared_ptr<planner_node> pnode);
76 
77 /** Get the name of the node from the type.
78  */
80 
81 /** Get the type of the node from the name.
82  */
83 planner_node_type planner_node_name_to_type(const std::string& name);
84 
85 /** Get the attribute struct from the type.
86  */
88 
89 /**
90  * Attempts to prove that the two inputs have equal length.
91  * Returns a pair [can_prove, is_equal_length].
92  * If can_prove is false, we were unable to confirm that the two inputs have
93  * equal or non-equal length; the value of is_equal_length is then meaningless
94  * but it will be set to false.
95  *
96  * If can_prove is true, and is_equal_length is true, we ensure that the two
97  * have equal length. If is_equal_length is false, we ensure that the two
98  * do not have equal length.
99  */
100 std::pair<bool, bool> prove_equal_length(const std::shared_ptr<planner_node>& a,
101  const std::shared_ptr<planner_node>& b);
102 ////////////////////////////////////////////////////////////////////////////////
103 
104 /** This operator consumes all inputs at the same rate, and there
105  * is exactly one row for every input row.
106  */
108 bool consumes_inputs_at_same_rates(const std::shared_ptr<planner_node>& n);
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 
112 /** A collection of flags used in actually doing the query
113  * optimization.
114  */
116 bool is_linear_transform(const std::shared_ptr<planner_node>& n);
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 
120 /** This operator consumes all inputs at the same rate, but reduces
121  * the rows in the output.
122  */
124 bool is_sublinear_transform(const std::shared_ptr<planner_node>& n);
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 
128 /**
129  * This operator is a source node.
130  */
131 bool is_source_node(const query_operator_attributes& attr);
132 bool is_source_node(const std::shared_ptr<planner_node>& n);
133 
134 /** Returns true if the output of this node can be parallel sliceable
135  * by the sources on this block, and false otherwise.
136  */
137 bool is_parallel_slicable(const std::shared_ptr<planner_node>& n);
138 
139 /** Returns true if the graph contains only linear transformations.
140  */
141 bool is_linear_graph(const std::shared_ptr<planner_node>& n);
142 
143 /** Returns a set of integers giving the different parallel slicable
144  * units for the inputs of a particular node. If
145  */
146 std::vector<size_t> get_parallel_slicable_codes(const std::shared_ptr<planner_node>& n);
147 
148 typedef std::function<std::string(std::shared_ptr<planner_node>)> pnode_tagger;
149 
150 /** Representation of the node as a string.
151  */
152 std::string planner_node_repr(const std::shared_ptr<planner_node>& node);
153 
154 
155 std::ostream& operator<<(std::ostream&,
156  const std::shared_ptr<planner_node>& node);
157 }}
158 
159 #endif /* _INFER_OPERATOR_FIELD_H_ */
bool is_linear_transform(const query_operator_attributes &attr)
int64_t infer_planner_node_length(std::shared_ptr< planner_node > pnode)
std::string planner_node_type_to_name(planner_node_type type)
std::vector< size_t > get_parallel_slicable_codes(const std::shared_ptr< planner_node > &n)
bool is_linear_graph(const std::shared_ptr< planner_node > &n)
bool is_parallel_slicable(const std::shared_ptr< planner_node > &n)
size_t infer_planner_node_num_dependency_nodes(std::shared_ptr< planner_node > pnode)
size_t infer_planner_node_num_output_columns(std::shared_ptr< planner_node > pnode)
std::shared_ptr< query_operator > planner_node_to_operator(std::shared_ptr< planner_node > pnode)
planner_node_type planner_node_name_to_type(const std::string &name)
query_operator_attributes planner_node_type_to_attributes(planner_node_type type)
std::string planner_node_repr(const std::shared_ptr< planner_node > &node)
bool is_sublinear_transform(const query_operator_attributes &attr)
std::pair< bool, bool > prove_equal_length(const std::shared_ptr< planner_node > &a, const std::shared_ptr< planner_node > &b)
bool consumes_inputs_at_same_rates(const query_operator_attributes &attr)
std::vector< flex_type_enum > infer_planner_node_type(std::shared_ptr< planner_node > pnode)
bool is_source_node(const query_operator_attributes &attr)