forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_trigger_handler.cpp
More file actions
920 lines (882 loc) · 37.3 KB
/
Copy pathob_trigger_handler.cpp
File metadata and controls
920 lines (882 loc) · 37.3 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
/*
* 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_ENG
#include "sql/engine/dml/ob_trigger_handler.h"
#include "sql/engine/expr/ob_expr_lob_utils.h"
#include "sql/engine/dml/ob_table_update_op.h"
#include "sql/ob_spi.h"
#include "pl/ob_pl_user_type.h"
#include "pl/ob_pl_stmt.h"
namespace oceanbase
{
using namespace common;
using namespace common::sqlclient;
using namespace storage;
using namespace share;
using namespace share::schema;
using namespace observer;
namespace sql
{
int TriggerHandle::init_trigger_row(
ObIAllocator &alloc, int64_t rowtype_col_count, pl::ObPLRecord *&record)
{
int ret = OB_SUCCESS;
int64_t init_size = pl::ObRecordType::get_init_size(rowtype_col_count);
init_size += sizeof(ObObj) * rowtype_col_count; //append hidden columns
if (OB_ISNULL(record = reinterpret_cast<pl::ObPLRecord*>(alloc.alloc(init_size)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to allocate memory", K(ret), K(init_size));
} else {
new (record)pl::ObPLRecord(OB_INVALID_ID, rowtype_col_count);
OZ (record->init_data(alloc, true));
if (OB_SUCC(ret)) {
ObObj *cells = record->get_element();
for (int64_t i = 0; OB_SUCC(ret) && i < rowtype_col_count; i++) {
new (&cells[i]) ObObj();
cells[i].set_null();
}
}
}
return ret;
}
/*
drop table t3;
create table t3 (
pk int primary key,
aa_int int,
bb_int int
);
insert into t3 values (400, 500, 500);
commit;
CREATE OR REPLACE TRIGGER seq_trigger_1
BEFORE UPDATE ON t3
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('Updating T3, original aa_int is ' || :NEW.aa_int);
:NEW.aa_int := :NEW.aa_int * 10;
DBMS_OUTPUT.PUT_LINE('Updating T3, modify aa_int to ' || :NEW.aa_int);
END;
/
CREATE OR REPLACE TRIGGER seq_trigger_2
BEFORE UPDATE ON t3
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('Updating T3, original aa_int is ' || :NEW.aa_int);
:NEW.aa_int := :NEW.aa_int * 10;
DBMS_OUTPUT.PUT_LINE('Updating T3, modify aa_int to ' || :NEW.aa_int);
END;
/
SQL> update t3 set aa_int = 700 where pk = 400;
Updating T3, original aa_int is 700
Updating T3, modify aa_int to 7000
Updating T3, original aa_int is 7000
Updating T3, modify aa_int to 70000
1 row updated.
* the following trigger which fired after previous trigger can see the modification
* happened in previous trigger, so we use ObArrayHelper here which can help all triggers
* to share the same old row and new row.
*/
int TriggerHandle::init_trigger_params(
ObDMLRtCtx &das_ctx,
uint64_t trigger_event,
const ObTrigDMLCtDef &trig_ctdef,
ObTrigDMLRtDef &trig_rtdef)
{
UNUSED(trigger_event);
int ret = OB_SUCCESS;
void *when_point_params_buf = NULL;
void *row_point_params_buf = NULL;
int64_t param_store_size = sizeof(ParamStore);
// TODO: This interface can be further refined, for example, when there is no when condition, the related logic of tg_when_point_params_ does not need to be executed,
// or when performing insert/delete operations, tg_init_point_params_ also does not need to be executed.
if (OB_ISNULL(when_point_params_buf = das_ctx.get_exec_ctx().get_allocator().alloc(param_store_size)) ||
OB_ISNULL(row_point_params_buf = das_ctx.get_exec_ctx().get_allocator().alloc(param_store_size))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to allocate memory", K(ret));
} else {
ObIAllocator &allocator = das_ctx.get_exec_ctx().get_allocator();
trig_rtdef.tg_when_point_params_ = new(when_point_params_buf)ParamStore(ObWrapperAllocator(allocator));
trig_rtdef.tg_row_point_params_ = new(row_point_params_buf)ParamStore(ObWrapperAllocator(allocator));
OZ (trig_rtdef.tg_when_point_params_->prepare_allocate(WHEN_POINT_PARAM_COUNT));
OZ (trig_rtdef.tg_row_point_params_->prepare_allocate(ROW_POINT_PARAM_COUNT));
pl::ObPLRecord *old_record = NULL;
pl::ObPLRecord *new_record = NULL;
// FIXME: for delete it has no new_rowid_expr, trig_col_info hasn't add rowid
int64_t rowtype_col_count = trig_ctdef.trig_col_info_.get_rowtype_count();
int64_t init_size = pl::ObRecordType::get_init_size(rowtype_col_count);
if (trig_ctdef.all_tm_points_.has_when_condition() || trig_ctdef.all_tm_points_.has_row_point()) {
OZ (init_trigger_row(das_ctx.get_exec_ctx().get_allocator(), rowtype_col_count, old_record));
OZ (init_trigger_row(das_ctx.get_exec_ctx().get_allocator(), rowtype_col_count, new_record));
}
LOG_DEBUG("trigger init", K(rowtype_col_count), K(ret));
if (OB_SUCC(ret) && trig_ctdef.all_tm_points_.has_when_condition()) {
trig_rtdef.tg_when_point_params_->at(0).set_extend(reinterpret_cast<int64_t>(old_record),
pl::PL_RECORD_TYPE, init_size);
trig_rtdef.tg_when_point_params_->at(0).set_param_meta();
trig_rtdef.tg_when_point_params_->at(1).set_extend(reinterpret_cast<int64_t>(new_record),
pl::PL_RECORD_TYPE, init_size);
trig_rtdef.tg_when_point_params_->at(1).set_param_meta();
}
if (OB_SUCC(ret) && trig_ctdef.all_tm_points_.has_row_point()) {
trig_rtdef.tg_row_point_params_->at(0).set_extend(reinterpret_cast<int64_t>(old_record),
pl::PL_RECORD_TYPE, init_size);
trig_rtdef.tg_row_point_params_->at(0).set_param_meta();
trig_rtdef.tg_row_point_params_->at(1).set_extend(reinterpret_cast<int64_t>(new_record),
pl::PL_RECORD_TYPE, init_size);
trig_rtdef.tg_row_point_params_->at(1).set_param_meta();
}
trig_rtdef.old_record_ = old_record;
trig_rtdef.new_record_ = new_record;
}
return ret;
}
int TriggerHandle::free_trigger_param_memory(ObTrigDMLRtDef &trig_rtdef, bool keep_composite_attr)
{
int ret = OB_SUCCESS;
pl::ObPLRecord *old_record = trig_rtdef.old_record_;
pl::ObPLRecord *new_record = trig_rtdef.new_record_;
ObObj tmp;
if (OB_NOT_NULL(old_record)) {
tmp.set_extend(reinterpret_cast<int64_t>(old_record), old_record->get_type(), old_record->get_init_size());
pl::ObUserDefinedType::destruct_obj(tmp, nullptr, keep_composite_attr);
}
if (OB_NOT_NULL(new_record)) {
tmp.set_extend(reinterpret_cast<int64_t>(new_record), new_record->get_type(), new_record->get_init_size());
pl::ObUserDefinedType::destruct_obj(tmp, nullptr, keep_composite_attr);
}
return ret;
}
int TriggerHandle::init_param_rows(
ObEvalCtx &eval_ctx,
const ObTrigDMLCtDef &trig_ctdef,
ObTrigDMLRtDef &trig_rtdef)
{
int ret = OB_SUCCESS;
OZ (init_param_old_row(eval_ctx, trig_ctdef, trig_rtdef));
OZ (init_param_new_row(eval_ctx, trig_ctdef, trig_rtdef));
return ret;
}
int TriggerHandle::init_param_old_row(
ObEvalCtx &eval_ctx,
const ObTrigDMLCtDef &trig_ctdef,
ObTrigDMLRtDef &trig_rtdef)
{
int ret = OB_SUCCESS;
if (trig_ctdef.all_tm_points_.has_when_condition() ||
trig_ctdef.all_tm_points_.has_row_point()) {
ObObj *cells = nullptr;
if (OB_ISNULL(trig_rtdef.old_record_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected status: old record is null", K(ret));
} else if (OB_ISNULL(cells = trig_rtdef.old_record_->get_element())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("cells is NULL", K(ret));
} else {
ObObj tmp;
tmp.set_extend(reinterpret_cast<int64_t>(trig_rtdef.old_record_), trig_rtdef.old_record_->get_type(), trig_rtdef.old_record_->get_init_size());
pl::ObUserDefinedType::destruct_obj(tmp, nullptr, true);
}
for (int64_t i = 0; i < trig_ctdef.old_row_exprs_.count() && OB_SUCC(ret); ++i) {
ObDatum *datum;
ObObj result;
ObObj dst;
bool is_udt = false;
if (OB_FAIL(trig_ctdef.old_row_exprs_.at(i)->eval(eval_ctx, datum))) {
LOG_WARN("failed to eval rowid expr", K(ret));
} else if (OB_ISNULL(datum)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("datum is NULL", K(ret));
} else if (OB_FAIL(datum->to_obj(result,
trig_ctdef.old_row_exprs_.at(i)->obj_meta_))) {
LOG_WARN("failed to datum to obj", K(ret));
} else if ((is_udt = ob_is_geometry(trig_ctdef.old_row_exprs_.at(i)->obj_meta_.get_type()))) {
if (OB_FAIL(OB_ISNULL(eval_ctx.exec_ctx_.get_sql_ctx()))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("sql ctx is null", K(ret));
} else if (OB_FAIL(convert_sql_type_to_pl_type(eval_ctx.exec_ctx_.get_my_session(),
eval_ctx.exec_ctx_,
eval_ctx.exec_ctx_.get_sql_ctx()->schema_guard_,
eval_ctx.exec_ctx_.get_allocator(),
result,
dst,
trig_ctdef.old_row_exprs_.at(i)->obj_meta_.get_type()))) {
LOG_WARN("failed to convert sql type to pl type", K(ret));
}
}
if (OB_FAIL(ret)) {
} else if (is_udt) {
if (OB_FAIL(pl::ObUserDefinedType::deep_copy_obj(*trig_rtdef.old_record_->get_allocator(), dst, cells[i]))) {
LOG_WARN("fail to deep copy obj", K(ret));
}
int tmp_ret = pl::ObUserDefinedType::destruct_obj(dst);
if (tmp_ret != OB_SUCCESS) {
LOG_WARN("destruct obj failed", K(ret));
}
} else {
if (OB_FAIL(deep_copy_obj(*trig_rtdef.old_record_->get_allocator(), result, cells[i]))) {
LOG_WARN("fail to deep copy obj", K(ret));
}
}
LOG_DEBUG("debug init param old expr", K(ret), K(i),
K(ObToStringExpr(eval_ctx, *trig_ctdef.old_row_exprs_.at(i))));
}
if (OB_FAIL(ret)) {
} else if (OB_NOT_NULL(trig_ctdef.rowid_old_expr_)) {
ObDatum *datum;
if (OB_FAIL(trig_ctdef.rowid_old_expr_->eval(eval_ctx, datum))) {
LOG_WARN("failed to eval rowid expr", K(ret));
}
LOG_DEBUG("debug init param rowid old expr", K(ret),
K(ObToStringExpr(eval_ctx, *trig_ctdef.rowid_old_expr_)));
}
}
return ret;
}
int TriggerHandle::init_param_new_row(
ObEvalCtx &eval_ctx,
const ObTrigDMLCtDef &trig_ctdef,
ObTrigDMLRtDef &trig_rtdef)
{
int ret = OB_SUCCESS;
LOG_DEBUG("debug init param new row", K(ret));
if (trig_ctdef.all_tm_points_.has_when_condition() ||
trig_ctdef.all_tm_points_.has_row_point()) {
ObObj *cells = nullptr;
if (OB_ISNULL(trig_rtdef.new_record_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected status: new record is null", K(ret));
} else if (OB_ISNULL(cells = trig_rtdef.new_record_->get_element())){
ret = OB_ERR_UNEXPECTED;
LOG_WARN("cells is NULL", K(ret));
} else {
ObObj tmp;
tmp.set_extend(reinterpret_cast<int64_t>(trig_rtdef.new_record_), trig_rtdef.new_record_->get_type(), trig_rtdef.new_record_->get_init_size());
pl::ObUserDefinedType::destruct_obj(tmp, nullptr, true);
}
for (int64_t i = 0; i < trig_ctdef.new_row_exprs_.count() && OB_SUCC(ret); ++i) {
ObDatum *datum;
ObObj result;
ObObj dst;
bool is_udt =false;
if (OB_FAIL(trig_ctdef.new_row_exprs_.at(i)->eval(eval_ctx, datum))) {
LOG_WARN("failed to eval rowid expr", K(ret));
} else if (OB_ISNULL(datum)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("datum is NULL", K(ret));
} else if (OB_FAIL(datum->to_obj(result,
trig_ctdef.new_row_exprs_.at(i)->obj_meta_))) {
LOG_WARN("failed to datum to obj", K(ret));
} else if ((is_udt = ob_is_geometry(trig_ctdef.new_row_exprs_.at(i)->obj_meta_.get_type()))) {
if (OB_FAIL(OB_ISNULL(eval_ctx.exec_ctx_.get_sql_ctx()))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("sql ctx is null", K(ret));
} else if (OB_FAIL(convert_sql_type_to_pl_type(eval_ctx.exec_ctx_.get_my_session(),
eval_ctx.exec_ctx_,
eval_ctx.exec_ctx_.get_sql_ctx()->schema_guard_,
eval_ctx.exec_ctx_.get_allocator(),
result,
dst,
trig_ctdef.new_row_exprs_.at(i)->obj_meta_.get_type()))) {
LOG_WARN("failed to convert sql type to pl type", K(ret));
}
}
if (OB_SUCC(ret) && !trig_ctdef.trig_col_info_.get_flags()[i].is_rowid_
&& ObCharType == result.get_type()) {
// pad space for char type
const common::ObObjType &col_type = trig_ctdef.new_row_exprs_.at(i)->obj_meta_.get_type();
common::ObAccuracy accuracy;
accuracy.length_ = trig_ctdef.new_row_exprs_.at(i)->max_length_;
accuracy.length_semantics_ = trig_ctdef.new_row_exprs_.at(i)->datum_meta_.length_semantics_;
if (OB_FAIL(ObSPIService::spi_pad_char_or_varchar(eval_ctx.exec_ctx_.get_my_session(),
col_type,
accuracy,
&eval_ctx.exec_ctx_.get_allocator(),
&result))) {
LOG_WARN("failed to pad space", K(col_type), K(accuracy), K(ret));
}
}
if (is_udt) {
OZ (pl::ObUserDefinedType::deep_copy_obj(*trig_rtdef.new_record_->get_allocator(), dst, cells[i]));
int tmp_ret = pl::ObUserDefinedType::destruct_obj(dst);
if (tmp_ret != OB_SUCCESS) {
LOG_WARN("destruct obj failed", K(ret));
}
} else {
OZ (deep_copy_obj(*trig_rtdef.new_record_->get_allocator(), result, cells[i]));
}
LOG_DEBUG("debug init param new expr", K(ret), K(i),
K(ObToStringExpr(eval_ctx, *trig_ctdef.new_row_exprs_.at(i))));
}
if (OB_FAIL(ret)) {
} else if (OB_NOT_NULL(trig_ctdef.rowid_new_expr_)) {
ObDatum *datum;
if (OB_FAIL(trig_ctdef.rowid_new_expr_->eval(eval_ctx, datum))) {
LOG_WARN("failed to eval rowid expr", K(ret));
}
LOG_DEBUG("debug init param rowid new expr", K(ret),
K(ObToStringExpr(eval_ctx, *trig_ctdef.rowid_new_expr_)));
}
}
return ret;
}
int TriggerHandle::set_rowid_into_row(
const ObTriggerColumnsInfo &cols,
ObEvalCtx &eval_ctx,
ObExpr *src_expr,
pl::ObPLRecord *record)
{
int ret = OB_SUCCESS;
ObObj* cells = nullptr;
CK (OB_NOT_NULL(cols.get_flags()));
CK (OB_NOT_NULL(record));
CK (OB_NOT_NULL(record->get_element()));
CK (OB_NOT_NULL(record->get_allocator()));
OX (cells = record->get_element());
// We can't distinguish urowid column and trigger rowid column in ObTriggerColumns,
// but we can ensure that the trigger rowid column must behind the urowid column if exist,
// because the trigger rowid column mock in optimizer
for (int64_t i = cols.get_count() - 1; OB_SUCC(ret) && i >= 0; ++i) {
if (cols.get_flags()[i].is_rowid_) {
ObDatum *datum;
ObObj result;
void *ptr = cells[i].get_deep_copy_obj_ptr();
if (nullptr != ptr) {
record->get_allocator()->free(ptr);
}
cells[i].set_null();
if (OB_FAIL(src_expr->eval(eval_ctx, datum))) {
LOG_WARN("failed to eval expr", K(ret));
} else if (OB_ISNULL(datum)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("datum is NULL", K(ret));
} else if (OB_FAIL(datum->to_obj(result, src_expr->obj_meta_))) {
LOG_WARN("failed to datum to obj", K(ret));
} else if (OB_FAIL(deep_copy_obj(*record->get_allocator(), result, cells[i]))) {
LOG_WARN("fail to deep copy obj", K(ret));
} else {
LOG_DEBUG("set_rowid_into_row done", K(ret), K(ObToStringExpr(eval_ctx, *src_expr)));
}
break;
}
}
LOG_DEBUG("set_rowid_into_row done", K(ret));
return ret;
}
// calculate rowid and set into new_record that is for pl
int TriggerHandle::do_handle_rowid_before_row(
ObTableModifyOp &dml_op,
const ObTrigDMLCtDef &trig_ctdef,
ObTrigDMLRtDef &trig_rtdef,
uint64_t tg_event)
{
// ObURowIDType
int ret = OB_SUCCESS;
if (NULL != trig_ctdef.rowid_new_expr_ && ObTriggerEvents::is_insert_event(tg_event)) {
// we set a invalid rowid obj into old_row/new_row
ret = OB_NOT_IMPLEMENT;
} else if (NULL != trig_ctdef.rowid_old_expr_ && ObTriggerEvents::is_delete_event(tg_event)) {
// new.rowid should be same with old.rowid
OZ (TriggerHandle::set_rowid_into_row(trig_ctdef.trig_col_info_,
dml_op.get_eval_ctx(),
trig_ctdef.rowid_old_expr_,
trig_rtdef.new_record_));
LOG_DEBUG("handle rowid before delete success", K(tg_event));
}
return ret;
}
int TriggerHandle::calc_when_condition(
ObTableModifyOp &dml_op,
ObTrigDMLRtDef &trig_rtdef,
uint64_t trigger_id,
bool &need_fire)
{
int ret = OB_SUCCESS;
ObObj result;
if (OB_ISNULL(trig_rtdef.tg_when_point_params_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret), K(trig_rtdef.tg_when_point_params_));
} else if (OB_FAIL(calc_trigger_routine(dml_op.get_exec_ctx(),
trigger_id, ROUTINE_IDX_CALC_WHEN,
*trig_rtdef.tg_when_point_params_, result))) {
LOG_WARN("failed to cacl trigger routine", K(ret));
} else {
need_fire = result.is_true();
LOG_DEBUG("TRIGGER", K(result), K(need_fire));
}
return ret;
}
int TriggerHandle::calc_trigger_routine(
ObExecContext &exec_ctx,
uint64_t trigger_id,
uint64_t routine_id,
ParamStore ¶ms)
{
int ret = OB_SUCCESS;
ObObj result;
OZ (calc_trigger_routine(exec_ctx, trigger_id, routine_id, params, result));
return ret;
}
int TriggerHandle::calc_trigger_routine(
ObExecContext &exec_ctx,
uint64_t trigger_id,
uint64_t routine_id,
ParamStore ¶ms,
ObObj &result)
{
int ret = OB_SUCCESS;
ObArray<int64_t> path;
ObArray<int64_t> nocopy_params;
trigger_id = ObTriggerInfo::get_trigger_spec_package_id(trigger_id);
bool old_flag = false;
common::ObArenaAllocator tmp_allocator(common::ObMemAttr(MTL_ID(), "TriggerExec"));
CK (OB_NOT_NULL(exec_ctx.get_my_session()));
OX (old_flag = exec_ctx.get_my_session()->is_for_trigger_package());
OX (exec_ctx.get_my_session()->set_for_trigger_package(true));
OV (OB_NOT_NULL(exec_ctx.get_pl_engine()));
OZ (exec_ctx.get_pl_engine()->execute(
exec_ctx, tmp_allocator, trigger_id, routine_id, path, params, nocopy_params, result),
trigger_id, routine_id, params);
CK (OB_NOT_NULL(exec_ctx.get_my_session()));
OZ (exec_ctx.get_my_session()->reset_all_package_state_by_dbms_session(true));
if (exec_ctx.get_my_session()->is_for_trigger_package()) {
// whether `ret == OB_SUCCESS`, need to restore flag
exec_ctx.get_my_session()->set_for_trigger_package(old_flag);
}
return ret;
}
int TriggerHandle::check_and_update_new_row(
ObTableModifyOp *self_op,
const ObTriggerColumnsInfo &columns,
ObEvalCtx &eval_ctx,
const ObIArray<ObExpr *> &new_row_exprs,
pl::ObPLRecord *new_record,
bool check)
{
int ret = OB_SUCCESS;
// plus 1 is for rowid
if (OB_ISNULL(new_record)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected status: old or new record is null", K(ret), K(new_record));
} else {
bool updated = false;
int64_t op_row_idx = 0;
ObObj *new_cells = new_record->get_element();
for (int64_t i = 0; OB_SUCC(ret) && i < columns.get_count(); i++) {
if (columns.get_flags()[i].is_rowid_) {
} else if (check) {
ObDatum *datum;
ObObj new_obj;
if (OB_FAIL(new_row_exprs.at(i)->eval(eval_ctx, datum))) {
LOG_WARN("failed to eval expr", K(ret));
} else if (OB_ISNULL(datum)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("datum is NULL", K(ret));
} else if (OB_FAIL(datum->to_obj(new_obj, new_row_exprs.at(i)->obj_meta_))) {
LOG_WARN("failed to to obj", K(ret));
} else {
bool is_strict_equal = false;
if (new_obj.is_lob_storage()) {
common::ObArenaAllocator lob_allocator(ObModIds::OB_LOB_READER, OB_MALLOC_NORMAL_BLOCK_SIZE, MTL_ID());
ObObj cmp_obj;
ObObj other_obj;
if (new_obj.is_delta_tmp_lob() || new_cells[i].is_delta_tmp_lob()) {
is_strict_equal = false;
} else if (OB_FAIL(ObTextStringIter::convert_outrow_lob_to_inrow_templob(new_obj,
cmp_obj,
NULL,
&lob_allocator))) {
LOG_WARN("failed to convert lob", K(ret), K(new_obj));
} else if (OB_FAIL(ObTextStringIter::convert_outrow_lob_to_inrow_templob(new_cells[i],
other_obj,
NULL,
&lob_allocator))) {
LOG_WARN("failed to convert lob", K(ret), K(i), K(new_cells[i]));
} else {
is_strict_equal = cmp_obj.strict_equal(other_obj);
}
} else {
is_strict_equal = new_obj.strict_equal(new_cells[i]);
}
if (OB_FAIL(ret)) {
} else if(!is_strict_equal) {
updated = true;
}
}
}
}
LOG_DEBUG("debug update row", K(updated), K(lbt()));
// updated
if (OB_SUCC(ret) && (updated || !check)) {
self_op->clear_dml_evaluated_flag();
LOG_DEBUG("debug update row", K(updated), K(check));
}
// case: create table t11( c2 generated always as (c1 + 1), c1 int);
// The generated column c2 is before normal column c1
// so we need calculate normal column value firstly, then generate column value
// Firstly calculate the basic columns
for (int64_t i = 0; OB_SUCC(ret) && i < columns.get_count(); i++) {
if (!columns.get_flags()[i].is_gen_col_) {
ObExpr *expr = new_row_exprs.at(i);
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expr is NULL", K(ret));
} else {
ObObj tmp_obj = new_cells[i];
ObDatum &write_datum = expr->locate_datum_for_write(eval_ctx);
if (ob_is_geometry(expr->obj_meta_.get_type())) {
if (OB_FAIL(convert_pl_type_to_sql_type(eval_ctx.exec_ctx_.get_my_session(),
eval_ctx.exec_ctx_,
eval_ctx.exec_ctx_.get_allocator(),
new_cells[i],
tmp_obj,
expr->obj_meta_.get_type()))) {
LOG_WARN("failed to convert pl type to sql type", K(ret));
}
} else if (OB_FAIL(deep_copy_obj(eval_ctx.exec_ctx_.get_allocator(), new_cells[i], tmp_obj))) {
LOG_WARN("failed to deep copy obj", K(ret));
}
if (OB_FAIL(ret)) {
} else if (OB_FAIL(write_datum.from_obj(tmp_obj))) {
LOG_WARN("failed to from obj", K(ret));
} else if (is_lob_storage(tmp_obj.get_type()) &&
OB_FAIL(ob_adjust_lob_datum(tmp_obj, expr->obj_meta_,
eval_ctx.exec_ctx_.get_allocator(), write_datum))) {
LOG_WARN("adjust lob datum failed", K(ret), K(tmp_obj.get_meta()), K(expr->obj_meta_));
} else {
expr->set_evaluated_flag(eval_ctx);
LOG_DEBUG("trigger write new datum", K(tmp_obj), K(i),
K(ObToStringExpr(eval_ctx, *new_row_exprs.at(i))));
}
}
}
}
// Secondly calculate the generated column
for (int64_t i = 0; OB_SUCC(ret) && i < columns.get_count(); i++) {
if (columns.get_flags()[i].is_gen_col_) {
// for generated column, the dependent column is already evaluated
// so the generated column can evalate by eval_function
ObDatum *datum;
if (OB_FAIL(new_row_exprs.at(i)->eval(eval_ctx, datum))) {
LOG_WARN("failed to eval expr", K(ret));
} else {
LOG_DEBUG("trigger write new datum", K(new_cells[i]), K(i),
K(ObToStringExpr(eval_ctx, *new_row_exprs.at(i))));
}
}
}
}
return ret;
}
int TriggerHandle::do_handle_before_row(
ObTableModifyOp &dml_op,
ObDASDMLBaseCtDef &das_base_ctdef,
const ObTrigDMLCtDef &trig_ctdef,
ObTrigDMLRtDef &trig_rtdef)
{
UNUSED(das_base_ctdef);
int ret = OB_SUCCESS;
if (trig_ctdef.all_tm_points_.has_before_row()) {
LOG_DEBUG("debug handle before row");
uint64_t tg_event = trig_ctdef.tg_event_;
if (OB_FAIL(do_handle_rowid_before_row(dml_op, trig_ctdef, trig_rtdef, tg_event))) {
LOG_WARN("do handle rowid before row failed", K(ret), K(tg_event));
} else {
ObSQLSessionInfo *my_session = dml_op.get_exec_ctx().get_my_session();
stmt::StmtType saved_stmt_type = stmt::T_NONE;
bool need_fire = true;
LOG_DEBUG("TRIGGER", K(trig_ctdef.tg_args_),
K(trig_ctdef.all_tm_points_.has_before_row()),
K(trig_ctdef.all_tm_points_.has_after_row()),
K(trig_ctdef.all_tm_points_.has_before_point()),
K(trig_ctdef.all_tm_points_.has_after_point()),
K(trig_ctdef.all_tm_points_.has_instead_row()));
OV (OB_NOT_NULL(my_session));
for (int64_t i = 0; OB_SUCC(ret) && i < trig_ctdef.tg_args_.count(); i++) {
const ObTriggerArg &tg_arg = trig_ctdef.tg_args_.at(i);
if (!tg_arg.has_before_row_point() || !tg_arg.has_trigger_events(tg_event)) {
need_fire = false;
} else if (tg_arg.has_when_condition()) {
OZ (calc_when_condition(dml_op, trig_rtdef, tg_arg.get_trigger_id(), need_fire));
} else {
need_fire = true;
}
LOG_DEBUG("TRIGGER handle before row", K(need_fire), K(i), K(lbt()));
if (OB_SUCC(ret) && need_fire) {
if (OB_ISNULL(trig_rtdef.tg_row_point_params_)) {
ret = OB_NOT_INIT;
LOG_WARN("trigger row point params is not init", K(ret));
} else {
const ObTableModifySpec &modify_spec = static_cast<const ObTableModifySpec&>(dml_op.get_spec());
if (OB_FAIL(calc_before_row(dml_op, trig_rtdef, tg_arg.get_trigger_id()))) {
LOG_WARN("failed to calc before row", K(ret));
} else if ((ObTriggerEvents::is_update_event(tg_event) ||
ObTriggerEvents::is_insert_event(tg_event))) {
if (!trig_ctdef.all_tm_points_.has_instead_row() &&
OB_FAIL(check_and_update_new_row(&dml_op,
trig_ctdef.trig_col_info_,
dml_op.get_eval_ctx(),
trig_ctdef.new_row_exprs_,
trig_rtdef.new_record_,
ObTriggerEvents::is_update_event(tg_event)))) {
LOG_WARN("failed to check updated new row", K(ret));
}
}
if (OB_SUCC(ret) && trig_ctdef.all_tm_points_.has_instead_row()) {
GET_PHY_PLAN_CTX(dml_op.get_exec_ctx())->add_affected_rows(1);
if (ObTriggerEvents::is_update_event(tg_event)) {
GET_PHY_PLAN_CTX(dml_op.get_exec_ctx())->add_row_matched_count(1);
GET_PHY_PLAN_CTX(dml_op.get_exec_ctx())->add_row_duplicated_count(1);
}
}
LOG_DEBUG("TRIGGER calc before row", K(need_fire), K(i));
}
}
}
}
}
return ret;
}
int TriggerHandle::calc_after_row(
ObTableModifyOp &dml_op, ObTrigDMLRtDef &trig_rtdef, uint64_t trigger_id)
{
int ret = OB_SUCCESS;
uint64_t idx = ROUTINE_IDX_AFTER_ROW;
if (OB_ISNULL(trig_rtdef.tg_row_point_params_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid arguement", K(ret));
} else if (OB_FAIL(calc_trigger_routine(dml_op.get_exec_ctx(),
trigger_id, idx,
*trig_rtdef.tg_row_point_params_))) {
LOG_WARN("failed to calc trigger routine", K(ret));
}
return ret;
}
int TriggerHandle::calc_before_row(
ObTableModifyOp &dml_op, ObTrigDMLRtDef &trig_rtdef, uint64_t trigger_id)
{
int ret = OB_SUCCESS;
uint64_t idx = ROUTINE_IDX_BEFORE_ROW;
if (OB_ISNULL(trig_rtdef.tg_row_point_params_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret), K(trig_rtdef.tg_row_point_params_));
} else if (OB_FAIL(calc_trigger_routine(dml_op.get_exec_ctx(),
trigger_id, idx,
*trig_rtdef.tg_row_point_params_))) {
LOG_WARN("failed to calc trigger routine", K(ret));
}
return ret;
}
int TriggerHandle::calc_before_stmt(
ObTableModifyOp &dml_op,
ObTrigDMLRtDef &trig_rtdef,
uint64_t trigger_id)
{
UNUSED(trig_rtdef);
int ret = OB_SUCCESS;
ParamStore params;
if (OB_FAIL(calc_trigger_routine(dml_op.get_exec_ctx(),
trigger_id, ROUTINE_IDX_BEFORE_STMT, params))) {
LOG_WARN("failed to calc trigger routine", K(ret));
}
return ret;
}
int TriggerHandle::do_handle_after_stmt(
ObTableModifyOp &dml_op,
const ObTrigDMLCtDef &trig_ctdef,
ObTrigDMLRtDef &trig_rtdef,
uint64_t tg_event)
{
int ret = OB_SUCCESS;
if (trig_ctdef.all_tm_points_.has_after_stmt()) {
LOG_DEBUG("TRIGGER", K(trig_ctdef.tg_args_));
for (int64_t i = 0; OB_SUCC(ret) && i < trig_ctdef.tg_args_.count(); i++) {
const ObTriggerArg &tg_arg = trig_ctdef.tg_args_.at(i);
if (tg_arg.has_after_stmt_point() && tg_arg.has_trigger_events(tg_event)) {
OZ (calc_after_stmt(dml_op, trig_rtdef, tg_arg.get_trigger_id()), ret);
}
}
}
int tmp_ret = OB_SUCCESS;
if (OB_SUCCESS != (tmp_ret = destroy_compound_trigger_state(dml_op.get_exec_ctx(), trig_ctdef))) {
LOG_WARN("destroy compound trigger state failed", K(tmp_ret), K(ret));
}
return ret;
}
int TriggerHandle::calc_after_stmt(
ObTableModifyOp &dml_op, ObTrigDMLRtDef &trig_rtdef, uint64_t trigger_id)
{
UNUSED(trig_rtdef);
int ret = OB_SUCCESS;
ParamStore params;
OZ (calc_trigger_routine(dml_op.get_exec_ctx(),
trigger_id, ROUTINE_IDX_AFTER_STMT, params));
return ret;
}
// only the first trigger need run statement trigger
int TriggerHandle::do_handle_before_stmt(
ObTableModifyOp &dml_op,
const ObTrigDMLCtDef &trig_ctdef,
ObTrigDMLRtDef &trig_rtdef,
uint64_t tg_event)
{
int ret = OB_SUCCESS;
if (trig_ctdef.all_tm_points_.has_before_stmt()) {
LOG_DEBUG("TRIGGER", K(trig_ctdef.tg_args_));
for (int64_t i = 0; OB_SUCC(ret) && i < trig_ctdef.tg_args_.count(); i++) {
const ObTriggerArg &tg_arg = trig_ctdef.tg_args_.at(i);
if (tg_arg.has_before_stmt_point() && tg_arg.has_trigger_events(tg_event)) {
if (OB_FAIL(calc_before_stmt(dml_op, trig_rtdef, tg_arg.get_trigger_id()))) {
LOG_WARN("failed to calc befeore stmt", K(ret));
}
}
}
}
return ret;
}
// to compatible with oralce
int TriggerHandle::do_handle_rowid_after_row(
ObTableModifyOp &dml_op,
const ObTrigDMLCtDef &trig_ctdef,
ObTrigDMLRtDef &trig_rtdef,
uint64_t tg_event)
{
int ret = OB_SUCCESS;
if (NULL != trig_ctdef.rowid_new_expr_ && ObTriggerEvents::is_insert_event(tg_event)) {
// old.rowid should be same with new.rowid for insert
trig_ctdef.rowid_new_expr_->get_eval_info(dml_op.get_eval_ctx()).clear_evaluated_flag();
OZ (TriggerHandle::set_rowid_into_row(trig_ctdef.trig_col_info_,
dml_op.get_eval_ctx(),
trig_ctdef.rowid_new_expr_,
trig_rtdef.old_record_));
OZ (TriggerHandle::set_rowid_into_row(trig_ctdef.trig_col_info_,
dml_op.get_eval_ctx(),
trig_ctdef.rowid_new_expr_,
trig_rtdef.new_record_));
LOG_DEBUG("handle rowid after insert success", K(tg_event));
} else if (NULL != trig_ctdef.rowid_old_expr_ && ObTriggerEvents::is_delete_event(tg_event)) {
// new.rowid should be same with old.rowid
OZ (TriggerHandle::set_rowid_into_row(trig_ctdef.trig_col_info_,
dml_op.get_eval_ctx(),
trig_ctdef.rowid_old_expr_,
trig_rtdef.new_record_));
LOG_DEBUG("handle rowid after delete success", K(tg_event));
}
return ret;
}
int TriggerHandle::do_handle_after_row(
ObTableModifyOp &dml_op,
const ObTrigDMLCtDef &trig_ctdef,
ObTrigDMLRtDef &trig_rtdef,
uint64_t tg_event)
{
int ret = OB_SUCCESS;
if (trig_ctdef.all_tm_points_.has_after_row()) {
if (OB_FAIL(do_handle_rowid_after_row(dml_op, trig_ctdef, trig_rtdef, tg_event))) {
LOG_WARN("do handle rowid after row failed", K(ret), K(tg_event));
} else {
bool need_fire = false;
LOG_DEBUG("TRIGGER", K(trig_ctdef.tg_args_));
for (int64_t i = 0; OB_SUCC(ret) && i < trig_ctdef.tg_args_.count(); i++) {
const ObTriggerArg &tg_arg = trig_ctdef.tg_args_.at(i);
if (!tg_arg.has_after_row_point() || !tg_arg.has_trigger_events(tg_event)) {
need_fire = false;
} else if (tg_arg.has_when_condition()) {
OZ (calc_when_condition(dml_op, trig_rtdef, tg_arg.get_trigger_id(), need_fire));
} else {
need_fire = true;
}
LOG_DEBUG("TRIGGER", K(need_fire));
if (need_fire) {
OZ (calc_after_row(dml_op, trig_rtdef, tg_arg.get_trigger_id()));
}
}
}
}
return ret;
}
int TriggerHandle::destroy_compound_trigger_state(ObExecContext &exec_ctx, const ObTrigDMLCtDef &trig_ctdef)
{
int ret = OB_SUCCESS;
ObSQLSessionInfo *session_info = exec_ctx.get_my_session();
ObSchemaGetterGuard *schema_guard = exec_ctx.get_sql_ctx()->schema_guard_;
OV (OB_NOT_NULL(session_info) && OB_NOT_NULL(schema_guard));
for (int64_t i = 0; OB_SUCC(ret) && i < trig_ctdef.tg_args_.count(); i++) {
uint64_t trg_id = trig_ctdef.tg_args_.at(i).get_trigger_id();
const ObTriggerInfo *trg_info = NULL;
OZ (schema_guard->get_trigger_info(session_info->get_effective_tenant_id(), trg_id, trg_info), trg_id);
OV (OB_NOT_NULL(trg_info));
if (OB_SUCC(ret) && trg_info->is_compound_dml_type()) {
OZ (pl::ObPLPackageManager::destory_package_state(*session_info,
ObTriggerInfo::get_trigger_body_package_id(trg_id)));
LOG_DEBUG("destroy trigger state", K(trg_id), K(ret));
}
}
return ret;
}
int64_t TriggerHandle::get_routine_param_count(const uint64_t routine_id)
{
int64_t count = OB_INVALID_COUNT;
count = ROW_POINT_PARAM_COUNT;
return count;
}
int TriggerHandle::convert_sql_type_to_pl_type(ObSQLSessionInfo *session,
ObExecContext &exec_ctx,
ObSchemaGetterGuard *schema_guard,
ObIAllocator &alloc,
ObObj &src,
ObObj &dst,
ObObjType obj_type)
{
int ret = OB_SUCCESS;
if (src.is_null()) {
if (OB_ISNULL(schema_guard)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("schema guard is null", K(ret));
} else {
//do nothing
}
} else if (OB_ISNULL(session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is null", K(ret));
} else {
const ObDataTypeCastParams dtc_params = ObBasicSessionInfo::create_dtc_params(session);
ObCastCtx cast_ctx(&alloc, &dtc_params, CM_NONE, src.get_collation_type());
cast_ctx.exec_ctx_ = &exec_ctx;
if (OB_FAIL(ObObjCaster::to_type(ObExtendType, cast_ctx, src, dst))) {
LOG_WARN("failed to cast to extend type", K(ret), K(src), K(dst), K(obj_type));
}
}
return ret;
}
int TriggerHandle::convert_pl_type_to_sql_type(ObSQLSessionInfo *session,
ObExecContext &exec_ctx,
ObIAllocator &alloc,
ObObj &src,
ObObj &dst,
ObObjType obj_type)
{
int ret = OB_SUCCESS;
if (ob_is_geometry(obj_type)) {
const ObDataTypeCastParams dtc_params = ObBasicSessionInfo::create_dtc_params(session);
ObCastCtx cast_ctx(&alloc, &dtc_params, CM_NONE, src.get_collation_type());
if (OB_FAIL(ObObjCaster::to_type(obj_type, cast_ctx, src, dst))) {
LOG_WARN("failed to cast to extend type", K(ret), K(src), K(dst), K(obj_type));
}
}
return ret;
}
} // namespace sql
} // namespace oceanbase