CUBRID Engine  latest
optimizer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * optimizer.h - Prototypes for all public query optimizer declarations
21  */
22 
23 #ifndef _OPTIMIZER_H_
24 #define _OPTIMIZER_H_
25 
26 #ident "$Id$"
27 
28 #if defined (SERVER_MODE)
29 #error Does not belong to server module
30 #endif /* SERVER_MODE */
31 
32 #include <stdarg.h>
33 #include <assert.h>
34 
35 #include "error_manager.h"
36 #include "memory_alloc.h"
37 #include "parser.h"
38 #include "release_string.h"
39 #include "parser.h"
40 
41 // forward definitions
42 struct xasl_node;
43 class regu_variable_node;
44 
45 /*
46  * These #defines are used in conjunction with assert() to announce
47  * unexpected conditions.
48  */
49 #define UNEXPECTED_CASE 0
50 #define UNREACHABLE 0
51 
52 #define QO_ASSERT(env, cond) \
53  do { \
54  if (!(cond)) \
55  qo_abort((env), __FILE__, __LINE__); \
56  } while(0)
57 
58 
59 #define QO_ABORT(env) qo_abort((env), __FILE__, __LINE__)
60 
61 #ifdef USE_ER_SET
62 #define QO_WARN(code) er_set(ER_WARNING_SEVERITY, ARG_FILE_LINE, (code), 0)
63 #define QO_WARN1(code, x) er_set(ER_WARNING_SEVERITY, ARG_FILE_LINE, (code), 1, x)
64 #define QO_ERROR(code) er_set(ER_ERROR_SEVERITY, ARG_FILE_LINE, (code), 0)
65 #define QO_ERROR1(code, x) er_set(ER_ERROR_SEVERITY, ARG_FILE_LINE, (code), 1, x)
66 #define QO_ERROR2(code, x, y) er_set(ER_ERROR_SEVERITY, ARG_FILE_LINE, (code), 2, x, y)
67 #else /* USE_ER_SET */
68 #define QO_WARN(code)
69 #define QO_WARN1(code, x)
70 #define QO_ERROR(code)
71 #define QO_ERROR1(code, x)
72 #define QO_ERROR2(code, x, y)
73 #endif /* USE_ER_SET */
74 
75 /*
76  * The PRM_OPTIMIZATION_LEVEL parameter actually encodes (at least) two
77  * different pieces of information: the actual level of optimization
78  * desired, and whether we're supposed to dump a readable version of the
79  * plan. These macros encapsulate the representation decisions for that
80  * information.
81  */
82 #define OPT_LEVEL(level) ((level) & 0xff)
83 #define OPTIMIZATION_ENABLED(level) (OPT_LEVEL(level) != 0)
84 #define PLAN_DUMP_ENABLED(level) ((level) >= 0x100)
85 #define SIMPLE_DUMP(level) ((level) & 0x100)
86 #define DETAILED_DUMP(level) ((level) & 0x200)
87 
88 typedef struct qo_env QO_ENV;
89 typedef struct qo_node QO_NODE;
90 typedef struct qo_segment QO_SEGMENT;
91 typedef struct qo_term QO_TERM;
92 typedef struct qo_eqclass QO_EQCLASS;
93 typedef struct qo_subquery QO_SUBQUERY;
94 typedef struct qo_planner QO_PLANNER;
95 typedef struct qo_info QO_INFO;
96 typedef struct qo_partition QO_PARTITION;
98 typedef struct qo_attr_info QO_ATTR_INFO;
99 typedef struct qo_summary QO_SUMMARY;
100 typedef struct qo_index QO_INDEX;
104 typedef struct qo_index_xasl_info QO_INDEX_XASL_INFO;
107 typedef struct bitset BITSET;
108 
110 {
113  double cardinality;
115 };
116 
117 typedef struct
118 {
119  DB_TYPE type; /* data type of the attribute */
120  int leafs; /* number of leaf pages including overflow pages */
121  int pages; /* number of total pages */
122  int height; /* the height of the B+tree */
123  int keys; /* number of keys */
124  TP_DOMAIN *key_type; /* The key type for the B+tree */
125  int pkeys_size; /* pkeys array size */
126  int *pkeys; /* partial keys info for example: index (a, b, ..., x) pkeys[0] -> # of {a} pkeys[1] ->
127  * # of {a, b} ... pkeys[key_size-1] -> # of {a, b, ..., x} */
131 
132 typedef struct qo_plan QO_PLAN;
133 
135 
136 typedef enum
137 {
140 } QO_PARAM;
141 
142 typedef struct qo_limit_info
143 {
146 } QO_LIMIT_INFO;
147 
148 extern QO_NODE *lookup_node (PT_NODE * attr, QO_ENV * env, PT_NODE ** entity);
149 
150 extern QO_SEGMENT *lookup_seg (QO_NODE * head, PT_NODE * name, QO_ENV * env);
151 
152 extern void qo_expr_segs (QO_ENV * env, PT_NODE * pt_expr, BITSET * result);
153 
154 extern void qo_get_optimization_param (void *, QO_PARAM, ...);
155 extern bool qo_need_skip_execution (void);
156 extern void qo_set_optimization_param (void *, QO_PARAM, ...);
158 extern xasl_node *qo_to_xasl (QO_PLAN *, xasl_node *);
159 extern void qo_plan_discard (QO_PLAN *);
160 extern void qo_plan_dump (QO_PLAN *, FILE *);
161 extern const char *qo_plan_set_cost_fn (const char *, int);
162 extern int qo_plan_get_cost_fn (const char *);
164 extern bool qo_plan_skip_orderby (QO_PLAN * plan);
165 extern bool qo_plan_skip_groupby (QO_PLAN * plan);
166 extern bool qo_is_index_covering_scan (QO_PLAN * plan);
167 extern bool qo_is_index_iss_scan (QO_PLAN * plan);
168 extern bool qo_is_index_loose_scan (QO_PLAN * plan);
169 extern bool qo_is_index_mro_scan (QO_PLAN * plan);
170 extern bool qo_plan_multi_range_opt (QO_PLAN * plan);
171 extern void qo_set_cost (DB_OBJECT * target, DB_VALUE * result, DB_VALUE * plan, DB_VALUE * cost);
172 
173 /*
174  * QO_XASL support functions
175  */
176 extern int qo_xasl_get_num_terms (QO_XASL_INDEX_INFO * info);
178 extern PT_NODE *qo_check_nullable_expr (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk);
179 extern PT_NODE *mq_optimize (PARSER_CONTEXT * parser, PT_NODE * statement);
180 
181 #if 0
182 extern void *qo_malloc (QO_ENV *, unsigned, const char *, int);
183 #endif
184 extern void qo_abort (QO_ENV *, const char *, int);
185 
186 extern double qo_expr_selectivity (QO_ENV * env, PT_NODE * pt_expr);
187 
189 
191 
193  bool ignore_lower);
194 
195 extern bool qo_check_iscan_for_multi_range_opt (QO_PLAN * plan);
196 extern bool qo_check_join_for_multi_range_opt (QO_PLAN * plan);
197 extern int qo_find_subplan_using_multi_range_opt (QO_PLAN * plan, QO_PLAN ** result, int *join_idx);
198 extern void qo_top_plan_print_json (PARSER_CONTEXT * parser, xasl_node * xasl, PT_NODE * select, QO_PLAN * plan);
199 extern void qo_top_plan_print_text (PARSER_CONTEXT * parser, xasl_node * xasl, PT_NODE * select, QO_PLAN * plan);
200 #endif /* _OPTIMIZER_H_ */
bool qo_check_iscan_for_multi_range_opt(QO_PLAN *plan)
bool qo_plan_skip_orderby(QO_PLAN *plan)
void qo_plan_dump(QO_PLAN *, FILE *)
void qo_top_plan_print_text(PARSER_CONTEXT *parser, xasl_node *xasl, PT_NODE *select, QO_PLAN *plan)
void qo_get_optimization_param(void *, QO_PARAM,...)
Definition: query_graph.c:269
Definition: query_graph.h:209
void qo_expr_segs(QO_ENV *env, PT_NODE *pt_expr, BITSET *result)
Definition: query_graph.c:2734
DB_TYPE
Definition: dbtype_def.h:670
double qo_expr_selectivity(QO_ENV *env, PT_NODE *pt_expr)
Definition: query_graph.h:243
PT_NODE ** qo_xasl_get_terms(QO_XASL_INDEX_INFO *)
void qo_set_optimization_param(void *, QO_PARAM,...)
Definition: query_graph.c:316
void qo_set_cost(DB_OBJECT *target, DB_VALUE *result, DB_VALUE *plan, DB_VALUE *cost)
double fixed_io_cost
Definition: optimizer.h:111
QO_PLAN * qo_optimize_query(PARSER_CONTEXT *, PT_NODE *)
Definition: query_graph.c:358
PT_NODE * qo_plan_iscan_sort_list(QO_PLAN *)
const char * qo_plan_set_cost_fn(const char *, int)
bool qo_check_join_for_multi_range_opt(QO_PLAN *plan)
TP_DOMAIN * key_type
Definition: optimizer.h:124
struct qo_limit_info QO_LIMIT_INFO
bool qo_is_index_loose_scan(QO_PLAN *plan)
regu_variable_node * lower
Definition: optimizer.h:144
int qo_xasl_get_num_terms(QO_XASL_INDEX_INFO *info)
unsigned int BITSET
Definition: esql_misc.h:94
void qo_top_plan_print_json(PARSER_CONTEXT *parser, xasl_node *xasl, PT_NODE *select, QO_PLAN *plan)
bool qo_is_index_mro_scan(QO_PLAN *plan)
SP_PARSER_CTX * parser
double variable_io_cost
Definition: optimizer.h:112
bool qo_is_index_covering_scan(QO_PLAN *plan)
double cardinality
Definition: optimizer.h:113
double fixed_cpu_cost
Definition: optimizer.h:111
bool qo_plan_multi_range_opt(QO_PLAN *plan)
bool qo_is_index_iss_scan(QO_PLAN *plan)
bool qo_need_skip_execution(void)
Definition: query_graph.c:297
xasl_node * qo_to_xasl(QO_PLAN *, xasl_node *)
PT_NODE * qo_check_nullable_expr(PARSER_CONTEXT *parser, PT_NODE *node, void *arg, int *continue_walk)
QO_LIMIT_INFO * qo_get_key_limit_from_ordbynum(PARSER_CONTEXT *parser, QO_PLAN *plan, xasl_node *xasl, bool ignore_lower)
regu_variable_node * upper
Definition: optimizer.h:145
xasl_node * xasl
Definition: optimizer.h:114
int qo_find_subplan_using_multi_range_opt(QO_PLAN *plan, QO_PLAN **result, int *join_idx)
int qo_plan_get_cost_fn(const char *)
void qo_plan_discard(QO_PLAN *)
xasl_node * qo_add_hq_iterations_access_spec(QO_PLAN *plan, xasl_node *xasl)
QO_NODE * lookup_node(PT_NODE *attr, QO_ENV *env, PT_NODE **entity)
Definition: query_graph.c:1357
bool qo_plan_skip_groupby(QO_PLAN *plan)
struct qo_index_xasl_info QO_INDEX_XASL_INFO
Definition: optimizer.h:104
QO_SEGMENT * lookup_seg(QO_NODE *head, PT_NODE *name, QO_ENV *env)
Definition: query_graph.c:1558
PT_NODE * mq_optimize(PARSER_CONTEXT *parser, PT_NODE *statement)
QO_PARAM
Definition: optimizer.h:136
Definition: query_graph.h:99
QO_LIMIT_INFO * qo_get_key_limit_from_instnum(PARSER_CONTEXT *parser, QO_PLAN *plan, xasl_node *xasl)
double variable_cpu_cost
Definition: optimizer.h:112
void qo_abort(QO_ENV *, const char *, int)
Definition: query_graph.c:5796