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
5643 lines (5448 loc) · 260 KB
/
Copy pathob_sql.cpp
File metadata and controls
5643 lines (5448 loc) · 260 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/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_ps_cache.h"
#include "sql/plan_cache/ob_pcv_set.h"
#include "sql/engine/table/ob_virtual_table_ctx.h"
#include "sql/ob_sql_init.h"
#include "sql/ob_sql_utils.h"
#include "sql/monitor/ob_security_audit_utils.h"
#include "sql/optimizer/ob_log_plan.h"
#include "sql/optimizer/ob_optimizer.h"
#include "sql/optimizer/ob_optimizer_context.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/cmd/ob_help_stmt.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/cmd/ob_call_procedure_stmt.h"
#include "sql/resolver/cmd/ob_anonymous_block_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_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/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 "sql/udr/ob_udr_utils.h"
#include "sql/udr/ob_udr_mgr.h"
#include "sql/udr/ob_udr_analyzer.h"
#include "common/ob_smart_call.h"
#include "sql/ob_optimizer_trace_impl.h"
#include "share/resource_manager/ob_resource_manager.h"
#include "share/resource_manager/ob_cgroup_ctrl.h"
#ifdef OB_BUILD_SPM
#include "sql/spm/ob_spm_controller.h"
#include "sql/spm/ob_spm_define.h"
#endif
#ifdef OB_BUILD_ORACLE_PL
#include "pl/sys_package/ob_json_pl_utils.h"
#endif
#include "sql/ob_optimizer_trace_impl.h"
#include "sql/monitor/ob_sql_plan.h"
#include "sql/optimizer/ob_explain_log_plan.h"
#include "sql/dblink/ob_dblink_utils.h"
#include "sql/plan_cache/ob_values_table_compression.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::ObOptStatManager *opt_stat_mgr,
ObReqTransport *transport,
common::ObITabletScan *vt_partition_service,
common::ObAddr &addr,
share::ObRsMgr &rs_mgr)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(opt_stat_mgr)
|| OB_ISNULL(transport)
|| OB_ISNULL(vt_partition_service)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid args",
K(ret),
KP(opt_stat_mgr),
KP(transport),
KP(vt_partition_service));
} else {
if (OB_FAIL(queue_.init(1, 512))) {
LOG_WARN("queue init failed", K(ret));
} else {
opt_stat_mgr_ = opt_stat_mgr;
transport_ = transport;
vt_partition_service_ = vt_partition_service;
self_addr_ = addr;
rs_mgr_ = &rs_mgr;
inited_ = true;
queue_.start();
}
}
return ret;
}
void ObSql::destroy() {
if (inited_) {
queue_.destroy();
inited_ = false;
}
}
void ObSql::stat()
{
sql::print_sql_stat();
}
#define STMT_SUPPORT_BY_TXN_FREE_ROUTE(stmt_type, allow_ps) \
(ObStmt::is_dml_stmt(stmt_type) \
|| (stmt_type == stmt::StmtType::T_VARIABLE_SET) \
|| (stmt_type == stmt::StmtType::T_USE_DATABASE) \
|| (allow_ps && stmt_type == stmt::StmtType::T_PREPARE) \
|| (allow_ps && stmt_type == stmt::StmtType::T_EXECUTE) \
|| (allow_ps && stmt_type == stmt::StmtType::T_DEALLOCATE))
#define CHECK_STMT_SUPPORTED_BY_TXN_FREE_ROUTE(result, allow_ps) \
if (OB_SUCC(ret)) { \
auto stmt_type = result.get_stmt_type(); \
auto &session = result.get_session(); \
if (!session.is_inner() && session.is_txn_free_route_temp()) { \
if (!STMT_SUPPORT_BY_TXN_FREE_ROUTE(stmt_type, allow_ps)) { \
ret = OB_TRANS_FREE_ROUTE_NOT_SUPPORTED; \
LOG_WARN("only DML stmt or SET command is supported to be executed on txn temporary node", \
KR(ret), K(stmt_type), K(session.get_txn_free_route_ctx()), K(session)); \
} \
} \
}
int ObSql::stmt_prepare(const common::ObString &stmt,
ObSqlCtx &context,
ObResultSet &result,
bool is_inner_sql/*true*/)
{
int ret = OB_SUCCESS;
LinkExecCtxGuard link_guard(result.get_session(), result.get_exec_context());
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));
}
CHECK_STMT_SUPPORTED_BY_TXN_FREE_ROUTE(result, false);
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;
LinkExecCtxGuard link_guard(result.get_session(), result.get_exec_context());
FLTSpanGuard(sql_compile);
ObTruncatedString trunc_stmt(stmt);
#ifndef NDEBUG
LOG_INFO("Begin to handle text statement", K(trunc_stmt),
"sess_id", result.get_session().get_sessid(),
"proxy_sess_id", result.get_session().get_proxy_sessid(),
"tenant_id", result.get_session().get_effective_tenant_id(),
"execution_id", result.get_session().get_current_execution_id());
#endif
NG_TRACE(parse_begin);
//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",
"stmt", context.is_sensitive_ ? ObString(OB_MASKED_STR) : stmt, K(ret));
}
}
CHECK_STMT_SUPPORTED_BY_TXN_FREE_ROUTE(result, true);
if (OB_SUCC(ret)) {
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);
}
if (OB_ISNULL(result.get_physical_plan())) {
} else {
FLT_SET_TAG(plan_hash, result.get_physical_plan()->get_plan_hash_value());
}
FLT_SET_TAG(database_id, result.get_session().get_database_id(),
sql_id, context.sql_id_);
NG_TRACE_EXT(stmt_query_end, OB_ID(stmt),
context.is_sensitive_ ? ObString(OB_MASKED_STR) : trunc_stmt.string(),
OB_ID(stmt_len), stmt.length());
return ret;
}
int ObSql::stmt_execute(const ObPsStmtId stmt_id,
const stmt::StmtType stmt_type,
const ParamStore ¶ms,
ObSqlCtx &context,
ObResultSet &result,
bool is_inner_sql)
{
int ret = OB_SUCCESS;
LinkExecCtxGuard link_guard(result.get_session(), result.get_exec_context());
if (OB_FAIL(sanity_check(context))) {
LOG_WARN("failed to do sanity check", K(ret));
} else if (OB_FAIL(init_result_set(context, result))) {
LOG_WARN("failed to init result set", 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));
}
}
CHECK_STMT_SUPPORTED_BY_TXN_FREE_ROUTE(result, false);
if (OB_SUCC(ret)) {
result.get_session().set_exec_min_cluster_version();
}
if (OB_FAIL(ret) && OB_SUCCESS == result.get_errcode()) {
result.set_errcode(ret);
}
FLT_SET_TAG(sql_id, context.sql_id_);
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 PlanCacheMode mode,
ObStmt &basic_stmt)
{
int ret = OB_SUCCESS;
ObStmt *stmt = &basic_stmt;
ObExecContext &ectx = result_set.get_exec_context();
ObPhysicalPlanCtx *pctx = ectx.get_physical_plan_ctx();
if (OB_UNLIKELY(NULL == context) || OB_UNLIKELY(NULL == context->session_info_) || OB_UNLIKELY(NULL == pctx)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret), K(context), K(pctx),
"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();
ObDelUpdStmt *del_upd_stmt = NULL;
ObField field;
common::ObIAllocator &alloc = result_set.get_mem_pool();
ObCharsetType result_charset = CHARSET_INVALID;
ObCollationType collation_type = CS_TYPE_INVALID;
context->session_info_->get_character_set_results(result_charset);
collation_type =
ObCharset::get_default_collation_by_mode(result_charset, lib::is_oracle_mode());
switch (stmt->get_stmt_type()) {
case stmt::T_SELECT: {
if (OB_FAIL(fill_select_result_set(result_set, context, mode, collation_type, type_name,
basic_stmt, field))) {
LOG_WARN("fill select result set failed", K(ret));
}
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)) {
if (ob_is_string_or_lob_type(expr->get_data_type())
&& CS_TYPE_BINARY != expr->get_collation_type()
&& ObCharset::is_valid_collation(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->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;
}
case stmt::T_HELP: {
ObHelpStmt *help_stmt = static_cast<ObHelpStmt *>(stmt);
if (OB_UNLIKELY(NULL == help_stmt)) {
ret = OB_ERR_PARSE_SQL;
LOG_WARN("logical plan of help statement error", K(ret));
} else {
ObString tname = ObString::make_string("help_table");
field.tname_ = tname;
field.org_tname_ = tname;
field.charsetnr_ = CS_TYPE_UTF8MB4_GENERAL_CI;
int64_t col_count = help_stmt->get_col_count();
if (OB_FAIL(result_set.reserve_field_columns(col_count))) {
LOG_WARN("reserve field columns failed", K(ret), K(col_count));
}
for (int64_t i = 0; OB_SUCC(ret) && i < col_count; ++i) {
field.type_.set_type(ObVarcharType);
field.type_.set_collation_type(CS_TYPE_UTF8MB4_GENERAL_CI);
field.type_.set_collation_level(CS_LEVEL_IMPLICIT);
field.type_.set_varchar(type_name);
ObString col_name;
if (OB_FAIL(help_stmt->get_col_name(i, col_name))) {
LOG_WARN("fail to get column name", K(ret), K(i));
} else if (OB_FAIL(ob_write_string(alloc, col_name, field.cname_))) {
LOG_WARN("fail to alloc string", K(ret), "name", col_name);
} else if (OB_FAIL(ob_write_string(alloc, col_name, field.org_cname_))) {
LOG_WARN("fail to alloc string", K(ret), "name", col_name);
} else if (OB_FAIL(result_set.add_field_column(field))) {
LOG_WARN("fail to add field column to result_set", K(ret));
} else {
field.cname_.assign(NULL, 0);
field.org_cname_.assign(NULL, 0);
}
}
}
break;
}
case stmt::T_CALL_PROCEDURE: {
ObCallProcedureStmt &call_stmt = static_cast<ObCallProcedureStmt&>(basic_stmt);
ObString tname = ObString::make_string("procedure");
if (NULL == call_stmt.get_call_proc_info()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("call proc info is null", K(ret));
} else {
int64_t size = call_stmt.get_call_proc_info()->get_output_count();
field.charsetnr_ = CS_TYPE_UTF8MB4_GENERAL_CI;
if (0 == size) {
break;
}
if (OB_SUCC(ret) && 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) {
ObCollationType charsetnr;
ObDataType *type = call_stmt.get_call_proc_info()->get_out_type().at(i).get_data_type();
ObObjType out_obj_type = call_stmt.get_call_proc_info()->get_out_type().at(i).get_obj_type();
if (ObUnknownType == out_obj_type
|| ObExtendType == out_obj_type) {
// do nothing ...
} else if (OB_NOT_NULL(type)) {
if (ob_is_string_or_lob_type(out_obj_type)
&& CS_TYPE_ANY == type->get_collation_type()) {
charsetnr = ObCharset::is_valid_collation(collation_type)
? collation_type
: CS_TYPE_UTF8MB4_BIN;
} else {
OZ (ObCharset::get_default_collation(type->get_collation_type(), charsetnr));
}
OX (field.charsetnr_ = static_cast<uint16_t>(charsetnr));
}
if (OB_SUCC(ret)) {
field.type_.set_type(out_obj_type);
if (OB_NOT_NULL(type)) {
field.accuracy_ = type->get_accuracy();
// Setup Collation and Collation levl
if (ob_is_string_or_lob_type(out_obj_type)
|| ob_is_raw(out_obj_type)
|| ob_is_enum_or_set_type(out_obj_type)) {
field.type_.set_collation_type(type->get_collation_type());
field.type_.set_collation_level(type->get_collation_level());
}
}
if (ObExtendType == out_obj_type) {
field.length_ = field.accuracy_.get_length();
} else if (ObCharType == out_obj_type
|| ObVarcharType == out_obj_type
|| ob_is_nstring_type(out_obj_type)) {
if (-1 == field.accuracy_.get_length()) {
field.length_ = ObCharType == out_obj_type ?
OB_MAX_ORACLE_CHAR_LENGTH_BYTE : OB_MAX_ORACLE_VARCHAR_LENGTH;
} else {
field.length_ = field.accuracy_.get_length();
}
} else if (ob_is_enum_or_set_type(out_obj_type)) {
CK (OB_NOT_NULL(type));
OZ (common::ObField::get_field_mb_length(field.type_.get_type(),
field.accuracy_,
type->get_collation_type(),
field.length_));
OX (field.type_.set_type(ObVarcharType));
} else {
OZ (common::ObField::get_field_mb_length(field.type_.get_type(),
field.accuracy_,
common::CS_TYPE_INVALID,
field.length_));
}
// Setup Scale
if (OB_SUCC(ret)) {
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, tname, field.tname_))) {
LOG_WARN("fail to alloc string", K(call_stmt.get_call_proc_info()->get_out_name().at(i)), K(ret));
} else if (OB_FAIL(ob_write_string(alloc, tname, field.org_tname_))) {
LOG_WARN("fail to alloc string", K(call_stmt.get_call_proc_info()->get_out_name().at(i)), K(ret));
} else if (OB_FAIL(ob_write_string(alloc, call_stmt.get_call_proc_info()->get_out_name().at(i), field.cname_))) {
LOG_WARN("fail to alloc string", K(call_stmt.get_call_proc_info()->get_out_name().at(i)), K(ret));
} else if (OB_FAIL(ob_write_string(alloc, call_stmt.get_call_proc_info()->get_out_name().at(i), field.org_cname_))) {
LOG_WARN("fail to alloc string", K(call_stmt.get_call_proc_info()->get_out_name().at(i)), K(ret));
} else if (OB_FAIL(ob_write_string(alloc, call_stmt.get_call_proc_info()->get_out_type_name().at(i), field.type_name_))) {
LOG_WARN("fail to alloc string", K(call_stmt.get_call_proc_info()->get_out_type_name().at(i)), K(ret));
} else if (OB_FAIL(ob_write_string(alloc, call_stmt.get_call_proc_info()->get_out_type_owner().at(i), field.type_owner_))) {
LOG_WARN("fail to alloc string", K(call_stmt.get_call_proc_info()->get_out_type_owner().at(i)), K(ret));
} else { /*do nothing*/ }
}
if (OB_SUCC(ret)) {
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);
field.type_name_.assign(NULL, 0);
field.type_owner_.assign(NULL, 0);
}
}
break;
}
}
default:
break;
}
const int64_t question_marks_count = pctx->get_is_ps_rewrite_sql() ?
pctx->get_orig_question_mark_cnt() : stmt->get_query_ctx()->get_prepare_param_count();
// param column is only needed in ps mode
if (OB_SUCC(ret) && question_marks_count > 0
&& (PC_PS_MODE == mode || PC_PL_MODE == mode)) {
if (OB_FAIL(result_set.reserve_param_columns(question_marks_count))) {
LOG_WARN("reserve param columns failed", K(ret), K(question_marks_count));
}
ObAnonymousBlockStmt *anonymous_stmt = NULL;
ObCallProcedureStmt *call_stmt = NULL;
if (stmt::T_ANONYMOUS_BLOCK == stmt->get_stmt_type()) {
CK (OB_NOT_NULL(anonymous_stmt = static_cast<ObAnonymousBlockStmt *>(stmt)));
OZ (result_set.reserve_field_columns(anonymous_stmt->get_out_idx().num_members()));
} else if (stmt::T_CALL_PROCEDURE == stmt->get_stmt_type()) {
CK (OB_NOT_NULL(call_stmt = static_cast<ObCallProcedureStmt *>(stmt)));
}
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("?");
if (OB_NOT_NULL(anonymous_stmt) && anonymous_stmt->get_out_idx().has_member(i)) {
ObField column_field;
column_field.type_.set_type(ObNullType);
OX (param_field.inout_mode_ = ObRoutineParamInOut::SP_PARAM_INOUT);
OZ (result_set.add_field_column(column_field),
K(i), K(question_marks_count), K(column_field));
} else if (OB_NOT_NULL(call_stmt) && call_stmt->get_call_proc_info()->is_out_param(i)) {
OX (param_field.inout_mode_ = ObRoutineParamInOut::SP_PARAM_INOUT);
}
OZ (result_set.add_param_column(param_field),
K(param_field), K(i), K(question_marks_count));
}
}
// for resolve returning_params and only work for ps_mode
// SQL: insert/update/delete ...returning expr1 ... into ?...
if (OB_SUCC(ret) && ObStmt::is_dml_write_stmt(stmt->get_stmt_type())
&& (PC_PS_MODE == mode || PC_PL_MODE == mode)) {
del_upd_stmt = static_cast<ObDelUpdStmt *>(stmt);
if (del_upd_stmt->is_returning()) {
int64_t returning_param_num = del_upd_stmt->get_returning_exprs().count();
if (OB_FAIL(result_set.reserve_returning_param_column(returning_param_num))) {
LOG_WARN("reserve returning param columns failed", K(ret), K(question_marks_count));
}
for (int i = 0; OB_SUCC(ret) && i < returning_param_num; ++i) {
ObField param_field;
// type is mock, client not depend it
param_field.type_.set_type(ObIntType);
param_field.cname_ = ObString::make_string("?");
param_field.inout_mode_ = ObRoutineParamInOut::SP_PARAM_OUT;
OZ (result_set.add_returning_param_column(param_field), param_field, i, returning_param_num);
}
}
}
}
return ret;
}
int ObSql::fill_select_result_set(ObResultSet &result_set, ObSqlCtx *context, const PlanCacheMode mode,
ObCollationType collation_type, const ObString &type_name,
ObStmt &basic_stmt, ObField &field)
{
int ret = OB_SUCCESS;
ObSelectStmt *select_stmt = static_cast<ObSelectStmt *>(&basic_stmt);
if (select_stmt->has_select_into()) { // for select into, no rows return
// do nothing.
} else {
int64_t size = select_stmt->get_select_item_size();
common::ObIAllocator &alloc = result_set.get_mem_pool();
number::ObNumber number;
number.set_zero();
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()
&& ObCharset::is_valid_collation(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()) {
if ((expr->is_query_ref_expr() && static_cast<ObQueryRefRawExpr*>(expr)->is_cursor())
|| (expr->is_udf_expr() && static_cast<ObUDFRawExpr*>(expr)->get_is_return_sys_cursor())) {
if (OB_FAIL(ob_write_string(alloc, "SYS_REFCURSOR", field.type_name_))) {
LOG_WARN("fail to alloc string", K(i), K(field), K(ret));
}
} else if (lib::is_oracle_mode() && expr->is_column_ref_expr() &&
static_cast<ObColumnRefRawExpr *>(expr)->is_xml_column()) {
// xmltype is supported, do nothing
} else if (NULL == context->secondary_namespace_ // pl resolve
&& NULL == context->session_info_->get_pl_context()) { // pl execute
ret = OB_NOT_SUPPORTED;
#ifdef OB_BUILD_ORACLE_PL
// error code compiltable with oracle
if (pl::ObPlJsonUtil::is_pl_jsontype(expr->get_result_type().get_udt_id())) {
ret = OB_ERR_PL_JSONTYPE_USAGE;
}
#endif
LOG_WARN("composite type use in pure sql context not supported!");
}
}
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 level
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_user_defined_sql_type()) {
if (expr->get_result_type().is_xml_sql_type()) {
// ToDo : @gehao, need record sub schemid on ObField?
field.type_.set_collation_type(CS_TYPE_BINARY);
field.type_.set_collation_level(CS_LEVEL_IMPLICIT);
field.charsetnr_ = CS_TYPE_BINARY;
field.length_ = OB_MAX_LONGTEXT_LENGTH; // set MAX_ACCURACY?
} else {
ret = OB_NOT_SUPPORTED;
LOG_WARN("udt type not supported", K(ret), "subschema id",expr->get_result_type().get_subschema_id());
}
} else 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), KPC(expr));
}
}
// SELECT ITEM的alias name和expr name规则举例:
// 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"
//
ObCollationType field_names_collation =
ObCharset::is_valid_collation(collation_type) ? collation_type : CS_TYPE_UTF8MB4_BIN;
if (OB_SUCC(ret)) {
if (OB_FAIL(ObSQLUtils::copy_and_convert_string_charset(alloc, select_item.alias_name_, field.cname_,
CS_TYPE_UTF8MB4_BIN, field_names_collation))) {
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)) {
bool is_contain_column_ref = false;
if (expr->is_column_ref_expr()
|| T_FUN_SYS_CALC_UROWID == expr->get_expr_type()
|| T_FUN_SET_TO_STR == expr->get_expr_type()
|| T_FUN_ENUM_TO_STR == expr->get_expr_type()) {
const TableItem *table_item = NULL;
ObString column_name(OB_HIDDEN_LOGICAL_ROWID_COLUMN_NAME);
if ((T_FUN_SET_TO_STR != expr->get_expr_type() &&
T_FUN_ENUM_TO_STR != expr->get_expr_type()) &&
(T_FUN_SYS_CALC_UROWID == expr->get_expr_type() ||
(lib::is_oracle_mode() &&
ObCharset::case_insensitive_equal(OB_HIDDEN_LOGICAL_ROWID_COLUMN_NAME,
static_cast<ObColumnRefRawExpr *>(expr)->get_column_name())))) {
//Although the current implement of rowid does not use mock a column schema, it should
//be as normal column when displayed externally.
if (T_FUN_SYS_CALC_UROWID == expr->get_expr_type()) {
bool got_it = false;
for (int64_t i = 0; !got_it && OB_SUCC(ret) && i < expr->get_param_count(); ++i) {
ObRawExpr *param_expr = NULL;
if (OB_ISNULL(param_expr = expr->get_param_expr(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(param_expr), KPC(expr));
} else if (param_expr->is_column_ref_expr()) {
uint64_t table_id = static_cast<ObColumnRefRawExpr *>(param_expr)->get_table_id();
table_item = select_stmt->get_table_item_by_id(table_id);
got_it = true;
is_contain_column_ref = true;
} else {/*do nothing*/}
}
} else {//mock empty column expr for generate table
is_contain_column_ref = true;
ObColumnRefRawExpr *column_expr = static_cast<ObColumnRefRawExpr *>(expr);
table_item = select_stmt->get_table_item_by_id(column_expr->get_table_id());
}
} else {
ObColumnRefRawExpr *column_expr = NULL;
if (OB_FAIL(ObRawExprUtils::get_col_ref_expr_recursively(expr, column_expr))) {
LOG_WARN("failed to get col ref expr recursively", K(ret));
} else if (OB_NOT_NULL(column_expr)) {
is_contain_column_ref = true;
uint64_t table_id = column_expr->get_table_id();
uint64_t column_id = column_expr->get_column_id();
ColumnItem *column_item = select_stmt->get_column_item_by_id(table_id, column_id);
if (OB_ISNULL(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 {
column_name = column_item->column_name_;
table_item = select_stmt->get_table_item_by_id(table_id);
if (column_expr->is_unique_key_column()) {
field.flags_ |= UNIQUE_KEY_FLAG;
}
if (column_expr->is_mul_key_column()) {
field.flags_ |= MULTIPLE_KEY_FLAG;
}
}
}
}
if (OB_SUCC(ret) && is_contain_column_ref) {
if (OB_ISNULL(table_item)) {
ret = OB_ERR_ILLEGAL_ID;
LOG_WARN("fail to get table item by id.", K(ret));
} else if (OB_FAIL(ob_write_string(alloc,
column_name,
field.org_cname_))) {
LOG_WARN("fail to alloc", K(ret), K(column_name));
} else if (OB_FAIL(ObSQLUtils::copy_and_convert_string_charset(
alloc, table_item->database_name_, field.dname_,
CS_TYPE_UTF8MB4_BIN, field_names_collation))) {
LOG_WARN("fail to alloc string", K(ret), K(table_item->database_name_));
} else if (table_item->alias_name_.length() > 0) {
if (OB_FAIL(ObSQLUtils::copy_and_convert_string_charset(
alloc, table_item->alias_name_, field.tname_,
CS_TYPE_UTF8MB4_BIN, field_names_collation))) {
LOG_WARN("fail to alloc string", K(ret), K(table_item->alias_name_));
}
} else {
if (OB_FAIL(ObSQLUtils::copy_and_convert_string_charset(
alloc, table_item->table_name_, field.tname_,
CS_TYPE_UTF8MB4_BIN, field_names_collation))) {
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_));
}
}
}
}
if (OB_SUCC(ret) && !(PC_PS_MODE == mode || PC_PL_MODE == 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_;
// 如果投影列是一个column
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;
}
}
}
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());
result.set_literal_stmt_type(stmt_info.get_literal_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 PsCacheInfoCtx &info_ctx,
ObSchemaGetterGuard &schema_guard,
ObResultSet &result)
{
int ret = OB_SUCCESS;
bool is_contain_tmp_tbl = false;
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 if (lib::is_mysql_mode() &&
OB_FAIL(check_contain_temporary_table(schema_guard, result, is_contain_tmp_tbl))) {
LOG_WARN("failed to check contain temporary table", K(ret));
} else {
ObPsStmtItem *ps_stmt_item = NULL;
ObPsStmtInfo *ref_stmt_info = NULL;
bool duplicate_prepare = false;
ObPsSqlKey ps_key;
ps_key.db_id_ = db_id;
ps_key.ps_sql_ = info_ctx.normalized_sql_;
ps_key.is_client_return_hidden_rowid_ = session.is_client_return_rowid();
// add stmt item
if (OB_FAIL(ps_cache->get_or_add_stmt_item(ps_key,
is_contain_tmp_tbl,
ps_stmt_item))) {
LOG_WARN("get or create stmt item faield", K(ret), K(db_id), K(info_ctx.normalized_sql_));
} else if (OB_FAIL(ps_cache->get_or_add_stmt_info(info_ctx,
result,
schema_guard,
ps_stmt_item,
ref_stmt_info))) {
LOG_WARN("get or create stmt info failed", K(ret), K(ps_stmt_item), K(db_id), K(info_ctx));
} 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));
} else {
ref_stmt_info->set_literal_stmt_type(result.get_literal_stmt_type());
}
if (NULL != ref_stmt_info) {
ref_stmt_info->set_is_sensitive_sql(info_ctx.is_sensitive_sql_);
}
//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,
info_ctx.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(info_ctx.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::check_contain_temporary_table(share::schema::ObSchemaGetterGuard &schema_guard,
ObResultSet &result,
bool &is_contain_tmp_tbl)
{
int ret = OB_SUCCESS;
is_contain_tmp_tbl = false;
const ObTableSchema *table_schema = nullptr;
for (int64_t i = 0; OB_SUCC(ret) && !is_contain_tmp_tbl && i < result.get_ref_objects().count(); i++) {
table_schema = nullptr;
ObSchemaObjVersion &obj_version = result.get_ref_objects().at(i);
if (DEPENDENCY_TABLE != obj_version.object_type_) {
// do nothing
} else if (OB_FAIL(schema_guard.get_table_schema(MTL_ID(),
obj_version.object_id_,
table_schema))) {
LOG_WARN("failed to get table schema", K(ret), K(obj_version), K(table_schema));
} else if (nullptr == table_schema) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get an unexpected null schema", K(ret), K(table_schema));
} else if (table_schema->is_tmp_table()) {
is_contain_tmp_tbl = true;
break;
}
}
return ret;