-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ob_table_location.h
1167 lines (1037 loc) · 47.7 KB
/
ob_table_location.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
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.
*/
#ifndef _OCEANBASE_SQL_OPTIMIZER_OB_TABLE_LOCATION_H
#define _OCEANBASE_SQL_OPTIMIZER_OB_TABLE_LOCATION_H
#include "lib/hash/ob_pointer_hashmap.h"
#include "sql/rewrite/ob_query_range.h"
#include "sql/engine/expr/ob_sql_expression.h"
#include "sql/engine/expr/ob_sql_expression_factory.h"
#include "sql/engine/expr/ob_expr_operator_factory.h"
namespace oceanbase {
namespace common {
class ObPartMgr;
}
namespace sql {
class ObRawExpr;
class ObPartHint;
class ObRawExprFactory;
class ObColumnRefRawExpr;
class ObExprEqualCheckContext;
typedef common::ObSEArray<int64_t, 1> RowkeyArray;
class ObPartIdRowMapManager {
public:
ObPartIdRowMapManager() : manager_(), part_idx_(common::OB_INVALID_INDEX)
{}
typedef common::ObSEArray<int64_t, 12> ObRowIdList;
struct MapEntry {
public:
MapEntry() : list_()
{}
TO_STRING_KV(K_(list));
int assign(const MapEntry& entry);
public:
ObRowIdList list_;
};
typedef common::ObSEArray<MapEntry, 1> ObPartRowManager;
int add_row_for_part(int64_t part_idx, int64_t row_id);
const ObRowIdList* get_row_id_list(int64_t part_index);
void reset()
{
manager_.reset();
part_idx_ = common::OB_INVALID_INDEX;
}
int64_t get_part_count() const
{
return manager_.count();
}
int assign(const ObPartIdRowMapManager& other);
int64_t get_part_idx() const
{
return part_idx_;
}
void set_part_idx(int64_t part_idx)
{
part_idx_ = part_idx;
}
const MapEntry& at(int64_t i) const
{
return manager_.at(i);
}
common::ObNewRow& get_part_row()
{
return part_row_;
}
TO_STRING_KV(K_(manager), K_(part_idx));
private:
ObPartRowManager manager_;
int64_t part_idx_; // used for parameter pass only.
common::ObNewRow part_row_;
private:
DISALLOW_COPY_AND_ASSIGN(ObPartIdRowMapManager);
};
class ObInsertStmt;
struct ObPartLocCalcNode {
enum NodeType {
INVALID = 0,
CALC_AND,
CALC_OR,
QUERY_RANGE,
FUNC_VALUE,
COLUMN_VALUE,
};
ObPartLocCalcNode() : node_type_(INVALID)
{}
virtual ~ObPartLocCalcNode()
{}
inline void set_node_type(NodeType node_type)
{
node_type_ = node_type;
}
inline NodeType get_node_type() const
{
return node_type_;
}
inline bool is_and_node() const
{
return CALC_AND == node_type_;
}
inline bool is_or_node() const
{
return CALC_OR == node_type_;
}
inline bool is_query_range_node() const
{
return QUERY_RANGE == node_type_;
}
inline bool is_func_value_node() const
{
return FUNC_VALUE == node_type_;
}
inline bool is_column_value_node() const
{
return COLUMN_VALUE == node_type_;
}
static ObPartLocCalcNode* create_part_calc_node(common::ObIAllocator& allocator,
common::ObIArray<ObPartLocCalcNode*>& calc_nodes, ObPartLocCalcNode::NodeType type);
virtual int deep_copy(common::ObIAllocator& allocator, common::ObIArray<ObPartLocCalcNode*>& calc_nodes,
ObPartLocCalcNode*& other) const = 0;
TO_STRING_KV(K_(node_type));
NodeType node_type_;
};
struct ObPLAndNode : public ObPartLocCalcNode {
ObPLAndNode() : left_node_(NULL), right_node_(NULL)
{
set_node_type(CALC_AND);
}
ObPLAndNode(common::ObIAllocator& allocator) : left_node_(NULL), right_node_(NULL)
{
UNUSED(allocator);
set_node_type(CALC_AND);
}
virtual ~ObPLAndNode()
{}
virtual int deep_copy(common::ObIAllocator& allocator, common::ObIArray<ObPartLocCalcNode*>& calc_nodes,
ObPartLocCalcNode*& other) const;
ObPartLocCalcNode* left_node_;
ObPartLocCalcNode* right_node_;
};
struct ObPLOrNode : public ObPartLocCalcNode {
ObPLOrNode() : left_node_(NULL), right_node_(NULL)
{
set_node_type(CALC_OR);
}
ObPLOrNode(common::ObIAllocator& allocator) : left_node_(NULL), right_node_(NULL)
{
UNUSED(allocator);
set_node_type(CALC_OR);
}
virtual ~ObPLOrNode()
{}
virtual int deep_copy(common::ObIAllocator& allocator, common::ObIArray<ObPartLocCalcNode*>& calc_nodes,
ObPartLocCalcNode*& other) const;
ObPartLocCalcNode* left_node_;
ObPartLocCalcNode* right_node_;
};
struct ObPLQueryRangeNode : public ObPartLocCalcNode {
ObPLQueryRangeNode() : pre_query_range_()
{
set_node_type(QUERY_RANGE);
}
ObPLQueryRangeNode(common::ObIAllocator& allocator) : pre_query_range_(allocator)
{
set_node_type(QUERY_RANGE);
}
virtual ~ObPLQueryRangeNode()
{
pre_query_range_.reset();
}
virtual int deep_copy(common::ObIAllocator& allocator, common::ObIArray<ObPartLocCalcNode*>& calc_nodes,
ObPartLocCalcNode*& other) const;
int get_range(common::ObIAllocator& allocator, const ParamStore& params, ObQueryRangeArray& query_ranges,
bool& is_all_single_value_ranges, bool& is_empty, const common::ObDataTypeCastParams& dtc_params) const;
ObQueryRange pre_query_range_;
};
struct ObPLFuncValueNode : public ObPartLocCalcNode {
struct ParamValuePair {
ParamValuePair(int64_t param_idx, const common::ObObj obj_val) : param_idx_(param_idx), obj_value_(obj_val)
{}
ParamValuePair() : param_idx_(-1), obj_value_()
{}
TO_STRING_KV(K_(param_idx), K_(obj_value));
int64_t param_idx_;
common::ObObj obj_value_;
};
ObPLFuncValueNode() : res_type_(), param_idx_(-1)
{
set_node_type(FUNC_VALUE);
}
ObPLFuncValueNode(common::ObIAllocator& allocator) : res_type_(), param_idx_(-1)
{
UNUSED(allocator);
set_node_type(FUNC_VALUE);
}
virtual ~ObPLFuncValueNode()
{}
virtual int deep_copy(common::ObIAllocator& allocator, common::ObIArray<ObPartLocCalcNode*>& calc_nodes,
ObPartLocCalcNode*& other) const;
ObExprResType res_type_;
int64_t param_idx_;
common::ObObj value_;
common::ObSEArray<ParamValuePair, 3, common::ModulePageAllocator, false> param_value_;
};
struct ObPLColumnValueNode : public ObPartLocCalcNode {
ObPLColumnValueNode() : param_idx_(-1)
{
set_node_type(COLUMN_VALUE);
}
ObPLColumnValueNode(common::ObIAllocator& allocator) : param_idx_(-1)
{
UNUSED(allocator);
set_node_type(COLUMN_VALUE);
}
virtual ~ObPLColumnValueNode()
{}
virtual int deep_copy(common::ObIAllocator& allocator, common::ObIArray<ObPartLocCalcNode*>& calc_nodes,
ObPartLocCalcNode*& other) const;
ObExprResType res_type_;
int64_t param_idx_;
common::ObObj value_;
};
struct ObListPartMapKey {
common::ObNewRow row_;
int64_t hash() const;
bool operator==(const ObListPartMapKey& other) const;
TO_STRING_KV(K_(row));
};
struct ObListPartMapValue {
ObListPartMapKey key_;
int64_t part_id_;
TO_STRING_KV(K_(key), K_(part_id));
};
template <class T, class V>
struct ObGetListPartMapKey {
void operator()(const T& t, const V& v) const
{
UNUSED(t);
UNUSED(v);
}
};
template <>
struct ObGetListPartMapKey<ObListPartMapKey, ObListPartMapValue*> {
ObListPartMapKey operator()(const ObListPartMapValue* value) const
{
return value->key_;
}
};
struct ObHashPartMapKey {
int64_t part_idx_;
int64_t hash() const;
bool operator==(const ObHashPartMapKey& other) const;
TO_STRING_KV(K_(part_idx));
};
struct ObHashPartMapValue {
ObHashPartMapKey key_;
int64_t part_id_;
TO_STRING_KV(K_(key), K_(part_id));
};
template <class T, class V>
struct ObGetHashPartMapKey {
void operator()(const T& t, const V& v) const
{
UNUSED(t);
UNUSED(v);
}
};
template <>
struct ObGetHashPartMapKey<ObHashPartMapKey, ObHashPartMapValue*> {
ObHashPartMapKey operator()(const ObHashPartMapValue* value) const
{
return value->key_;
}
};
struct TableLocationKey {
uint64_t table_id_;
uint64_t ref_table_id_;
bool operator==(const TableLocationKey& other) const;
bool operator!=(const TableLocationKey& other) const;
inline uint64_t hash() const
{
uint64_t hash_ret = 0;
hash_ret = common::murmurhash(&table_id_, sizeof(uint64_t), hash_ret);
hash_ret = common::murmurhash(&ref_table_id_, sizeof(uint64_t), hash_ret);
return hash_ret;
}
TO_STRING_KV(K_(table_id), K_(ref_table_id));
};
class ObOptimizerContext;
class ObTableLocation {
public:
enum TablePartType {
NONE,
HASH,
RANGE,
LIST,
};
enum FastCalcType { // Is it possible to quickly calculate the partition id
NONE_FAST_CALC, // Default initial value
UNKNOWN_FAST_CALC, // Insert statement: the value after the type matches; query statement: equivalent condition
// filtering;
CANNOT_FAST_CALC,
CAN_FAST_CALC,
};
typedef common::ObSEArray<ObSqlExpression*, 32, common::ModulePageAllocator, false> KeyExprs;
typedef common::ObIArray<ObSqlExpression*> IKeyExprs;
class PartProjector {
OB_UNIS_VERSION(1);
public:
PartProjector(common::ObIAllocator& allocator, ObSqlExpressionFactory& sql_expr_factory,
ObExprOperatorFactory& expr_op_factory)
: allocator_(allocator), sql_expr_factory_(sql_expr_factory), expr_op_factory_(expr_op_factory)
{
reset();
}
inline void reset()
{
column_cnt_ = 0;
part_projector_ = NULL;
part_projector_size_ = 0;
subpart_projector_ = NULL;
subpart_projector_size_ = 0;
virtual_column_exprs_.reset();
}
int deep_copy(const PartProjector& other);
int init_part_projector(const ObRawExpr* part_expr, share::schema::ObPartitionLevel part_level, RowDesc& row_desc);
int init_part_projector(const ObRawExpr* part_expr, RowDesc& row_desc);
int init_subpart_projector(const ObRawExpr* part_expr, RowDesc& row_desc);
inline void set_column_cnt(int64_t column_cnt)
{
column_cnt_ = column_cnt;
}
int calc_part_row(const stmt::StmtType& stmt_type, ObExecContext& ctx, const common::ObNewRow& input_row,
common::ObNewRow*& part_row) const;
void project_part_row(share::schema::ObPartitionLevel part_level, common::ObNewRow& part_row) const;
TO_STRING_KV(K_(virtual_column_exprs), K_(column_cnt), "part_projector",
common::ObArrayWrap<int32_t>(part_projector_, part_projector_size_), "subpart_projector",
common::ObArrayWrap<int32_t>(subpart_projector_, subpart_projector_size_));
private:
int init_part_projector(
const ObRawExpr* part_expr, RowDesc& row_desc, int32_t*& projector, int64_t& projector_size);
private:
common::ObDList<ObSqlExpression> virtual_column_exprs_;
int64_t column_cnt_;
int32_t* part_projector_;
int64_t part_projector_size_;
int32_t* subpart_projector_;
int64_t subpart_projector_size_;
common::ObIAllocator& allocator_;
ObSqlExpressionFactory& sql_expr_factory_;
ObExprOperatorFactory& expr_op_factory_;
};
static int get_location_type(const common::ObAddr& server,
const ObPhyPartitionLocationInfoIArray& phy_part_loc_info_list, ObTableLocationType& location_type);
// get virtual talbe partition ids or fake id. ref_table_id should be partitioned virtual table
//@param [in] ref_table_id partitioned virtual table
//@param [out] partition ids. all partition ids
//@param [out] fake id. Fake id, if has local partition return local part_id
static int get_vt_partition_id(
ObExecContext& exec_ctx, const uint64_t ref_table_id, common::ObIArray<int64_t>* partition_ids, int64_t* fake_id);
OB_UNIS_VERSION(1);
public:
// for array: new(&data_[count_]) T();
// for normal usage, like plan set
ObTableLocation()
: inited_(false),
table_id_(common::OB_INVALID_ID),
ref_table_id_(common::OB_INVALID_ID),
is_partitioned_(true),
part_level_(share::schema::PARTITION_LEVEL_ZERO),
part_type_(share::schema::PARTITION_FUNC_TYPE_MAX),
subpart_type_(share::schema::PARTITION_FUNC_TYPE_MAX),
part_get_all_(false),
subpart_get_all_(false),
is_col_part_expr_(false),
is_col_subpart_expr_(false),
is_oracle_temp_table_(false),
tablet_size_(common::OB_DEFAULT_TABLET_SIZE),
inner_allocator_(common::ObModIds::OB_SQL_TABLE_LOCATION),
allocator_(inner_allocator_),
calc_node_(NULL),
gen_col_node_(NULL),
subcalc_node_(NULL),
sub_gen_col_node_(NULL),
sql_expression_factory_(allocator_),
expr_op_factory_(allocator_),
part_expr_(NULL),
gen_col_expr_(NULL),
subpart_expr_(NULL),
sub_gen_col_expr_(NULL),
simple_insert_(false),
stmt_type_(stmt::T_NONE),
literal_stmt_type_(stmt::T_NONE),
hint_read_consistency_(common::INVALID_CONSISTENCY),
is_contain_inner_table_(false),
is_contain_select_for_update_(false),
is_contain_mv_(false),
key_exprs_(),
subkey_exprs_(),
num_values_(0),
key_conv_exprs_(),
subkey_conv_exprs_(),
part_hint_ids_(),
part_num_(1),
direction_(MAX_DIR),
index_table_id_(common::OB_INVALID_ID),
part_col_type_(ObNullType),
part_collation_type_(CS_TYPE_INVALID),
subpart_col_type_(ObNullType),
subpart_collation_type_(CS_TYPE_INVALID),
first_partition_id_(-1),
is_in_hit_(false),
part_projector_(allocator_, sql_expression_factory_, expr_op_factory_),
is_global_index_(false),
related_part_expr_idx_(OB_INVALID_INDEX),
related_subpart_expr_idx_(OB_INVALID_INDEX),
use_list_part_map_(false),
list_default_part_id_(OB_INVALID_ID),
use_hash_part_opt_(false),
use_range_part_opt_(false),
fast_calc_part_opt_(NONE_FAST_CALC),
duplicate_type_(ObDuplicateType::NOT_DUPLICATE),
first_part_num_(1),
partition_num_(1),
range_obj_arr_(NULL),
range_part_id_arr_(NULL),
use_calc_part_by_rowid_(false),
is_valid_range_columns_part_range_(false),
is_valid_range_columns_subpart_range_(false),
report_err_for_pruned_partition_not_exist_(false),
is_valid_temporal_part_range_(false),
is_valid_temporal_subpart_range_(false),
params_(NULL)
{}
// Used in situations where the optimizer does not adjust the destructor, to ensure that
// each member array is passed to the external allocator
ObTableLocation(common::ObIAllocator& allocator)
: inited_(false),
table_id_(common::OB_INVALID_ID),
ref_table_id_(common::OB_INVALID_ID),
is_partitioned_(true),
part_level_(share::schema::PARTITION_LEVEL_ZERO),
part_type_(share::schema::PARTITION_FUNC_TYPE_MAX),
subpart_type_(share::schema::PARTITION_FUNC_TYPE_MAX),
part_get_all_(false),
subpart_get_all_(false),
is_col_part_expr_(false),
is_col_subpart_expr_(false),
is_oracle_temp_table_(false),
tablet_size_(common::OB_DEFAULT_TABLET_SIZE),
allocator_(allocator),
calc_node_(NULL),
gen_col_node_(NULL),
subcalc_node_(NULL),
sub_gen_col_node_(NULL),
calc_nodes_(common::OB_MALLOC_NORMAL_BLOCK_SIZE, common::ModulePageAllocator(allocator_)),
sql_expression_factory_(allocator_),
expr_op_factory_(allocator_),
part_expr_(NULL),
gen_col_expr_(NULL),
subpart_expr_(NULL),
sub_gen_col_expr_(NULL),
simple_insert_(false),
stmt_type_(stmt::T_NONE),
literal_stmt_type_(stmt::T_NONE),
hint_read_consistency_(common::INVALID_CONSISTENCY),
is_contain_inner_table_(false),
is_contain_select_for_update_(false),
is_contain_mv_(false),
key_exprs_(common::OB_MALLOC_NORMAL_BLOCK_SIZE, common::ModulePageAllocator(allocator_)),
subkey_exprs_(common::OB_MALLOC_NORMAL_BLOCK_SIZE, common::ModulePageAllocator(allocator_)),
num_values_(0),
key_conv_exprs_(common::OB_MALLOC_NORMAL_BLOCK_SIZE, common::ModulePageAllocator(allocator_)),
subkey_conv_exprs_(common::OB_MALLOC_NORMAL_BLOCK_SIZE, common::ModulePageAllocator(allocator_)),
part_hint_ids_(common::OB_MALLOC_NORMAL_BLOCK_SIZE, common::ModulePageAllocator(allocator_)),
part_num_(1),
direction_(MAX_DIR),
index_table_id_(OB_INVALID_ID),
part_col_type_(ObNullType),
part_collation_type_(CS_TYPE_INVALID),
subpart_col_type_(ObNullType),
subpart_collation_type_(CS_TYPE_INVALID),
first_partition_id_(-1),
is_in_hit_(false),
part_projector_(allocator_, sql_expression_factory_, expr_op_factory_),
is_global_index_(false),
related_part_expr_idx_(OB_INVALID_INDEX),
related_subpart_expr_idx_(OB_INVALID_INDEX),
use_list_part_map_(false),
list_default_part_id_(OB_INVALID_ID),
use_hash_part_opt_(false),
use_range_part_opt_(false),
fast_calc_part_opt_(NONE_FAST_CALC),
duplicate_type_(ObDuplicateType::NOT_DUPLICATE),
first_part_num_(1),
partition_num_(1),
range_obj_arr_(NULL),
range_part_id_arr_(NULL),
list_part_map_(common::ModulePageAllocator(allocator_)),
list_part_array_(common::OB_MALLOC_NORMAL_BLOCK_SIZE, common::ModulePageAllocator(allocator_)),
hash_part_map_(common::ModulePageAllocator(allocator_)),
hash_part_array_(common::OB_MALLOC_NORMAL_BLOCK_SIZE, common::ModulePageAllocator(allocator_)),
use_calc_part_by_rowid_(false),
is_valid_range_columns_part_range_(false),
is_valid_range_columns_subpart_range_(false),
report_err_for_pruned_partition_not_exist_(false),
is_valid_temporal_part_range_(false),
is_valid_temporal_subpart_range_(false),
params_(NULL)
{}
virtual ~ObTableLocation()
{
reset();
}
ObTableLocation(const ObTableLocation& other);
ObTableLocation& operator=(const ObTableLocation& other);
void reset();
inline bool is_inited() const
{
return inited_;
}
// get preliminary_query_range and partition_expr. For sql with filters.
template <typename SchemaGuardType>
int init(SchemaGuardType& schema_guard, ObDMLStmt& stmt, ObSQLSessionInfo* session_info,
const common::ObIArray<ObRawExpr*>& filter_exprs, const uint64_t table_id, const uint64_t ref_table_id,
const ObPartHint* part_hint, const common::ObDataTypeCastParams& dtc_params, const bool is_dml_table,
common::ObIArray<ObRawExpr*>* sort_exprs = NULL, const ParamStore *params = NULL)
{
int ret = common::OB_SUCCESS;
const share::schema::ObTableSchema* table_schema = NULL;
if (OB_FAIL(schema_guard.get_table_schema(ref_table_id, table_schema))) {
SQL_OPT_LOG(WARN, "failed to get table schema", K(ret), K(ref_table_id));
} else if (OB_FAIL(init(table_schema,
stmt,
session_info,
filter_exprs,
table_id,
ref_table_id,
part_hint,
dtc_params,
is_dml_table,
sort_exprs,
params))) {
SQL_OPT_LOG(WARN, "failed to init", K(ret), K(ref_table_id));
}
return ret;
}
int init(const share::schema::ObTableSchema* table_schema, ObDMLStmt& stmt, ObSQLSessionInfo* session_info,
const common::ObIArray<ObRawExpr*>& filter_exprs, const uint64_t table_id, const uint64_t ref_table_id,
const ObPartHint* part_hint, const common::ObDataTypeCastParams& dtc_params, const bool is_dml_table,
common::ObIArray<ObRawExpr*>* sort_exprs = NULL, const ParamStore *params = NULL);
int get_is_weak_read(ObExecContext& exec_ctx, bool& is_weak_read) const;
/**
* Calculate the table's partition location list from the input parameters.
*
* This function is used after ObTableLocation inited.
* The function assumes that:
* ObTableLocation has been inited.
*
* @param params[in] SQL parameters
* @param location_cache[in] location cache
* @param partition_location_list[out] the table's partition_location_list
*/
int calculate_partition_location_infos(ObExecContext& exec_ctx, common::ObPartMgr* part_mgr, const ParamStore& params,
share::ObIPartitionLocationCache& location_cache, ObPhyPartitionLocationInfoIArray& phy_part_loc_info_list,
const common::ObDataTypeCastParams& dtc_params, bool nonblock = false) const;
bool calculate_partition_ids_fast(
ObExecContext& exec_ctx, const ParamStore& params, ObIArray<int64_t>& partition_ids) const;
int calculate_partition_ids_fast_for_insert(ObExecContext& exec_ctx, const ParamStore& param_store,
common::ObObj& result, // Partition column value
bool& has_optted) const; // With or without optimization
int calculate_partition_ids_fast_for_non_insert(ObExecContext& exec_ctx, const ParamStore& param_store,
common::ObObj& result, // Partition column value
bool& has_optted) const; // With or without optimization
/**
* Calculate table partition location ids from input parameters.
*
* This function is used by calculate, has the same assumes with calculate.
*
* @param params[in] SQL parameters
* @param part_ids[out] partition location ids,
* if part_ids' count == 0. it means error.
* @return int, if success, return OB_SUCCESS, else return other error code
*/
int calculate_partition_ids(ObExecContext& exec_ctx, common::ObPartMgr* part_mgr, const ParamStore& params,
common::ObIArray<int64_t>& part_ids, const common::ObDataTypeCastParams& dtc_params) const;
// Pass in rowkey and table_id, and then calculate the part_ids
// corresponding to rowkey and the rowkey_list corresponding to
// part_id. The corresponding rules are: part_ids and rowkey_lists
// are both arrays, and their subscripts correspond one-to-one.
int calculate_partition_ids_by_rowkey(ObSQLSessionInfo& session_info,
share::schema::ObSchemaGetterGuard& schema_guard, uint64_t table_id, const common::ObIArray<ObRowkey>& rowkeys,
common::ObIArray<int64_t>& part_ids, common::ObIArray<RowkeyArray>& rowkey_lists);
int init_table_location(ObSqlSchemaGuard& schema_guard, uint64_t table_id, uint64_t ref_table_id, ObDMLStmt& stmt,
const RowDesc& row_desc, const bool is_dml_table, const ObOrderDirection& direction = default_asc_direction());
int init_table_location_with_rowkey(ObSqlSchemaGuard& schema_guard, uint64_t table_id, ObSQLSessionInfo& session_info,
const bool is_dml_table = true);
int calculate_partition_ids_by_row(ObExecContext& exec_ctx, ObPartMgr* part_mgr, const common::ObNewRow& row,
ObIArray<int64_t>& part_ids, int64_t& part_idx) const;
int calculate_partition_id_by_row(
ObExecContext& exec_ctx, ObPartMgr* part_mgr, const common::ObNewRow& row, int64_t& part_id) const;
/// Get the partition locations of partition ids
static int set_partition_locations(ObExecContext& exec_ctx, share::ObIPartitionLocationCache& location_cache,
const uint64_t ref_table_id, const common::ObIArray<int64_t>& partition_ids,
ObPhyPartitionLocationInfoIArray& phy_part_loc_info_list, bool nonblock = false);
inline void set_table_id(uint64_t table_id)
{
table_id_ = table_id;
}
inline uint64_t get_table_id() const
{
return table_id_;
}
inline uint64_t get_ref_table_id() const
{
return ref_table_id_;
}
inline uint64_t get_is_global_index() const
{
return is_global_index_;
}
void set_related_part_expr_idx(int32_t related_part_expr_idx)
{
related_part_expr_idx_ = related_part_expr_idx;
}
void set_related_subpart_expr_idx(int32_t related_subpart_expr_idx)
{
related_subpart_expr_idx_ = related_subpart_expr_idx;
}
int64_t get_partition_num() const
{
return part_num_;
}
bool is_partitioned() const
{
return is_partitioned_;
}
share::schema::ObPartitionLevel get_part_level() const
{
return part_level_;
}
bool can_fast_calc_part_ids() const
{
return CAN_FAST_CALC == fast_calc_part_opt_;
}
int64_t get_tablet_size()
{
return tablet_size_;
}
int get_ranges(ObIAllocator& allocator, const ParamStore& params, ObSQLSessionInfo* session_info,
const ObPhyTableLocationInfo& phy_table_location, ObIArray<ObNewRange>& ranges) const;
// ObQueryRange& get_preliminary_query_range() { return preliminary_query_range_; }
const stmt::StmtType& get_stmt_type() const
{
return stmt_type_;
}
const ObConsistencyLevel& get_hint_read_consistency() const
{
return hint_read_consistency_;
}
bool is_contain_inner_table() const
{
return is_contain_inner_table_;
}
int set_log_op_infos(const uint64_t index_table_id, const ObOrderDirection& direction);
// int set_pre_query_range(const ObQueryRange *pre_query_range);
// int set_direction(const ObOrderDirection &direction);
const ObOrderDirection& get_direction() const
{
return direction_;
}
int get_not_insert_dml_part_sort_expr(ObDMLStmt& stmt, common::ObIArray<ObRawExpr*>* sort_exprs) const;
bool is_contain_mv() const
{
return is_contain_mv_;
}
bool has_generated_column() const
{
return NULL != gen_col_expr_ || NULL != sub_gen_col_expr_ || NULL != gen_col_node_ || NULL != sub_gen_col_node_;
}
inline ObSqlExpression* get_part_expr()
{
return part_expr_;
}
const inline ObSqlExpression* get_part_expr() const
{
return part_expr_;
}
inline ObSqlExpression* get_subpart_expr()
{
return subpart_expr_;
}
const inline ObSqlExpression* get_subpart_expr() const
{
return subpart_expr_;
}
static int get_phy_table_location_info(ObExecContext& ctx, uint64_t table_location_key, uint64_t ref_table_id,
bool is_weak, const common::ObIArray<int64_t>& part_ids, share::ObIPartitionLocationCache& location_cache,
ObPhyTableLocationInfo& phy_loc_info);
static int append_phy_table_location(ObExecContext& ctx, uint64_t table_location_key, uint64_t ref_table_id,
bool is_weak, const common::ObIArray<int64_t>& part_ids, const ObOrderDirection& order_direction);
int add_part_idx_to_part_id_map(const share::schema::ObTableSchema& table_schema);
int get_part_id_from_part_idx(int64_t part_idx, int64_t& part_id) const;
int add_list_part_schema(const share::schema::ObTableSchema& table_schema, const common::ObTimeZoneInfo* tz_info);
int add_hash_part_schema(const share::schema::ObTableSchema& table_schema);
int add_range_part_schema(const share::schema::ObTableSchema& table_schema);
int get_list_part(const common::ObNewRow& row, bool insert_or_replace, common::ObIArray<int64_t>& partition_ids,
int64_t* part_idx) const;
int get_list_part(
common::ObObj& obj, bool insert_or_replace, common::ObIArray<int64_t>& partition_ids, int64_t* part_idx) const;
int get_range_part(const common::ObNewRow& obj, bool insert_or_replace, common::ObIArray<int64_t>& partition_ids,
int64_t* part_idx) const;
int get_range_part(
const common::ObNewRange* range, bool insert_or_replace, common::ObIArray<int64_t>& partition_ids) const;
int get_hash_part(const common::ObObj& obj, bool insert_or_replace, common::ObIArray<int64_t>& partition_ids,
int64_t* part_idx) const;
bool is_duplicate_table() const
{
return ObDuplicateType::NOT_DUPLICATE != duplicate_type_;
}
bool is_duplicate_table_not_in_dml() const
{
return ObDuplicateType::DUPLICATE == duplicate_type_;
}
void set_duplicate_type(ObDuplicateType v)
{
duplicate_type_ = v;
}
ObDuplicateType get_duplicate_type() const
{
return duplicate_type_;
}
const common::ObIArray<int64_t>& get_part_expr_param_idxs() const
{
return part_expr_param_idxs_;
}
int deal_dml_partition_selection(int64_t part_id) const;
int add_part_hint_ids(const ObIArray<int64_t>& part_ids)
{
return append_array_no_dup(part_hint_ids_, part_ids);
}
int get_partition_ids_by_range(ObExecContext& exec_ctx, ObPartMgr* part_mgr, const ObNewRange* part_range,
const ObNewRange* gen_range, ObIArray<int64_t>& partition_ids, const ObIArray<int64_t>* level_one_part_ids) const;
int calc_partition_ids_by_range(ObExecContext& exec_ctx, common::ObPartMgr* part_mgr, const ObNewRange* range,
ObIArray<int64_t>& partition_ids, bool& all_part, const ObIArray<int64_t>* part_ids,
const ObSqlExpression* gen_col_expr = NULL) const;
int init_table_location_with_row_desc(
ObSqlSchemaGuard& schema_guard, uint64_t table_id, RowDesc& input_row_desc, ObSQLSessionInfo& session_info,
const bool is_dml_table);
int generate_row_desc_from_row_desc(ObDMLStmt& stmt, const uint64_t data_table_id, ObRawExprFactory& expr_factory,
const RowDesc& input_row_desc, RowDesc& row_desc);
int init_table_location_with_rowid(ObSqlSchemaGuard& schema_guard, const common::ObIArray<int64_t>& param_idxs,
const uint64_t table_id, const uint64_t ref_table_id, ObSQLSessionInfo& session_info, const bool is_dml_table);
int calc_partition_location_infos_with_rowid(ObExecContext& exec_ctx,
share::schema::ObSchemaGetterGuard& schema_guard, const ParamStore& params,
share::ObIPartitionLocationCache& location_cache, ObPhyPartitionLocationInfoIArray& phy_part_loc_info_list,
bool nonblock = false) const;
bool use_calc_part_by_rowid() const
{
return use_calc_part_by_rowid_;
}
int calculate_partition_ids_with_rowid(ObExecContext& exec_ctx, share::schema::ObSchemaGetterGuard& schema_guard,
const ParamStore& params, common::ObIArray<int64_t>& part_ids) const;
inline bool is_all_partition() const
{
return (part_level_ == share::schema::PARTITION_LEVEL_ZERO) ||
(part_get_all_ && (part_level_ == share::schema::PARTITION_LEVEL_ONE)) ||
(part_get_all_ && subpart_get_all_ && (part_level_ == share::schema::PARTITION_LEVEL_TWO));
}
TO_STRING_KV(K_(table_id), K_(ref_table_id), K_(part_num), K_(is_global_index), K_(duplicate_type),
K_(part_expr_param_idxs), K_(part_projector), K_(part_expr), K_(gen_col_expr));
private:
// get partition columns and generate partition_expr_
// partition_columns:partition columns
// gen_cols: columns dependented by generated partition column
// partition_raw_expr: raw expr of partition
// gen_col_expression: dependented expression of generated partition column
int get_partition_column_info(ObDMLStmt& stmt, const share::schema::ObPartitionLevel part_level,
common::ObIArray<ColumnItem>& partition_columns, common::ObIArray<ColumnItem>& gen_cols,
const ObRawExpr*& partition_raw_expr, ObSqlExpression*& partition_expression,
ObSqlExpression*& gen_col_expression, bool& is_col_part_expr);
int generate_rowkey_desc(ObDMLStmt& stmt, const common::ObRowkeyInfo& rowkey_info, uint64_t data_table_id,
ObRawExprFactory& expr_factory, RowDesc& row_desc);
// Take the intersection of partition ids and to_inter_ids, and store the result in partition_ids
int intersect_partition_ids(
const common::ObIArray<int64_t>& to_inter_ids, common::ObIArray<int64_t>& partition_ids) const;
// add partition columns.
// For insert stmt with generated column,
// the dependent columns would added to partitoin columns.
// gen_cols: For non-insert statements, the partition expr of
// a single column stores the dependent column
// of the generated_column
// gen_col_expr: generated_column's dependent expr
int add_partition_columns(ObDMLStmt& stmt, const ObRawExpr* part_expr,
common::ObIArray<ColumnItem>& partition_columns, common::ObIArray<ColumnItem>& gen_cols, ObRawExpr*& gen_col_expr,
RowDesc& row_desc, RowDesc& gen_row_desc, const bool only_gen_cols = false);
// add partition column
// add column to row desc and partiton columns
int add_partition_column(ObDMLStmt& stmt, const uint64_t table_id, const uint64_t column_id,
common::ObIArray<ColumnItem>& partition_columns, RowDesc& row_desc);
int record_not_insert_dml_partition_info(ObDMLStmt& stmt, const share::schema::ObTableSchema* table_schema,
const common::ObIArray<ObRawExpr*>& filter_exprs, const common::ObDataTypeCastParams& dtc_params);
int record_insert_partition_info(
ObDMLStmt& stmt, const share::schema::ObTableSchema* table_schema, ObSQLSessionInfo* session_info);
// record part info for insert stmt
//@param[in]: partition_columns, the partition columns of some part level
//@param[out]: key_exprs, record insert values for calc column conv expr of partition column
//@params[out]: key_conv_exprs, record column conv expr of partition column
//@params[out]: insert_up_exprs, record expr of partition column in insert_up assignments
//@params[out]: update_set_key_num, record partition column num in insert_up assignment
int record_insert_part_info(ObInsertStmt& insert_stmt, ObSQLSessionInfo* session_info,
const common::ObIArray<ColumnItem>& partition_columns, IKeyExprs& key_exprs, IKeyExprs& key_conv_exprs);
int gen_insert_part_row_projector(const ObRawExpr* partition_raw_expr,
const common::ObIArray<ColumnItem>& partition_columns, share::schema::ObPartitionLevel part_level,
share::schema::ObPartitionFuncType part_type);
//
int add_key_conv_expr(ObInsertStmt& insert_stmt, ObSQLSessionInfo* session_info, RowDesc& value_row_desc,
RowDesc& extra_row_desc, const uint64_t column_id, IKeyExprs& key_conv_exprs);
int add_key_col_idx_in_val_desc(const common::ObIArray<ObColumnRefRawExpr*>& value_desc, const uint64_t column_id,
common::ObIArray<int64_t>& value_need_idx);
/// Add partition key expr.
int add_key_expr(IKeyExprs& key_exprs, ObRawExpr* expr);
int add_key_expr_with_row_desc(IKeyExprs& key_exprs, RowDesc& row_desc, ObRawExpr* expr);
//////end of
int clear_columnlized_in_row_desc(RowDesc& row_desc);
int calc_partition_ids_by_stored_expr(ObExecContext& exec_ctx, common::ObPartMgr* part_mgr,
common::ObIArray<int64_t>& partition_ids, const common::ObDataTypeCastParams& dtc_params) const;
// int calc_partition_ids_by_sub_select(ObExecContext &exec_ctx,
// common::ObPartMgr *part_mgr,
// common::ObIArray<int64_t> &partition_ids) const;
int calc_partition_ids_by_ranges(ObExecContext& exec_ctx, const common::ObIArray<common::ObNewRange*>& ranges,
common::ObIArray<int64_t>& partition_ids) const;
// gen_col_node: partition information of dependented column of generated partition column
// gen_col_expr: expression of dependented column of generated partition column
int calc_partition_ids_by_calc_node(ObExecContext& exec_ctx, common::ObPartMgr* part_mgr, const ParamStore& params,
const ObPartLocCalcNode* calc_node, const ObPartLocCalcNode* gen_col_node, const ObSqlExpression* gen_col_expr,
const bool part_col_get_all, common::ObIArray<int64_t>& partition_ids,
const common::ObDataTypeCastParams& dtc_params, const common::ObIArray<int64_t>* part_ids = NULL) const;
int calc_partition_ids_by_calc_node(ObExecContext& exec_ctx, common::ObPartMgr* part_mgr, const ParamStore& params,
const ObPartLocCalcNode* calc_node, common::ObIArray<int64_t>& partition_ids, bool& all_part,
const common::ObDataTypeCastParams& dtc_params, const common::ObIArray<int64_t>* part_ids = NULL) const;
int calc_and_partition_ids(ObExecContext& exec_ctx, common::ObPartMgr* part_mgr, const ParamStore& params,
const ObPLAndNode* calc_node, common::ObIArray<int64_t>& partition_ids, bool& all_part,
const common::ObDataTypeCastParams& dtc_params, const common::ObIArray<int64_t>* part_ids = NULL) const;
int calc_or_partition_ids(ObExecContext& exec_ctx, common::ObPartMgr* part_mgr, const ParamStore& params,
const ObPLOrNode* calc_node, common::ObIArray<int64_t>& partition_ids, bool& all_part,
const common::ObDataTypeCastParams& dtc_params, const common::ObIArray<int64_t>* part_ids = NULL) const;
int calc_query_range_partition_ids(ObExecContext& exec_ctx, common::ObPartMgr* part_mgr, const ParamStore& params,
const ObPLQueryRangeNode* calc_node, common::ObIArray<int64_t>& partition_ids, bool& all_part,
const common::ObDataTypeCastParams& dtc_params, const common::ObIArray<int64_t>* part_ids,
const ObSqlExpression* part_expr = NULL) const;
int calc_func_value_partition_ids(ObExecContext& exec_ctx, common::ObPartMgr* part_mgr, const ParamStore& params,
const ObPLFuncValueNode* calc_node, common::ObIArray<int64_t>& partition_ids, bool& all_part,
const common::ObDataTypeCastParams& dtc_params, const common::ObIArray<int64_t>* part_ids = NULL) const;
int calc_column_value_partition_ids(ObExecContext& exec_ctx, common::ObPartMgr* part_mgr, const ParamStore& params,
const ObPLColumnValueNode* calc_node, common::ObIArray<int64_t>& partition_ids, bool& all_part,
const common::ObDataTypeCastParams& dtc_params, const common::ObIArray<int64_t>* part_ids = NULL) const;
int calc_partition_id_by_func_value(common::ObExprCtx& expr_ctx, common::ObPartMgr* part_mgr,
const common::ObObj& func_value, const bool calc_oracle_hash, common::ObIArray<int64_t>& partition_ids,
const common::ObIArray<int64_t>* part_ids = NULL, int64_t* part_idx = NULL) const;
/// Get all partition ids with virtual table considered.
int get_all_part_ids(ObExecContext& exec_ctx, common::ObPartMgr* part_mgr, common::ObIArray<int64_t>& partition_ids,
const common::ObIArray<int64_t>* part_ids = NULL) const;
/// Get partition key row desc for generating partition expr
int deal_partition_selection(common::ObIArray<int64_t>& partition_ids) const;
int get_location_calc_node(common::ObIArray<ColumnItem>& partition_columns, const ObRawExpr* partition_expr,
const common::ObIArray<ObRawExpr*>& filter_exprs, ObPartLocCalcNode*& res_node, bool& get_all,
const common::ObDataTypeCastParams& dtc_params);
int analyze_filter(const common::ObIArray<ColumnItem>& partition_columns, const ObRawExpr* partition_expr,
uint64_t column_id, const ObRawExpr* filter, bool& always_true, ObPartLocCalcNode*& calc_node,
bool& cnt_func_expr, const common::ObDataTypeCastParams& dtc_params);
int get_query_range_node(const ColumnIArray& range_columns, const common::ObIArray<ObRawExpr*>& filter_exprs,
bool& always_true, ObPartLocCalcNode*& calc_node, const common::ObDataTypeCastParams& dtc_params);
int extract_eq_op(const ObRawExpr* l_expr, const ObRawExpr* r_expr, const ObRawExpr* partition_expr,
const uint64_t column_id, const ObExprResType& res_type, bool& cnt_func_expr, bool& always_true,
ObPartLocCalcNode*& calc_node);
int check_expr_equal(
const ObRawExpr* partition_expr, const ObRawExpr* check_expr, bool& equal, ObExprEqualCheckContext& equal_ctx);
int add_and_node(ObPartLocCalcNode* l_node, ObPartLocCalcNode*& r_node);
int add_or_node(ObPartLocCalcNode* l_node, ObPartLocCalcNode*& r_node);
int calc_partition_ids_by_ranges(ObExecContext& exec_ctx, common::ObPartMgr* part_mgr,
const common::ObIArray<common::ObNewRange*>& ranges, const bool is_all_single_value_ranges,