OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CardinalityEstimator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
25 #ifndef QUERYENGINE_CARDINALITYESTIMATOR_H
26 #define QUERYENGINE_CARDINALITYESTIMATOR_H
27 
28 #include "RelAlgExecutionUnit.h"
29 
30 #include "../Analyzer/Analyzer.h"
31 #include "Shared/Logger.h"
32 
33 class CardinalityEstimationRequired : public std::runtime_error {
34  public:
35  CardinalityEstimationRequired() : std::runtime_error("CardinalityEstimationRequired") {}
36 };
37 
38 namespace Analyzer {
39 
40 /*
41  * @type Estimator
42  * @brief Infrastructure to define estimators which take an expression tuple, are called
43  * for every row and need a buffer to track state.
44  */
45 class Estimator : public Analyzer::Expr {
46  public:
48 
49  // The tuple argument received by the estimator for every row.
50  virtual const std::list<std::shared_ptr<Analyzer::Expr>>& getArgument() const = 0;
51 
52  // The size of the working buffer used by the estimator.
53  virtual size_t getBufferSize() const = 0;
54 
55  // The name for the estimator runtime function which is called for every row.
56  // The runtime function will receive four arguments:
57  // uint8_t* the pointer to the beginning of the estimator buffer
58  // uint32_t the size of the estimator buffer, in bytes
59  // uint8_t* the concatenated bytes for the argument tuple
60  // uint32_t the size of the argument tuple, in bytes
61  virtual std::string getRuntimeFunctionName() const = 0;
62 
63  std::shared_ptr<Analyzer::Expr> deep_copy() const override {
64  CHECK(false);
65  return nullptr;
66  }
67 
68  bool operator==(const Expr& rhs) const override {
69  CHECK(false);
70  return false;
71  }
72 
73  std::string toString() const override {
74  CHECK(false);
75  return "";
76  }
77 };
78 
79 /*
80  * @type NDVEstimator
81  * @brief Provides an estimate for the number of distinct tuples. Not a real
82  * Analyzer expression, it's only used in RelAlgExecutionUnit synthesized
83  * for the cardinality estimation before running an user-provided query.
84  */
86  public:
87  NDVEstimator(const std::list<std::shared_ptr<Analyzer::Expr>>& expr_tuple)
88  : expr_tuple_(expr_tuple) {}
89 
90  const std::list<std::shared_ptr<Analyzer::Expr>>& getArgument() const override {
91  return expr_tuple_;
92  }
93 
94  size_t getBufferSize() const override { return 1024 * 1024; }
95 
96  std::string getRuntimeFunctionName() const override {
97  return "linear_probabilistic_count";
98  }
99 
100  private:
101  const std::list<std::shared_ptr<Analyzer::Expr>> expr_tuple_;
102 };
103 
104 } // namespace Analyzer
105 
107 
109  const RelAlgExecutionUnit& ra_exe_unit,
110  std::shared_ptr<Analyzer::Expr> replacement_target);
111 
113  const RelAlgExecutionUnit& ra_exe_unit,
114  std::vector<std::pair<ResultSetPtr, std::vector<size_t>>>& results_per_device);
115 
116 #endif // QUERYENGINE_CARDINALITYESTIMATOR_H
const std::list< std::shared_ptr< Analyzer::Expr > > expr_tuple_
const std::list< std::shared_ptr< Analyzer::Expr > > & getArgument() const override
std::shared_ptr< Analyzer::Expr > deep_copy() const override
virtual std::string getRuntimeFunctionName() const =0
std::string getRuntimeFunctionName() const override
bool operator==(const Expr &rhs) const override
std::shared_ptr< ResultSet > ResultSetPtr
RelAlgExecutionUnit create_ndv_execution_unit(const RelAlgExecutionUnit &ra_exe_unit)
RelAlgExecutionUnit create_count_all_execution_unit(const RelAlgExecutionUnit &ra_exe_unit, std::shared_ptr< Analyzer::Expr > replacement_target)
NDVEstimator(const std::list< std::shared_ptr< Analyzer::Expr >> &expr_tuple)
CHECK(cgen_state)
bool g_enable_smem_group_by true
virtual size_t getBufferSize() const =0
size_t getBufferSize() const override
ResultSetPtr reduce_estimator_results(const RelAlgExecutionUnit &ra_exe_unit, std::vector< std::pair< ResultSetPtr, std::vector< size_t >>> &results_per_device)
Definition: sqltypes.h:48
std::string toString() const override
Execution unit for relational algebra. It&#39;s a low-level description of any relational algebra operati...
virtual const std::list< std::shared_ptr< Analyzer::Expr > > & getArgument() const =0