forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_sql.cpp
More file actions
3442 lines (3299 loc) · 147 KB
/
ob_sql.cpp
File metadata and controls
3442 lines (3299 loc) · 147 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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL
#include "sql/ob_sql.h"
#include "lib/container/ob_array.h"
#include "lib/container/ob_se_array.h"
#include "lib/encrypt/ob_encrypted_helper.h"
#include "lib/json/ob_json.h"
#include "lib/profile/ob_profile_log.h"
#include "lib/profile/ob_trace_id.h"
#include "lib/thread_local/ob_tsi_factory.h"
#include "lib/string/ob_sql_string.h"
#include "lib/json/ob_json_print_utils.h"
#include "lib/profile/ob_perf_event.h"
#include "lib/rc/context.h"
#include "share/stat/ob_stat_manager.h"
#include "share/ob_truncated_string.h"
#include "share/partition_table/ob_partition_location.h"
#include "share/schema/ob_schema_getter_guard.h"
#include "share/ob_autoincrement_service.h"
#include "share/ob_rs_mgr.h"
#include "share/config/ob_server_config.h"
#include "common/sql_mode/ob_sql_mode_utils.h"
#include "sql/ob_sql_context.h"
#include "sql/ob_result_set.h"
#include "sql/optimizer/ob_log_plan_factory.h"
#include "sql/plan_cache/ob_plan_cache.h"
#include "sql/plan_cache/ob_pcv_set.h"
#include "sql/engine/join/ob_nested_loop_join.h"
#include "sql/engine/table/ob_virtual_table_ctx.h"
#include "sql/engine/basic/ob_values.h"
#include "sql/ob_sql_init.h"
#include "sql/ob_sql_utils.h"
#include "sql/ob_sql_partition_location_cache.h"
#include "sql/optimizer/ob_log_plan.h"
#include "sql/optimizer/ob_optimizer.h"
#include "sql/optimizer/ob_optimizer_context.h"
#include "sql/optimizer/ob_optimizer_partition_location_cache.h"
#include "sql/parser/ob_parser.h"
#include "sql/parser/parse_malloc.h"
#include "sql/parser/parse_node.h"
#include "sql/parser/parse_define.h"
#include "sql/resolver/ob_cmd.h"
#include "sql/resolver/ob_resolver.h"
#include "sql/resolver/ob_schema_checker.h"
#include "sql/resolver/cmd/ob_variable_set_stmt.h"
#include "sql/resolver/ob_resolver_utils.h"
#include "sql/privilege_check/ob_privilege_check.h"
#include "share/system_variable/ob_system_variable_alias.h"
#include "sql/rewrite/ob_transformer_impl.h"
#include "sql/rewrite/ob_transform_simplify.h"
#include "sql/rewrite/ob_transform_project_pruning.h"
#include "sql/rewrite/ob_transform_pre_process.h"
#include "sql/plan_cache/ob_cache_object_factory.h"
#include "sql/monitor/ob_phy_plan_monitor_info.h"
#include "sql/plan_cache/ob_ps_sql_utils.h"
#include "lib/utility/ob_tracepoint.h"
#include "observer/ob_server_struct.h"
#include "observer/omt/ob_th_worker.h"
#include "sql/resolver/dml/ob_del_upd_stmt.h"
#include "sql/resolver/dml/ob_update_stmt.h"
#include "sql/resolver/expr/ob_raw_expr_printer.h"
#include "sql/rewrite/ob_constraint_process.h"
#include "sql/engine/px/ob_px_admission.h"
#include "sql/code_generator/ob_code_generator.h"
#include "observer/omt/ob_tenant_config_mgr.h"
#include "sql/executor/ob_executor_rpc_impl.h"
#include "sql/executor/ob_remote_executor_processor.h"
#include "common/ob_smart_call.h"
namespace oceanbase {
using namespace common;
using namespace rpc::frame;
using namespace obrpc;
using namespace share;
using namespace share::schema;
namespace sql {
const int64_t ObSql::max_error_length = 80;
const int64_t ObSql::SQL_MEM_SIZE_LIMIT = 1024 * 1024 * 64;
int ObSql::init(common::ObStatManager* stat_mgr, common::ObOptStatManager* opt_stat_mgr, ObReqTransport* transport,
storage::ObPartitionService* partition_service, common::ObIDataAccessService* vt_partition_service,
share::ObIPartitionLocationCache* partition_location_cache, common::ObAddr& addr, share::ObRsMgr& rs_mgr)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stat_mgr) || OB_ISNULL(transport) || OB_ISNULL(partition_service) || OB_ISNULL(vt_partition_service) ||
OB_ISNULL(partition_location_cache)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid args",
K(ret),
KP(stat_mgr),
KP(transport),
KP(partition_service),
KP(vt_partition_service),
KP(partition_location_cache));
} else {
if (OB_FAIL(plan_cache_manager_.init(partition_location_cache, addr))) {
LOG_WARN("Failed to init plan cache manager", K(ret));
} else {
stat_mgr_ = stat_mgr;
opt_stat_mgr_ = opt_stat_mgr;
transport_ = transport;
partition_service_ = partition_service;
vt_partition_service_ = vt_partition_service;
self_addr_ = addr;
rs_mgr_ = &rs_mgr;
inited_ = true;
}
}
return ret;
}
void ObSql::destroy()
{
if (inited_) {
plan_cache_manager_.destroy();
inited_ = false;
}
}
void ObSql::stat()
{
sql::print_sql_stat();
}
int ObSql::stmt_prepare(
const common::ObString& stmt, ObSqlCtx& context, ObResultSet& result, bool is_inner_sql /*true*/)
{
int ret = OB_SUCCESS;
if (OB_FAIL(sanity_check(context))) {
LOG_WARN("Failed to do sanity check", K(ret));
} else if (OB_FAIL(handle_ps_prepare(stmt, context, result, is_inner_sql))) {
LOG_WARN("failed to handle ps query", K(stmt), K(ret));
}
if (OB_FAIL(ret) && OB_SUCCESS == result.get_errcode()) {
result.set_errcode(ret);
}
return ret;
}
int ObSql::stmt_query(const common::ObString& stmt, ObSqlCtx& context, ObResultSet& result)
{
int ret = OB_SUCCESS;
ObTruncatedString trunc_stmt(stmt);
#if !defined(NDEBUG)
LOG_INFO("Begin to handle text statement",
K(trunc_stmt),
"sess_id",
result.get_session().get_sessid(),
"execution_id",
result.get_session().get_current_execution_id());
#endif
NG_TRACE_EXT(parse_begin, OB_ID(stmt), trunc_stmt.string(), OB_ID(stmt_len), stmt.length());
// 1 check inited
if (OB_FAIL(sanity_check(context))) {
LOG_WARN("Failed to do sanity check", K(ret));
} else if (OB_FAIL(handle_text_query(stmt, context, result))) {
if (OB_EAGAIN != ret && OB_ERR_PROXY_REROUTE != ret) {
LOG_WARN("fail to handle text query", K(stmt), K(ret));
}
} else {
result.get_session().set_exec_min_cluster_version();
}
// LOG_DEBUG("result errno", N_ERR_CODE, result.get_errcode(), K(ret));
if (OB_SUCCESS != ret && OB_SUCCESS == result.get_errcode()) {
result.set_errcode(ret);
}
return ret;
}
int ObSql::stmt_execute(const ObPsStmtId stmt_id, const stmt::StmtType stmt_type, const ParamStore& params,
ObSqlCtx& context, ObResultSet& result, bool is_inner_sql)
{
int ret = OB_SUCCESS;
if (OB_FAIL(sanity_check(context))) {
LOG_WARN("failed to do sanity check", K(ret));
} else if (OB_FAIL(handle_ps_execute(stmt_id, stmt_type, params, context, result, is_inner_sql))) {
if (OB_ERR_PROXY_REROUTE != ret) {
LOG_WARN("failed to handle ps execute", K(stmt_id), K(ret));
}
} else {
result.get_session().set_exec_min_cluster_version();
}
if (OB_FAIL(ret) && OB_SUCCESS == result.get_errcode()) {
result.set_errcode(ret);
}
return ret;
}
int ObSql::stmt_list_field(
const common::ObString& table_name, const common::ObString& wild_str, ObSqlCtx& context, ObResultSet& result)
{
UNUSED(table_name);
UNUSED(wild_str);
UNUSED(context);
UNUSED(result);
return OB_NOT_IMPLEMENT;
}
int ObSql::fill_result_set(ObResultSet& result_set, ObSqlCtx* context, const bool is_ps_mode, ObStmt& basic_stmt)
{
int ret = OB_SUCCESS;
ObStmt* stmt = &basic_stmt;
if (OB_UNLIKELY(NULL == context) || OB_UNLIKELY(NULL == context->session_info_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret), K(context), "session", (context != NULL) ? context->session_info_ : NULL);
} else {
result_set.set_affected_rows(0);
result_set.set_warning_count(0);
result_set.set_message("");
ObString type_name = ObString::make_string("varchar");
number::ObNumber number;
number.set_zero();
ObSelectStmt* select_stmt = NULL;
ObDelUpdStmt* del_upd_stmt = NULL;
ObField field;
common::ObIAllocator& alloc = result_set.get_mem_pool();
ObCollationType collation_type = context->session_info_->get_local_collation_connection();
switch (stmt->get_stmt_type()) {
case stmt::T_SELECT: {
select_stmt = static_cast<ObSelectStmt*>(stmt);
if (select_stmt->has_select_into()) { // for select into, no rows return
break;
}
int64_t size = select_stmt->get_select_item_size();
if (OB_FAIL(result_set.reserve_field_columns(size))) {
LOG_WARN("reserve field columns failed", K(ret), K(size));
}
for (int64_t i = 0; OB_SUCC(ret) && i < size; ++i) {
const SelectItem& select_item = select_stmt->get_select_item(i);
LOG_DEBUG("select item info", K(select_item));
ObRawExpr* expr = select_item.expr_;
if (OB_UNLIKELY(NULL == expr)) {
ret = OB_ERR_ILLEGAL_ID;
LOG_WARN("fail to get expr", K(ret), K(i), K(size));
} else {
if (ob_is_string_or_lob_type(expr->get_data_type()) && CS_TYPE_BINARY != expr->get_collation_type()) {
field.charsetnr_ = static_cast<uint16_t>(collation_type);
} else {
field.charsetnr_ = static_cast<uint16_t>(expr->get_collation_type());
}
}
if (OB_SUCC(ret) && expr->get_result_type().is_ext()) {
ret = OB_NOT_SUPPORTED;
LOG_WARN("not supported complex type in select item", K(ret), K(expr->get_result_type().is_ext()));
}
if (OB_SUCC(ret)) {
// Setup field Type and Accuracy
field.type_.set_type(expr->get_data_type());
field.accuracy_ = expr->get_accuracy();
field.flags_ = static_cast<uint16_t>(expr->get_result_flag());
// Setup Collation and Collation levl
if (ob_is_string_or_lob_type(static_cast<ObObjType>(expr->get_data_type())) ||
ob_is_raw(static_cast<ObObjType>(expr->get_data_type())) ||
ob_is_enum_or_set_type(static_cast<ObObjType>(expr->get_data_type()))) {
field.type_.set_collation_type(expr->get_collation_type());
field.type_.set_collation_level(expr->get_collation_level());
}
// Setup Scale
if (ObVarcharType == field.type_.get_type()) {
field.type_.set_varchar(type_name);
} else if (ObNumberType == field.type_.get_type()) {
field.type_.set_number(number);
}
if (!expr->get_result_type().is_ext() && OB_FAIL(expr->get_length_for_meta_in_bytes(field.length_))) {
LOG_WARN("get length failed", K(ret));
} else {
// do nothing
}
}
// examples of alias name and expr name rules for SELECT ITEM:
// SELECT field1+field2 AS f1, field1+3, "thanks", field2 AS f2, field3, "hello" as f4, field1+4 as f5 FROM t1
// "is_alias":true, "alias_name":"f1", "expr_name":"f1"
// "is_alias":false, "alias_name":"", "expr_name":"field1+3"
// "is_alias":false, "alias_name":"", "expr_name":", "thanks"",
// "is_alias":true, "alias_name":"f2", "expr_name":"f2", "column_name":"field2",
// "is_alias":true, "alias_name":"field3", "expr_name":"field3", "column_name":"field3",
// "is_alias":true, "alias_name":"f4", "expr_name":"f4"
// "is_alias":true, "alias_name":"f5", "expr_name":"f5"
//
if (OB_SUCC(ret)) {
if (OB_FAIL(ObSQLUtils::copy_and_convert_string_charset(
alloc, select_item.alias_name_, field.cname_, CS_TYPE_UTF8MB4_BIN, collation_type))) {
LOG_WARN("fail to alloc string", K(select_item.alias_name_), K(ret));
} else {
field.is_hidden_rowid_ = select_item.is_hidden_rowid_;
LOG_TRACE("is_hidden_rowid", K(select_item));
}
}
if (OB_SUCC(ret)) {
if (select_stmt->get_set_op() != ObSelectStmt::NONE) {
if (OB_FAIL(ob_write_string(alloc, select_item.alias_name_, field.org_cname_))) {
LOG_WARN("fail to alloc string", K(select_item.alias_name_), K(ret));
}
} else if (expr->is_column_ref_expr()) {
ObColumnRefRawExpr* column_expr = static_cast<ObColumnRefRawExpr*>(expr);
uint64_t table_id = column_expr->get_table_id();
uint64_t column_id = column_expr->get_column_id();
if (table_id != OB_INVALID_ID) {
ColumnItem* column_item = select_stmt->get_column_item_by_id(table_id, column_id);
const TableItem* table_item = select_stmt->get_table_item_by_id(table_id);
if (OB_UNLIKELY(NULL == column_item)) {
ret = OB_ERR_ILLEGAL_ID;
LOG_WARN("fail to get column item by id.", K(ret), K(table_id), K(column_id));
} else if (OB_FAIL(ob_write_string(alloc, column_item->column_name_, field.org_cname_))) {
LOG_WARN("fail to alloc", K(ret), K(column_item->column_name_));
} else if (OB_UNLIKELY(NULL == table_item)) {
ret = OB_ERR_ILLEGAL_ID;
LOG_WARN("fail to get table item by id.", K(ret), K(table_id));
} else if (OB_FAIL(ob_write_string(alloc, table_item->database_name_, field.dname_))) {
LOG_WARN("fail to alloc string", K(ret), K(table_item->database_name_));
} else if (table_item->alias_name_.length() > 0) {
if (OB_FAIL(ob_write_string(alloc, table_item->alias_name_, field.tname_))) {
LOG_WARN("fail to alloc string", K(ret), K(table_item->alias_name_));
}
} else {
if (OB_FAIL(ob_write_string(alloc, table_item->table_name_, field.tname_))) {
LOG_WARN("fail to alloc string", K(ret), K(table_item->table_name_));
}
}
if (OB_FAIL(ob_write_string(alloc, table_item->table_name_, field.org_tname_))) {
LOG_WARN("fail to alloc string", K(ret), K(table_item->table_name_));
}
}
} else if (OB_FAIL(ob_write_string(alloc, select_item.alias_name_, field.org_cname_))) {
LOG_WARN("fail to alloc string", K(ret), K(select_item.alias_name_));
}
}
if (OB_SUCC(ret) && !is_ps_mode) {
void* buf = NULL;
if (OB_ISNULL(buf = alloc.alloc(sizeof(ObParamedSelectItemCtx)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to allocate memory", K(ret));
} else {
field.paramed_ctx_ = new (buf) ObParamedSelectItemCtx();
if (OB_FAIL(
ob_write_string(alloc, select_item.paramed_alias_name_, field.paramed_ctx_->paramed_cname_))) {
LOG_WARN("failed to copy paramed cname", K(ret));
} else if (OB_FAIL(field.paramed_ctx_->param_str_offsets_.assign(select_item.questions_pos_))) {
LOG_WARN("failed to copy param_str_offsets_", K(ret));
} else if (OB_FAIL(field.paramed_ctx_->param_idxs_.assign(select_item.params_idx_))) {
LOG_WARN("failed to copy param idxs", K(ret));
} else {
field.paramed_ctx_->neg_param_idxs_ = select_item.neg_param_idx_;
field.paramed_ctx_->esc_str_flag_ = select_item.esc_str_flag_;
field.paramed_ctx_->need_check_dup_name_ = select_item.need_check_dup_name_;
field.paramed_ctx_->is_column_field_ = (T_REF_COLUMN == expr->get_expr_type());
field.is_paramed_select_item_ = true;
}
}
}
LOG_TRACE("column field info", K(field), K(select_item));
if (OB_FAIL(ret)) {
// do nothing
} else if (OB_FAIL(result_set.add_field_column(field))) {
LOG_WARN("failed to add field column", K(ret));
} else {
field.cname_.assign(NULL, 0);
field.org_cname_.assign(NULL, 0);
field.dname_.assign(NULL, 0);
field.tname_.assign(NULL, 0);
field.org_tname_.assign(NULL, 0);
field.type_.reset();
field.type_.set_type(ObExtendType);
field.paramed_ctx_ = NULL;
}
}
break;
}
case stmt::T_INSERT:
case stmt::T_REPLACE:
case stmt::T_UPDATE:
case stmt::T_DELETE: {
del_upd_stmt = static_cast<ObDelUpdStmt*>(stmt);
if (!del_upd_stmt->is_returning()) {
break;
}
const common::ObIArray<ObRawExpr*>* returning_exprs = &(del_upd_stmt->get_returning_exprs());
const common::ObIArray<ObString>& returning_strs = del_upd_stmt->get_returning_strs();
int64_t size = returning_exprs->count();
field.charsetnr_ = CS_TYPE_UTF8MB4_GENERAL_CI;
if (OB_FAIL(result_set.reserve_field_columns(size))) {
LOG_WARN("reserve field columns failed", K(ret), K(size));
}
for (int64_t i = 0; OB_SUCC(ret) && i < size; i++) {
ObRawExpr* expr = returning_exprs->at(i);
if (OB_UNLIKELY(OB_ISNULL(expr))) {
ret = OB_ERR_ILLEGAL_ID;
LOG_WARN("fail to get expr", K(ret), K(i), K(size));
}
if (OB_SUCC(ret)) {
ObCollationType charsetnr;
if (OB_FAIL(ObCharset::get_default_collation(expr->get_collation_type(), charsetnr))) {
LOG_WARN("fail to get table item charset collation", K(expr->get_collation_type()), K(i), K(ret));
} else {
field.charsetnr_ = static_cast<uint16_t>(charsetnr);
}
}
if (OB_SUCC(ret)) {
expr->deduce_type(context->session_info_);
field.type_.set_type(expr->get_data_type());
field.accuracy_ = expr->get_accuracy();
field.flags_ = static_cast<uint16_t>(expr->get_result_flag());
// Setup Collation and Collation levl
if (ob_is_string_or_lob_type(static_cast<ObObjType>(expr->get_data_type())) ||
ob_is_raw(static_cast<ObObjType>(expr->get_data_type())) ||
ob_is_enum_or_set_type(static_cast<ObObjType>(expr->get_data_type()))) {
field.type_.set_collation_type(expr->get_collation_type());
field.type_.set_collation_level(expr->get_collation_level());
}
if (ObVarcharType == field.type_.get_type()) {
field.type_.set_varchar(type_name);
} else if (ObNumberType == field.type_.get_type()) {
field.type_.set_number(number);
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(ob_write_string(alloc, returning_strs.at(i), field.cname_))) {
LOG_WARN("fail to alloc", K(ret), K(returning_strs.at(i)));
}
}
if (OB_SUCC(ret)) {
field.is_paramed_select_item_ = false;
field.paramed_ctx_ = NULL;
if (OB_FAIL(result_set.add_field_column(field))) {
LOG_WARN("fail to add field column to result_set", K(ret));
}
}
if (OB_SUCC(ret)) {
field.cname_.assign(NULL, 0);
field.org_cname_.assign(NULL, 0);
field.dname_.assign(NULL, 0);
field.tname_.assign(NULL, 0);
field.org_tname_.assign(NULL, 0);
field.type_.reset();
field.type_.set_type(ObExtendType);
}
}
break;
}
case stmt::T_EXPLAIN: {
ObString tname = ObString::make_string("explain_table");
ObString cname = ObString::make_string("Query Plan");
field.tname_ = tname;
field.org_tname_ = tname;
field.cname_ = cname;
field.org_cname_ = cname;
field.type_.set_type(ObVarcharType);
field.charsetnr_ = CS_TYPE_UTF8MB4_GENERAL_CI;
field.type_.set_collation_type(CS_TYPE_UTF8MB4_GENERAL_CI);
field.type_.set_collation_level(CS_LEVEL_IMPLICIT);
field.type_.set_varchar(type_name);
if (OB_FAIL(result_set.reserve_field_columns(1))) {
LOG_WARN("reserve field columns failed", K(ret));
} else if (OB_FAIL(result_set.add_field_column(field))) {
LOG_WARN("fail to add field column to result_set", K(ret));
}
break;
}
default:
break;
}
const int64_t question_marks_count = stmt->get_prepare_param_count();
if (OB_SUCC(ret) && is_ps_mode && question_marks_count > 0) { // param column is only needed in ps mode
if (OB_FAIL(result_set.reserve_param_columns(question_marks_count))) {
LOG_WARN("reserve param columns failed", K(ret), K(question_marks_count));
}
for (int64_t i = 0; OB_SUCC(ret) && i < question_marks_count; ++i) {
ObField param_field;
param_field.type_.set_type(ObIntType); // @bug
param_field.cname_ = ObString::make_string("?");
OZ(result_set.add_param_column(param_field), param_field, i, question_marks_count);
}
}
}
return ret;
}
int ObSql::fill_result_set(const ObPsStmtId stmt_id, const ObPsStmtInfo& stmt_info, ObResultSet& result)
{
int ret = OB_SUCCESS;
result.set_statement_id(stmt_id);
result.set_stmt_type(stmt_info.get_stmt_type());
const ObPsSqlMeta& sql_meta = stmt_info.get_ps_sql_meta();
result.set_p_param_fileds(const_cast<common::ParamsFieldIArray*>(&sql_meta.get_param_fields()));
result.set_p_column_fileds(const_cast<common::ParamsFieldIArray*>(&sql_meta.get_column_fields()));
// ObPsSqlMeta::const_column_iterator column_iter = sql_meta.column_begin();
// result.reserve_field_columns(sql_meta.get_column_size());
// for (; OB_SUCC(ret) && column_iter != sql_meta.column_end(); ++column_iter) {
// if (OB_ISNULL(column_iter) || OB_ISNULL(*column_iter)) {
// ret = OB_ERR_UNEXPECTED;
// LOG_WARN("column iter is null", K(ret), K(column_iter));
//} else if (OB_FAIL(result.add_field_column(**column_iter))) {
// LOG_WARN("add column field failed", K(ret));
//}
//}
// ObPsSqlMeta::const_param_iterator param_iter = sql_meta.param_begin();
// for (; OB_SUCC(ret) && param_iter != sql_meta.param_end(); ++param_iter) {
// if (OB_ISNULL(param_iter) || OB_ISNULL(param_iter)) {
// ret = OB_ERR_UNEXPECTED;
// LOG_WARN("param iter is null", K(ret), K(param_iter));
//} else if (OB_FAIL(result.add_param_column(**param_iter))) {
// LOG_WARN("add param field faield", K(ret));
//}
//}
return ret;
}
int ObSql::do_add_ps_cache(const ObString& sql, int64_t param_cnt, ObSchemaGetterGuard& schema_guard,
stmt::StmtType stmt_type, ObResultSet& result, bool is_inner_sql)
{
int ret = OB_SUCCESS;
ObSQLSessionInfo& session = result.get_session();
ObPsCache* ps_cache = session.get_ps_cache();
uint64_t db_id = OB_INVALID_ID;
(void)session.get_database_id(db_id);
if (OB_ISNULL(ps_cache)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ps plan cache should not be null", K(ret));
} else {
ObPsStmtItem* ps_stmt_item = NULL;
ObPsStmtInfo* ref_stmt_info = NULL;
bool duplicate_prepare = false;
// add stmt item
if (OB_FAIL(ps_cache->get_or_add_stmt_item(db_id, sql, ps_stmt_item))) {
LOG_WARN("get or create stmt item faield", K(ret), K(db_id), K(sql));
} else if (OB_FAIL(ps_cache->get_or_add_stmt_info(
result, param_cnt, schema_guard, stmt_type, ps_stmt_item, ref_stmt_info))) {
LOG_WARN("get or create stmt info failed", K(ret), K(ps_stmt_item), K(db_id), K(sql));
} else if (OB_ISNULL(ps_stmt_item) || OB_ISNULL(ref_stmt_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_item or stmt_info is NULL", K(ret), KP(ps_stmt_item), KP(ref_stmt_info));
}
// add session info
if (OB_SUCC(ret)) {
ObPsStmtId inner_stmt_id = ps_stmt_item->get_ps_stmt_id();
ObPsStmtId client_stmt_id = OB_INVALID_ID;
if (OB_FAIL(
session.prepare_ps_stmt(inner_stmt_id, ref_stmt_info, client_stmt_id, duplicate_prepare, is_inner_sql))) {
LOG_WARN("prepare_ps_stmt failed", K(ret), K(inner_stmt_id), K(client_stmt_id));
} else {
result.set_statement_id(client_stmt_id);
result.set_stmt_type(stmt_type);
LOG_TRACE("add ps session info",
K(ret),
K(*ref_stmt_info),
K(client_stmt_id),
K(*ps_stmt_item),
K(session.get_sessid()));
}
}
if (OB_FAIL(ret) || duplicate_prepare) { // dec ref count
if (NULL != ps_stmt_item) {
if (NULL != ref_stmt_info) {
ObPsStmtId inner_stmt_id = ps_stmt_item->get_ps_stmt_id();
ps_cache->deref_stmt_info(inner_stmt_id);
}
ps_stmt_item->dec_ref_count_check_erase();
}
}
}
return ret;
}
int ObSql::do_real_prepare(const ObString& sql, ObSqlCtx& context, ObResultSet& result, bool is_inner_sql)
{
int ret = OB_SUCCESS;
ParseResult parse_result;
ObStmt* basic_stmt = NULL;
stmt::StmtType stmt_type = stmt::T_NONE;
int64_t param_cnt = 0;
ObString normalized_sql;
ObIAllocator& allocator = result.get_mem_pool();
ObSQLSessionInfo& session = result.get_session();
ObParser parser(allocator, session.get_sql_mode(), session.get_local_collation_connection());
ParseMode parse_mode = context.is_dbms_sql_ ? DBMS_SQL_MODE : STD_MODE;
CHECK_COMPATIBILITY_MODE(context.session_info_);
if (OB_ISNULL(context.session_info_) || OB_ISNULL(context.schema_guard_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session info is NULL", K(ret));
} else if (OB_FAIL(parser.parse(sql, parse_result, parse_mode))) {
LOG_WARN("generate syntax tree failed", K(sql), K(ret));
} else if (is_mysql_mode() && ObSQLUtils::is_mysql_ps_not_support_stmt(parse_result)) {
ret = OB_ER_UNSUPPORTED_PS;
LOG_WARN("This command is not supported in the prepared statement protocol yet", K(ret));
} else if (result.is_simple_ps_protocol()) {
if (OB_FAIL(ObResolverUtils::resolve_stmt_type(parse_result, stmt_type))) {
LOG_WARN("failed to resolve stmt type", K(ret));
} else {
param_cnt = parse_result.question_mark_ctx_.count_;
normalized_sql = context.is_dynamic_sql_ && parse_result.no_param_sql_len_ > 0
? ObString(parse_result.no_param_sql_len_, parse_result.no_param_sql_)
: sql;
}
} else {
if (context.is_dynamic_sql_ && !context.is_dbms_sql_) {
parse_result.input_sql_ = parse_result.no_param_sql_;
parse_result.input_sql_len_ = parse_result.no_param_sql_len_;
}
if (OB_FAIL(generate_stmt(parse_result, NULL, context, allocator, result, basic_stmt))) {
LOG_WARN("generate stmt failed", K(ret));
} else if (OB_ISNULL(basic_stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("generate stmt success, but stmt is NULL", K(ret));
} else if (stmt::T_CALL_PROCEDURE == basic_stmt->get_stmt_type() &&
FALSE_IT(result.set_cmd(dynamic_cast<ObICmd*>(basic_stmt)))) {
} else if (OB_FAIL(fill_result_set(result, &context, true, *basic_stmt))) {
LOG_WARN("Failed to fill result set", K(ret));
} else if (OB_ISNULL(result.get_param_fields())) {
LOG_WARN("invalid argument", K(result.get_param_fields()), K(ret));
} else {
param_cnt = result.get_param_fields()->count();
stmt_type = basic_stmt->get_stmt_type();
// If it is inner sql, such as pl internal sql, you need to use a formatted text string,
// Because it is necessary to replace the sql variable in the pl with the standard ps text for hard syntax parse
// The ps text of the external request cannot be formatted, because checksum verification is required when parsing
// the ps execute package. It is necessary to ensure that the text of prepare is consistent with the text sent by
// the client
if (!is_inner_sql) {
normalized_sql = sql;
} else {
normalized_sql = basic_stmt->get_sql_stmt();
}
}
LOG_INFO("generate new stmt", K(param_cnt), K(stmt_type), K(normalized_sql), K(sql));
}
if (OB_SUCC(ret)) {
if (OB_FAIL(do_add_ps_cache(normalized_sql, param_cnt, *context.schema_guard_, stmt_type, result, is_inner_sql))) {
LOG_WARN("add to ps plan cache failed", K(ret));
}
}
LOG_INFO("add ps cache", K(normalized_sql), K(param_cnt), K(ret));
return ret;
}
int ObSql::handle_ps_prepare(const ObString& stmt, ObSqlCtx& context, ObResultSet& result, bool is_inner_sql)
{
int ret = OB_SUCCESS;
ObString trimed_stmt = const_cast<ObString&>(stmt).trim();
if (trimed_stmt.empty()) {
ret = OB_ERR_EMPTY_QUERY;
LOG_WARN("query is empty", K(ret));
} else if (OB_FAIL(init_result_set(context, result))) {
LOG_WARN("failed to init result set", K(ret));
}
if (OB_SUCC(ret)) {
ObSQLSessionInfo& session = result.get_session();
ObPsCache* ps_cache = session.get_ps_cache();
ObExecContext& ectx = result.get_exec_context();
ObPhysicalPlanCtx* pctx = ectx.get_physical_plan_ctx();
ObSchemaGetterGuard* schema_guard = context.schema_guard_;
#ifndef NDEBUG
LOG_INFO("Begin to handle prepare stmtement", K(session.get_sessid()), K(stmt));
#endif
if (OB_ISNULL(ps_cache) || OB_ISNULL(pctx) || OB_ISNULL(schema_guard)) {
ret = OB_INVALID_ARGUMENT;
LOG_ERROR("physical plan context or ps plan cache is NULL or schema_guard is null", K(ret), K(pctx), K(ps_cache));
} else {
bool need_do_real_prepare = false;
uint64_t db_id = OB_INVALID_ID;
(void)session.get_database_id(db_id);
ObPsStmtId inner_stmt_id = OB_INVALID_STMT_ID;
ObPsStmtId client_stmt_id = OB_INVALID_STMT_ID;
ObPsStmtInfo* stmt_info = NULL;
ObPsStmtItem* stmt_item = NULL;
bool duplicate_prepare = false;
bool is_expired = false;
if (OB_FAIL(ps_cache->ref_stmt_item(db_id, stmt, stmt_item))) {
if (OB_HASH_NOT_EXIST == ret) {
ret = OB_SUCCESS;
need_do_real_prepare = true;
if (REACH_TIME_INTERVAL(1000000)) {
LOG_INFO("stmt id not exist", K(db_id), K(stmt), K(need_do_real_prepare));
}
} else {
LOG_WARN("fail to get stmt id", K(ret), K(db_id), K(stmt));
}
} else if (OB_ISNULL(stmt_item) || OB_INVALID_STMT_ID == (inner_stmt_id = stmt_item->get_ps_stmt_id())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt id is invalid", K(ret), K(inner_stmt_id), K(db_id), K(stmt), K(stmt_item));
} else if (OB_FAIL(ps_cache->ref_stmt_info(inner_stmt_id, stmt_info))) {
// inc stmt_info ref for session
if (OB_HASH_NOT_EXIST == ret) {
need_do_real_prepare = true;
if (REACH_TIME_INTERVAL(1000000)) {
LOG_INFO("stmt info not exist", K(db_id), K(stmt), K(inner_stmt_id), K(ret));
}
ret = OB_SUCCESS;
} else {
LOG_WARN("fail to get stmt info", K(ret), K(db_id), K(stmt), K(inner_stmt_id));
}
} else if (OB_ISNULL(stmt_info)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("stmt info is null", K(ret), K(inner_stmt_id));
// check stmt_info whether expired, if expired, do nothing
} else if (OB_FAIL(ps_cache->check_schema_version(*context.schema_guard_, *stmt_info, is_expired))) {
LOG_WARN("fail to check schema version", K(ret));
} else if (is_expired) {
ObPsSqlKey ps_sql_key;
stmt_info->set_is_expired();
ps_sql_key.set_db_id(stmt_info->get_db_id());
ps_sql_key.set_ps_sql(stmt_info->get_ps_sql());
if (OB_FAIL(ps_cache->erase_stmt_item(ps_sql_key))) {
LOG_WARN("fail to erase stmt item", K(ret), K(*stmt_info));
}
need_do_real_prepare = true;
} else if (OB_FAIL(session.prepare_ps_stmt(
inner_stmt_id, stmt_info, client_stmt_id, duplicate_prepare, is_inner_sql))) {
LOG_WARN("add ps session info failed", K(ret), K(inner_stmt_id), K(client_stmt_id));
} else if (OB_FAIL(fill_result_set(client_stmt_id, *stmt_info, result))) {
IGNORE_RETURN session.close_ps_stmt(client_stmt_id);
LOG_WARN("fill result set failed", K(ret), K(client_stmt_id));
}
LOG_DEBUG("prepare done", K(need_do_real_prepare), K(duplicate_prepare), K(ret));
if (OB_FAIL(ret) || need_do_real_prepare || duplicate_prepare) {
if (NULL != stmt_item) {
stmt_item->dec_ref_count_check_erase();
}
if (NULL != stmt_info) {
ps_cache->deref_stmt_info(inner_stmt_id);
}
}
if (OB_SUCC(ret) && need_do_real_prepare) {
if (OB_FAIL(do_real_prepare(stmt, context, result, is_inner_sql))) {
LOG_WARN("do_real_prepare failed", K(ret));
}
}
if (OB_SUCC(ret)) {
if (false == need_do_real_prepare) {
ps_cache->inc_access_and_hit_count();
} else {
ps_cache->inc_access_count();
}
}
}
}
return ret;
}
int ObSql::construct_param_store(const ParamStore& params, ParamStore& param_store)
{
int ret = OB_SUCCESS;
if (OB_FAIL(param_store.reserve(params.count()))) {
LOG_WARN("failed to reserve array", K(ret));
}
for (int i = 0; OB_SUCC(ret) && i < params.count(); ++i) {
if (share::is_oracle_mode() && ((params.at(i).is_varchar() && 0 == params.at(i).get_varchar().length()) ||
(params.at(i).is_char() && 0 == params.at(i).get_char().length()) ||
(params.at(i).is_nstring() && 0 == params.at(i).get_string_len()))) {
const_cast<ObObjParam&>(params.at(i)).set_null();
const_cast<ObObjParam&>(params.at(i)).set_param_meta();
}
if (OB_FAIL(param_store.push_back(params.at(i)))) {
LOG_WARN("pushback param failed", K(ret));
}
LOG_TRACE("ps param is", K(params.at(i)), K(i));
}
return ret;
}
int ObSql::construct_not_paramalize(
const ObIArray<int64_t>& offsets, const ParamStore& param_store, ObPlanCacheCtx& plan_ctx)
{
int ret = OB_SUCCESS;
plan_ctx.not_param_var_.set_capacity(offsets.count());
for (int i = 0; OB_SUCC(ret) && i < offsets.count(); ++i) {
const int64_t offset = offsets.at(i);
PsNotParamInfo ps_not_param_var;
ps_not_param_var.idx_ = offset;
ps_not_param_var.ps_param_ = param_store.at(offset);
if (OB_FAIL(plan_ctx.not_param_var_.push_back(ps_not_param_var))) {
LOG_WARN("fail to push item to array", K(ret));
} else if (OB_FAIL(plan_ctx.not_param_index_.add_member(offset))) {
LOG_WARN("add member failed", K(ret), K(offset));
}
}
return ret;
}
int ObSql::construct_no_check_type_params(const ObIArray<int64_t>& offsets, ParamStore& params_store)
{
int ret = OB_SUCCESS;
for (int64_t i = 0; OB_SUCC(ret) && i < offsets.count(); i++) {
const int64_t offset = offsets.at(i);
if (offset < 0 || offset >= params_store.count()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("Invalid offset", K(ret), K(offset), K(params_store.count()));
} else if (!params_store.at(offset).is_ext()) { // extend type need to be checked
params_store.at(offset).set_need_to_check_type(false);
} else {
// real type do not need to be checked
params_store.at(offset).set_need_to_check_extend_type(false);
}
} // for end
LOG_DEBUG("ps obj param infos", K(params_store), K(offsets));
return ret;
}
int ObSql::construct_ps_param(const ParamStore& params, ObPlanCacheCtx& phy_ctx)
{
int ret = OB_SUCCESS;
phy_ctx.fp_result_.ps_params_.set_allocator(&phy_ctx.allocator_);
phy_ctx.fp_result_.ps_params_.set_capacity(params.count());
for (int i = 0; OB_SUCC(ret) && i < params.count(); ++i) {
if (OB_FAIL(phy_ctx.fp_result_.ps_params_.push_back(¶ms.at(i)))) {
LOG_WARN("add ps param failed", K(ret));
}
}
return ret;
}
int ObSql::handle_ps_execute(const ObPsStmtId client_stmt_id, const stmt::StmtType stmt_type, const ParamStore& params,
ObSqlCtx& context, ObResultSet& result, bool is_inner_sql)
{
int ret = OB_SUCCESS;
int get_plan_err = OB_SUCCESS;
context.is_prepare_protocol_ = true;
ObPsStmtId inner_stmt_id = client_stmt_id;
context.stmt_type_ = stmt_type;
if (OB_FAIL(init_result_set(context, result))) {
LOG_WARN("failed to init result set", K(ret));
} else {
ObIAllocator& allocator = result.get_mem_pool();
ObSQLSessionInfo& session = result.get_session();
ObExecContext& ectx = result.get_exec_context();
ObPsCache* ps_cache = session.get_ps_cache();
ObPlanCache* plan_cache = session.get_plan_cache();
bool use_plan_cache = session.get_local_ob_enable_plan_cache();
ObPhysicalPlanCtx* pctx = ectx.get_physical_plan_ctx();
ObSchemaGetterGuard* schema_guard = context.schema_guard_;
if (OB_ISNULL(ps_cache) || OB_ISNULL(pctx) || OB_ISNULL(schema_guard) || OB_ISNULL(plan_cache)) {
ret = OB_INVALID_ARGUMENT;
LOG_ERROR("physical plan context or ps plan cache is NULL or schema_guard is null", K(ret), K(pctx), K(ps_cache));
} else if (!is_inner_sql && OB_FAIL(session.get_inner_ps_stmt_id(client_stmt_id, inner_stmt_id))) {
LOG_WARN("get_inner_ps_stmt_id failed", K(ret), K(client_stmt_id), K(inner_stmt_id));
} else {
context.statement_id_ = inner_stmt_id;
ObPsStmtInfoGuard guard;
ObPsStmtInfo* ps_info = NULL;
if (OB_FAIL(ps_cache->get_stmt_info_guard(inner_stmt_id, guard))) {
LOG_WARN("get stmt info guard failed", K(ret), K(inner_stmt_id));
} else if (OB_ISNULL(ps_info = guard.get_stmt_info())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get stmt info is null", K(ret));
} else if (ps_info->get_question_mark_count() != params.count()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("Incorrect arguments to execute",
K(ps_info->get_question_mark_count()),
K(ps_info->get_ps_sql()),
K(params.count()),
K(ret));
LOG_USER_ERROR(OB_INVALID_ARGUMENT, "execute");
} else if (OB_FAIL(construct_param_store(params, pctx->get_param_store_for_update()))) {
LOG_WARN("construct param store failed", K(ret));
} else {
LOG_DEBUG("handle execute stmt", K(*ps_info));
const ObString& sql = ps_info->get_ps_sql();
context.cur_sql_ = sql;
#ifndef NDEBUG
LOG_INFO("Begin to handle execute stmtement", K(session.get_sessid()), K(sql));
#endif
if (OB_FAIL(session.store_query_string(sql))) {
LOG_WARN("store query string fail", K(ret));
} else if (OB_LIKELY(ObStmt::is_dml_stmt(stmt_type))) {
// if plan not exist, generate plan
ObPlanCacheCtx pc_ctx(sql,
true, /*is_ps_mode*/
allocator,
context,
ectx,
session.get_effective_tenant_id());
pc_ctx.fp_result_.pc_key_.key_id_ = inner_stmt_id;
pc_ctx.normal_parse_const_cnt_ = params.count();
pc_ctx.bl_key_.db_id_ = session.get_database_id();
if (OB_FAIL(construct_ps_param(params, pc_ctx))) {
LOG_WARN("construct_ps_param failed", K(ret));
} else {
if (!use_plan_cache) {
/*do nothing*/
} else if (OB_FAIL(pc_get_plan_and_fill_result(
pc_ctx, result, get_plan_err, ectx.get_need_disconnect_for_update()))) {
LOG_DEBUG("fail to get plan", K(ret));
}
if (OB_FAIL(ret)) { // do nothing
} else if (!result.get_is_from_plan_cache()) {
pctx->get_param_store_for_update().reset();
if (OB_FAIL(construct_param_store(params, pctx->get_param_store_for_update()))) {
LOG_WARN("construct param store failed", K(ret));
} else if (OB_FAIL(handle_physical_plan(sql, context, result, pc_ctx, get_plan_err))) {
if (OB_ERR_PROXY_REROUTE == ret) {
LOG_DEBUG("fail to handle physical plan", K(ret));
} else {
LOG_WARN("fail to handle physical plan", K(ret));
}
}
}
if (OB_SUCC(ret) &&
(OB_FAIL(after_get_plan(
pc_ctx, session, result.get_physical_plan(), result.get_is_from_plan_cache(), ¶ms)))) {
LOG_WARN("fail to handle after get plan", K(ret));
}
}
} else {
ObParser parser(allocator, session.get_sql_mode(), session.get_local_collation_connection());
ParseResult parse_result;
ParseMode parse_mode = context.is_dbms_sql_ ? DBMS_SQL_MODE : STD_MODE;
if (OB_FAIL(parser.parse(sql, parse_result, parse_mode))) {
LOG_WARN("failed to parse sql", K(ret), K(sql), K(stmt_type));
} else if (OB_FAIL(generate_physical_plan(parse_result, NULL, context, result, true))) {
LOG_WARN("generate physical plan failed", K(ret), K(sql), K(stmt_type));
}
}
}
}
}
return ret;
}
int ObSql::handle_remote_query(const ObRemoteSqlInfo& remote_sql_info, ObSqlCtx& context, ObExecContext& exec_ctx,
ObPhysicalPlan*& plan, CacheRefHandleID& ref_handle_id)
{
int ret = OB_SUCCESS;
// trim the sql first, let 'select c1 from t' and ' select c1 from t' and hit the same plan_cache
const ObString& trimed_stmt = remote_sql_info.remote_sql_;
ObIAllocator& allocator = THIS_WORKER.get_sql_arena_allocator();
ObSQLSessionInfo* session = exec_ctx.get_my_session();
int get_plan_err = OB_SUCCESS; // used for judge whether add plan to plan cache
bool is_from_plan_cache = false;
ObPlanCacheCtx* pc_ctx = NULL;
ObSEArray<ObString, 1> queries;
if (OB_ISNULL(session) || OB_ISNULL(remote_sql_info.ps_params_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is null", K(ret), K(session), K(remote_sql_info.ps_params_));
} else if (OB_ISNULL(pc_ctx = static_cast<ObPlanCacheCtx*>(allocator.alloc(sizeof(ObPlanCacheCtx))))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("fail to alloc memory", K(ret), K(sizeof(ObPlanCacheCtx)));
} else {
#if !defined(NDEBUG)
LOG_INFO("Begin to handle remote statement",
K(remote_sql_info),
"sess_id",
session->get_sessid(),
"execution_id",
session->get_current_execution_id());
#endif
const uint64_t tenant_id = session->get_effective_tenant_id();
bool use_plan_cache = session->get_local_ob_enable_plan_cache();
context.self_add_plan_ = false;
pc_ctx = new (pc_ctx) ObPlanCacheCtx(trimed_stmt,
remote_sql_info.use_ps_, /*is_ps_mode*/
allocator,
context,
exec_ctx,
tenant_id);
if (remote_sql_info.use_ps_) {
// the execution plan of the ps mode and the ordinary text protocol cannot be reused,
// it is necessary to distinguish here to avoid some problems when querying the plan
// because the common text protocol key_id is OB_INVALID_ID, here we use
// key_id=0+name=parameterized SQL to distinguish
pc_ctx->fp_result_.pc_key_.key_id_ = 0;
pc_ctx->fp_result_.pc_key_.name_ = trimed_stmt;
pc_ctx->normal_parse_const_cnt_ = remote_sql_info.ps_params_->count();
pc_ctx->bl_key_.db_id_ = session->get_database_id();
if (OB_FAIL(construct_ps_param(*remote_sql_info.ps_params_, *pc_ctx))) {
LOG_WARN("construct_ps_param failed", K(ret));
}
} else if (remote_sql_info.is_batched_stmt_) {
// keep it consistent with the control end here. If it is batched stmt,
// you need to do a parser segmentation first the cut query finally uses
// the logic of batched multi stmt to query the plan cache and generate the plan
ObParser parser(allocator, session->get_sql_mode(), session->get_local_collation_connection());
ObMPParseStat parse_stat;
if (OB_FAIL(parser.split_multiple_stmt(remote_sql_info.remote_sql_, queries, parse_stat))) {
LOG_WARN("split multiple stmt failed", K(ret), K(remote_sql_info));
} else {
context.multi_stmt_item_.set_batched_queries(&queries);
}