/
rse.cpp
3784 lines (3263 loc) · 96.7 KB
/
rse.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: rse.cpp
* DESCRIPTION: Run time record fetching
*
* 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): ______________________________________.
*
* 2001.07.28: John Bellardo: Implemented rse_skip and made rse_first work with
* seekable streams.
* 2002.02.22 Claudio Valderrama: Fix SF Bugs #225283, #518279, #514186 & #221925.
*
* 2002.10.29 Sean Leyne - Removed obsolete "Netware" port
*
* 2002.10.30 Sean Leyne - Removed support for obsolete "PC_PLATFORM" define
*
*/
#include "firebird.h"
#include <errno.h>
#include "../jrd/common.h"
#include <stdio.h>
#include <string.h>
#include "memory_routines.h"
#include "../jrd/jrd.h"
#include "../jrd/req.h"
#include "../jrd/val.h"
#include "../jrd/exe.h"
#include "../jrd/rse.h"
#include "../jrd/sbm.h"
#include "../jrd/lls.h"
#include "../jrd/sort.h"
#include "../jrd/intl.h"
#include "../jrd/ods.h"
#include "../jrd/btr.h"
#include "../jrd/lck.h"
#include "../jrd/cch.h"
#include "../jrd/tra.h"
#include "gen/iberror.h"
#include "../jrd/gdsassert.h"
#include "../jrd/dpm_proto.h"
#include "../jrd/err_proto.h"
#include "../jrd/evl_proto.h"
#include "../jrd/exe_proto.h"
#include "../jrd/ext_proto.h"
#include "../jrd/gds_proto.h"
#include "../jrd/intl_proto.h"
#include "../jrd/jrd_proto.h"
#include "../jrd/met_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/nav_proto.h"
#include "../jrd/rlck_proto.h"
#include "../jrd/rse_proto.h"
#include "../jrd/sort_proto.h"
#include "../jrd/thd.h"
#include "../jrd/vio_proto.h"
#include "../jrd/VirtualTable.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#if defined(WIN_NT)
#include <io.h> // close
#endif
using namespace Jrd;
static void close_merge(thread_db*, RecordSource*, irsb_mrg*);
static void close_procedure(thread_db*, RecordSource*);
static SSHORT compare(thread_db*, jrd_nod*, jrd_nod*);
static SSHORT compare_longs(const SLONG*, const SLONG*, USHORT);
#ifdef SCROLLABLE_CURSORS
static bool fetch_record(thread_db*, RecordSource*, SSHORT, RSE_GET_MODE);
static bool get_merge_join(thread_db*, RecordSource*, irsb_mrg*, RSE_GET_MODE);
static bool get_merge_fetch(thread_db*, RecordSource*, SSHORT, RSE_GET_MODE);
static SLONG get_merge_record(thread_db*, RecordSource*, irsb_mrg::irsb_mrg_repeat *,
RSE_GET_MODE);
static UCHAR *get_sort(thread_db*, RecordSource*, RSE_GET_MODE);
static void resynch_merge(thread_db*, RecordSource*, irsb_mrg*, RSE_GET_MODE);
static void unget_sort(thread_db*, RecordSource*, UCHAR *);
#else
static bool fetch_record(thread_db*, RecordSource*, SSHORT);
static bool get_merge_join(thread_db*, RecordSource*, irsb_mrg*);
static bool get_merge_fetch(thread_db*, RecordSource*, SSHORT);
static SLONG get_merge_record(thread_db*, RecordSource*, irsb_mrg::irsb_mrg_repeat *);
static UCHAR *get_sort(thread_db*, RecordSource*);
#endif
#ifdef SCROLLABLE_CURSORS
static bool fetch_left(thread_db*, RecordSource*, IRSB, RSE_GET_MODE);
#else
static bool fetch_left(thread_db*, RecordSource*, IRSB);
#endif
static UCHAR* get_merge_data(thread_db*, merge_file*, SLONG);
static bool get_procedure(thread_db*, RecordSource*, irsb_procedure*, record_param*);
static bool get_record(thread_db*, RecordSource*, RecordSource*, RSE_GET_MODE);
static bool get_union(thread_db*, RecordSource*, IRSB);
static void invalidate_child_rpbs(thread_db*, RecordSource*);
static void join_to_nulls(thread_db*, RecordSource*, StreamStack*);
static void map_sort_data(jrd_req*, SortMap*, UCHAR *);
static void open_merge(thread_db*, RecordSource*, irsb_mrg*);
static void open_procedure(thread_db*, RecordSource*, irsb_procedure*);
static void open_sort(thread_db*, RecordSource*, irsb_sort*, UINT64);
static void proc_assignment(const dsc*, const dsc*, UCHAR*, dsc*, SSHORT, Record*);
static void pop_rpbs(jrd_req*, RecordSource*);
static void push_rpbs(thread_db*, jrd_req*, RecordSource*);
static ULONG read_merge_block(thread_db*, merge_file*, ULONG);
static bool reject(const UCHAR*, const UCHAR*, void*);
static void restore_record(record_param*);
static void save_record(thread_db*, record_param*);
static void write_merge_block(thread_db*, merge_file*, ULONG);
static const char* SCRATCH = "fb_merge_";
void RSE_close(thread_db* tdbb, RecordSource* rsb)
{
/**************************************
*
* R S E _ c l o s e
*
**************************************
*
* Functional description
*
**************************************/
SET_TDBB(tdbb);
invalidate_child_rpbs(tdbb, rsb);
while (true) {
irsb_sort* impure = (irsb_sort*) ((UCHAR *) tdbb->tdbb_request + rsb->rsb_impure);
if (!(impure->irsb_flags & irsb_open))
return;
impure->irsb_flags &= ~irsb_open;
switch (rsb->rsb_type) {
case rsb_indexed:
case rsb_navigate:
return;
case rsb_sequential:
{
jrd_req* request = tdbb->tdbb_request;
record_param* rpb = &request->req_rpb[rsb->rsb_stream];
if (rpb->getWindow(tdbb).win_flags & WIN_large_scan &&
rpb->rpb_relation->rel_scan_count)
{
--rpb->rpb_relation->rel_scan_count;
}
return;
}
case rsb_first:
case rsb_skip:
case rsb_boolean:
case rsb_aggregate:
rsb = rsb->rsb_next;
break;
case rsb_cross:
{
RecordSource** ptr = rsb->rsb_arg;
for (const RecordSource* const* const end = ptr + rsb->rsb_count;
ptr < end; ptr++)
{
RSE_close(tdbb, *ptr);
}
return;
}
case rsb_left_cross:
RSE_close(tdbb, rsb->rsb_arg[RSB_LEFT_outer]);
RSE_close(tdbb, rsb->rsb_arg[RSB_LEFT_inner]);
return;
case rsb_procedure:
close_procedure(tdbb, rsb);
return;
case rsb_merge:
close_merge(tdbb, rsb, (irsb_mrg*) impure);
return;
case rsb_sort:
SORT_fini(impure->irsb_sort_handle, tdbb->tdbb_attachment);
impure->irsb_sort_handle = NULL;
rsb = rsb->rsb_next;
break;
case rsb_union:
{
const USHORT i = ((IRSB) impure)->irsb_count;
if (i >= rsb->rsb_count)
return;
rsb = rsb->rsb_arg[i];
}
break;
case rsb_recurse:
RSBRecurse::close(tdbb, rsb, (irsb_recurse*)impure);
return;
case rsb_ext_sequential:
case rsb_ext_indexed:
case rsb_ext_dbkey:
EXT_close(rsb);
return;
case rsb_virt_sequential:
VirtualTable::close(tdbb, rsb);
return;
default:
BUGCHECK(166); /* msg 166 invalid rsb type */
}
}
}
bool RSE_get_record(thread_db* tdbb, RecordSource* rsb, RSE_GET_MODE mode)
{
/**************************************
*
* R S E _ g e t _ r e c o r d
*
**************************************
*
* Functional description
* External entrypoint for get_record().
* Handle the "stickiness" of directionality.
* Check whether we need to count records
* retrieved at the top level of the rsb tree.
*
**************************************/
SET_TDBB(tdbb);
jrd_req* request = tdbb->tdbb_request;
IRSB impure = (IRSB) ((UCHAR *) request + rsb->rsb_impure);
#ifdef SCROLLABLE_CURSORS
/* The mode RSE_get_next is a generic mode which requests that
we continue on in the last direction we were going. Oblige
by converting the mode to the appropriate direction. */
if (mode == RSE_get_next)
{
mode =
(impure->irsb_flags & irsb_last_backwards) ? RSE_get_backward :
RSE_get_forward;
}
request->req_flags &= ~req_fetch_required;
#endif
/* Turn off the flag so that records at a
lower level will not be counted. */
const bool count = (request->req_flags & req_count_records) != 0;
request->req_flags &= ~req_count_records;
bool result;
while ( (result = get_record(tdbb, rsb, NULL, mode)) )
{
if (rsb->rsb_flags & rsb_writelock)
{
// Lock record if we were asked for it
jrd_tra* transaction = request->req_transaction;
RecordSource* table_rsb = rsb;
// Skip nodes without streams
while (table_rsb->rsb_type == rsb_boolean ||
table_rsb->rsb_type == rsb_sort ||
table_rsb->rsb_type == rsb_first ||
table_rsb->rsb_type == rsb_skip)
{
table_rsb = table_rsb->rsb_next;
}
// Raise error if we cannot lock this kind of stream
if (table_rsb->rsb_type != rsb_sequential &&
table_rsb->rsb_type != rsb_indexed &&
table_rsb->rsb_type != rsb_navigate)
{
ERR_post(isc_record_lock_not_supp, 0);
}
record_param* org_rpb = request->req_rpb + table_rsb->rsb_stream;
jrd_rel* relation = org_rpb->rpb_relation;
// Raise error if we cannot lock this kind of stream
if (!relation ||
relation->rel_view_rse ||
relation->rel_file ||
relation->isVirtual())
{
ERR_post(isc_record_lock_not_supp, 0);
}
RLCK_reserve_relation(tdbb, transaction, relation, true, true);
// Fetch next record if current was deleted before being locked
if (!VIO_writelock(tdbb, org_rpb, rsb, transaction)) {
continue;
}
}
if (count) {
request->req_records_selected++;
request->req_records_affected.bumpFetched();
}
break;
}
/* reset the flag to whatever it was */
if (count)
request->req_flags |= req_count_records;
return result;
}
void RSE_open(thread_db* tdbb, RecordSource* rsb)
{
/**************************************
*
* R S E _ o p e n
*
**************************************
*
* Functional description
* Open a stream, as represented by the
* record source block (rsb).
*
**************************************/
SINT64 first_records = -1, skip_records = 0;
SET_TDBB(tdbb);
jrd_req* request = tdbb->tdbb_request;
// Initialize dependent invariants if any
if (rsb->rsb_invariants) {
SLONG *ptr, *end;
for (ptr = rsb->rsb_invariants->begin(), end = rsb->rsb_invariants->end();
ptr < end; ptr++)
{
reinterpret_cast<impure_value*>((SCHAR *) request + *ptr)->vlu_flags = 0;
}
}
while (true) {
irsb_index* impure = (irsb_index*) ((SCHAR *) request + rsb->rsb_impure);
impure->irsb_flags |= irsb_first | irsb_open;
impure->irsb_flags &=
~(irsb_singular_processed | irsb_checking_singular);
record_param* rpb = &request->req_rpb[rsb->rsb_stream];
rpb->getWindow(tdbb).win_flags = 0;
switch (rsb->rsb_type) {
case rsb_indexed:
impure->irsb_bitmap = EVL_bitmap(tdbb, (jrd_nod*) rsb->rsb_arg[0], NULL);
impure->irsb_prefetch_number = -1;
case rsb_navigate:
case rsb_sequential:
#ifdef SCROLLABLE_CURSORS
if (rsb->rsb_type == rsb_navigate) {
impure->irsb_flags |= irsb_bof;
impure->irsb_flags &= ~irsb_eof;
}
#endif
if (rsb->rsb_type == rsb_sequential) {
Database* dbb = tdbb->tdbb_database;
BufferControl* bcb = dbb->dbb_bcb;
/* Unless this is the only attachment, limit the cache flushing
effect of large sequential scans on the page working sets of
other attachments. */
Attachment* attachment = tdbb->tdbb_attachment;
if (attachment &&
(attachment != dbb->dbb_attachments
|| attachment->att_next))
{
/* If the relation has more data pages than the number of
pages in the buffer cache then mark the input window
block as a large scan so that a data page is released
to the LRU tail after its last record is fetched.
A database backup treats everything as a large scan
because the cumulative effect of scanning all relations
is equal to that of a single large relation. */
if (attachment->att_flags & ATT_gbak_attachment ||
DPM_data_pages(tdbb,
rpb->rpb_relation) > (SLONG) bcb->bcb_count)
{
rpb->getWindow(tdbb).win_flags = WIN_large_scan;
rpb->rpb_org_scans =
rpb->rpb_relation->rel_scan_count++;
}
}
}
RLCK_reserve_relation(tdbb, request->req_transaction,
rpb->rpb_relation, false, true);
rpb->rpb_number.setValue(BOF_NUMBER);
return;
case rsb_cross:
return;
case rsb_sort:
#ifdef SCROLLABLE_CURSORS
impure->irsb_flags |= irsb_bof;
impure->irsb_flags &= ~irsb_eof;
#endif
// dimitr: we can avoid reading and sorting the entire
// record set, if there's actually nothing to return
if (first_records) {
open_sort(tdbb, rsb, (irsb_sort*) impure,
(first_records < 0) ? 0 : (UINT64) first_records + skip_records);
}
else {
((irsb_sort*) impure)->irsb_sort_handle = NULL;
}
return;
case rsb_procedure:
open_procedure(tdbb, rsb, (irsb_procedure*) impure);
return;
case rsb_first:
{
const dsc* desc = EVL_expr(tdbb, (jrd_nod*) rsb->rsb_arg[0]);
first_records = (desc && !(request->req_flags & req_null)) ?
MOV_get_int64(desc, 0) : 0;
if (first_records < 0)
ERR_post(isc_bad_limit_param, 0);
((irsb_first_n*) impure)->irsb_count = first_records;
rsb = rsb->rsb_next;
}
break;
case rsb_skip:
{
const dsc* desc = EVL_expr(tdbb, (jrd_nod*) rsb->rsb_arg[0]);
skip_records = (desc && !(request->req_flags & req_null)) ?
MOV_get_int64(desc, 0) : 0;
if (skip_records < 0)
ERR_post(isc_bad_skip_param, 0);
((irsb_skip_n*) impure)->irsb_count = skip_records + 1;
rsb = rsb->rsb_next;
}
break;
case rsb_boolean:
rsb = rsb->rsb_next;
break;
case rsb_union:
{
((IRSB) impure)->irsb_count = 0;
VIO_record(tdbb, rpb, rsb->rsb_format, tdbb->getDefaultPool());
/* Initialize the record number of each stream in the union */
RecordSource** ptr = &rsb->rsb_arg[rsb->rsb_count];
for (const RecordSource* const* const end = ptr + (USHORT)(IPTR) *ptr;
++ptr <= end;)
{
request->req_rpb[(USHORT)(IPTR) *ptr].rpb_number.setValue(BOF_NUMBER);
}
rsb = rsb->rsb_arg[0];
}
break;
case rsb_recurse:
RSBRecurse::open(tdbb, rsb, (irsb_recurse*)impure);
return;
case rsb_aggregate:
((IRSB) impure)->irsb_count = 3;
VIO_record(tdbb, rpb, rsb->rsb_format, tdbb->getDefaultPool());
return;
case rsb_merge:
open_merge(tdbb, rsb, (irsb_mrg*) impure);
return;
case rsb_ext_sequential:
case rsb_ext_indexed:
case rsb_ext_dbkey:
EXT_open(rsb);
return;
case rsb_virt_sequential:
VirtualTable::open(tdbb, rsb);
return;
case rsb_left_cross:
{
RSE_open(tdbb, rsb->rsb_arg[RSB_LEFT_outer]);
impure->irsb_flags &=
~(irsb_first | irsb_in_opened | irsb_join_full);
impure->irsb_flags |= irsb_mustread;
/* Allocate a record block for each union/aggregate/procedure
stream in the right sub-stream. The block will be needed
if we join to nulls before opening the rsbs */
for (RsbStack::iterator stack(*(rsb->rsb_left_rsbs));
stack.hasData(); ++stack)
{
VIO_record(tdbb,
&request->req_rpb[stack.object()->rsb_stream],
stack.object()->rsb_format, tdbb->getDefaultPool());
}
return;
}
default:
BUGCHECK(166); /* msg 166 invalid rsb type */
}
}
}
static void close_merge(thread_db* tdbb, RecordSource* rsb, irsb_mrg* impure)
{
/**************************************
*
* c l o s e _ m e r g e
*
**************************************
*
* Functional description
* Close a merge stream by closing all substreams
* and cleaning up after any prior sort-merge retrieval.
*
**************************************/
SET_TDBB(tdbb);
/* do two simultaneous but unrelated things in one loop */
irsb_mrg::irsb_mrg_repeat* tail = impure->irsb_mrg_rpt;
RecordSource** ptr = rsb->rsb_arg;
for (const RecordSource* const* const end = ptr + rsb->rsb_count * 2;
ptr < end; ptr += 2, tail++)
{
/* close all the substreams for the sort-merge */
RSE_close(tdbb, *ptr);
/* Release memory associated with the merge file block
and the sort file block. Also delete the merge file
if one exists. */
merge_file* mfb = &tail->irsb_mrg_file;
sort_work_file* sfb = mfb->mfb_sfb;
if (sfb) {
delete sfb->sfb_space;
delete sfb;
mfb->mfb_sfb = 0;
}
if (mfb->mfb_block_data) {
delete[] mfb->mfb_block_data;
mfb->mfb_block_data = 0;
}
}
}
static void close_procedure(thread_db* tdbb, RecordSource* rsb)
{
/**************************************
*
* c l o s e _ p r o c e d u r e
*
**************************************
*
* Functional description
* Shut down a procedural view.
*
**************************************/
SET_TDBB(tdbb);
jrd_req* request = tdbb->tdbb_request;
irsb_procedure* impure = (irsb_procedure*) ((UCHAR *) request + rsb->rsb_impure);
jrd_req* proc_request = impure->irsb_req_handle;
if (proc_request) {
EXE_unwind(tdbb, proc_request);
proc_request->req_flags &= ~req_in_use;
impure->irsb_req_handle = 0;
proc_request->req_attachment = NULL;
}
if (impure->irsb_message) {
delete impure->irsb_message;
impure->irsb_message = NULL;
}
}
// It seems strange this function doesn't take two const nodes,
// but EVL_expr can change node1 and node2' args.
static SSHORT compare(thread_db* tdbb, jrd_nod* node1, jrd_nod* node2)
{
/**************************************
*
* c o m p a r e
*
**************************************
*
* Functional description
* Compare values of two sorts lists, return result. This is used for
* sort merge join.
*
**************************************/
SET_TDBB(tdbb);
jrd_req* request = tdbb->tdbb_request;
jrd_nod* const* ptr1 = node1->nod_arg;
jrd_nod* const* ptr2 = node2->nod_arg;
for (const jrd_nod* const* const end = ptr1 + node1->nod_count;
ptr1 < end; ptr1++, ptr2++)
{
const dsc* desc1 = EVL_expr(tdbb, *ptr1);
const ULONG flags = request->req_flags;
const dsc* desc2 = EVL_expr(tdbb, *ptr2);
if (flags & req_null) {
if (!(request->req_flags & req_null)) {
return -1;
}
else {
// AB: When both expression evaluated NULL then
// we return 0 ( (NULL = NULL) = true).
//
// Currently this (0 and higher) isn't used by the
// MERGE procedure, but when we allow MERGE to
// handle outer-joins we must not forget this one !!!
return 0;
}
}
else if (request->req_flags & req_null) {
return 1;
}
// AB: MOV_compare can't handle NULL parameters
// therefore check before passing all null flags.
const SSHORT result = MOV_compare(desc1, desc2);
if (result != 0) {
return result;
}
}
return 0;
}
static SSHORT compare_longs(const SLONG* p, const SLONG* q, USHORT count)
{
/**************************************
*
* c o m p a r e _ l o n g s
*
**************************************
*
* Functional description
* Compare two strings of SLONG words.
*
**************************************/
for (; count; p++, q++, --count)
if (*p > *q)
return 1;
else if (*p < *q)
return -1;
return 0;
}
static bool fetch_record(thread_db* tdbb, RecordSource* rsb, SSHORT n
#ifdef SCROLLABLE_CURSORS
, RSE_GET_MODE mode
#endif
)
{
/**************************************
*
* f e t c h _ r e c o r d
*
**************************************
*
* Functional description
* Get a record for a sub-stream of a cross. If the
* sub-stream is dry, close it and recurse to get the
* next record from the previous stream. If there isn't
* a previous stream, then the cross is finished, so
* return false.
*
**************************************/
SET_TDBB(tdbb);
RecordSource* sub_rsb = rsb->rsb_arg[n];
if (get_record(tdbb, sub_rsb, NULL
#ifdef SCROLLABLE_CURSORS
, mode
#else
, RSE_get_forward
#endif
))
{
return true;
}
/* we have exhausted this stream, so close it; if there is
another candidate record from the n-1 streams to the left,
then reopen the stream and start again from the beginning */
while (true)
{
RSE_close(tdbb, sub_rsb);
if (n == 0 || !fetch_record(tdbb, rsb, n - 1
#ifdef SCROLLABLE_CURSORS
, mode
#endif
))
{
return false;
}
RSE_open(tdbb, sub_rsb);
if (get_record(tdbb, sub_rsb, NULL
#ifdef SCROLLABLE_CURSORS
, mode
#else
, RSE_get_forward
#endif
))
{
return true;
}
}
}
#ifdef SCROLLABLE_CURSORS
static bool fetch_left(thread_db* tdbb, RecordSource* rsb, IRSB impure, RSE_GET_MODE mode)
{
/**************************************
*
* f e t c h _ l e f t ( I B _ V 4 _ 1 )
*
**************************************
*
* Functional description
* Get records for a left outer join. Records are read
* from the left sub-stream when the right sub-stream is
* dry or when it is not yet open. When the left
* sub-stream's boolean is true, open the right sub-stream
* and read a record. When the right sub-stream becomes dry,
* close it, and if nothing has been joined to the left
* sub-stream's current record, join a null valued right
* sub-stream record. When the left sub-stream is dry,
* the outer join is finished, so return false.
*
**************************************/
SET_TDBB(tdbb);
/* loop through the outer join in either the forward or the backward direction;
the various modes indicate what state of the join we are in */
while (true)
{
if (!(impure->irsb_flags & irsb_join_full))
{
/* mustread indicates to get the next record from the outer stream */
if (impure->irsb_flags & irsb_mustread)
{
if (!get_record(tdbb, rsb->rsb_arg[RSB_LEFT_outer], NULL, mode))
{
if (mode == RSE_get_backward)
return false;
else
{
if (!rsb->rsb_arg[RSB_LEFT_inner_streams])
return false;
}
/* We have a full outer join. Open up the inner stream
one more time. */
RSE_close(tdbb, rsb->rsb_arg[RSB_LEFT_outer]);
impure->irsb_flags |= irsb_join_full;
RSE_open(tdbb, rsb->rsb_arg[RSB_LEFT_inner]);
continue;
}
/* check if the outer record qualifies for the boolean */
if (rsb->rsb_arg[RSB_LEFT_boolean] &&
!EVL_boolean(tdbb, rsb->rsb_arg[RSB_LEFT_boolean]))
{
/* The boolean pertaining to the left sub-stream is false
so just join sub-stream to a null valued right sub-stream */
join_to_nulls(tdbb, rsb, rsb->rsb_left_streams);
return true;
}
impure->irsb_flags &= ~(irsb_mustread | irsb_joined);
impure->irsb_flags |= irsb_in_opened;
RSE_open(tdbb, rsb->rsb_arg[RSB_LEFT_inner]);
}
/* fetch records from the inner stream until exhausted */
while (get_record(tdbb, rsb->rsb_arg[RSB_LEFT_inner], NULL, mode))
{
if (!rsb->rsb_arg[RSB_LEFT_inner_boolean] ||
EVL_boolean(tdbb, rsb->rsb_arg[RSB_LEFT_inner_boolean]))
{
impure->irsb_flags |= irsb_joined;
return true;
}
}
/* out of inner records, go back to reading the next outer record */
RSE_close(tdbb, rsb->rsb_arg[RSB_LEFT_inner]);
impure->irsb_flags |= irsb_mustread;
/* The right stream did not have any matching records. Join
the left stream to a null valued right sub-stream */
if (!(impure->irsb_flags & irsb_joined)) {
join_to_nulls(tdbb, rsb, rsb->rsb_left_streams);
return true;
}
}
else
{
/* Continue with a full outer join. */
RecordSource* full = rsb->rsb_arg[RSB_LEFT_inner];
full = (full->rsb_type == rsb_boolean) ? full->rsb_next : full;
if (impure->irsb_flags & irsb_in_opened)
{
/* The inner stream was opened at some point. If it doesn't have a
boolean, then all of its records have been returned. Otherwise,
find the records that haven't been. */
bool found;
do {
if (!get_record(tdbb, full, NULL, mode)) {
if (mode == RSE_get_forward)
return false;
else
goto return_to_outer;
}
RSE_open(tdbb, rsb->rsb_arg[RSB_LEFT_outer]);
while (found =
get_record(tdbb, rsb->rsb_arg[RSB_LEFT_outer],
NULL, mode))
{
if (
(!rsb->rsb_arg[RSB_LEFT_boolean]
|| EVL_boolean(tdbb,
rsb->
rsb_arg[RSB_LEFT_boolean]))
&& (!rsb->rsb_arg[RSB_LEFT_inner_boolean]
|| EVL_boolean(tdbb,
rsb->rsb_arg
[RSB_LEFT_inner_boolean]))
&& (full == rsb->rsb_arg[RSB_LEFT_inner]
|| EVL_boolean(tdbb,
rsb->
rsb_arg
[RSB_LEFT_inner]->rsb_arg
[0])))
{
break;
}
}
RSE_close(tdbb, rsb->rsb_arg[RSB_LEFT_outer]);
} while (found);
}
else if (!get_record(tdbb, full, NULL, mode))
{
if (mode == RSE_get_forward)
return false;
else
goto return_to_outer;
}
join_to_nulls(tdbb, rsb, rsb->rsb_left_inner_streams);
return true;
return_to_outer:
impure->irsb_flags &= ~(irsb_join_full | irsb_in_opened);
impure->irsb_flags |= irsb_mustread;
RSE_close(tdbb, rsb->rsb_arg[RSB_LEFT_inner]);
RSE_close(tdbb, rsb->rsb_arg[RSB_LEFT_outer]);
RSE_open(tdbb, rsb->rsb_arg[RSB_LEFT_outer]);
}
}
/* NOTREACHED */
return true;
}
#else
static bool fetch_left(thread_db* tdbb, RecordSource* rsb, IRSB impure)
{
/**************************************
*
* f e t c h _ l e f t
*
**************************************
*
* Functional description
* Get records for a left outer join. Records are read
* from the left sub-stream when the right sub-stream is
* dry or when it is not yet open. When the left
* sub-stream's boolean is true, open the right sub-stream
* and read a record. When the right sub-stream becomes dry,
* close it, and if nothing has been joined to the left
* sub-stream's current record, join a null valued right
* sub-stream record. When the left sub-stream is dry,
* the outer join is finished, so return false.
*
**************************************/
SET_TDBB(tdbb);
if (!(impure->irsb_flags & irsb_join_full))
{
while (true)
{
if (impure->irsb_flags & irsb_mustread)
{
if (!get_record
(tdbb, rsb->rsb_arg[RSB_LEFT_outer], NULL,
RSE_get_forward))
{
if (rsb->rsb_left_inner_streams->isEmpty())
return false;
/* We have a full outer join. Open up the inner stream
one more time. */
RSE_close(tdbb, rsb->rsb_arg[RSB_LEFT_outer]);
impure->irsb_flags |= irsb_join_full;
RSE_open(tdbb, rsb->rsb_arg[RSB_LEFT_inner]);
break;
}
if (rsb->rsb_arg[RSB_LEFT_boolean] &&
!EVL_boolean(tdbb, (jrd_nod*) rsb->rsb_arg[RSB_LEFT_boolean]))
{
/* The boolean pertaining to the left sub-stream is false
so just join sub-stream to a null valued right sub-stream */
join_to_nulls(tdbb, rsb, rsb->rsb_left_streams);
return true;
}
impure->irsb_flags &= ~(irsb_mustread | irsb_joined);
impure->irsb_flags |= irsb_in_opened;
RSE_open(tdbb, rsb->rsb_arg[RSB_LEFT_inner]);
}
while (get_record(tdbb, rsb->rsb_arg[RSB_LEFT_inner], NULL,
RSE_get_forward))
{
if (!rsb->rsb_arg[RSB_LEFT_inner_boolean]
|| EVL_boolean(tdbb,
(jrd_nod*)
rsb->rsb_arg[RSB_LEFT_inner_boolean]))
{