forked from FirebirdSQL/firebird
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoolNodes.cpp
More file actions
2243 lines (1770 loc) · 58.8 KB
/
Copy pathBoolNodes.cpp
File metadata and controls
2243 lines (1770 loc) · 58.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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): ______________________________________.
* Adriano dos Santos Fernandes - refactored from pass1.cpp, gen.cpp, cmp.cpp, par.cpp and evl.cpp
*/
#include "firebird.h"
#include "../common/classes/VaryStr.h"
#include "../dsql/BoolNodes.h"
#include "../dsql/ExprNodes.h"
#include "../dsql/StmtNodes.h"
#include "../jrd/align.h"
#include "firebird/impl/blr.h"
#include "../jrd/tra.h"
#include "../jrd/recsrc/RecordSource.h"
#include "../jrd/recsrc/Cursor.h"
#include "../jrd/optimizer/Optimizer.h"
#include "../jrd/blb_proto.h"
#include "../jrd/btr_proto.h"
#include "../jrd/cmp_proto.h"
#include "../jrd/evl_proto.h"
#include "../jrd/intl_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/par_proto.h"
#include "../jrd/Collation.h"
#include "../dsql/ddl_proto.h"
#include "../dsql/errd_proto.h"
#include "../dsql/gen_proto.h"
#include "../dsql/make_proto.h"
#include "../dsql/pass1_proto.h"
#include "../dsql/DSqlDataTypeUtil.h"
#include "../jrd/cvt2_proto.h"
using namespace Firebird;
using namespace Jrd;
namespace
{
// Maximum members in "IN" list. For eg. SELECT * FROM T WHERE F IN (1, 2, 3, ...)
// Beware: raising the limit beyond the 16-bit boundaries would be an incompatible BLR change.
static const unsigned MAX_MEMBER_LIST = MAX_USHORT;
// Compare two comparisons with the boolean literal of the form:
// [NOT] <value> [{ = | <> } { TRUE | FALSE }]
// and detect whether they're logically the same.
// For example: (NOT A) == (A = FALSE) == (A <> TRUE) == NOT (A = TRUE) etc.
bool sameBoolComparison(const ComparativeBoolNode* node1, const ComparativeBoolNode* node2, bool ignoreStreams)
{
fb_assert(node1 && node2);
if (node1->blrOp != blr_eql && node1->blrOp != blr_neq)
return false;
if (node2->blrOp != blr_eql && node2->blrOp != blr_neq)
return false;
bool isTrue1 = false;
const ValueExprNode* arg1 = nullptr;
if (const auto literal = nodeAs<LiteralNode>(node1->arg1))
{
if (literal->litDesc.isBoolean())
{
isTrue1 = literal->getBoolean();
arg1 = node1->arg2;
}
}
else if (const auto literal = nodeAs<LiteralNode>(node1->arg2))
{
if (literal->litDesc.isBoolean())
{
isTrue1 = literal->getBoolean();
arg1 = node1->arg1;
}
}
if (!arg1)
return false;
if (node1->blrOp == blr_neq)
isTrue1 = !isTrue1;
bool isTrue2 = false;
const ValueExprNode* arg2 = nullptr;
if (const auto literal = nodeAs<LiteralNode>(node2->arg1))
{
if (literal->litDesc.isBoolean())
{
isTrue2 = literal->getBoolean();
arg2 = node2->arg2;
}
}
else if (const auto literal = nodeAs<LiteralNode>(node2->arg2))
{
if (literal->litDesc.isBoolean())
{
isTrue2 = literal->getBoolean();
arg2 = node2->arg1;
}
}
if (!arg2)
return false;
if (node2->blrOp == blr_neq)
isTrue2 = !isTrue2;
if (!arg1->sameAs(arg2, ignoreStreams) || isTrue1 != isTrue2)
return false;
return true;
}
} // namespace
//--------------------
BoolExprNode* BoolExprNode::pass2(thread_db* tdbb, CompilerScratch* csb)
{
pass2Boolean(tdbb, csb, [=, this] { ExprNode::pass2(tdbb, csb); });
if (nodFlags & FLAG_INVARIANT)
{
// Bind values of invariant nodes to top-level RSE (if present)
if (csb->csb_current_nodes.hasData())
{
RseNode* topRseNode = nodeAs<RseNode>(csb->csb_current_nodes[0]);
fb_assert(topRseNode);
if (!topRseNode->rse_invariants)
{
topRseNode->rse_invariants =
FB_NEW_POOL(*tdbb->getDefaultPool()) VarInvariantArray(*tdbb->getDefaultPool());
}
topRseNode->rse_invariants->add(impureOffset);
}
}
return this;
}
//--------------------
static RegisterBoolNode<BinaryBoolNode> regBinaryBoolNode({blr_and, blr_or});
BinaryBoolNode::BinaryBoolNode(MemoryPool& pool, UCHAR aBlrOp, BoolExprNode* aArg1,
BoolExprNode* aArg2)
: TypedNode<BoolExprNode, ExprNode::TYPE_BINARY_BOOL>(pool),
blrOp(aBlrOp),
arg1(aArg1),
arg2(aArg2)
{
}
DmlNode* BinaryBoolNode::parse(thread_db* tdbb, MemoryPool& pool, CompilerScratch* csb, const UCHAR blrOp)
{
BinaryBoolNode* node = FB_NEW_POOL(pool) BinaryBoolNode(pool, blrOp);
node->arg1 = PAR_parse_boolean(tdbb, csb);
node->arg2 = PAR_parse_boolean(tdbb, csb);
return node;
}
string BinaryBoolNode::internalPrint(NodePrinter& printer) const
{
BoolExprNode::internalPrint(printer);
NODE_PRINT(printer, blrOp);
NODE_PRINT(printer, arg1);
NODE_PRINT(printer, arg2);
return "BinaryBoolNode";
}
BoolExprNode* BinaryBoolNode::dsqlPass(DsqlCompilerScratch* dsqlScratch)
{
return FB_NEW_POOL(dsqlScratch->getPool()) BinaryBoolNode(dsqlScratch->getPool(), blrOp,
doDsqlPass(dsqlScratch, arg1), doDsqlPass(dsqlScratch, arg2));
}
void BinaryBoolNode::genBlr(DsqlCompilerScratch* dsqlScratch)
{
dsqlScratch->appendUChar(blrOp);
GEN_expr(dsqlScratch, arg1);
GEN_expr(dsqlScratch, arg2);
}
bool BinaryBoolNode::dsqlMatch(DsqlCompilerScratch* dsqlScratch, const ExprNode* other, bool ignoreMapCast) const
{
if (!BoolExprNode::dsqlMatch(dsqlScratch, other, ignoreMapCast))
return false;
const BinaryBoolNode* o = nodeAs<BinaryBoolNode>(other);
fb_assert(o);
return blrOp == o->blrOp;
}
bool BinaryBoolNode::sameAs(const ExprNode* other, bool ignoreStreams) const
{
const BinaryBoolNode* const otherNode = nodeAs<BinaryBoolNode>(other);
if (!otherNode || blrOp != otherNode->blrOp)
return false;
if (arg1->sameAs(otherNode->arg1, ignoreStreams) &&
arg2->sameAs(otherNode->arg2, ignoreStreams))
{
return true;
}
// A AND B is equivalent to B AND A, ditto for A OR B and B OR A.
return arg1->sameAs(otherNode->arg2, ignoreStreams) &&
arg2->sameAs(otherNode->arg1, ignoreStreams);
}
BoolExprNode* BinaryBoolNode::copy(thread_db* tdbb, NodeCopier& copier) const
{
BinaryBoolNode* node = FB_NEW_POOL(*tdbb->getDefaultPool()) BinaryBoolNode(*tdbb->getDefaultPool(),
blrOp);
node->nodFlags = nodFlags;
node->arg1 = copier.copy(tdbb, arg1);
node->arg2 = copier.copy(tdbb, arg2);
return node;
}
TriState BinaryBoolNode::execute(thread_db* tdbb, Request* request) const
{
switch (blrOp)
{
case blr_and:
return executeAnd(tdbb, request);
case blr_or:
return executeOr(tdbb, request);
}
fb_assert(false);
return TriState::empty();
}
TriState BinaryBoolNode::executeAnd(thread_db* tdbb, Request* request) const
{
// 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
const TriState value1 = arg1->execute(tdbb, request);
if (value1.isAssigned())
{
if (value1 != TriState(true))
return TriState(false);
return arg2->execute(tdbb, request);
}
const TriState value2 = arg2->execute(tdbb, request);
return value2 == TriState(false) ? TriState(false) : TriState::empty();
}
TriState BinaryBoolNode::executeOr(thread_db* tdbb, Request* request) const
{
// 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
const TriState first = arg1->execute(tdbb, request);
if (first.isAssigned())
{
if (first.asBool())
return TriState(true);
return arg2->execute(tdbb, request);
}
const TriState value2 = arg2->execute(tdbb, request);
return value2 == TriState(true) ? TriState(true) : TriState::empty();
}
//--------------------
static RegisterBoolNode<ComparativeBoolNode> regComparativeBoolNode({
blr_eql,
blr_geq,
blr_gtr,
blr_leq,
blr_lss,
blr_neq,
blr_equiv,
blr_between,
blr_like,
blr_ansi_like,
blr_containing,
blr_starting,
blr_similar,
blr_matching,
blr_matching2
});
ComparativeBoolNode::ComparativeBoolNode(MemoryPool& pool, UCHAR aBlrOp,
ValueExprNode* aArg1, ValueExprNode* aArg2, ValueExprNode* aArg3)
: TypedNode<BoolExprNode, ExprNode::TYPE_COMPARATIVE_BOOL>(pool),
blrOp(aBlrOp),
dsqlCheckBoolean(false),
dsqlFlag(DFLAG_NONE),
arg1(aArg1),
arg2(aArg2),
arg3(aArg3),
dsqlSpecialArg(nullptr)
{
}
ComparativeBoolNode::ComparativeBoolNode(MemoryPool& pool, UCHAR aBlrOp,
ValueExprNode* aArg1, DsqlFlag aDsqlFlag, ExprNode* aSpecialArg)
: TypedNode<BoolExprNode, ExprNode::TYPE_COMPARATIVE_BOOL>(pool),
blrOp(aBlrOp),
dsqlCheckBoolean(false),
dsqlFlag(aDsqlFlag),
arg1(aArg1),
arg2(nullptr),
arg3(nullptr),
dsqlSpecialArg(aSpecialArg)
{
}
DmlNode* ComparativeBoolNode::parse(thread_db* tdbb, MemoryPool& pool, CompilerScratch* csb, const UCHAR blrOp)
{
ComparativeBoolNode* node = FB_NEW_POOL(pool) ComparativeBoolNode(pool, blrOp);
node->arg1 = PAR_parse_value(tdbb, csb);
node->arg2 = PAR_parse_value(tdbb, csb);
if (blrOp == blr_between || blrOp == blr_ansi_like || blrOp == blr_matching2)
{
if (blrOp == blr_ansi_like)
node->blrOp = blr_like;
node->arg3 = PAR_parse_value(tdbb, csb);
}
else if (blrOp == blr_similar)
{
if (csb->csb_blr_reader.getByte() != 0)
node->arg3 = PAR_parse_value(tdbb, csb); // escape
}
return node;
}
string ComparativeBoolNode::internalPrint(NodePrinter& printer) const
{
BoolExprNode::internalPrint(printer);
NODE_PRINT(printer, blrOp);
NODE_PRINT(printer, dsqlFlag);
NODE_PRINT(printer, arg1);
NODE_PRINT(printer, arg2);
NODE_PRINT(printer, arg3);
NODE_PRINT(printer, dsqlSpecialArg);
return "ComparativeBoolNode";
}
BoolExprNode* ComparativeBoolNode::dsqlPass(DsqlCompilerScratch* dsqlScratch)
{
NestConst<ValueExprNode> procArg1 = arg1;
NestConst<ValueExprNode> procArg2 = arg2;
NestConst<ValueExprNode> procArg3 = arg3;
if (dsqlSpecialArg)
{
if (const auto listNode = nodeAs<ValueListNode>(dsqlSpecialArg))
{
if (listNode->items.getCount() > MAX_MEMBER_LIST)
{
ERRD_post(Arg::Gds(isc_sqlerr) << Arg::Num(-901) <<
Arg::Gds(isc_imp_exc) <<
Arg::Gds(isc_dsql_too_many_values) << Arg::Num(MAX_MEMBER_LIST));
}
if (listNode->items.getCount() == 1)
{
// Convert A IN (B) into A = B
ComparativeBoolNode* const resultNode = FB_NEW_POOL(dsqlScratch->getPool())
ComparativeBoolNode(dsqlScratch->getPool(),
blr_eql, arg1, listNode->items.front());
return resultNode->dsqlPass(dsqlScratch);
}
// Generate the IN LIST boolean
InListBoolNode* const resultNode = FB_NEW_POOL(dsqlScratch->getPool())
InListBoolNode(dsqlScratch->getPool(), procArg1, listNode);
return resultNode->dsqlPass(dsqlScratch);
}
if (const auto selNode = nodeAs<SelectExprNode>(dsqlSpecialArg))
{
fb_assert(!(selNode->dsqlFlags & RecordSourceNode::DFLAG_SINGLETON));
UCHAR newBlrOp = blr_any;
if (dsqlFlag == DFLAG_ANSI_ANY)
newBlrOp = blr_ansi_any;
else if (dsqlFlag == DFLAG_ANSI_ALL)
newBlrOp = blr_ansi_all;
return createRseNode(dsqlScratch, newBlrOp);
}
fb_assert(false);
}
procArg1 = doDsqlPass(dsqlScratch, procArg1);
procArg2 = doDsqlPass(dsqlScratch, procArg2);
procArg3 = doDsqlPass(dsqlScratch, procArg3);
const auto convertLiteralToOperand =
[&](NestConst<ValueExprNode>& literalArg, NestConst<ValueExprNode>& referenceArg)
{
LiteralNode* const literal = nodeAs<LiteralNode>(literalArg);
if (!literal || literal->litDesc.dsc_dtype != dtype_text)
return;
dsc referenceDesc;
DsqlDescMaker::fromNode(dsqlScratch, &referenceDesc, referenceArg);
if (referenceDesc.isUnknown())
return;
if (ValueExprNode* const value = MAKE_constant_from_literal(literal, &referenceDesc))
literalArg = value;
};
convertLiteralToOperand(procArg1, procArg2);
convertLiteralToOperand(procArg2, procArg1);
if (blrOp == blr_between)
convertLiteralToOperand(procArg3, procArg1);
ComparativeBoolNode* node = FB_NEW_POOL(dsqlScratch->getPool()) ComparativeBoolNode(dsqlScratch->getPool(), blrOp,
procArg1,
procArg2,
procArg3);
if (dsqlCheckBoolean)
{
dsc desc;
DsqlDescMaker::fromNode(dsqlScratch, &desc, node->arg1);
if (desc.dsc_dtype != dtype_boolean && desc.dsc_dtype != dtype_unknown && !desc.isNull())
{
ERRD_post(Arg::Gds(isc_sqlerr) << Arg::Num(-104) <<
Arg::Gds(isc_invalid_boolean_usage));
}
}
switch (blrOp)
{
case blr_eql:
case blr_neq:
case blr_gtr:
case blr_geq:
case blr_lss:
case blr_leq:
case blr_equiv:
case blr_between:
{
// Try to force arg1 to be same type as arg2 eg: ? = FIELD case
PASS1_set_parameter_type(dsqlScratch, node->arg1, procArg2, false);
// Try to force arg2 to be same type as arg1 eg: FIELD = ? case
// Try even when the above call succeeded, because "arg2" may
// have arg-expressions that should be resolved.
PASS1_set_parameter_type(dsqlScratch, procArg2, node->arg1, false);
// X BETWEEN Y AND ? case
if (!PASS1_set_parameter_type(dsqlScratch, node->arg3, node->arg1, false))
{
// ? BETWEEN Y AND ? case
PASS1_set_parameter_type(dsqlScratch, node->arg3, procArg2, false);
}
break;
}
case blr_containing:
case blr_like:
case blr_similar:
case blr_starting:
// Try to force arg1 to be same type as arg2 eg: ? LIKE FIELD case
PASS1_set_parameter_type(dsqlScratch, node->arg1, procArg2, true);
// Try to force arg2 same type as arg 1 eg: FIELD LIKE ? case
// Try even when the above call succeeded, because "arg2" may
// have arg-expressions that should be resolved.
PASS1_set_parameter_type(dsqlScratch, procArg2, node->arg1, true);
// X LIKE Y ESCAPE ? case
PASS1_set_parameter_type(dsqlScratch, node->arg3, procArg2, true);
}
return node;
}
void ComparativeBoolNode::genBlr(DsqlCompilerScratch* dsqlScratch)
{
dsqlScratch->appendUChar(blrOp == blr_like && arg3 ? blr_ansi_like : blrOp);
GEN_expr(dsqlScratch, arg1);
GEN_expr(dsqlScratch, arg2);
if (blrOp == blr_similar)
dsqlScratch->appendUChar(arg3 ? 1 : 0);
if (arg3)
GEN_expr(dsqlScratch, arg3);
}
bool ComparativeBoolNode::dsqlMatch(DsqlCompilerScratch* dsqlScratch, const ExprNode* other, bool ignoreMapCast) const
{
if (!BoolExprNode::dsqlMatch(dsqlScratch, other, ignoreMapCast))
return false;
const ComparativeBoolNode* o = nodeAs<ComparativeBoolNode>(other);
fb_assert(o);
return dsqlFlag == o->dsqlFlag && blrOp == o->blrOp;
}
bool ComparativeBoolNode::sameAs(const ExprNode* other, bool ignoreStreams) const
{
const ComparativeBoolNode* const otherNode = nodeAs<ComparativeBoolNode>(other);
if (!otherNode)
return false;
if (sameBoolComparison(this, otherNode, ignoreStreams))
return true;
if (blrOp != otherNode->blrOp)
return false;
bool matching = arg1->sameAs(otherNode->arg1, ignoreStreams) &&
arg2->sameAs(otherNode->arg2, ignoreStreams);
if (matching)
{
matching = (!arg3 == !otherNode->arg3) &&
(!arg3 || arg3->sameAs(otherNode->arg3, ignoreStreams));
if (matching)
return true;
}
// TODO match A > B to B <= A, etc
if (blrOp == blr_eql || blrOp == blr_equiv || blrOp == blr_neq)
{
// A = B is equivalent to B = A, etc.
if (arg1->sameAs(otherNode->arg2, ignoreStreams) &&
arg2->sameAs(otherNode->arg1, ignoreStreams))
{
return true;
}
}
return false;
}
BoolExprNode* ComparativeBoolNode::copy(thread_db* tdbb, NodeCopier& copier) const
{
ComparativeBoolNode* node = FB_NEW_POOL(*tdbb->getDefaultPool()) ComparativeBoolNode(
*tdbb->getDefaultPool(), blrOp);
node->nodFlags = nodFlags;
node->arg1 = copier.copy(tdbb, arg1);
node->arg2 = copier.copy(tdbb, arg2);
if (arg3)
node->arg3 = copier.copy(tdbb, arg3);
return node;
}
BoolExprNode* ComparativeBoolNode::pass1(thread_db* tdbb, CompilerScratch* csb)
{
bool invariantCheck = false;
switch (blrOp)
{
case blr_like:
case blr_similar:
case blr_containing:
case blr_starting:
invariantCheck = true;
break;
}
doPass1(tdbb, csb, arg1.getAddress());
if (invariantCheck)
{
// We need to take care of invariantness expressions to be able to pre-compile the pattern.
nodFlags |= FLAG_INVARIANT;
csb->csb_current_nodes.push(this);
}
doPass1(tdbb, csb, arg2.getAddress());
doPass1(tdbb, csb, arg3.getAddress());
if (invariantCheck)
{
csb->csb_current_nodes.pop();
// If there is no top-level RSE present and patterns are not constant, unmark node as invariant
// because it may be dependent on data or variables.
if ((nodFlags & FLAG_INVARIANT) &&
(!nodeIs<LiteralNode>(arg2) || (arg3 && !nodeIs<LiteralNode>(arg3))))
{
for (const auto& ctxNode : csb->csb_current_nodes)
{
if (nodeIs<RseNode>(ctxNode))
return this;
}
nodFlags &= ~FLAG_INVARIANT;
}
}
return this;
}
void ComparativeBoolNode::pass2Boolean(thread_db* tdbb, CompilerScratch* csb, std::function<void ()> process)
{
if (nodFlags & FLAG_INVARIANT)
csb->csb_invariants.push(&impureOffset);
process();
RecordKeyNode* keyNode;
if (arg3)
{
if ((keyNode = nodeAs<RecordKeyNode>(arg3)) && keyNode->aggregate)
ERR_post(Arg::Gds(isc_bad_dbkey));
dsc descriptor_c;
arg1->getDesc(tdbb, csb, &descriptor_c);
if (DTYPE_IS_DATE(descriptor_c.dsc_dtype))
{
arg1->nodFlags |= FLAG_DATE;
arg2->nodFlags |= FLAG_DATE;
}
}
if (((keyNode = nodeAs<RecordKeyNode>(arg1)) && keyNode->aggregate) ||
((keyNode = nodeAs<RecordKeyNode>(arg2)) && keyNode->aggregate))
{
ERR_post(Arg::Gds(isc_bad_dbkey));
}
dsc descriptor_a, descriptor_b;
arg1->getDesc(tdbb, csb, &descriptor_a);
arg2->getDesc(tdbb, csb, &descriptor_b);
if (DTYPE_IS_DATE(descriptor_a.dsc_dtype))
arg2->nodFlags |= FLAG_DATE;
else if (DTYPE_IS_DATE(descriptor_b.dsc_dtype))
arg1->nodFlags |= FLAG_DATE;
if (nodFlags & FLAG_INVARIANT)
impureOffset = csb->allocImpure<impure_value>();
// Do not use FLAG_PATTERN_MATCHER_CACHE for blr_starting as it has very fast compilation.
else if (blrOp == blr_containing || blrOp == blr_like || blrOp == blr_similar)
{
impureOffset = csb->allocImpure<impure_value>();
nodFlags |= FLAG_PATTERN_MATCHER_CACHE;
}
}
TriState ComparativeBoolNode::execute(thread_db* tdbb, Request* request) const
{
dsc* desc[2] = {nullptr, nullptr};
bool computed_invariant = false;
request->req_flags &= ~req_same_tx_upd;
// 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
desc[0] = EVL_expr(tdbb, request, arg1);
// arg1 IS NULL
const bool null1 = !desc[0];
bool force_equal = (request->req_flags & req_same_tx_upd) != 0;
// Currently only nod_like, nod_contains, nod_starts and nod_similar may be marked invariant
if (nodFlags & FLAG_INVARIANT)
{
impure_value* impure = request->getImpure<impure_value>(impureOffset);
// 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))
computed_invariant = true;
}
else
{
desc[1] = EVL_expr(tdbb, request, arg2);
if (!desc[1])
impure->vlu_flags |= VLU_computed | 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, request, arg2);
// arg2 IS NULL
const bool null2 = !computed_invariant && !desc[1];
// An equivalence operator evaluates to true when both operands
// are NULL and behaves like an equality operator otherwise.
if (blrOp == blr_equiv)
{
if (null1 && null2)
return TriState(true);
if (null1 || null2)
return TriState(false);
}
// If either of expressions above returned NULL return unknown.
// The exception is BETWEEN operator that could
// return FALSE even when arg2 IS NULL, for example:
// 1 BETWEEN NULL AND 0
if (null1 || (null2 && (blrOp != blr_between)))
return TriState::empty();
force_equal |= (request->req_flags & req_same_tx_upd) != 0;
int comparison; // while the two switch() below are in sync, no need to initialize
switch (blrOp)
{
case blr_eql:
case blr_equiv:
case blr_gtr:
case blr_geq:
case blr_lss:
case blr_leq:
case blr_neq:
comparison = MOV_compare(tdbb, desc[0], desc[1]);
break;
case blr_between:
if (!null2)
{
comparison = MOV_compare(tdbb, desc[0], desc[1]);
if (comparison < 0)
return TriState(false);
}
else
comparison = -1;
break;
}
// If we are checking equality of record_version
// and same transaction updated the record, force equality.
const RecordKeyNode* recVersionNode = nodeAs<RecordKeyNode>(arg1);
if (recVersionNode && recVersionNode->blrOp == blr_record_version && force_equal)
comparison = 0;
request->req_flags &= ~req_same_tx_upd;
switch (blrOp)
{
case blr_eql:
case blr_equiv:
return TriState(comparison == 0);
case blr_gtr:
return TriState(comparison > 0);
case blr_geq:
return TriState(comparison >= 0);
case blr_lss:
return TriState(comparison < 0);
case blr_leq:
return TriState(comparison <= 0);
case blr_neq:
return TriState(comparison != 0);
case blr_between:
desc[1] = EVL_expr(tdbb, request, arg3);
if (!desc[1])
return (!null2 && comparison < 0) ? TriState(false) : TriState::empty();
{
// arg1 <= arg3
const bool cmp1_3 = (MOV_compare(tdbb, desc[0], desc[1]) <= 0);
return (null2 && cmp1_3) ? TriState::empty() : TriState(cmp1_3);
}
case blr_containing:
case blr_starting:
case blr_matching:
case blr_like:
case blr_similar:
return stringBoolean(tdbb, request, desc[0], desc[1], computed_invariant);
case blr_matching2:
return TriState(sleuth(tdbb, request, desc[0], desc[1]));
}
return TriState(false);
}
// Perform one of the complex string functions CONTAINING, MATCHES, or STARTS WITH.
TriState ComparativeBoolNode::stringBoolean(thread_db* tdbb, Request* request,
dsc* desc1, dsc* desc2, bool computedInvariant) const
{
SET_TDBB(tdbb);
TTypeId type1;
if (!desc1->isBlob())
type1 = desc1->getTextType();
else
{
// No MATCHES support for blob
if (blrOp == blr_matching)
return TriState(false);
// Non-text blob is treated here as NONE, not OCTETS
type1 = desc1->dsc_sub_type == isc_blob_text ? desc1->dsc_blob_ttype() : ttype_none;
}
Collation* obj = INTL_texttype_lookup(tdbb, type1);
CharSet* charset = obj->getCharSet();
VaryStr<TEMP_STR_LENGTH> escapeTemp;
const UCHAR* escapeStr = nullptr;
USHORT escapeLen = 0;
// Handle escape for LIKE and SIMILAR
if (blrOp == blr_like || blrOp == blr_similar)
{
// ensure 3rd argument (escape char) is in operation text type
if (arg3 && !computedInvariant)
{
// Convert ESCAPE to operation character set
dsc* desc = EVL_expr(tdbb, request, arg3);
if (!desc)
{
if (nodFlags & FLAG_INVARIANT)
{
impure_value* impure = request->getImpure<impure_value>(impureOffset);
impure->vlu_flags |= VLU_computed;
impure->vlu_flags |= VLU_null;
}
return TriState::empty();
}
escapeLen = MOV_make_string(tdbb, desc, type1,
reinterpret_cast<const char**>(&escapeStr), &escapeTemp, sizeof(escapeTemp));
if (!escapeLen || charset->length(escapeLen, escapeStr, true) != 1)
{
// If characters left, or null byte character, return error
ERR_post(Arg::Gds(isc_escape_invalid));
}
USHORT escape[2] = {0, 0};
charset->getConvToUnicode().convert(escapeLen, escapeStr, sizeof(escape), escape);
if (!escape[0])
{
// If or null byte character, return error
ERR_post(Arg::Gds(isc_escape_invalid));
}
}
}
UCHAR* patternStr = nullptr;
ULONG patternLen = 0;
MoveBuffer patternBuffer;
auto createMatcher = [&]()
{
return blrOp == blr_containing ? obj->createContainsMatcher(*tdbb->getDefaultPool(), patternStr, patternLen) :
blrOp == blr_starting ? obj->createStartsMatcher(*tdbb->getDefaultPool(), patternStr, patternLen) :
blrOp == blr_like ? obj->createLikeMatcher(*tdbb->getDefaultPool(),
patternStr, patternLen, escapeStr, escapeLen) :
blrOp == blr_similar ? obj->createSimilarToMatcher(tdbb, *tdbb->getDefaultPool(),
patternStr, patternLen, escapeStr, escapeLen) :
nullptr; // blr_matching
};
// Get address and length of search string - convert to datatype of data