forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_select_stmt_printer.cpp
More file actions
606 lines (576 loc) · 22.1 KB
/
Copy pathob_select_stmt_printer.cpp
File metadata and controls
606 lines (576 loc) · 22.1 KB
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
* Copyright (c) 2025 OceanBase.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define USING_LOG_PREFIX SQL
#include "sql/printer/ob_select_stmt_printer.h"
namespace oceanbase
{
using namespace common;
namespace sql
{
void ObSelectStmtPrinter::init(char *buf, int64_t buf_len, int64_t *pos,
ObSelectStmt *stmt,
ObIArray<ObString> *column_list)
{
ObDMLStmtPrinter::init(buf, buf_len, pos, stmt);
column_list_ = column_list;
}
int ObSelectStmtPrinter::do_print()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt should not be NULL", K(ret));
} else if (!stmt_->is_select_stmt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Not a valid select stmt", K(stmt_->get_stmt_type()), K(ret));
} else {
const ObSelectStmt *select_stmt = static_cast<const ObSelectStmt *>(stmt_);
if (OB_UNLIKELY(NULL != column_list_
&& column_list_->count() != select_stmt->get_select_item_size())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("column_list size should be equal select_item size", K(ret),
K(column_list_->count()), K(select_stmt->get_select_item_size()));
} else {
expr_printer_.init(buf_,
buf_len_,
pos_,
schema_guard_,
print_params_,
param_store_);
if (OB_FAIL(SMART_CALL(print()))) {
LOG_WARN("fail to print stmt", KPC(stmt_), K(ret));
}
}
}
return ret;
}
int ObSelectStmtPrinter::print()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ should not be NULL", K(ret));
} else if (!stmt_->is_select_stmt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Not a valid select stmt", K(stmt_->get_stmt_type()), K(ret));
} else {
const ObSelectStmt *select_stmt = static_cast<const ObSelectStmt*>(stmt_);
if (OB_FAIL(print_with())) {
LOG_WARN("print with failed");
} else if (OB_FAIL(print_temp_table_as_cte())) {
LOG_WARN("failed to print cte", K(ret));
} else if (select_stmt->is_set_stmt()) {
if (select_stmt->is_recursive_union() &&
!print_params_.print_origin_stmt_) {
// for dblink, print a embeded recursive union query block
if (OB_FAIL(print_recursive_union_stmt())) {
LOG_WARN("failed to print recursive union stmt", K(ret));
}
} else if (OB_FAIL(print_set_op_stmt())) {
LOG_WARN("fail to print set_op stmt", K(ret), K(*stmt_));
}
} else if (OB_FAIL(print_basic_stmt())) {
LOG_WARN("fail to print basic stmt", K(ret), K(*stmt_));
}
}
return ret;
}
int ObSelectStmtPrinter::print_set_op_stmt()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ should not be NULL", K(ret));
} else if (!stmt_->is_select_stmt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Not a valid select stmt", K(stmt_->get_stmt_type()), K(ret));
} else {
const ObSelectStmt *select_stmt = static_cast<const ObSelectStmt*>(stmt_);
// todo: Currently union flattening cannot guarantee to be the same as the original sql
ObSEArray<ObSelectStmt*, 2> child_stmts;
if (!select_stmt->is_set_stmt() || 2 > select_stmt->get_set_query().count()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ should have set_op", K(ret), K(select_stmt->is_set_stmt()),
K(select_stmt->get_set_query().count()));
} else if (OB_FAIL(child_stmts.assign(select_stmt->get_set_query()))) {
LOG_WARN("failed to assign stmts", K(ret));
} else if (OB_ISNULL(child_stmts.at(0))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("child_stmt should not be NULL", K(ret));
} else {
if (select_stmt->get_children_swapped()) {
std::swap(child_stmts.at(0), child_stmts.at(1));
}
DATA_PRINTF("(");
ObSelectStmtPrinter stmt_printer(buf_,
buf_len_,
pos_,
child_stmts.at(0),
schema_guard_,
print_params_,
param_store_,
/*force_col_alias*/true,
session_);
stmt_printer.set_column_list(column_list_);
stmt_printer.set_is_first_stmt_for_hint(is_first_stmt_for_hint_);
ObString set_op_str = ObString::make_string(
ObSelectStmt::set_operator_str(select_stmt->get_set_op()));
if (OB_FAIL(stmt_printer.do_print())) {
LOG_WARN("fail to print left stmt", K(ret), K(*child_stmts.at(0)));
} else {
stmt_printer.set_is_first_stmt_for_hint(false);
DATA_PRINTF(")");
}
for (int64_t i = 1; OB_SUCC(ret) && i < child_stmts.count(); ++i) {
DATA_PRINTF(" %.*s ", LEN_AND_PTR(set_op_str)); // print set_op
if (!select_stmt->is_set_distinct()) {
DATA_PRINTF(" all ");
}
if (OB_FAIL(ret)) {
} else if (OB_ISNULL(child_stmts.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("child_stmt should not be NULL", K(ret));
} else {
DATA_PRINTF("(");
stmt_printer.init(buf_, buf_len_, pos_, child_stmts.at(i), column_list_);
if (OB_FAIL(stmt_printer.do_print())) {
LOG_WARN("fail to print child stmt", K(ret));
} else {
DATA_PRINTF(")");
}
}
}
if (OB_FAIL(ret)) {
} else if (OB_FAIL(print_order_by())) {
LOG_WARN("fail to print order by",K(ret));
} else if (OB_FAIL(print_limit())) {
LOG_WARN("fail to print limit", K(ret));
} else if (OB_FAIL(print_fetch())) {
LOG_WARN("fail to print fetch", K(ret));
} else if (OB_FAIL(print_with_check_option())) {
LOG_WARN("fail to print with check option", K(ret));
}
}
}
return ret;
}
int ObSelectStmtPrinter::print_recursive_union_stmt()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ should not be NULL", K(ret));
} else if (!stmt_->is_select_stmt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Not a valid select stmt", K(stmt_->get_stmt_type()), K(ret));
} else {
const ObSelectStmt *select_stmt = static_cast<const ObSelectStmt*>(stmt_);
TableItem *table = NULL;
if (OB_FAIL(find_recursive_cte_table(select_stmt, table))) {
LOG_WARN("failed to find recursive cte table", K(ret));
} else if (OB_ISNULL(table)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null table item", K(ret));
} else {
DATA_PRINTF("WITH RECURSIVE ");
DATA_PRINTF("%.*s", LEN_AND_PTR(table->table_name_));
if (OB_FAIL(print_cte_define_title(select_stmt))) {
LOG_WARN("failed to printf cte title", K(ret));
} else {
DATA_PRINTF("(");
if (OB_FAIL(print_set_op_stmt())) {
LOG_WARN("failed to print", K(ret));
} else {
DATA_PRINTF(")");
DATA_PRINTF(" select * from ");
DATA_PRINTF("%.*s", LEN_AND_PTR(table->table_name_));
}
}
}
}
return ret;
}
int ObSelectStmtPrinter::print_basic_stmt()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ should not be NULL", K(ret));
} else if (OB_FAIL(print_select())) {
LOG_WARN("fail to print select", K(ret), K(*stmt_));
} else if (OB_FAIL(print_from())) {
LOG_WARN("fail to print from", K(ret), K(*stmt_));
} else if (OB_FAIL(print_where())) {
LOG_WARN("fail to print where", K(ret), K(*stmt_));
} else if (OB_FAIL(print_group_by())) {
LOG_WARN("fail to print group by", K(ret), K(*stmt_));
} else if (OB_FAIL(print_having())) {
LOG_WARN("fail to print having", K(ret), K(*stmt_));
} else if (OB_FAIL(print_order_by())) {
LOG_WARN("fail to print order by", K(ret), K(*stmt_));
} else if (OB_FAIL(print_approx())) {
LOG_WARN("fail to print order by", K(ret), K(*stmt_));
} else if (OB_FAIL(print_limit())) {
LOG_WARN("fail to print limit", K(ret), K(*stmt_));
} else if (OB_FAIL(print_vector_index_query_param())) {
LOG_WARN("fail to print vector index query params", K(ret), K(*stmt_));
} else if (OB_FAIL(print_fetch())) {
LOG_WARN("fail to print fetch", K(ret), K(*stmt_));
} else if (OB_FAIL(print_with_check_option())) {
LOG_WARN("fail to print with check option", K(ret));
} else if (OB_FAIL(print_for_update())) {
LOG_WARN("fail to print for update", K(ret), K(*stmt_));
} else {
// do-nothing
}
return ret;
}
int ObSelectStmtPrinter::print_select()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_) || OB_ISNULL(buf_) || OB_ISNULL(pos_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ is NULL or buf_ is NULL or pos_ is NULL", K(ret));
} else if (!stmt_->is_select_stmt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Not a valid select stmt", K(stmt_->get_stmt_type()), K(ret));
} else {
const ObSelectStmt *select_stmt = static_cast<const ObSelectStmt*>(stmt_);
DATA_PRINTF("select ");
if (OB_SUCC(ret)) {
if (OB_FAIL(print_hint())) { // hint
LOG_WARN("fail to print hint", K(ret), K(*select_stmt));
} else {
if (select_stmt->is_select_straight_join()) { // straight_join
DATA_PRINTF("straight_join ");
}
if (select_stmt->has_distinct()) { // distinct
DATA_PRINTF("distinct ");
}
if (select_stmt->get_select_item_size() == 0) {
DATA_PRINTF("1 ");
}
for (int64_t i = 0; OB_SUCC(ret) && i < select_stmt->get_select_item_size(); ++i) {
const SelectItem &select_item = select_stmt->get_select_item(i);
if (select_item.is_implicit_added_ || select_item.implicit_filled_) {
continue;
}
ObRawExpr *expr = select_item.expr_;
// mysql will add an alias to the select_item of the top-level stmt in view_definition (only the top level)
// Alias rule see case:
// create view(a,b) v as select c1,c2 as alias from t1;
// replaced with: select c1 as a, c2 as b from t1
// Therefore need to replace the func_name of the corresponding alias expression in the top-level stmt with column_name
bool need_add_alias = need_print_alias() || select_item.is_real_alias_;
if (OB_SUCC(ret)) {
ObRawExpr *tmp_expr = expr;
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expr is null", K(ret), K(expr));
} else if (OB_FAIL(ObRawExprUtils::erase_inner_added_exprs(tmp_expr, expr))) {
LOG_WARN("erase inner cast expr failed", K(ret));
} else if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expr is null");
} else if (need_add_alias && NULL != column_list_ && select_item.is_real_alias_) {
expr->set_alias_column_name(column_list_->at(i));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(expr_printer_.do_print(expr, T_FIELD_LIST_SCOPE))) {
LOG_WARN("fail to print select expr", K(ret));
}
}
if (OB_SUCC(ret) && need_add_alias) {
ObString alias_string;
if (NULL != column_list_) {
alias_string = column_list_->at(i);
} else if (!select_item.alias_name_.empty()) {
alias_string = select_item.alias_name_;
} else {
alias_string = select_item.expr_name_;
}
/* In Oracle mode, due to some function aliases possibly appearing with double quotes "", which will cause errors during secondary parsing, it is necessary to remove these double quotes
*
*/
ObArenaAllocator arena_alloc;
DATA_PRINTF(" AS ");
PRINT_IDENT_WITH_QUOT(alias_string);
}
DATA_PRINTF(",");
}
if (OB_SUCC(ret)) {
--*pos_;
}
}
// select_items
}
}
return ret;
}
int ObSelectStmtPrinter::print_group_by()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_) || OB_ISNULL(buf_) || OB_ISNULL(pos_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ is NULL or buf_ is NULL or pos_ is NULL", K(ret));
} else if (!stmt_->is_select_stmt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Not a valid select stmt", K(stmt_->get_stmt_type()), K(ret));
} else {
const ObSelectStmt *select_stmt = static_cast<const ObSelectStmt*>(stmt_);
const ObIArray<ObRawExpr*> &group_exprs = select_stmt->get_group_exprs();
const ObIArray<ObRawExpr*> &rollup_exprs = select_stmt->get_rollup_exprs();
int64_t group_exprs_size = group_exprs.count();
int64_t rollup_exprs_size = rollup_exprs.count();
if (group_exprs_size + rollup_exprs_size > 0) {
DATA_PRINTF(" group by ");
// print exprs
for (int64_t i = 0; OB_SUCC(ret) && i < group_exprs_size; ++i) {
if (OB_FAIL(print_expr_except_const_number(group_exprs.at(i), T_GROUP_SCOPE))) {
LOG_WARN("fail to print group expr", K(ret));
}
DATA_PRINTF(",");
}
// print rollup
if (OB_SUCC(ret)) {
if (lib::is_mysql_mode() && rollup_exprs_size > 0) {
for (int64_t i = 0; OB_SUCC(ret) && i < rollup_exprs_size; ++i) {
if (OB_FAIL(print_expr_except_const_number(rollup_exprs.at(i), T_GROUP_SCOPE))) {
LOG_WARN("fail to print group expr", K(ret));
}
DATA_PRINTF(",");
}
if (OB_SUCC(ret)) {
--*pos_;
}
DATA_PRINTF(" with rollup ");
} else if (rollup_exprs_size > 0) {
DATA_PRINTF(" rollup( ");
for (int64_t i = 0; OB_SUCC(ret) && i < rollup_exprs_size; ++i) {
if (OB_FAIL(print_expr_except_const_number(rollup_exprs.at(i), T_GROUP_SCOPE))) {
LOG_WARN("fail to print group expr", K(ret));
}
DATA_PRINTF(",");
}
if (OB_SUCC(ret)) {
--*pos_;
}
DATA_PRINTF("),");
}
}
// remove ","
if (OB_SUCC(ret)) {
--*pos_;
}
}
}
return ret;
}
int ObSelectStmtPrinter::print_having()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_) || OB_ISNULL(buf_) || OB_ISNULL(pos_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ is NULL or buf_ is NULL or pos_ is NULL", K(ret));
} else if (!stmt_->is_select_stmt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Not a valid select stmt", K(stmt_->get_stmt_type()), K(ret));
} else {
const ObSelectStmt *select_stmt = static_cast<const ObSelectStmt*>(stmt_);
const ObIArray<ObRawExpr*> &having_exprs = select_stmt->get_having_exprs();
int64_t having_exprs_size = having_exprs.count();
if (having_exprs_size > 0) {
DATA_PRINTF(" having ");
for (int64_t i = 0; OB_SUCC(ret) && i < having_exprs_size; ++i) {
if (OB_FAIL(expr_printer_.do_print(having_exprs.at(i), T_HAVING_SCOPE))) {
LOG_WARN("fail to print having expr", K(ret));
}
DATA_PRINTF(" and ");
}
if (OB_SUCC(ret)) {
*pos_ -= 5; // strlen(" and ")
}
}
}
return ret;
}
int ObSelectStmtPrinter::print_order_by()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_) || OB_ISNULL(buf_) || OB_ISNULL(pos_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ is NULL or buf_ is NULL or pos_ is NULL", K(ret));
} else if (!stmt_->is_select_stmt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Not a valid select stmt", K(stmt_->get_stmt_type()), K(ret));
} else {
const ObSelectStmt *select_stmt = static_cast<const ObSelectStmt*>(stmt_);
ObArenaAllocator alloc;
ObConstRawExpr expr(alloc);
int64_t order_item_size = select_stmt->get_order_item_size();
if (order_item_size > 0) {
DATA_PRINTF(" order by");
for (int64_t i = 0; OB_SUCC(ret) && i < order_item_size; ++i) {
const OrderItem &order_item = select_stmt->get_order_item(i);
ObRawExpr *order_expr = order_item.expr_;
int64_t sel_item_pos = 1;
bool found = false;
if (OB_ISNULL(order_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null", K(ret));
} else if (T_FUN_SYS_CAST == order_expr->get_expr_type() &&
CM_IS_IMPLICIT_CAST(order_expr->get_cast_mode())) {
order_expr = order_expr->get_param_expr(0);
}
for (int64_t j = 0; OB_SUCC(ret) && !found && j < select_stmt->get_select_item_size(); ++j) {
const SelectItem &select_item = select_stmt->get_select_item(j);
ObRawExpr *select_expr = select_item.expr_;
bool skip = false;
if (OB_ISNULL(select_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null", K(ret));
} else if (select_item.is_implicit_added_ || select_item.implicit_filled_) {
skip = true;
} else if (T_FUN_SYS_CAST == select_expr->get_expr_type() &&
CM_IS_IMPLICIT_CAST(select_expr->get_cast_mode())) {
select_expr = select_expr->get_param_expr(0);
}
if (OB_SUCC(ret)) {
if (skip) {
} else if (order_expr == select_expr) {
found = true;
} else {
sel_item_pos ++;
}
}
}
if (found) {
DATA_PRINTF(" %ld", sel_item_pos);
} else {
DATA_PRINTF(" ");
if (FAILEDx(print_expr_except_const_number(order_item.expr_, T_ORDER_SCOPE))) {
LOG_WARN("fail to print order by expr", K(ret));
}
}
if (OB_SUCC(ret)) {
if (lib::is_mysql_mode()) {
if (is_descending_direction(order_item.order_type_)) {
DATA_PRINTF(" desc");
}
} else if (order_item.order_type_ == NULLS_FIRST_ASC) {
DATA_PRINTF(" asc nulls first");
} else if (order_item.order_type_ == NULLS_LAST_ASC) {//use default value
/*do nothing*/
} else if (order_item.order_type_ == NULLS_FIRST_DESC) {//use default value
DATA_PRINTF(" desc");
} else if (order_item.order_type_ == NULLS_LAST_DESC) {
DATA_PRINTF(" desc nulls last");
} else {/*do nothing*/}
DATA_PRINTF(",");
}
}
if (OB_SUCC(ret)) {
--*pos_;
}
}
}
return ret;
}
int ObSelectStmtPrinter::print_for_update()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_) || OB_ISNULL(buf_) || OB_ISNULL(pos_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ is NULL or buf_ is NULL or pos_ is NULL", K(ret));
} else if (!stmt_->is_select_stmt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Not a valid select stmt", K(stmt_->get_stmt_type()), K(ret));
} else {
const ObSelectStmt *select_stmt = static_cast<const ObSelectStmt*>(stmt_);
if (select_stmt->get_table_size() > 0) {
const TableItem *table_item = select_stmt->get_table_item(0);
if (OB_ISNULL(table_item)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("table item is NULL", K(ret));
} else if (table_item->for_update_) {
DATA_PRINTF(" for update");
if (OB_SUCC(ret) && table_item->for_update_wait_us_ > 0) {
DATA_PRINTF(" wait %lld", table_item->for_update_wait_us_ / 1000000LL);
}
} else { /*do nothing*/ }
}
}
return ret;
}
int ObSelectStmtPrinter::print_with_check_option()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_) || OB_ISNULL(buf_) || OB_ISNULL(pos_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ is NULL or buf_ is NULL or pos_ is NULL", K(ret));
} else if (!stmt_->is_select_stmt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Not a valid select stmt", K(stmt_->get_stmt_type()), K(ret));
} else {
const ObSelectStmt *select_stmt = static_cast<const ObSelectStmt*>(stmt_);
if (select_stmt->is_view_stmt()) {
/* only print 'with check option' of subquery.
* with check option of view is printed in ObSchemaPrinter::print_view_definiton.
* otherwise it may cause a syntax error when ViewResolver resolve definition of a view.
* case: create view v as select * from t with check option.
* if we print 'with check option' of a view here, definition of the view is
* 'select * from t with check option', which will cause a syntax error in both mysql and oracle mode.
*/
} else {
ViewCheckOption view_check_option = select_stmt->get_check_option();
if (VIEW_CHECK_OPTION_CASCADED == view_check_option) {
DATA_PRINTF(" with check option");
} else if (VIEW_CHECK_OPTION_LOCAL == view_check_option) {
DATA_PRINTF(" with local check option");
}
}
}
return ret;
}
int ObSelectStmtPrinter::find_recursive_cte_table(const ObSelectStmt* stmt, TableItem* &table)
{
int ret = OB_SUCCESS;
table = NULL;
ObSelectStmt* set_query = NULL;
if (OB_ISNULL(stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null stmt", K(ret));
} else if (!stmt->is_recursive_union() ||
OB_ISNULL(set_query=stmt->get_set_query(1))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expect recurisve cte stmt", K(ret));
}
for (int i = 0; OB_SUCC(ret) && !table && i < set_query->get_table_items().count(); ++i) {
TableItem *table_item = set_query->get_table_item(i);
if (OB_ISNULL(table_item)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null table item", K(ret));
} else if (!table_item->is_fake_cte_table()) {
//do nothing
} else {
table = table_item;
}
}
return ret;
}
} //end of namespace sql
} //end of namespace oceanbase