forked from WesleyBlq/sql_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jd_exec_plan.h
566 lines (509 loc) · 19.5 KB
/
jd_exec_plan.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/************************************************************
Copyright (C), 2013-2015
FileName: jd_exec_plan.h
Author: qinbo
Version :
Date:
Description: exec plan generator
History:
<author> <time> <version > <desc>
qinbo 2013/9/15 1.0 build this moudle
***********************************************************/
#ifndef _JD_EXEC_PLAN_H
#define _JD_EXEC_PLAN_H
#include <stdint.h>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <map>
#include <string>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <map>
#include "parse_malloc.h"
#include "parse_node.h"
#include "utility.h"
#include "ob_define.h"
#include "ob_obj_type.h"
#include "ob_expr_obj.h"
#include "ob_logical_plan.h"
#include "ob_select_stmt.h"
#include "ob_delete_stmt.h"
#include "ob_insert_stmt.h"
#include "ob_update_stmt.h"
#include "dml_build_plan.h"
#include "crud_build_plan.h"
#include "ob_logical_plan.h"
#include "query_reduce.h"
using namespace oceanbase::common;
using namespace oceanbase::sql;
#define MAX_SQL_EXEC_PLAN_SHARD_NUM 200
#define WHERE_IS_OR_AND 0
#define WHERE_IS_SUBQUERY 1
#define SEND_SQL_TO_CONFIG_SERVER 0
#define SEND_SQL_TO_DATA_NODE 1
#define SHARD_NOT_FOUND 0xffffffff
/*��ִ�е�Ԫ�ķ�װ*/
class ExecPlanUnit
{
private:
string sql;
schema_shard* shard_info;
public:
ExecPlanUnit();
virtual ~ExecPlanUnit();
void set_exec_unit_sql(string sql_)
{
sql = sql_;
}
string get_exec_unit_sql()
{
return sql;
}
void set_exec_uint_shard_info(schema_shard* shard_info_)
{
shard_info = shard_info_;
}
schema_shard* get_exec_unit_shard_info()
{
return shard_info;
}
};
/*��ִ�мƻ��ķ�װ*/
class SameLevelExecPlan
{
private:
vector<ExecPlanUnit*> exec_plan_units;
QueryPostReduce* query_post_reduce_info; //record sql post process info
bool is_1st_plan;
char parent_sql_type; //if need to be reparsed
uint8_t sql_sent_goal; //where this sql should be sent
public:
SameLevelExecPlan();
virtual ~SameLevelExecPlan();
bool get_parent_sql_type();
void set_parent_sql_type(uint8_t parent_sql_type);
void set_first_plan_true();
void is_first_plan();
void add_exec_plan_unit(ExecPlanUnit* exec_plan_unit);
vector<ExecPlanUnit*> get_all_exec_plan_units();
void set_query_post_reduce_info(QueryPostReduce* query_post_reduce_info_)
{
query_post_reduce_info = query_post_reduce_info_;
}
QueryPostReduce* get_query_post_reduce_info()
{
return query_post_reduce_info;
}
void set_sql_sent_goal(uint8_t sql_sent_goal_)
{
sql_sent_goal = sql_sent_goal_;
}
uint8_t get_sql_sent_goal()
{
return sql_sent_goal;
}
};
/*ִ�мƻ������class*/
class FinalExecPlan
{
private:
vector<SameLevelExecPlan*> same_level_exec_plans;
bool union_is_distinct;
int to_do_exec_plan_sequ;
public:
FinalExecPlan();
virtual ~FinalExecPlan();
void set_union_is_distinct(bool union_is_distinct);
bool get_union_is_distinct();
int get_TODO_exec_plan_sequ()
{
return to_do_exec_plan_sequ;
}
void set_TODO_exec_plan_sequ(int to_do_exec_plan_sequ_)
{
to_do_exec_plan_sequ = to_do_exec_plan_sequ_;
}
void add_same_level_exec_plan(SameLevelExecPlan* same_level_exec_plan);
vector<SameLevelExecPlan*> get_all_same_level_exec_plans();
};
/*���ڲ���ִ�мƻ��Ķ�����class*/
class QueryActuator
{
public:
QueryActuator(string current_db_name);
virtual ~QueryActuator();
FinalExecPlan* popActuator();
void pushActuator(FinalExecPlan* exec_plan);
bool is_all_plan_done();
void set_all_plan_done();
void set_next_plan_reparsed();
bool get_next_plan_reparsed();
void reparse_next_plan();
/**************************************************
Funtion : init_exec_plan
Author : qinbo
Date : 2013.10.28
Description : generate exec plan
Input : string current_db_name
Output :
**************************************************/
int init_exec_plan(string current_db_name);
/**************************************************
Funtion : release_exec_plan
Author : qinbo
Date : 2013.10.28
Description : generate exec plan
Input :
Output :
**************************************************/
void release_exec_plan();
/**************************************************
Funtion : release_exec_plan
Author : qinbo
Date : 2013.10.28
Description : generate exec plan
Input :
Output :
**************************************************/
int generate_exec_plan(
string sql,
uint32_t* index = NULL);
/**************************************************
Funtion : set_final_exec_plan
Author : qinbo
Date : 2013.11.1
Description : set QueryActuator exec plans
Input :
Output :
**************************************************/
void set_final_exec_plan(FinalExecPlan* final_exec_plan_);
/**************************************************
Funtion : get_final_exec_plan
Author : qinbo
Date : 2013.11.1
Description : set QueryActuator exec plans
Input :
Output :
**************************************************/
FinalExecPlan* get_final_exec_plan();
/**************************************************
Funtion : release_exec_plan
Author : qinbo
Date : 2013.10.28
Description : generate exec plan
Input :
Output :
**************************************************/
ResultPlan get_result_plan()
{
return result_plan;
}
/**************************************************
Funtion : get_stmt_instance
Author : qinbo
Date : 2013.10.9
Description : generate exec plan
Input : ResultPlan& result_plan,
T *& stmt
Output :
**************************************************/
template <class T>
bool get_stmt_instance(
ResultPlan& result_plan,
T *& stmt)
{
ObLogicalPlan* logical_plan = static_cast<ObLogicalPlan*> (result_plan.plan_tree_);
// get statement
stmt = dynamic_cast<T*> (logical_plan->get_main_stmt());
if (stmt == NULL)
{
return false;
}
return true;
}
/*BEGIN: add by tangchao 20131225 */
int get_query_type()
{
return query_type;
}
/*END: add by tangchao 20131225 */
private:
// types and constants
bool is_plan_done;
bool is_next_plan_reparsed;
ParseResult result;
ResultPlan result_plan;
FinalExecPlan* final_exec_plan;
/*BEGIN: add by tangchao 20131225 */
int query_type;
/*END: add by tangchao 20131225 */
//internal functions
int generate_select_plan_single_table(
ResultPlan& result_plan,
FinalExecPlan* physical_plan,
ErrStat& err_stat,
const uint64_t& query_id,
uint32_t* index);
int generate_select_plan_multi_table(
ResultPlan& result_plan,
FinalExecPlan* physical_plan,
ErrStat& err_stat,
const uint64_t& query_id,
uint32_t* index);
int gen_exec_plan_update(
ResultPlan& result_plan,
FinalExecPlan* physical_plan,
ErrStat& err_stat,
const uint64_t& query_id,
uint32_t* index);
int gen_exec_plan_replace(
ResultPlan& result_plan,
FinalExecPlan* physical_plan,
ErrStat& err_stat,
const uint64_t& query_id,
uint32_t* index);
int gen_exec_plan_delete(
ResultPlan& result_plan,
FinalExecPlan* physical_plan,
ErrStat& err_stat,
const uint64_t& query_id,
uint32_t* index);
int gen_exec_plan_select(
ResultPlan& result_plan,
FinalExecPlan* physical_plan,
ErrStat& err_stat,
const uint64_t& query_id,
uint32_t* index);
int gen_exec_plan_insert(
ResultPlan& result_plan,
FinalExecPlan* physical_plan,
ErrStat& err_stat,
const uint64_t& query_id,
uint32_t* index);
template <class T>
int get_stmt(
ObLogicalPlan *logical_plan,
ErrStat& err_stat,
const uint64_t& query_id,
T *& stmt);
/**************************************************
Funtion : reparse_where_with_route_for_one_table
Author : qinbo
Date : 2013.10.17
Description : reparse distributed where conditions items
Input : ResultPlan& result_plan,
schema_table table_info,
vector<vector<ObRawExpr*> > un_opt_raw_exprs,
multimap<uint32_t, vector<ObRawExpr*> > &opted_raw_exprs,
vector<schema_shard*> &all_table_shards
Output :
**************************************************/
int reparse_where_with_route_for_one_table(
ResultPlan& result_plan,
schema_table* table_info,
vector<vector<ObRawExpr*> > &un_opt_raw_exprs,
multimap<uint32_t, vector<ObRawExpr*> > &opted_raw_exprs,
vector<schema_shard*> &all_table_shards);
/**************************************************
Funtion : reparse_where_with_route_for_multi_tables
Author : qinbo
Date : 2013.10.29
Description : reparse distributed where conditions items
Input : ResultPlan& result_plan,
ObSelectStmt *select_stmt,
vector<vector<ObRawExpr*> > &un_opt_raw_exprs,
multimap<uint32_t, vector<ObRawExpr*> > &opted_raw_exprs,
vector<vector<schema_shard*> > &all_binding_tables_shards
Output :
**************************************************/
int reparse_where_with_route_for_multi_tables(
ResultPlan& result_plan,
ObSelectStmt *select_stmt,
vector<vector<ObRawExpr*> > &un_opt_raw_exprs,
multimap<uint32_t, vector<ObRawExpr*> > &opted_raw_exprs,
vector<vector<schema_shard*> > &all_binding_tables_shards);
/**************************************************
Funtion : build_shard_exprs_array_with_route_one_table
Author : qinbo
Date : 2013.10.17
Description : generate distributed where conditions items
Input : ResultPlan& result_plan,
schema_table* table_info,
vector<ObRawExpr*> partition_sql_exprs,
vector<ObRawExpr*> atomic_exprs,
multimap<schema_shard*, vector<ObRawExpr*> > &opted_raw_exprs
vector<schema_shard*> &all_table_shards
Output :
**************************************************/
int build_shard_exprs_array_with_route_one_table(
ResultPlan& result_plan,
schema_table* table_info,
vector<ObRawExpr*> partition_sql_exprs,
vector<ObRawExpr*> atomic_exprs,
multimap<uint32_t, vector<ObRawExpr*> > &opted_raw_exprs,
vector<schema_shard*> &all_table_shards);
/**************************************************
Funtion : search_shard_from_one_table_shards
Author : qinbo
Date : 2013.12.24
Description : search one shard from one table's shards and return
index
Input : vector<schema_shard*> &table_all_shards
schema_shard* goal_shard
Output : index
**************************************************/
int search_shard_from_one_table_shards( vector<schema_shard*> &table_all_shards,
schema_shard* goal_shard);
/**************************************************
Funtion : build_shard_exprs_array_with_route_multi_table
Author : qinbo
Date : 2013.10.17
Description : generate distributed where conditions items
Input : ResultPlan& result_plan,
ObSelectStmt *select_stmt,
vector<ObRawExpr*> partition_sql_exprs,
vector<ObRawExpr*> atomic_exprs,
multimap<uint32_t, vector<ObRawExpr*> > &opted_raw_exprs
vector<vector<schema_shard*> > &all_binding_tables_shards
Output :
**************************************************/
int build_shard_exprs_array_with_route_multi_table(
ResultPlan& result_plan,
vector<ObRawExpr*> partition_sql_exprs,
vector<ObRawExpr*> atomic_exprs,
multimap<uint32_t, vector<ObRawExpr*> > &opted_raw_exprs,
vector<vector<schema_shard*> > &all_binding_tables_shards);
/**************************************************
Funtion : decompose_where_items
Author : qinbo
Date : 2013.9.24
Description : generate distributed where conditions items
Input : ObRawExpr* sql_expr
Output : vector<vector<ObRawExpr*> > &atomic_exprs_array
return :
**************************************************/
int decompose_where_items(ObRawExpr* sql_expr, vector<vector<ObRawExpr*> > &atomic_exprs_array);
/**************************************************
Funtion : search_partition_sql_exprs
Author : qinbo
Date : 2013.10.18
Description : find all expr that need to be partitioned
Input : ResultPlan& result_plan,
vector<ObRawExpr*> atomic_exprs,
vector<ObRawExpr*> &partition_sql_exprs
Output :
**************************************************/
int search_partition_sql_exprs( ResultPlan& result_plan,
vector<ObRawExpr*> &atomic_exprs,
vector<ObRawExpr*> &partition_sql_exprs);
/**************************************************
Funtion : append_distributed_where_items
Author : qinbo
Date : 2013.10.17
Description : generate distributed where conditions items
Input : ResultPlan& result_plan,
string &sql
vector<vector<ObRawExpr*> > &atomic_exprs_array
Output :
**************************************************/
void append_distributed_where_items(ResultPlan& result_plan,
string &sql,
vector<vector<ObRawExpr*> > &atomic_exprs_array);
/**************************************************
Funtion : generate_all_table_shards
Author : qinbo
Date : 2013.10.24
Description : generate all tables' shards
Input : ResultPlan& result_plan,
vector<FromItem> &from_items,
vector<vector<schema_shard*> > &all_tables_shards
Output :
**************************************************/
void generate_all_table_shards( ResultPlan& result_plan,
vector<FromItem> &from_items,
vector<vector<schema_shard*> > &all_tables_shards);
/**************************************************
Funtion : search_shard_from_multi_tables_shards
Author : qinbo
Date : 2013.12.24
Description : search one shard from all_binding_tables_shards and return
all_binding_tables_shards index
Input : vector<vector<schema_shard*> > &all_binding_tables_shards,
schema_shard* goal_shard
Output : index
**************************************************/
int search_shard_from_multi_tables_shards(vector<vector<schema_shard*> > &all_binding_tables_shards,
schema_shard* goal_shard);
/**************************************************
Funtion : vector_elem_exist_already
Author : qinbo
Date : 2013.10.30
Description : vector elem is already existing
Input : vector<schema_shard*> vector_shards,
schema_shard* single_shard
Output :
**************************************************/
bool vector_elem_exist_already( vector<schema_shard*> vector_shards,
schema_shard* single_shard);
/**************************************************
Funtion : distribute_sql_to_all_shards
Author : qinbo
Date : 2013.11.6
Description : vector elem is already existing
Input : ResultPlan& result_plan,
const uint64_t& query_id,
schema_table* table_schema,
SameLevelExecPlan* exec_plan
Output :
return :
**************************************************/
int distribute_sql_to_all_shards(
ResultPlan& result_plan,
const uint64_t& query_id,
schema_table* table_schema,
SameLevelExecPlan* exec_plan);
/**************************************************
Funtion : send_sql_to_config_server
Author : qinbo
Date : 2013.12.11
Description : send sql(show/set command) to config server
Input : ResultPlan& result_plan,
FinalExecPlan* physical_plan,
string sql
Output :
return :
**************************************************/
int send_sql_to_config_server(
ResultPlan& result_plan,
FinalExecPlan* physical_plan,
string sql);
/**************************************************
Funtion : is_from_tables_binding
Author : qinbo
Date : 2013.12.24
Description : whether all from tables are binding(shard with the same COLUMN KEY)
Input : ResultPlan& result_plan,
vector<FromItem> &from_items
Output :
return : bool
**************************************************/
bool is_from_tables_binding(ResultPlan& result_plan,
vector<FromItem> &from_items);
/**************************************************
Funtion : set_sql_dispatched_info
Author : qinbo
Date : 2013.12.24
Description : set "select_stmt" "is_sql_dispatched_multi_shards"
Input : ObSelectStmt *select_stmt,
multimap<uint32_t, vector<ObRawExpr*> > &opted_raw_exprs
Output :
return :
**************************************************/
void set_sql_dispatched_info(ObSelectStmt *select_stmt,
multimap<uint32_t, vector<ObRawExpr*> > &opted_raw_exprs);
};
#endif /* _JD_EXEC_PLAN_H */