/
evl.cpp
5307 lines (4487 loc) · 149 KB
/
evl.cpp
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
/*
* PROGRAM: JRD Access Method
* MODULE: evl.cpp
* DESCRIPTION: Expression evaluation
*
* The contents of this file are subject to the Interbase Public
* License Version 1.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.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
/*
* Modified by: Patrick J. P. Griffin
* Date: 11/24/2000
* Problem: select count(0)+1 from rdb$relations where 0=1; returns 0
* In the EVL_group processing, the internal assigment for
* the literal in the computation is being done on every
* statement fetch, so if there are no statements fetched
* then the internal field never gets set.
* Change: Added an assignment process for the literal
* before the first fetch.
*
* Modified by: Neil McCalden
* Date: 05 Jan 2001
* Problem: Firebird bug: 127375
* Group by on a calculated expression would cause segv
* when it encountered a NULL value as the calculation
* was trying reference a null pointer.
* Change: Test the null flag before trying to expand the value.
*
* 2001.6.17 Claudio Valderrama: Fix the annoying behavior that causes silent
* overflow in dialect 1. If you define the macro FIREBIRD_AVOID_DIALECT1_OVERFLOW
* it will work with double should an overflow happen. Otherwise, an error will be
* issued to the user if the overflow happens. The multiplication is done using
* SINT64 quantities. I had the impression that casting this SINT64 result to double
* when we detect overflow was faster than achieving the failed full multiplication
* with double operands again. Usage will tell the truth.
* For now, the aforementioned macro is enabled.
* 2001.6.18 Claudio Valderrama: substring() is working with international charsets,
* thanks to Dave Schnepper's directions.
* 2002.2.15 Claudio Valderrama: divide2() should not mangle negative values.
* 2002.04.16 Paul Beach HP10 Port - (UCHAR*) desc.dsc_address = p; modified for HP
* Compiler
* 2002.09.28 Dmitry Yemanov: Reworked internal_info stuff, enhanced
* exception handling in SPs/triggers,
* implemented ROWS_AFFECTED system variable
* 2003.08.10 Claudio Valderrama: Fix SF bug# 784121.
* Adriano dos Santos Fernandes
*/
#include "firebird.h"
#include <string.h>
#include <math.h>
#include "../jrd/common.h"
#include "../jrd/ibase.h"
#include "../jrd/jrd.h"
#include "../jrd/val.h"
#include "../jrd/req.h"
#include "../jrd/exe.h"
#include "../jrd/sbm.h"
#include "../jrd/blb.h"
#include "gen/iberror.h"
#include "../jrd/scl.h"
#include "../jrd/lck.h"
#include "../jrd/lls.h"
#include "../jrd/intl.h"
#include "../jrd/intl_classes.h"
#include "../jrd/rse.h"
#include "../jrd/quad.h"
#include "../jrd/sort.h"
#include "../jrd/blr.h"
#include "../jrd/tra.h"
#include "../jrd/gdsassert.h"
#include "../common/classes/timestamp.h"
#include "../jrd/blb_proto.h"
#include "../jrd/btr_proto.h"
#include "../jrd/cvt_proto.h"
#include "../jrd/DataTypeUtil.h"
#include "../jrd/dpm_proto.h"
#include "../jrd/dsc_proto.h"
#include "../jrd/err_proto.h"
#include "../jrd/evl_proto.h"
#include "../jrd/exe_proto.h"
#include "../jrd/fun_proto.h"
#include "../jrd/intl_proto.h"
#include "../jrd/lck_proto.h"
#include "../jrd/met_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/pag_proto.h"
#include "../jrd/rlck_proto.h"
#include "../jrd/rse_proto.h"
#include "../jrd/scl_proto.h"
#include "../jrd/thd.h"
#include "../jrd/sort_proto.h"
#include "../jrd/gds_proto.h"
#include "../jrd/align.h"
#include "../jrd/met_proto.h"
#include "../jrd/cvt_proto.h"
#include "../jrd/misc_func_ids.h"
#include "../common/config/config.h"
#include "../jrd/evl_string.h"
#include "../jrd/SysFunction.h"
const int TEMP_LENGTH = 128;
const SINT64 MAX_INT64_LIMIT = MAX_SINT64 / 10;
const SINT64 MIN_INT64_LIMIT = MIN_SINT64 / 10;
// arbitrary size static part for optimization
typedef Firebird::HalfStaticArray<USHORT, 100> Ucs2Buffer;
#ifdef VMS
double MTH$CVT_D_G(), MTH$CVT_G_D();
#endif
/* *** DANGER DANGER WILL ROBINSON ***
* add(), multiply(), and divide() all take the same three arguments, but
* they don't all take them in the same order. Be careful out there.
* The order should be made to agree as part of the next code cleanup.
*/
using namespace Jrd;
static dsc* add(const dsc*, const jrd_nod*, impure_value*);
static dsc* add2(const dsc*, const jrd_nod*, impure_value*);
static dsc* add_datetime(const dsc*, const jrd_nod*, impure_value*);
static dsc* add_sql_date(const dsc*, const jrd_nod*, impure_value*);
static dsc* add_sql_time(const dsc*, const jrd_nod*, impure_value*);
static dsc* add_timestamp(const dsc*, const jrd_nod*, impure_value*);
static void adjust_text_descriptor(thread_db*, dsc*);
static dsc* binary_value(thread_db*, const jrd_nod*, impure_value*);
static dsc* cast(thread_db*, dsc*, const jrd_nod*, impure_value*);
static void compute_agg_distinct(thread_db*, jrd_nod*);
static dsc* concatenate(thread_db*, const dsc*, const dsc*, impure_value*);
static dsc* dbkey(thread_db*, const jrd_nod*, impure_value*);
static dsc* eval_statistical(thread_db*, jrd_nod*, impure_value*);
static dsc* extract(thread_db*, jrd_nod*, impure_value*);
static void fini_agg_distinct(thread_db* tdbb, const jrd_nod *const);
static SINT64 get_day_fraction(const dsc* d);
static dsc* get_mask(thread_db*, jrd_nod*, impure_value*);
static SINT64 get_timestamp_to_isc_ticks(const dsc* d);
static void init_agg_distinct(thread_db*, const jrd_nod*);
static dsc* lock_state(thread_db*, jrd_nod*, impure_value*);
static dsc* low_up_case(thread_db*, const dsc*, impure_value*,
int (*intl_str_to_case)(thread_db*, DSC*),
ULONG (TextType::*tt_str_to_case)(ULONG, const UCHAR*, ULONG, UCHAR*));
static dsc* multiply(const dsc*, impure_value*, const jrd_nod*);
static dsc* multiply2(const dsc*, impure_value*, const jrd_nod*);
static dsc* divide2(const dsc*, impure_value*, const jrd_nod*);
static dsc* negate_dsc(thread_db*, const dsc*, impure_value*);
static dsc* record_version(thread_db*, const jrd_nod*, impure_value*);
static bool reject_duplicate(const UCHAR*, const UCHAR*, void*);
static dsc* scalar(thread_db*, jrd_nod*, impure_value*);
static bool sleuth(thread_db*, jrd_nod*, const dsc*, const dsc*);
static bool string_boolean(thread_db*, jrd_nod*, dsc*, dsc*, bool);
static bool string_function(thread_db*, jrd_nod*, SLONG, const UCHAR*, SLONG, const UCHAR*, USHORT, bool);
static dsc* string_length(thread_db*, jrd_nod*, impure_value*);
static dsc* substring(thread_db*, impure_value*, dsc*, const dsc*, const dsc*);
static dsc* trim(thread_db*, jrd_nod*, impure_value*);
static dsc* internal_info(thread_db*, const dsc*, impure_value*);
const SINT64 SECONDS_PER_DAY = 24 * 60 * 60;
const SINT64 ISC_TICKS_PER_DAY = SECONDS_PER_DAY * ISC_TIME_SECONDS_PRECISION;
const SCHAR DIALECT_3_TIMESTAMP_SCALE = -9;
const SCHAR DIALECT_1_TIMESTAMP_SCALE = 0;
#ifdef SCROLLABLE_CURSORS
static const RSE_GET_MODE g_RSE_get_mode = RSE_get_next;
#else
static const RSE_GET_MODE g_RSE_get_mode = RSE_get_forward;
#endif
dsc* EVL_assign_to(thread_db* tdbb, jrd_nod* node)
{
/**************************************
*
* E V L _ a s s i g n _ t o
*
**************************************
*
* Functional description
* Evaluate the descriptor of the
* destination node of an assignment.
*
**************************************/
dsc* desc;
Format* format;
jrd_nod* message;
Record* record;
SET_TDBB(tdbb);
DEV_BLKCHK(node, type_nod);
jrd_req* request = tdbb->getRequest();
impure_value* impure = (impure_value*) ((SCHAR *) request + node->nod_impure);
/* The only nodes that can be assigned to are: argument, field and variable. */
int arg_number;
switch (node->nod_type) {
case nod_argument:
message = node->nod_arg[e_arg_message];
format = (Format*) message->nod_arg[e_msg_format];
arg_number = (int) (IPTR)node->nod_arg[e_arg_number];
desc = &format->fmt_desc[arg_number];
impure->vlu_desc.dsc_address =
(UCHAR *) request + message->nod_impure + (IPTR) desc->dsc_address;
impure->vlu_desc.dsc_dtype = desc->dsc_dtype;
impure->vlu_desc.dsc_length = desc->dsc_length;
impure->vlu_desc.dsc_scale = desc->dsc_scale;
impure->vlu_desc.dsc_sub_type = desc->dsc_sub_type;
if (DTYPE_IS_TEXT(desc->dsc_dtype) &&
((INTL_TTYPE(desc) == ttype_dynamic) ||
(INTL_GET_CHARSET(desc) == CS_dynamic)))
{
/* Value is a text value, we're assigning it back to the user
process, user process has not specified a subtype, user
process specified dynamic translation and the dsc isn't from
a 3.3 type request (blr_cstring2 instead of blr_cstring) so
convert the charset to the declared charset of the process. */
INTL_ASSIGN_DSC(&impure->vlu_desc,
tdbb->getAttachment()->att_charset, COLLATE_NONE);
}
return &impure->vlu_desc;
case nod_field:
record =
request->req_rpb[(int) (IPTR) node->nod_arg[e_fld_stream]].rpb_record;
if (!EVL_field(0, record, (USHORT)(IPTR) node->nod_arg[e_fld_id],
&impure->vlu_desc))
{
// The below condition means that EVL_field() returned
// a read-only dummy value which cannot be assigned to.
// The usual reason is a field being unexpectedly dropped.
if (impure->vlu_desc.dsc_address &&
!(impure->vlu_desc.dsc_flags & DSC_null))
{
ERR_post(isc_field_disappeared, isc_arg_end);
}
}
if (!impure->vlu_desc.dsc_address)
ERR_post(isc_read_only_field, isc_arg_end);
return &impure->vlu_desc;
case nod_null:
return NULL;
case nod_variable:
// Calculate descriptor
node = node->nod_arg[e_var_variable];
impure = (impure_value*) ((SCHAR *) request + node->nod_impure);
return &impure->vlu_desc;
default:
BUGCHECK(229); /* msg 229 EVL_assign_to: invalid operation */
}
return NULL;
}
RecordBitmap** EVL_bitmap(thread_db* tdbb, jrd_nod* node, RecordBitmap* bitmap_and)
{
/**************************************
*
* E V L _ b i t m a p
*
**************************************
*
* Functional description
* Evaluate bitmap valued expression.
*
**************************************/
SET_TDBB(tdbb);
DEV_BLKCHK(node, type_nod);
if (--tdbb->tdbb_quantum < 0)
JRD_reschedule(tdbb, 0, true);
switch (node->nod_type) {
case nod_bit_and:
{
RecordBitmap** bitmap = EVL_bitmap(tdbb, node->nod_arg[0], bitmap_and);
if (!(*bitmap) || !(*bitmap)->getFirst())
return bitmap;
else
return EVL_bitmap(tdbb, node->nod_arg[1], *bitmap);
}
case nod_bit_or:
return RecordBitmap::bit_or(
EVL_bitmap(tdbb, node->nod_arg[0], bitmap_and),
EVL_bitmap(tdbb, node->nod_arg[1], bitmap_and));
case nod_bit_in:
{
RecordBitmap** inv_bitmap = EVL_bitmap(tdbb, node->nod_arg[0], bitmap_and);
BTR_evaluate(tdbb,
reinterpret_cast<IndexRetrieval*>(node->nod_arg[1]->nod_arg[e_idx_retrieval]),
inv_bitmap, bitmap_and);
return inv_bitmap;
}
case nod_bit_dbkey:
{
impure_inversion* impure = (impure_inversion*) ((SCHAR*) tdbb->getRequest() + node->nod_impure);
RecordBitmap::reset(impure->inv_bitmap);
const dsc* desc = EVL_expr(tdbb, node->nod_arg[0]);
if (!(tdbb->getRequest()->req_flags & req_null) &&
desc->dsc_length == sizeof(RecordNumber::Packed))
{
const USHORT id = (USHORT)(IPTR) node->nod_arg[1];
RecordNumber::Packed* numbers = reinterpret_cast<RecordNumber::Packed*>(desc->dsc_address);
RecordNumber rel_dbkey;
rel_dbkey.bid_decode(&numbers[id]);
// NS: Why the heck we decrement record number here? I have no idea, but retain the algorithm for now.
// hvlad: because from the user point of view db_key's begins from 1
rel_dbkey.decrement();
if (!bitmap_and || bitmap_and->test(rel_dbkey.getValue()))
RBM_SET(tdbb->getDefaultPool(), &impure->inv_bitmap, rel_dbkey.getValue());
}
return &impure->inv_bitmap;
}
case nod_index:
{
impure_inversion* impure = (impure_inversion*) ((SCHAR*) tdbb->getRequest() + node->nod_impure);
RecordBitmap::reset(impure->inv_bitmap);
BTR_evaluate(tdbb,
reinterpret_cast<IndexRetrieval*>(node->nod_arg[e_idx_retrieval]),
&impure->inv_bitmap, bitmap_and);
return &impure->inv_bitmap;
}
default:
BUGCHECK(230); /* msg 230 EVL_bitmap: invalid operation */
}
return NULL;
}
bool EVL_boolean(thread_db* tdbb, jrd_nod* node)
{
/**************************************
*
* E V L _ b o o l e a n
*
**************************************
*
* Functional description
* Evaluate a boolean.
*
**************************************/
dsc* desc[2];
bool value;
SSHORT comparison;
impure_value* impure;
bool computed_invariant = false;
SET_TDBB(tdbb);
DEV_BLKCHK(node, type_nod);
/* Handle and pre-processing possible for various nodes. This includes
evaluating argument and checking NULL flags */
jrd_req* request = tdbb->getRequest();
jrd_nod** ptr = node->nod_arg;
switch (node->nod_type)
{
case nod_contains:
case nod_starts:
case nod_matches:
case nod_like:
case nod_equiv:
case nod_eql:
case nod_neq:
case nod_gtr:
case nod_geq:
case nod_lss:
case nod_leq:
case nod_between:
case nod_sleuth:
{
request->req_flags &= ~req_same_tx_upd;
SSHORT force_equal = 0;
/* Evaluate arguments. If either is null, result is null, but in
any case, evaluate both, since some expressions may later depend
on mappings which are developed here */
const jrd_nod* rec_version = *ptr;
desc[0] = EVL_expr(tdbb, *ptr++);
const ULONG flags = request->req_flags;
request->req_flags &= ~req_null;
force_equal |= request->req_flags & req_same_tx_upd;
// Currently only nod_like and nod_contains may be marked invariant
if (node->nod_flags & nod_invariant) {
impure = reinterpret_cast<impure_value*>((SCHAR *)request + node->nod_impure);
// Check that data type of operand is still the same.
// It may change due to multiple formats present in stream
// System tables are the good example of such streams -
// data coming from ini.epp has ASCII ttype, user data is UNICODE_FSS
//
// Note that value descriptor may be NULL pointer if value is SQL NULL
if ((impure->vlu_flags & VLU_computed) && desc[0] &&
(impure->vlu_desc.dsc_dtype != desc[0]->dsc_dtype ||
impure->vlu_desc.dsc_sub_type != desc[0]->dsc_sub_type ||
impure->vlu_desc.dsc_scale != desc[0]->dsc_scale)
)
{
impure->vlu_flags &= ~VLU_computed;
}
if (impure->vlu_flags & VLU_computed) {
if (impure->vlu_flags & VLU_null)
request->req_flags |= req_null;
else
computed_invariant = true;
}
else {
desc[1] = EVL_expr(tdbb, *ptr++);
if (request->req_flags & req_null) {
impure->vlu_flags |= VLU_computed;
impure->vlu_flags |= VLU_null;
}
else {
impure->vlu_flags &= ~VLU_null;
// Search object depends on operand data type.
// Thus save data type which we use to compute invariant
if (desc[0]) {
impure->vlu_desc.dsc_dtype = desc[0]->dsc_dtype;
impure->vlu_desc.dsc_sub_type = desc[0]->dsc_sub_type;
impure->vlu_desc.dsc_scale = desc[0]->dsc_scale;
}
else {
// Indicate we do not know type of expression.
// This code will force pattern recompile for the next non-null value
impure->vlu_desc.dsc_dtype = 0;
impure->vlu_desc.dsc_sub_type = 0;
impure->vlu_desc.dsc_scale = 0;
}
}
}
}
else
desc[1] = EVL_expr(tdbb, *ptr++);
// An equivalence operator evaluates to true when both operands
// are NULL and behaves like an equality operator otherwise.
// Note that this operator never sets req_null flag
if (node->nod_type == nod_equiv)
{
if ((flags & req_null) && (request->req_flags & req_null))
{
request->req_flags &= ~req_null;
return true;
}
if ((flags & req_null) || (request->req_flags & req_null))
{
request->req_flags &= ~req_null;
return false;
}
}
// If either of expressions above returned NULL set req_null flag
// and return false
if (flags & req_null)
request->req_flags |= req_null;
if (request->req_flags & req_null)
return false;
force_equal |= request->req_flags & req_same_tx_upd;
if (node->nod_flags & nod_comparison) {
comparison = MOV_compare(desc[0], desc[1]);
}
/* If we are checking equality of record_version
* and same transaction updated the record,
* force equality.
*/
if ((rec_version->nod_type == nod_rec_version) && (force_equal)) {
comparison = 0;
}
request->req_flags &= ~(req_null | req_same_tx_upd);
}
break;
case nod_and:
value = EVL_boolean(tdbb, *ptr++);
break;
case nod_or:
value = EVL_boolean(tdbb, *ptr++);
break;
case nod_not:
if ((*ptr)->nod_type == nod_ansi_any ||
(*ptr)->nod_type == nod_ansi_all)
{
request->req_flags |= req_ansi_not;
}
value = EVL_boolean(tdbb, *ptr++);
break;
default: /* Shut up some compiler warnings */
break;
}
/* Evaluate node */
// TODO: Verify and remove this flag once FB1.5beta3 is out.
// Default to not eval complete expression (i.e. do short-circuit
// optimizied evaluation). Both to get possible early warnings from
// users, and to default to the faster of the two options.
static bool bEvalCompleteExpression = Config::getCompleteBooleanEvaluation();
switch (node->nod_type)
{
case nod_and:
{
/* for and,
if either operand is FALSE, then the result is FALSE;
if both are TRUE, the result is TRUE;
otherwise, the result is NULL
op 1 op 2 result
---- ---- ------
F F F
F T F
F N F
T F F
T T T
T N N
N F F
N T N
N N N
*/
/* save null state and get other operand */
const USHORT firstnull = request->req_flags & req_null;
request->req_flags &= ~req_null;
if (!bEvalCompleteExpression && !value && !firstnull)
{
// First term is FALSE, why the whole expression is false.
// NULL flag is already turned off a few lines above.
return false;
}
const bool value2 = EVL_boolean(tdbb, *ptr);
const USHORT secondnull = request->req_flags & req_null;
request->req_flags &= ~req_null;
if ((!value && !firstnull) || (!value2 && !secondnull)) {
return false; /* at least one operand was FALSE */
}
if (value && value2) {
return true; /* both true */
}
request->req_flags |= req_null;
return false; /* otherwise, return null */
}
case nod_any:
case nod_ansi_any:
case nod_ansi_all:
{
USHORT* invariant_flags;
if (node->nod_flags & nod_invariant)
{
impure = (impure_value*) ((SCHAR *) request + node->nod_impure);
invariant_flags = & impure->vlu_flags;
if (*invariant_flags & VLU_computed) {
/* An invariant node has already been computed. */
if ((node->nod_type == nod_ansi_any) &&
(*invariant_flags & VLU_null))
{
request->req_flags |= req_null;
}
else
{
request->req_flags &= ~req_null;
}
return impure->vlu_misc.vlu_short != 0;
}
}
/* for ansi ANY clauses (and ALL's, which are negated ANY's)
the unoptimized boolean expression must be used, since the
processing of these clauses is order dependant (see rse.cpp) */
RecordSource* select = (RecordSource*) (node->nod_arg[e_any_rsb]);
if (node->nod_type != nod_any)
{
select->rsb_any_boolean =
((RecordSelExpr*) (node->nod_arg[e_any_rse]))->rse_boolean;
if (node->nod_type == nod_ansi_any) {
request->req_flags |= req_ansi_any;
}
else {
request->req_flags |= req_ansi_all;
// dimitr: Even if we can evaluate ANY without a residual
// boolean (what I still doubt), it's impossible
// for ALL. Hence this assertion.
fb_assert(select->rsb_type == rsb_boolean);
}
}
RSE_open(tdbb, select);
value = RSE_get_record(tdbb, select, g_RSE_get_mode);
RSE_close(tdbb, select);
if (node->nod_type == nod_any)
request->req_flags &= ~req_null;
/* If this is an invariant node, save the return value. */
if (node->nod_flags & nod_invariant)
{
*invariant_flags |= VLU_computed;
if ((node->nod_type != nod_any) &&
(request->req_flags & req_null))
{
*invariant_flags |= VLU_null;
}
impure->vlu_misc.vlu_short = value ? TRUE : FALSE;
}
return value;
}
case nod_contains:
case nod_starts:
case nod_matches:
case nod_like:
return string_boolean(tdbb, node, desc[0], desc[1], computed_invariant);
case nod_sleuth:
return sleuth(tdbb, node, desc[0], desc[1]);
case nod_missing:
EVL_expr(tdbb, *ptr);
if (request->req_flags & req_null) {
value = true;
request->req_flags &= ~req_null;
}
else {
value = false;
}
return value;
case nod_not:
if (request->req_flags & req_null)
return false;
return !value;
case nod_or:
{
/* for or, if either operand is TRUE, then the result is TRUE; if
both are FALSE, the result is FALSE; otherwise, the result is NULL
op 1 op 2 result
---- ---- ------
F F F
F T T
F N N
T F T
T T T
T N T
N F N
N T T
N N N
also, preserve first operand's value and null state, but still
evaluate second operand, since latter field mappings may
depend on the evaluation */
const ULONG flags = request->req_flags;
request->req_flags &= ~req_null;
if (!bEvalCompleteExpression && value)
{
// First term is TRUE, why the whole expression is true.
// NULL flag is already turned off a few lines above.
return true;
}
const bool value2 = EVL_boolean(tdbb, *ptr);
if (value || value2) {
request->req_flags &= ~req_null;
return true;
}
/* restore saved NULL state */
if (flags & req_null) {
request->req_flags |= req_null;
}
return false;
}
case nod_unique:
{
USHORT* invariant_flags;
if (node->nod_flags & nod_invariant)
{
impure = (impure_value*) ((SCHAR *) request + node->nod_impure);
invariant_flags = & impure->vlu_flags;
if (*invariant_flags & VLU_computed)
{
/* An invariant node has already been computed. */
request->req_flags &= ~req_null;
return impure->vlu_misc.vlu_short != 0;
}
}
RecordSource* urs = reinterpret_cast<RecordSource*>(node->nod_arg[e_any_rsb]);
RSE_open(tdbb, urs);
value = RSE_get_record(tdbb, urs, g_RSE_get_mode);
if (value)
{
value = !RSE_get_record(tdbb, urs, g_RSE_get_mode);
}
RSE_close(tdbb, urs);
request->req_flags &= ~req_null;
/* If this is an invariant node, save the return value. */
if (node->nod_flags & nod_invariant)
{
*invariant_flags |= VLU_computed;
impure->vlu_misc.vlu_short = value ? TRUE : FALSE;
}
return value;
}
case nod_equiv:
case nod_eql:
return (comparison == 0);
case nod_neq:
return (comparison != 0);
case nod_gtr:
return (comparison > 0);
case nod_geq:
return (comparison >= 0);
case nod_lss:
return (comparison < 0);
case nod_leq:
return (comparison <= 0);
case nod_between:
desc[1] = EVL_expr(tdbb, node->nod_arg[2]);
if (request->req_flags & req_null)
return false;
return (comparison >= 0 && MOV_compare(desc[0], desc[1]) <= 0);
default:
BUGCHECK(231); /* msg 231 EVL_boolean: invalid operation */
}
return false;
}
dsc* EVL_expr(thread_db* tdbb, jrd_nod* const node)
{
/**************************************
*
* E V L _ e x p r
*
**************************************
*
* Functional description
* Evaluate a value expression.
*
**************************************/
DEV_BLKCHK(node, type_nod);
if (!node)
BUGCHECK(303); /* msg 303 Invalid expression for evaluation */
SET_TDBB(tdbb);
if (--tdbb->tdbb_quantum < 0)
JRD_reschedule(tdbb, 0, true);
jrd_req* const request = tdbb->getRequest();
impure_value* const impure = (impure_value*) ((SCHAR *) request + node->nod_impure);
request->req_flags &= ~req_null;
/* Do a preliminary screen for either simple nodes or nodes that
are special cased elsewhere */
switch (node->nod_type) {
case nod_add:
case nod_subtract:
case nod_divide:
case nod_multiply:
case nod_add2:
case nod_subtract2:
case nod_divide2:
case nod_multiply2:
case nod_concatenate:
return binary_value(tdbb, node, impure);
case nod_argument:
{
const dsc* desc;
if (node->nod_arg[e_arg_flag]) {
desc = EVL_expr(tdbb, node->nod_arg[e_arg_flag]);
if (MOV_get_long(desc, 0)) {
request->req_flags |= req_null;
}
}
const jrd_nod* message = node->nod_arg[e_arg_message];
const Format* format = (Format*) message->nod_arg[e_msg_format];
desc = &format->fmt_desc[(int)(IPTR) node->nod_arg[e_arg_number]];
impure->vlu_desc.dsc_address = (UCHAR *) request +
message->nod_impure + (IPTR) desc->dsc_address;
impure->vlu_desc.dsc_dtype = desc->dsc_dtype;
impure->vlu_desc.dsc_length = desc->dsc_length;
impure->vlu_desc.dsc_scale = desc->dsc_scale;
impure->vlu_desc.dsc_sub_type = desc->dsc_sub_type;
if (impure->vlu_desc.dsc_dtype == dtype_text)
adjust_text_descriptor(tdbb, &impure->vlu_desc);
USHORT* impure_flags = (USHORT*) ((UCHAR *) request +
(IPTR) message->nod_arg[e_msg_impure_flags] +
(sizeof(USHORT) * (IPTR) node->nod_arg[e_arg_number]));
if (!(*impure_flags & VLU_checked))
{
if (node->nod_arg[e_arg_info])
{
EVL_validate(tdbb,
Item(nod_argument, (IPTR) node->nod_arg[e_arg_message]->nod_arg[e_msg_number],
(IPTR) node->nod_arg[e_arg_number]),
reinterpret_cast<const ItemInfo*>(node->nod_arg[e_arg_info]),
&impure->vlu_desc, request->req_flags & req_null);
}
*impure_flags |= VLU_checked;
}
return &impure->vlu_desc;
}
case nod_dbkey:
return dbkey(tdbb, node, impure);
case nod_rec_version:
return record_version(tdbb, node, impure);
case nod_field:
{
Record* record =
request->req_rpb[(int) (IPTR)node->nod_arg[e_fld_stream]].rpb_record;
jrd_rel* relation = request->req_rpb[(USHORT)(IPTR) node->nod_arg[e_fld_stream]].rpb_relation;
/* In order to "map a null to a default" value (in EVL_field()),
* the relation block is referenced.
* Reference: Bug 10116, 10424
*/
if (!EVL_field(relation,
record,
(USHORT)(IPTR) node->nod_arg[e_fld_id],
&impure->vlu_desc))
{
request->req_flags |= req_null;
}
if (!relation || !(relation->rel_flags & REL_system))
{
if (impure->vlu_desc.dsc_dtype == dtype_text)
adjust_text_descriptor(tdbb, &impure->vlu_desc);
}
return &impure->vlu_desc;
}
case nod_function:
FUN_evaluate(reinterpret_cast<UserFunction*>(node->nod_arg[e_fun_function]),
node->nod_arg[e_fun_args], impure);
return &impure->vlu_desc;
case nod_sys_function:
{
const SysFunction* sysFunction = reinterpret_cast<SysFunction*>(node->nod_arg[e_sysfun_function]);
return sysFunction->evlFunc(tdbb, sysFunction, node->nod_arg[e_sysfun_args], impure);
}
case nod_literal:
return &((Literal*) node)->lit_desc;
case nod_lock_state:
return lock_state(tdbb, node, impure);
case nod_null:
request->req_flags |= req_null;
return NULL;
case nod_prot_mask:
return get_mask(tdbb, node, impure);
case nod_current_time:
case nod_current_date:
case nod_current_timestamp:
{
// Use the request timestamp
fb_assert(!request->req_timestamp.isEmpty());
ISC_TIMESTAMP enc_times = request->req_timestamp.value();
memset(&impure->vlu_desc, 0, sizeof(impure->vlu_desc));
impure->vlu_desc.dsc_address =
(UCHAR *) &impure->vlu_misc.vlu_timestamp;
if (node->nod_type == nod_current_time ||
node->nod_type == nod_current_timestamp)
{
const int precision = (int)(IPTR) node->nod_arg[0];
fb_assert(precision >= 0);
Firebird::TimeStamp::round_time(enc_times.timestamp_time,
precision);
}
switch (node->nod_type) {
case nod_current_time:
impure->vlu_desc.dsc_dtype = dtype_sql_time;
impure->vlu_desc.dsc_length = type_lengths[dtype_sql_time];
*(ULONG *) impure->vlu_desc.dsc_address =
enc_times.timestamp_time;
break;
case nod_current_date:
impure->vlu_desc.dsc_dtype = dtype_sql_date;
impure->vlu_desc.dsc_length = type_lengths[dtype_sql_date];
*(ULONG *) impure->vlu_desc.dsc_address =
enc_times.timestamp_date;
break;
case nod_current_timestamp:
impure->vlu_desc.dsc_dtype = dtype_timestamp;
impure->vlu_desc.dsc_length = type_lengths[dtype_timestamp];
*((ISC_TIMESTAMP *) impure->vlu_desc.dsc_address) = enc_times;
break;
default:
fb_assert(false);
break;
}
}
return &impure->vlu_desc;
case nod_user_name:
{
impure->vlu_desc.dsc_dtype = dtype_text;
impure->vlu_desc.dsc_sub_type = 0;
impure->vlu_desc.dsc_scale = 0;
INTL_ASSIGN_TTYPE(&impure->vlu_desc, ttype_metadata);
const char* cur_user = 0;
if (tdbb->getAttachment()->att_user)