forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_sql_context.cpp
More file actions
933 lines (874 loc) · 33.7 KB
/
Copy pathob_sql_context.cpp
File metadata and controls
933 lines (874 loc) · 33.7 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
/*
* 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_RESV
#include "ob_sql_context.h"
#include "share/catalog/ob_cached_catalog_meta_getter.h"
#include "share/external_table/ob_external_object_ctx.h"
#include "sql/optimizer/ob_log_plan.h"
#include "sql/ob_sql_mock_schema_utils.h"
#include "share/schema/ob_schema_getter_guard.h"
#include "src/storage/tx/ob_trans_define_v4.h"
using namespace ::oceanbase::common;
namespace oceanbase
{
using namespace share::schema;
namespace sql
{
bool LocationConstraint::operator==(const LocationConstraint &other) const {
return key_ == other.key_ && phy_loc_type_ == other.phy_loc_type_ && constraint_flags_ == other.constraint_flags_ ;
}
int ObLocationConstraintContext::calc_constraints_inclusion(const ObPwjConstraint *left,
const ObPwjConstraint *right,
InclusionType &inclusion_result)
{
int ret = OB_SUCCESS;
inclusion_result = NotSubset;
if (OB_ISNULL(left) || OB_ISNULL(right)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(left), K(right));
} else {
const ObPwjConstraint *set1 = NULL, *set2 = NULL;
bool is_subset = true;
// insure set1.count() >= set2.count()
if (left->count() >= right->count()) {
inclusion_result = LeftIsSuperior;
set1 = left;
set2 = right;
} else {
inclusion_result = RightIsSuperior;
set1 = right;
set2 = left;
}
for (int64_t i = 0; is_subset && i < set2->count(); i++) {
bool detected = false;
for (int64_t j = 0; !detected && j < set1->count(); j++) {
if (set2->at(i) == set1->at(j)) {
detected = true;
}
}
// if the element is not in set1, set1 can not contain all the elements in set2
if (!detected) {
is_subset = false;
}
}
if (!is_subset) {
inclusion_result = NotSubset;
}
}
return ret;
}
int ObQueryRetryInfo::init()
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(inited_)) {
ret = OB_INIT_TWICE;
LOG_ERROR("init twice", K(ret));
} else {
inited_ = true;
}
return ret;
}
void ObQueryRetryInfo::reset()
{
inited_ = false;
is_rpc_timeout_ = false;
last_query_retry_err_ = OB_SUCCESS;
retry_cnt_ = 0;
query_switch_leader_retry_timeout_ts_ = 0;
query_retry_ash_info_.reset();
}
void ObQueryRetryInfo::clear()
{
// Here cannot set inited_ to false
is_rpc_timeout_ = false;
//last_query_retry_err_ = OB_SUCCESS;
}
void ObQueryRetryInfo::set_is_rpc_timeout(bool is_rpc_timeout)
{
is_rpc_timeout_ = is_rpc_timeout;
}
bool ObQueryRetryInfo::is_rpc_timeout() const
{
return is_rpc_timeout_;
}
ObSqlCtx::ObSqlCtx()
: session_info_(NULL),
schema_guard_(NULL),
secondary_namespace_(NULL),
plan_cache_hit_(false),
self_add_plan_(false),
disable_privilege_check_(PRIV_CHECK_FLAG_NORMAL),
force_print_trace_(false),
is_show_trace_stmt_(false),
retry_times_(OB_INVALID_COUNT),
exec_type_(InvalidType),
is_prepare_protocol_(false),
is_pre_execute_(false),
is_prepare_stage_(false),
is_dynamic_sql_(false),
is_dbms_sql_(false),
is_cursor_(false),
is_remote_sql_(false),
statement_id_(common::OB_INVALID_ID),
stmt_type_(stmt::T_NONE),
is_restore_(false),
need_late_compile_(false),
all_plan_const_param_constraints_(nullptr),
all_possible_const_param_constraints_(nullptr),
all_equal_param_constraints_(nullptr),
all_pre_calc_constraints_(nullptr),
all_expr_constraints_(nullptr),
all_priv_constraints_(nullptr),
need_match_all_params_(false),
all_local_session_vars_(nullptr),
is_ddl_from_primary_(false),
cur_stmt_(NULL),
cur_plan_(nullptr),
can_reroute_sql_(false),
is_sensitive_(false),
is_protocol_weak_read_(false),
flashback_query_expr_(nullptr),
is_execute_call_stmt_(false),
enable_sql_resource_manage_(false),
resource_map_rule_(),
is_text_ps_mode_(false),
first_plan_hash_(0),
is_bulk_(false),
ins_opt_ctx_(),
flags_(0),
ccl_rule_id_(0),
ccl_match_time_(0),
reroute_info_(nullptr)
{
sql_id_[0] = '\0';
sql_id_[common::OB_MAX_SQL_ID_LENGTH] = '\0';
format_sql_id_[0] = '\0';
format_sql_id_[common::OB_MAX_SQL_ID_LENGTH] = '\0';
}
void ObSqlCtx::reset()
{
multi_stmt_item_.reset();
session_info_ = NULL;
schema_guard_ = NULL;
plan_cache_hit_ = false;
self_add_plan_ = false;
disable_privilege_check_ = PRIV_CHECK_FLAG_NORMAL;
force_print_trace_ = false;
is_show_trace_stmt_ = false;
retry_times_ = OB_INVALID_COUNT;
sql_id_[0] = '\0';
sql_id_[common::OB_MAX_SQL_ID_LENGTH] = '\0';
format_sql_id_[0] = '\0';
format_sql_id_[common::OB_MAX_SQL_ID_LENGTH] = '\0';
exec_type_ = InvalidType;
is_prepare_protocol_ = false;
is_pre_execute_ = false;
is_prepare_stage_ = false;
is_dynamic_sql_ = false;
is_remote_sql_ = false;
is_restore_ = false;
need_late_compile_ = false;
all_plan_const_param_constraints_ = nullptr;
all_possible_const_param_constraints_ = nullptr;
all_equal_param_constraints_ = nullptr;
all_pre_calc_constraints_ = nullptr;
all_expr_constraints_ = nullptr;
all_priv_constraints_ = nullptr;
need_match_all_params_ = false;
all_local_session_vars_ = nullptr;
is_ddl_from_primary_ = false;
can_reroute_sql_ = false;
is_sensitive_ = false;
enable_sql_resource_manage_ = false;
resource_map_rule_.reset();
is_protocol_weak_read_ = false;
first_plan_hash_ = 0;
first_outline_data_.reset();
first_equal_param_cons_cnt_ = 0;
first_const_param_cons_cnt_ = 0;
first_expr_cons_cnt_ = 0;
if (nullptr != reroute_info_) {
reroute_info_->reset();
op_reclaim_free(reroute_info_);
reroute_info_ = nullptr;
}
clear();
flashback_query_expr_ = nullptr;
stmt_type_ = stmt::T_NONE;
cur_plan_ = nullptr;
is_execute_call_stmt_ = false;
is_text_ps_mode_ = false;
enable_strict_defensive_check_ = false;
enable_user_defined_rewrite_ = false;
is_bulk_ = false;
ins_opt_ctx_.reset();
ccl_rule_id_ = 0;
ccl_match_time_ = 0;
reconstruct_ps_sql_.reset();
matched_ccl_rule_level_values_.reset();
matched_ccl_format_sqlid_level_values_.reset();
}
//release dynamic allocated memory
void ObSqlCtx::clear()
{
partition_infos_.reset();
related_user_var_names_.reset();
base_constraints_.reset();
strict_constraints_.reset();
non_strict_constraints_.reset();
dup_table_replica_cons_.reset();
multi_stmt_rowkey_pos_.reset();
bl_key_.reset();
cur_stmt_ = nullptr;
is_text_ps_mode_ = false;
ins_opt_ctx_.clear();
cur_plan_ = nullptr;
}
OB_SERIALIZE_MEMBER(ObSqlCtx, stmt_type_);
void ObSqlSchemaGuard::reset()
{
mocked_database_schemas_.reset();
table_schemas_.reset();
schema_guard_ = NULL;
allocator_.reset();
next_link_table_id_ = 1;
dblink_scn_.reuse();
mocked_schema_id_counter_ = OB_MIN_EXTERNAL_OBJECT_ID;
}
bool ObSqlSchemaGuard::is_link_table(const ObDMLStmt *stmt, uint64_t table_id)
{
bool is_link = false;
TableItem *table_item = NULL;
if (NULL != stmt) {
table_item = stmt->get_table_item_by_id(table_id);
is_link = (NULL == table_item) ? false : table_item->is_link_table();
}
return is_link;
}
int ObSqlSchemaGuard::add_mocked_table_schema(const ObTableSchema &table_schema)
{
int ret = OB_SUCCESS;
ObTableSchema *temp_schema = NULL;
OZ (ObSchemaUtils::alloc_schema(allocator_, table_schema, temp_schema));
OZ (table_schemas_.push_back(temp_schema));
return ret;
}
int ObSqlSchemaGuard::add_mocked_database_schema(const share::schema::ObDatabaseSchema &database_schema)
{
int ret = OB_SUCCESS;
ObDatabaseSchema *tmp_schema = NULL;
OZ(ObSchemaUtils::alloc_schema(allocator_, database_schema, tmp_schema));
OZ(mocked_database_schemas_.push_back(tmp_schema));
return ret;
}
int ObSqlSchemaGuard::get_mocked_table_schema(uint64_t ref_table_id, const ObTableSchema *&table_schema) const
{
int ret = OB_SUCCESS;
table_schema = NULL;
for (int i = 0; OB_SUCC(ret) && i < table_schemas_.count(); i++) {
const ObTableSchema *cur_table_schema = table_schemas_.at(i);
if (OB_NOT_NULL(cur_table_schema) && cur_table_schema->get_table_id() == ref_table_id) {
table_schema = cur_table_schema;
break;
}
}
OV (OB_NOT_NULL(table_schema));
return ret;
}
int ObSqlSchemaGuard::recover_schema_from_external_object(const share::ObExternalObject &external_object)
{
int ret = OB_SUCCESS;
switch (external_object.type) {
case share::ObExternalObjectType::TABLE_SCHEMA: {
const uint64_t tenant_id = external_object.tenant_id;
const uint64_t catalog_id = external_object.catalog_id;
const uint64_t database_id = external_object.database_id;
const common::ObString table_name = external_object.table_name;
const uint64_t table_id = external_object.table_id;
const ObTableSchema *table_schema = NULL;
if (OB_FAIL(get_catalog_table_schema(tenant_id, catalog_id, database_id, table_name, table_schema))) {
LOG_WARN("get catalog table schema failed", K(ret));
} else if (OB_ISNULL(table_schema)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("table schema is null", K(ret));
} else {
// reset table_id, because of sql_schema_guard.get_catalog_table_schema will reassign table_id
ObTableSchema *non_const_table_schema = const_cast<ObTableSchema *>(table_schema);
non_const_table_schema->set_table_id(table_id);
}
break;
}
case share::ObExternalObjectType::DATABASE_SCHEMA: {
const uint64_t tenant_id = external_object.tenant_id;
const uint64_t catalog_id = external_object.catalog_id;
const uint64_t database_id = external_object.database_id;
const common::ObString database_name = external_object.database_name;
const ObDatabaseSchema *db_schema = NULL;
if (OB_FAIL(get_catalog_database_schema(tenant_id, catalog_id, database_name, db_schema))) {
LOG_WARN("get catalog database schema failed", K(ret));
} else if (OB_ISNULL(db_schema)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("database schema is null", K(ret));
} else {
ObDatabaseSchema *non_const_db_schema = const_cast<ObDatabaseSchema *>(db_schema);
// reset database_id, because of sql_schema_guard.get_catalog_database_schema will reassign database_id
non_const_db_schema->set_database_id(database_id);
}
break;
}
default: {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected", K(ret));
}
}
return ret;
}
int ObSqlSchemaGuard::recover_schema_from_external_objects(const ObIArray<share::ObExternalObject> &external_objects)
{
int ret = OB_SUCCESS;
// recover mocked database schema first, because mocked table schema rely on database schema
for (int64_t i = 0; OB_SUCC(ret) && i < external_objects.count(); i++) {
const share::ObExternalObject &external_object = external_objects.at(i);
if (external_object.type == share::ObExternalObjectType::DATABASE_SCHEMA) {
OZ(recover_schema_from_external_object(external_object));
}
}
for (int64_t i = 0; OB_SUCC(ret) && i < external_objects.count(); i++) {
const share::ObExternalObject &external_object = external_objects.at(i);
if (external_object.type == share::ObExternalObjectType::TABLE_SCHEMA) {
OZ(recover_schema_from_external_object(external_object));
}
}
return ret;
}
int ObSqlSchemaGuard::get_table_schema(uint64_t table_id,
uint64_t ref_table_id,
const ObDMLStmt *stmt,
const ObTableSchema *&table_schema)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt)) {
ret = OB_INVALID_ARGUMENT;;
LOG_WARN("get unexpected null", K(ret), K(stmt));
} else {
const TableItem *item = stmt->get_table_item_by_id(table_id);
if (is_external_object_id(table_id)) {
if (OB_FAIL(get_mocked_table_schema(ref_table_id, table_schema))) {
LOG_WARN("failed to get mocked table schema", K(ref_table_id), K(ret));
}
} else if (OB_FAIL(get_table_schema(ref_table_id, table_schema))) {
LOG_WARN("failed to get table schema", K(table_id), K(ret));
}
}
return ret;
}
int ObSqlSchemaGuard::get_table_schema(uint64_t table_id,
const TableItem *table_item,
const ObTableSchema *&table_schema)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(table_item) ) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("get unexpected null", K(ret), K(table_item));
} else if (is_external_object_id(table_id)) {
if (OB_FAIL(get_mocked_table_schema(table_id, table_schema))) {
LOG_WARN("failed to get mocked table schema", K(table_id), K(ret));
}
} else if (OB_FAIL(get_table_schema(table_id, table_schema))) {
LOG_WARN("failed to get table schema", K(table_id), K(ret));
}
return ret;
}
int ObSqlSchemaGuard::get_table_schema(uint64_t table_id,
const ObTableSchema *&table_schema,
bool is_link /* = false*/) const
{
int ret = OB_SUCCESS;
if (is_external_object_id(table_id)) {
if (OB_FAIL(get_mocked_table_schema(table_id, table_schema))) {
LOG_WARN("failed to get mocked table schema", K(table_id), K(ret));
}
} else {
const uint64_t tenant_id = MTL_ID();
OV (OB_NOT_NULL(schema_guard_));
OZ (schema_guard_->get_table_schema(tenant_id, table_id, table_schema), table_id, is_link);
}
return ret;
}
int ObSqlSchemaGuard::get_table_schema(const uint64_t tenant_id,
const uint64_t table_id,
const share::schema::ObTableSchema *&table_schema,
bool is_link /* = false*/)
{
int ret = OB_SUCCESS;
if (is_external_object_id(table_id)) {
if (OB_FAIL(get_mocked_table_schema(table_id, table_schema))) {
LOG_WARN("failed to get mocked table schema", K(table_id), K(ret));
}
} else {
OV (OB_NOT_NULL(schema_guard_));
OZ (schema_guard_->get_table_schema(tenant_id, table_id, table_schema), table_id, is_link);
}
return ret;
}
int ObSqlSchemaGuard::get_database_schema(const uint64_t tenant_id,
const uint64_t database_id,
const ObDatabaseSchema *&database_schema)
{
int ret = OB_SUCCESS;
database_schema = NULL;
if (is_external_object_id(database_id)) {
for (int64_t i = 0; OB_SUCC(ret) && i < mocked_database_schemas_.count(); i++) {
const share::schema::ObDatabaseSchema *&tmp_schema = mocked_database_schemas_.at(i);
if (OB_ISNULL(tmp_schema)) {
// Ignore this null, continue loop
// ignore ret
LOG_WARN("get unexpected null", K(ret));
} else if (database_id == tmp_schema->get_database_id()) {
database_schema = tmp_schema;
break;
}
}
// not found
if (OB_SUCC(ret) && OB_ISNULL(database_schema)) {
LOG_WARN("database not found", K(ret), K(database_id));
ret = OB_ERR_BAD_DATABASE;
}
} else {
OV(OB_NOT_NULL(schema_guard_));
OZ(schema_guard_->get_database_schema(tenant_id, database_id, database_schema), tenant_id, database_id);
}
return ret;
}
int ObSqlSchemaGuard::get_database_schema(const uint64_t database_id,
const ObDatabaseSchema *&database_schema)
{
int ret = OB_SUCCESS;
const uint64_t tenant_id = MTL_ID();
if (OB_FAIL(get_database_schema(tenant_id, database_id, database_schema))) {
LOG_WARN("failed to get database schema", K(ret), K(tenant_id), K(database_id));
}
return ret;
}
int ObSqlSchemaGuard::get_catalog_database_schema(const uint64_t tenant_id,
const uint64_t catalog_id,
const ObString &database_name,
const ObDatabaseSchema *&database_schema)
{
int ret = OB_SUCCESS;
ObNameCaseMode case_mode = OB_NAME_CASE_INVALID;
database_schema = NULL;
if (OB_ISNULL(schema_guard_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(schema_guard_->get_tenant_name_case_mode(tenant_id, case_mode))) {
LOG_WARN("failed to get case mode", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < mocked_database_schemas_.count(); i++) {
const share::schema::ObDatabaseSchema *&tmp_schema = mocked_database_schemas_.at(i);
if (OB_ISNULL(tmp_schema)) {
// Ignore this null, continue loop
// ignore ret
LOG_WARN("get unexpected null", K(ret));
} else if (tenant_id == tmp_schema->get_tenant_id() && catalog_id == tmp_schema->get_catalog_id()
&& ObCharset::case_mode_equal(case_mode, database_name, tmp_schema->get_database_name())) {
database_schema = tmp_schema;
break;
}
}
}
if (OB_SUCC(ret) && OB_ISNULL(database_schema)) {
// not found from local, find from catalog and push into catalog_database_schemas_
ObDatabaseSchema tmp_schema;
ObCachedCatalogMetaGetter catalog_meta_getter{*schema_guard_, allocator_};
// assign database id first
tmp_schema.set_database_id(get_next_mocked_schema_id());
if (OB_FAIL(catalog_meta_getter.fetch_namespace_schema(tenant_id, catalog_id, database_name, case_mode, tmp_schema))) {
LOG_WARN("failed to fetch_namespace_schema", K(ret));
} else if (OB_FAIL(add_mocked_database_schema(tmp_schema))) {
LOG_WARN("failed to add_mocked_schema", K(ret));
} else {
// retrieve ObDatabaseSchema from mocked_database_schemas_
database_schema = mocked_database_schemas_.at(mocked_database_schemas_.count() - 1);
}
}
return ret;
}
int ObSqlSchemaGuard::get_catalog_database_id(const uint64_t tenant_id,
const uint64_t catalog_id,
const ObString &database_name,
uint64_t &database_id)
{
int ret = OB_SUCCESS;
database_id = OB_INVALID_ID;
const ObDatabaseSchema *schema = NULL;
if (OB_FAIL(get_catalog_database_schema(tenant_id, catalog_id, database_name, schema))) {
LOG_WARN("failed to get_catalog_database_schema", K(ret));
} else if (OB_ISNULL(schema)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("database schema must not be null", K(ret));
} else {
database_id = schema->get_database_id();
}
return ret;
}
int ObSqlSchemaGuard::get_catalog_table_schema(const uint64_t tenant_id,
const uint64_t catalog_id,
const uint64_t database_id,
const ObString &database_name,
const ObString &tbl_name,
const ObTableSchema *&table_schema)
{
int ret = OB_SUCCESS;
ObNameCaseMode case_mode = OB_NAME_CASE_INVALID;
table_schema = NULL;
if (OB_ISNULL(schema_guard_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret));
} else if (OB_FAIL(schema_guard_->get_tenant_name_case_mode(tenant_id, case_mode))) {
LOG_WARN("failed to get case mode", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < table_schemas_.count(); i++) {
const ObTableSchema *&tmp_schema = table_schemas_.at(i);
if (OB_ISNULL(tmp_schema)) {
// Ignore this null, continue loop
// ignore ret
LOG_WARN("get unexpected null", K(ret));
} else if (tenant_id == tmp_schema->get_tenant_id() && catalog_id == tmp_schema->get_catalog_id()
&& database_id == tmp_schema->get_database_id()
&& ObCharset::case_mode_equal(case_mode, tbl_name, tmp_schema->get_table_name())) {
table_schema = tmp_schema;
break;
}
}
}
if (OB_SUCC(ret) && OB_ISNULL(table_schema)) {
// not found local, fetch from remote
ObTableSchema tmp_schema;
int64_t schema_version = 0;
ObCachedCatalogMetaGetter catalog_meta_getter{*schema_guard_, allocator_};
tmp_schema.set_database_id(database_id);
tmp_schema.set_table_id(get_next_mocked_schema_id());
if (OB_FAIL(catalog_meta_getter.fetch_table_schema(tenant_id, catalog_id, database_name, tbl_name, case_mode, tmp_schema))) {
LOG_WARN("failed to fetch_table_schema", K(ret));
} else if (OB_FAIL(schema_guard_->get_schema_version(tenant_id, schema_version))) {
LOG_WARN("get schema version failed", K(ret));
} else if (FALSE_IT(tmp_schema.set_schema_version(schema_version))) {
} else if (OB_FAIL(add_mocked_table_schema(tmp_schema))) {
LOG_WARN("add mocked table schema failed", K(ret));
} else {
table_schema = table_schemas_.at(table_schemas_.count() - 1);
}
}
return ret;
}
int ObSqlSchemaGuard::get_catalog_table_id(const uint64_t tenant_id,
const uint64_t catalog_id,
const uint64_t database_id,
const ObString &tbl_name,
uint64_t &table_id)
{
int ret = OB_SUCCESS;
table_id = OB_INVALID_ID;
const ObTableSchema *table_schema = NULL;
if (OB_FAIL(get_catalog_table_schema(tenant_id, catalog_id, database_id, tbl_name, table_schema))) {
LOG_WARN("get_catalog_table_schema failed", K(ret));
} else if (OB_ISNULL(table_schema)) {
ret = OB_ERR_UNEXPECTED;
} else if (OB_FALSE_IT(table_id = table_schema->get_table_id())) {
}
return ret;
}
int ObSqlSchemaGuard::get_catalog_table_schema(const uint64_t tenant_id,
const uint64_t catalog_id,
const uint64_t database_id,
const ObString &tbl_name,
const ObTableSchema *&table_schema)
{
int ret = OB_SUCCESS;
const ObDatabaseSchema *database_schema = NULL;
table_schema = NULL;
if (OB_FAIL(get_database_schema(database_id, database_schema))) {
LOG_WARN("get database schema failed", K(ret));
} else if (OB_ISNULL(database_schema)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get database schema failed", K(ret));
} else if (OB_FAIL(get_catalog_table_schema(tenant_id,
catalog_id,
database_id,
database_schema->get_database_name(),
tbl_name,
table_schema))) {
LOG_WARN("get table schema failed", K(ret));
} else if (OB_ISNULL(table_schema)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get table schema failed", K(ret));
}
return ret;
}
int ObSqlSchemaGuard::get_column_schema(uint64_t table_id, const ObString &column_name,
const ObColumnSchemaV2 *&column_schema,
bool is_link /* = false */) const
{
int ret = OB_SUCCESS;
if (is_external_object_id(table_id)) {
const ObTableSchema *table_schema = NULL;
OZ (get_mocked_table_schema(table_id, table_schema));
if (OB_NOT_NULL(table_schema)) {
column_schema = table_schema->get_column_schema(column_name);
}
} else {
// first get table_schema, than try mock column_schema for part id
const uint64_t tenant_id = MTL_ID();
const ObTableSchema *table_schema = NULL;
OV (OB_NOT_NULL(schema_guard_));
OV ((OB_INVALID_ID != table_id && !column_name.empty()));
OZ (schema_guard_->get_table_schema(tenant_id, table_id, table_schema));
if (table_schema == NULL) {
// do nothing, same as schema_guard_->get_column_schema()
} else {
OZ (sql::ObSQLMockSchemaUtils::try_mock_partid(table_schema, table_schema));
OX (column_schema = table_schema->get_column_schema(column_name));
}
}
return ret;
}
int ObSqlSchemaGuard::get_column_schema(uint64_t table_id, uint64_t column_id,
const ObColumnSchemaV2 *&column_schema,
bool is_link /* = false */) const
{
int ret = OB_SUCCESS;
if (is_external_object_id(table_id)) {
const ObTableSchema *table_schema = NULL;
OZ (get_mocked_table_schema(table_id, table_schema));
if (OB_NOT_NULL(table_schema)) {
column_schema = table_schema->get_column_schema(column_id);
}
} else {
// first get table_schema, than try mock column_schema for part id
const uint64_t tenant_id = MTL_ID();
const ObTableSchema *table_schema = NULL;
OV (OB_NOT_NULL(schema_guard_));
OV ((OB_INVALID_ID != table_id && OB_INVALID_ID != column_id));
OZ (schema_guard_->get_table_schema(tenant_id, table_id, table_schema));
if (table_schema == NULL) {
// do nothing, same as schema_guard_->get_column_schema()
} else {
OZ (sql::ObSQLMockSchemaUtils::try_mock_partid(table_schema, table_schema));
OX (column_schema = table_schema->get_column_schema(column_id));
}
}
return ret;
}
int ObSqlSchemaGuard::get_table_schema_version(const uint64_t table_id,
int64_t &schema_version) const
{
int ret = OB_SUCCESS;
const uint64_t tenant_id = MTL_ID();
OV (OB_NOT_NULL(schema_guard_));
OZ (schema_guard_->get_schema_version(TABLE_SCHEMA, tenant_id, table_id, schema_version), table_id);
return ret;
}
int ObSqlSchemaGuard::get_can_read_index_array(uint64_t table_id,
uint64_t *index_tid_array,
int64_t &size,
bool with_mv,
bool with_global_index,
bool with_domain_index,
bool with_spatial_index,
bool with_vector_index)
{
int ret = OB_SUCCESS;
const uint64_t tenant_id = MTL_ID();
if (is_external_object_id(table_id)) {
size = 0;
} else {
OV (OB_NOT_NULL(schema_guard_));
OZ (schema_guard_->get_can_read_index_array(tenant_id, table_id,
index_tid_array, size, with_mv,
with_global_index, with_domain_index,
with_spatial_index, with_vector_index));
}
return ret;
}
int ObSqlSchemaGuard::get_table_mlog_schema(const uint64_t table_id,
const ObTableSchema *&mlog_schema)
{
int ret = OB_SUCCESS;
const uint64_t tenant_id = MTL_ID();
OV (OB_NOT_NULL(schema_guard_));
OZ (schema_guard_->get_table_mlog_schema(tenant_id, table_id, mlog_schema));
return ret;
}
common::ObIArray<const share::schema::ObDatabaseSchema *> &ObSqlSchemaGuard::get_mocked_database_schemas()
{
return mocked_database_schemas_;
}
common::ObIArray<const share::schema::ObTableSchema *> &ObSqlSchemaGuard::get_mocked_table_schemas()
{
return table_schemas_;
}
int ObSqlCtx::set_partition_infos(const ObTablePartitionInfoArray &info, ObIAllocator &allocator)
{
int ret = OB_SUCCESS;
int64_t count = info.count();
partition_infos_.reset();
if (count > 0) {
partition_infos_.set_allocator(&allocator);
if (OB_FAIL(partition_infos_.init(count))) {
LOG_WARN("init partition info failed", K(ret), K(count));
} else {
for (int64_t i = 0; i < count && OB_SUCC(ret); ++i) {
if (OB_FAIL(partition_infos_.push_back(info.at(i)))) {
LOG_WARN("push partition info failed", K(ret), K(count));
}
}
}
}
return ret;
}
int ObSqlCtx::set_related_user_var_names(const ObIArray<ObString> &user_var_names,
ObIAllocator &allocator)
{
int ret = OB_SUCCESS;
if (user_var_names.count() > 0) {
related_user_var_names_.reset();
related_user_var_names_.set_allocator(&allocator);
if (OB_FAIL(related_user_var_names_.init(user_var_names.count()))) {
LOG_WARN("failed to init related_user_var_names", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < user_var_names.count(); i++) {
if (OB_FAIL(related_user_var_names_.push_back(user_var_names.at(i)))) {
LOG_WARN("failed to push back user var names", K(ret));
}
}
}
}
if (OB_FAIL(ret)) {
related_user_var_names_.reset();
}
return ret;
}
int ObSqlCtx::set_location_constraints(const ObLocationConstraintContext &location_constraint,
ObIAllocator &allocator)
{
int ret = OB_SUCCESS;
base_constraints_.reset();
strict_constraints_.reset();
non_strict_constraints_.reset();
dup_table_replica_cons_.reset();
const ObIArray<LocationConstraint> &base_constraints = location_constraint.base_table_constraints_;
const ObIArray<ObPwjConstraint *> &strict_constraints = location_constraint.strict_constraints_;
const ObIArray<ObPwjConstraint *> &non_strict_constraints = location_constraint.non_strict_constraints_;
const ObIArray<ObDupTabConstraint> &dup_table_replica_cons = location_constraint.dup_table_replica_cons_;
if (base_constraints.count() > 0) {
base_constraints_.set_allocator(&allocator);
if (OB_FAIL(base_constraints_.init(base_constraints.count()))) {
LOG_WARN("init base constraints failed", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < base_constraints.count(); i++) {
if (OB_FAIL(base_constraints_.push_back(base_constraints.at(i)))) {
LOG_WARN("failed to push back base constraint", K(ret));
} else {
// table_partition_info_ is only used during the plan generation phase
base_constraints_.at(i).table_partition_info_ = NULL;
}
}
LOG_DEBUG("set base constraints", K(base_constraints.count()));
}
}
if (OB_SUCC(ret) && strict_constraints.count() > 0) {
strict_constraints_.set_allocator(&allocator);
if (OB_FAIL(strict_constraints_.init(strict_constraints.count()))) {
LOG_WARN("init strict constraints failed", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < strict_constraints.count(); i++) {
if (OB_FAIL(strict_constraints_.push_back(strict_constraints.at(i)))) {
LOG_WARN("failed to push back location constraint", K(ret));
}
}
LOG_DEBUG("set strict constraints", K(strict_constraints.count()));
}
}
if (OB_SUCC(ret) && non_strict_constraints.count() > 0) {
non_strict_constraints_.set_allocator(&allocator);
if (OB_FAIL(non_strict_constraints_.init(non_strict_constraints.count()))) {
LOG_WARN("init non strict constraints failed", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < non_strict_constraints.count(); i++) {
if (OB_FAIL(non_strict_constraints_.push_back(non_strict_constraints.at(i)))) {
LOG_WARN("failed to push back location constraint", K(ret));
}
}
LOG_DEBUG("set non strict constraints", K(non_strict_constraints.count()));
}
}
if (OB_SUCC(ret) && dup_table_replica_cons.count() > 0) {
dup_table_replica_cons_.set_allocator(&allocator);
if (OB_FAIL(dup_table_replica_cons_.init(dup_table_replica_cons.count()))) {
LOG_WARN("init duplicate table replica constraints failed", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < dup_table_replica_cons.count(); i++) {
if (OB_FAIL(dup_table_replica_cons_.push_back(dup_table_replica_cons.at(i)))) {
LOG_WARN("failed to push back location constraint", K(ret));
}
}
LOG_DEBUG("set duplicate table replica constraints", K(dup_table_replica_cons.count()));
}
}
return ret;
}
int ObSqlCtx::set_multi_stmt_rowkey_pos(const common::ObIArray<int64_t> &multi_stmt_rowkey_pos,
common::ObIAllocator &alloctor)
{
int ret = OB_SUCCESS;
if (!multi_stmt_rowkey_pos.empty()) {
multi_stmt_rowkey_pos_.set_allocator(&alloctor);
if (OB_FAIL(multi_stmt_rowkey_pos_.init(multi_stmt_rowkey_pos.count()))) {
LOG_WARN("failed to init rowkey count", K(ret));
} else if (OB_FAIL(append(multi_stmt_rowkey_pos_, multi_stmt_rowkey_pos))) {
LOG_WARN("failed to append multi stmt rowkey pos", K(ret));
} else { /*do nothing*/ }
}
return ret;
}
int ObQueryCtx::add_local_session_vars(ObIAllocator *alloc, const ObLocalSessionVar &local_session_var, int64_t &idx) {
int ret = OB_SUCCESS;
if (OB_FAIL(all_local_session_vars_.push_back(ObLocalSessionVar()))) {
LOG_WARN("push back local session var failed", K(ret));
} else {
idx = all_local_session_vars_.count() - 1;
ObLocalSessionVar &local_var = all_local_session_vars_.at(idx);
local_var.set_allocator(alloc);
if (OB_FAIL(local_var.deep_copy(local_session_var))) {
LOG_WARN("deep copy local session var failed", K(ret));
}
}
return ret;
}
int ObQueryCtx::get_local_session_vars(const int64_t idx, const ObLocalSessionVar *&local_session_var) const
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(idx < 0 || idx >= all_local_session_vars_.count())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get invalid idx", K(ret), K(idx), K(all_local_session_vars_.count()));
} else {
local_session_var = &all_local_session_vars_.at(idx);
}
return ret;
}
}
}