summaryrefslogtreecommitdiffstats
path: root/storage/connect/blkfil.cpp
blob: 93ae5a5ef0c219976f51b7b9529dd28f2cc2e8e4 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
/************* BlkFil C++ Program Source Code File (.CPP) **************/
/* PROGRAM NAME: BLKFIL                                                */
/* -------------                                                       */
/*  Version 2.6                                                        */
/*                                                                     */
/* COPYRIGHT:                                                          */
/* ----------                                                          */
/*  (C) Copyright to the author Olivier BERTRAND          2004-2017    */
/*                                                                     */
/* WHAT THIS PROGRAM DOES:                                             */
/* -----------------------                                             */
/*  This program is the implementation of block indexing classes.      */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/*  Include relevant MariaDB header file.                              */
/***********************************************************************/
#include "my_global.h"
#include "sql_class.h"
//#include "sql_time.h"

#if defined(_WIN32)
//#include <windows.h>
#else   // !_WIN32
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif  // !_WIN32

/***********************************************************************/
/*  Include application header files:                                  */
/***********************************************************************/
#include "global.h"      // global declarations
#include "plgdbsem.h"    // DB application declarations
#include "xindex.h"      // Key Index class declarations
#include "filamtxt.h"    // File access method dcls
#include "tabdos.h"      // TDBDOS and DOSCOL class dcls
#include "array.h"       // ARRAY classes dcls
#include "blkfil.h"      // Block Filter classes dcls

/* ------------------------ Class BLOCKFILTER ------------------------ */

/***********************************************************************/
/*  BLOCKFILTER constructor.                                           */
/***********************************************************************/
BLOCKFILTER::BLOCKFILTER(PTDBDOS tdbp, int op)
  {
  Tdbp = tdbp;
  Correl = FALSE;
  Opc = op;
  Opm = 0;
  Result = 0;
  } // end of BLOCKFILTER constructor

/***********************************************************************/
/*  Make file output of BLOCKFILTER contents.                          */
/***********************************************************************/
void BLOCKFILTER::Printf(PGLOBAL, FILE *f, uint n)
  {
  char m[64];

  memset(m, ' ', n);                    // Make margin string
  m[n] = '\0';

  fprintf(f, "%sBLOCKFILTER: at %p opc=%d opm=%d result=%d\n",
          m, this, Opc, Opm, Result);
  } // end of Printf

/***********************************************************************/
/*  Make string output of BLOCKFILTER contents.                        */
/***********************************************************************/
void BLOCKFILTER::Prints(PGLOBAL, char *ps, uint z)
  {
  strncat(ps, "BlockFilter(s)", z);
  } // end of Prints


/* ---------------------- Class BLKFILLOG ---------------------------- */

/***********************************************************************/
/*  BLKFILLOG constructor.                                             */
/***********************************************************************/
BLKFILLOG::BLKFILLOG(PTDBDOS tdbp, int op, PBF *bfp, int n)
         : BLOCKFILTER(tdbp, op)
  {
  N = n;
  Fil = bfp;

  for (int i = 0; i < N; i++)
    if (Fil[i])
      Correl |= Fil[i]->Correl;

  } // end of BLKFILLOG constructor

/***********************************************************************/
/*  Reset: this function is used only to check the existence of a      */
/*  BLKFILIN block and have it reset its Bot value for sorted columns. */
/***********************************************************************/
void BLKFILLOG::Reset(PGLOBAL g)
  {
  for (int i = 0; i < N; i++)
    if (Fil[i])
      Fil[i]->Reset(g);

  } // end of Reset

/***********************************************************************/
/*  This function is used for block filter evaluation. We use here a   */
/*  fuzzy logic between the values returned by evaluation blocks:      */
/* -2: the condition will be always false for the rest of the file.    */
/* -1: the condition will be false for the whole group.                */
/*  0: the condition may be true for some of the group values.         */
/*  1: the condition will be true for the whole group.                 */
/*  2: the condition will be always true for the rest of the file.     */
/***********************************************************************/
int BLKFILLOG::BlockEval(PGLOBAL g)
  {
  int  i, rc;

  for (i = 0; i < N; i++) {
    // 0: Means some block filter value may be True
    rc = (Fil[i]) ? Fil[i]->BlockEval(g) : 0;

    if (!i)
      Result = (Opc == OP_NOT) ? -rc : rc;
    else switch (Opc) {
      case OP_AND:
        Result = MY_MIN(Result, rc);
        break;
      case OP_OR:
        Result = MY_MAX(Result, rc);
        break;
      default:
        // Should never happen
        Result = 0;
        return Result;
      } // endswitch Opc

    } // endfor i

  return Result;
  } // end of BlockEval

/* ---------------------- Class BLKFILARI----------------------------- */

/***********************************************************************/
/*  BLKFILARI constructor.                                             */
/***********************************************************************/
BLKFILARI::BLKFILARI(PGLOBAL g, PTDBDOS tdbp, int op, PXOB *xp)
         : BLOCKFILTER(tdbp, op)
  {
  Colp = (PDOSCOL)xp[0];

  if (xp[1]->GetType() == TYPE_COLBLK) {
    Cpx = (PCOL)xp[1];      // Subquery pseudo constant column
    Correl = TRUE;
  } else
    Cpx = NULL;

  Sorted = Colp->IsSorted() > 0;

  // Don't remember why this was changed. Anyway it is no good for
  // correlated subqueries because the Value must reflect changes
  if (Cpx)
    Valp = xp[1]->GetValue();
  else
    Valp = AllocateValue(g, xp[1]->GetValue());

  } // end of BLKFILARI constructor

/***********************************************************************/
/*  Reset: re-eval the constant value in the case of pseudo constant   */
/*  column use in a correlated subquery.                               */
/***********************************************************************/
void BLKFILARI::Reset(PGLOBAL g)
  {
  if (Cpx) {
    Cpx->Reset();
    Cpx->Eval(g);
    MakeValueBitmap();      // Does nothing for class BLKFILARI
    } // endif Cpx

  } // end of Reset

/***********************************************************************/
/*  Evaluate block filter for arithmetic operators.                    */
/***********************************************************************/
int BLKFILARI::BlockEval(PGLOBAL)
  {
  int mincmp, maxcmp, n;

#if defined(_DEBUG)
  assert (Colp->IsClustered());
#endif

  n = ((PTDBDOS)Colp->GetTo_Tdb())->GetCurBlk();
  mincmp = Colp->GetMin()->CompVal(Valp, n);
  maxcmp = Colp->GetMax()->CompVal(Valp, n);

  switch (Opc) {
    case OP_EQ:
    case OP_NE:
      if (mincmp < 0)                // Means minval > Val
        Result = (Sorted) ? -2 : -1;
      else if (maxcmp > 0)           // Means maxval < Val
        Result = -1;
      else if (!mincmp && !maxcmp)   // minval = maxval = val
        Result = 1;
      else
        Result = 0;

      break;
    case OP_GT:
    case OP_LE:
      if (mincmp < 0)                // minval > Val
        Result = (Sorted) ? 2 : 1;
      else if (maxcmp < 0)           // maxval > Val
        Result = 0;
      else                           // maxval <= Val
        Result = -1;

      break;
    case OP_GE:
    case OP_LT:
      if (mincmp <= 0)               // minval >= Val
        Result = (Sorted) ? 2 : 1;
      else if (maxcmp <= 0)          // Maxval >= Val
        Result = 0;
      else                           // Maxval < Val
        Result = -1;

      break;
    } // endswitch Opc

  switch (Opc) {
    case OP_NE:
    case OP_LE:
    case OP_LT:
      Result = -Result;
      break;
    } // endswitch Opc

  if (trace(1))
    htrc("BlockEval: op=%d n=%d rc=%d\n", Opc, n, Result);

  return Result;
  } // end of BlockEval

/* ---------------------- Class BLKFILAR2----------------------------- */

/***********************************************************************/
/*  BLKFILAR2 constructor.                                             */
/***********************************************************************/
BLKFILAR2::BLKFILAR2(PGLOBAL g, PTDBDOS tdbp, int op, PXOB *xp)
         : BLKFILARI(g, tdbp, op, xp)
  {
  MakeValueBitmap();
  } // end of BLKFILAR2 constructor

/***********************************************************************/
/*  MakeValueBitmap: Set the constant value bit map. It can be void    */
/*  if the constant value is not in the column distinct values list.   */
/***********************************************************************/
void BLKFILAR2::MakeValueBitmap(void)
  {
  int   i; // ndv = Colp->GetNdv();
  bool  found = FALSE;
  PVBLK dval = Colp->GetDval();

  assert(dval);

  /*********************************************************************/
  /*  Here we cannot use Find because we must get the index            */
  /*  of where to put the value if it is not found in the array.       */
  /*  This is needed by operators other than OP_EQ or OP_NE.           */
  /*********************************************************************/
  found = dval->Locate(Valp, i);

  /*********************************************************************/
  /*  Set the constant value bitmap. The bitmaps are really matching   */
  /*  the OP_EQ, OP_LE, and OP_LT operator but are also used for the   */
  /*  other operators for which the Result will be inverted.           */
  /*  The reason the bitmaps are not directly complemented for them is */
  /*  to be able to test easily the cases of sorted columns with Bxp,  */
  /*  and the case of a void bitmap, which happens if the constant     */
  /*  value is not in the column distinct values list.                 */
  /*********************************************************************/
  if (found) {
    Bmp = 1 << i;               // Bit of the found value
    Bxp = Bmp - 1;              // All smaller values

    if (Opc != OP_LT && Opc != OP_GE)
      Bxp |= Bmp;               // Found value must be included

  } else {
    Bmp = 0;
    Bxp = (1 << i) - 1;
  } // endif found

  if (!(Opc == OP_EQ || Opc == OP_NE))
    Bmp = Bxp;

  } // end of MakeValueBitmap

/***********************************************************************/
/*  Evaluate XDB2 block filter for arithmetic operators.               */
/***********************************************************************/
int BLKFILAR2::BlockEval(PGLOBAL)
  {
#if defined(_DEBUG)
  assert (Colp->IsClustered());
#endif

  int   n = ((PTDBDOS)Colp->GetTo_Tdb())->GetCurBlk();
  uint  bkmp = *(uint*)Colp->GetBmap()->GetValPtr(n);
  uint  bres = Bmp & bkmp;

  // Set result as if Opc were OP_EQ, OP_LT, or OP_LE
  if (!bres) {
    if (!Bmp)
      Result = -2;              // No good block in the table file
    else if (!Sorted)
      Result = -1;              // No good values in this block
    else    // Sorted column, test for no more good blocks in file
      Result = (Bxp & bkmp) ? -1 : -2;

  } else
    // Test whether all block values are good or only some ones
    Result = (bres == bkmp) ? 1 : 0;

  // For OP_NE, OP_GE, and OP_GT the result must be inverted.
  switch (Opc) {
    case OP_NE:
    case OP_GE:
    case OP_GT:
      Result = -Result;
      break;
    } // endswitch Opc

  if (trace(1))
    htrc("BlockEval2: op=%d n=%d rc=%d\n", Opc, n, Result);

  return Result;
  } // end of BlockEval

/* ---------------------- Class BLKFILMR2----------------------------- */

/***********************************************************************/
/*  BLKFILMR2 constructor.                                             */
/***********************************************************************/
BLKFILMR2::BLKFILMR2(PGLOBAL g, PTDBDOS tdbp, int op, PXOB *xp)
         : BLKFILARI(g, tdbp, op, xp)
  {
  Nbm = Colp->GetNbm();
  Bmp = (uint*)PlugSubAlloc(g, NULL, Nbm * sizeof(uint));
  Bxp = (uint*)PlugSubAlloc(g, NULL, Nbm * sizeof(uint));
  MakeValueBitmap();
  } // end of BLKFILMR2 constructor

/***********************************************************************/
/*  MakeValueBitmap: Set the constant value bit map. It can be void    */
/*  if the constant value is not in the column distinct values list.   */
/***********************************************************************/
void BLKFILMR2::MakeValueBitmap(void)
  {
  int   i; // ndv = Colp->GetNdv();
  bool  found = FALSE, noteq = !(Opc == OP_EQ || Opc == OP_NE);
  PVBLK dval = Colp->GetDval();

  assert(dval);

  for (i = 0; i < Nbm; i++)
    Bmp[i] = Bxp[i] = 0;

  /*********************************************************************/
  /*  Here we cannot use Find because we must get the index            */
  /*  of where to put the value if it is not found in the array.       */
  /*  This is needed by operators other than OP_EQ or OP_NE.           */
  /*********************************************************************/
  found = dval->Locate(Valp, i);

  /*********************************************************************/
  /*  For bitmaps larger than a ULONG, we must know where Bmp and Bxp  */
  /*  are positioned in the ULONG bit map block array.                 */
  /*********************************************************************/
  N = i / MAXBMP;
  i %= MAXBMP;

  /*********************************************************************/
  /*  Set the constant value bitmaps. The bitmaps are really matching  */
  /*  the OP_EQ, OP_LE, and OP_LT operator but are also used for the   */
  /*  other operators for which the Result will be inverted.           */
  /*  The reason the bitmaps are not directly complemented for them is */
  /*  to be able to easily test the cases of sorted columns with Bxp,  */
  /*  and the case of a void bitmap, which happens if the constant     */
  /*  value is not in the column distinct values list.                 */
  /*********************************************************************/
  if (found) {
    Bmp[N] = 1 << i;
    Bxp[N] = Bmp[N] - 1;

    if (Opc != OP_LT && Opc != OP_GE)
      Bxp[N] |= Bmp[N];    // Found value must be included

  } else
    Bxp[N] = (1 << i) - 1;

  if (noteq)
    Bmp[N] = Bxp[N];

  Void = !Bmp[N];          // There are no good values in the file

  for (i = 0; i < N; i++) {
    Bxp[i] = ~0;

    if (noteq)
      Bmp[i] = Bxp[i];

    Void = Void && !Bmp[i];
    } // endfor i

  if (!Bmp[N] && !Bxp[N])
    N--;

  } // end of MakeValueBitmap

/***********************************************************************/
/*  Evaluate XDB2 block filter for arithmetic operators.               */
/***********************************************************************/
int BLKFILMR2::BlockEval(PGLOBAL)
  {
#if defined(_DEBUG)
  assert (Colp->IsClustered());
#endif

  int    i, n = ((PTDBDOS)Colp->GetTo_Tdb())->GetCurBlk();
  bool   fnd = FALSE, all = TRUE, gt = TRUE;
  uint   bres;
  uint  *bkmp = (uint*)Colp->GetBmap()->GetValPtr(n * Nbm);

  // Set result as if Opc were OP_EQ, OP_LT, or OP_LE
  for (i = 0; i < Nbm; i++)
    if (i <= N) {
      if ((bres = Bmp[i] & bkmp[i]))
        fnd = TRUE;         // Some good value(s) found in the block

      if (bres != bkmp[i])
        all = FALSE;        // Not all block values are good

      if (Bxp[i] & bkmp[i])
        gt = FALSE;         // Not all block values are > good value(s)

    } else if (bkmp[i]) {
      all = FALSE;
      break;
    } // endif's

  if (!fnd) {
    if (Void || (gt && Sorted))
      Result = -2;          // No (more) good block in file
    else
      Result = -1;          // No good values in this block

  } else
    Result = (all) ? 1 : 0;                 // All block values are good

  // For OP_NE, OP_GE, and OP_GT the result must be inverted.
  switch (Opc) {
    case OP_NE:
    case OP_GE:
    case OP_GT:
      Result = -Result;
      break;
    } // endswitch Opc

  if (trace(1))
    htrc("BlockEval2: op=%d n=%d rc=%d\n", Opc, n, Result);

  return Result;
  } // end of BlockEval

/***********************************************************************/
/*  BLKSPCARI constructor.                                             */
/***********************************************************************/
BLKSPCARI::BLKSPCARI(PTDBDOS tdbp, int op, PXOB *xp, int bsize)
         : BLOCKFILTER(tdbp, op)
  {
  if (xp[1]->GetType() == TYPE_COLBLK) {
    Cpx = (PCOL)xp[1];      // Subquery pseudo constant column
    Correl = TRUE;
  } else
    Cpx = NULL;

  Valp = xp[1]->GetValue();
  Val = (int)xp[1]->GetValue()->GetIntValue();
  Bsize = bsize;
  } // end of BLKFILARI constructor

/***********************************************************************/
/*  Reset: re-eval the constant value in the case of pseudo constant   */
/*  column use in a correlated subquery.                               */
/***********************************************************************/
void BLKSPCARI::Reset(PGLOBAL g)
  {
  if (Cpx) {
    Cpx->Reset();
    Cpx->Eval(g);
    Val = (int)Valp->GetIntValue();
    } // endif Cpx

  } // end of Reset

/***********************************************************************/
/*  Evaluate block filter for arithmetic operators (ROWID)             */
/***********************************************************************/
int BLKSPCARI::BlockEval(PGLOBAL)
  {
  int mincmp, maxcmp, n, m;

  n = Tdbp->GetCurBlk();
  m = n * Bsize + 1;     // Minimum Rowid value for this block
  mincmp = (Val > m) ? 1 : (Val < m) ? (-1) : 0;
  m = (n + 1) * Bsize;   // Maximum Rowid value for this block
  maxcmp = (Val > m) ? 1 : (Val < m) ? (-1) : 0;

  switch (Opc) {
    case OP_EQ:
    case OP_NE:
      if (mincmp < 0)                // Means minval > Val
        Result = -2;                 // Always sorted
      else if (maxcmp > 0)           // Means maxval < Val
        Result = -1;
      else if (!mincmp && !maxcmp)   // minval = maxval = val
        Result = 1;
      else
        Result = 0;

      break;
    case OP_GT:
    case OP_LE:
      if (mincmp < 0)                // minval > Val
        Result = 2;                  // Always sorted
      else if (maxcmp < 0)           // maxval > Val
        Result = 0;
      else                           // maxval <= Val
        Result = -1;

      break;
    case OP_GE:
    case OP_LT:
      if (mincmp <= 0)               // minval >= Val
        Result = 2;                  // Always sorted
      else if (maxcmp <= 0)          // Maxval >= Val
        Result = 0;
      else                           // Maxval < Val
        Result = -1;

      break;
    } // endswitch Opc

  switch (Opc) {
    case OP_NE:
    case OP_LE:
    case OP_LT:
      Result = -Result;
      break;
    } // endswitch Opc

  if (trace(1))
    htrc("BlockEval: op=%d n=%d rc=%d\n", Opc, n, Result);

  return Result;
  } // end of BlockEval

/* ------------------------ Class BLKFILIN --------------------------- */

/***********************************************************************/
/*  BLKFILIN constructor.                                              */
/***********************************************************************/
BLKFILIN::BLKFILIN(PGLOBAL g, PTDBDOS tdbp, int op, int opm, PXOB *xp)
        : BLOCKFILTER(tdbp, op)
  {
  if (op == OP_IN) {
    Opc = OP_EQ;
    Opm = 1;
  } else {
    Opc = op;
    Opm = opm;
  } // endif op

  Colp = (PDOSCOL)xp[0];
  Arap = (PARRAY)xp[1];
  Type = Arap->GetResultType();

  if (Colp->GetResultType() != Type) {
    sprintf(g->Message, "BLKFILIN: %s", MSG(VALTYPE_NOMATCH));
		throw g->Message;
	} else if (Colp->GetValue()->IsCi())
    Arap->SetPrecision(g, 1);        // Case insensitive

  Sorted = Colp->IsSorted() > 0;
  } // end of BLKFILIN constructor

/***********************************************************************/
/*  Reset: have the sorted array reset its Bot value to -1 (bottom).   */
/***********************************************************************/
void BLKFILIN::Reset(PGLOBAL)
  {
  Arap->Reset();
//  MakeValueBitmap();      // Does nothing for class BLKFILIN
  } // end of Reset

/***********************************************************************/
/*  Evaluate block filter for a IN operator on a constant array.       */
/*  Note: here we need to use the GetValPtrEx function to get a zero   */
/*  ended string in case of string argument. This is because the ARRAY */
/*  can have a different width than the char column.                   */
/***********************************************************************/
int BLKFILIN::BlockEval(PGLOBAL g)
  {
  int   n = ((PTDBDOS)Colp->GetTo_Tdb())->GetCurBlk();
  void *minp = Colp->GetMin()->GetValPtrEx(n);
  void *maxp = Colp->GetMax()->GetValPtrEx(n);

  Result = Arap->BlockTest(g, Opc, Opm, minp, maxp, Sorted);
  return Result;
  } // end of BlockEval

/* ------------------------ Class BLKFILIN2 -------------------------- */

/***********************************************************************/
/*  BLKFILIN2 constructor.                                             */
/*  New version that takes care of all operators and modificators.     */
/*  It is also ready to handle the case of correlated sub-selects.     */
/***********************************************************************/
BLKFILIN2::BLKFILIN2(PGLOBAL g, PTDBDOS tdbp, int op, int opm, PXOB *xp)
         : BLKFILIN(g, tdbp, op, opm, xp)
  {
  Nbm = Colp->GetNbm();
  Valp = AllocateValue(g, Colp->GetValue());
  Invert = (Opc == OP_NE || Opc == OP_GE || Opc ==OP_GT);
  Bmp = (uint*)PlugSubAlloc(g, NULL, Nbm * sizeof(uint));
  Bxp = (uint*)PlugSubAlloc(g, NULL, Nbm * sizeof(uint));
  MakeValueBitmap();
  } // end of BLKFILIN2 constructor

/***********************************************************************/
/*  MakeValueBitmap: Set the constant values bit map. It can be void   */
/*  if the constant values are not in the column distinct values list. */
/*  The bitmaps are prepared for the EQ, LE, and LT operators and      */
/*  takes care of the ALL and ANY modificators. If the operators are   */
/*  NE, GE, or GT the modificator is inverted and the result will be.  */
/***********************************************************************/
void BLKFILIN2::MakeValueBitmap(void)
  {
  int   i, k, n, ndv = Colp->GetNdv();
  bool  found, noteq = !(Opc == OP_EQ || Opc == OP_NE);
  bool  all = (!Invert) ? (Opm == 2) : (Opm != 2);
  uint  btp;
  PVBLK dval = Colp->GetDval();

  N = -1;

  // Take care of special cases
  if (!(n = Arap->GetNval())) {
    // Return TRUE for ALL because it means that there are no item that
    // does not verify the condition, which is true indeed.
    // Return FALSE for ANY because TRUE means that there is at least
    // one item that verifies the condition, which is false.
    Result = (Opm == 2) ? 2 : -2;
    return;
  } else if (!noteq && all && n > 1) {
    // An item cannot be equal to all different values
    // or an item is always unequal to any different values
    Result = (Opc == OP_EQ) ? -2 : 2;
    return;
  } // endif's

  for (i = 0; i < Nbm; i++)
    Bmp[i] = Bxp[i] = 0;

  for (k = 0; k < n; k++) {
    Arap->GetNthValue(Valp, k);
    found = dval->Locate(Valp, i);
    N = i / MAXBMP;
    btp = 1 << (i % MAXBMP);

    if (found)
      Bmp[N] |= btp;

    // For LT and LE if ALL the condition applies to the smallest item
    // if ANY it applies to the largest item. In the case of EQ we come
    // here only if ANY or if n == 1, so it does applies to the largest.
    if ((!k && all) || (k == n - 1 && !all)) {
      Bxp[N] = btp - 1;

      if (found && Opc != OP_LT && Opc != OP_GE)
        Bxp[N] |= btp;     // Found value must be included

      } // endif k, opm

    } // endfor k

  if (noteq)
    Bmp[N] = Bxp[N];

  Void = !Bmp[N];          // There are no good values in the file

  for (i = 0; i < N; i++) {
    Bxp[i] = ~0;

    if (noteq) {
      Bmp[i] = Bxp[i];
      Void = FALSE;
      } // endif noteq

    } // endfor i

  if (!Bmp[N] && !Bxp[N]) {
    if (--N < 0)
      // All array values are smaller than block values
      Result = (Invert) ? 2 : -2;

  } else if (N == Nbm - 1 && (signed)Bmp[N] == (1 << (ndv % MAXBMP)) - 1) {
    // Condition will be always TRUE or FALSE for the whole file
    Result = (Invert) ? -2 : 2;
    N = -1;
  } // endif's

  } // end of MakeValueBitmap

/***********************************************************************/
/*  Evaluate block filter for set operators on a constant array.       */
/*  Note: here we need to use the GetValPtrEx function to get a zero   */
/*  ended string in case of string argument. This is because the ARRAY */
/*  can have a different width than the char column.                   */
/***********************************************************************/
int BLKFILIN2::BlockEval(PGLOBAL)
  {
  if (N < 0)
    return Result;                  // Was set in MakeValueBitmap

  int    i, n = ((PTDBDOS)Colp->GetTo_Tdb())->GetCurBlk();
  bool   fnd = FALSE, all = TRUE, gt = TRUE;
  uint   bres;
  uint  *bkmp = (uint*)Colp->GetBmap()->GetValPtr(n * Nbm);

  // Set result as if Opc were OP_EQ, OP_LT, or OP_LE
  // The difference between ALL or ANY was handled in MakeValueBitmap
  for (i = 0; i < Nbm; i++)
    if (i <= N) {
      if ((bres = Bmp[i] & bkmp[i]))
        fnd = TRUE;

      if (bres != bkmp[i])
        all = FALSE;

      if (Bxp[i] & bkmp[i])
        gt = FALSE;

    } else if (bkmp[i]) {
      all = FALSE;
      break;
    } // endif's

  if (!fnd) {
    if (Void || (Sorted && gt))
      Result = -2;              // No more good block in file
    else
      Result = -1;              // No good values in this block

  } else if (all)
    Result = 1;                 // All block values are good
  else
    Result = 0;                 // Block contains some good values

  // For OP_NE, OP_GE, and OP_GT the result must be inverted.
  switch (Opc) {
    case OP_NE:
    case OP_GE:
    case OP_GT:
      Result = -Result;
      break;
    } // endswitch Opc

  return Result;
  } // end of BlockEval

#if 0
/***********************************************************************/
/*  BLKFILIN2 constructor.                                             */
/***********************************************************************/
BLKFILIN2::BLKFILIN2(PGLOBAL g, PTDBDOS tdbp, int op, int opm, PXOB *xp)
         : BLKFILIN(g, tdbp, op, opm, xp)
  {
  // Currently, bitmap matching is only implemented for the IN operator
  if (!(Bitmap = (op == OP_IN || (op == OP_EQ && opm != 2)))) {
    Nbm = Colp->GetNbm();
    N = 0;
    return;           // Revert to standard minmax method
    } // endif minmax

  int   i, n;
  ULONG btp;
  PVAL  valp = AllocateValue(g, Colp->GetValue());
  PVBLK dval = Colp->GetDval();

  Nbm = Colp->GetNbm();
  N = -1;
  Bmp = (PULONG)PlugSubAlloc(g, NULL, Nbm * sizeof(ULONG));
  Bxp = (PULONG)PlugSubAlloc(g, NULL, Nbm * sizeof(ULONG));

  for (i = 0; i < Nbm; i++)
    Bmp[i] = Bxp[i] = 0;

  for (n = 0; n < Arap->GetNval(); n++) {
    Arap->GetNthValue(valp, n);

    if ((i = dval->Find(valp)) >= 0)
      Bmp[i / MAXBMP] |= 1 << (i % MAXBMP);

    } // endfor n

  for (i = Nbm - 1; i >= 0; i--)
    if (Bmp[i]) {
      for (btp = Bmp[i]; btp; btp >>= 1)
        Bxp[i] |= btp;

      for (N = i--; i >= 0; i--)
        Bxp[i] = ~0;

      break;
      } // endif Bmp

  } // end of BLKFILIN2 constructor

/***********************************************************************/
/*  Evaluate block filter for a IN operator on a constant array.       */
/*  Note: here we need to use the GetValPtrEx function to get a zero   */
/*  ended string in case of string argument. This is because the ARRAY */
/*  can have a different width than the char column.                   */
/***********************************************************************/
int BLKFILIN2::BlockEval(PGLOBAL g)
  {
  if (N < 0)
    return -2;                  // IN list contains no good values

  int    i, n = ((PTDBDOS)Colp->GetTo_Tdb())->GetCurBlk();
  bool   fnd = FALSE, all = TRUE, gt = TRUE;
  ULONG  bres;
  PULONG bkmp = (PULONG)Colp->GetBmap()->GetValPtr(n * Nbm);

  if (Bitmap) {
    // For IN operator use the bitmap method
    for (i = 0; i < Nbm; i++)
      if (i <= N) {
        if ((bres = Bmp[i] & bkmp[i]))
          fnd = TRUE;

        if (bres != bkmp[i])
          all = FALSE;

        if (Bxp[i] & bkmp[i])
          gt = FALSE;

      } else if (bkmp[i]) {
        all = FALSE;
        break;
      } // endif's

    if (!fnd) {
      if (Sorted && gt)
        Result = -2;              // No more good block in file
      else
        Result = -1;              // No good values in this block

    } else if (all)
      Result = 1;                 // All block values are good
    else
      Result = 0;                 // Block contains some good values

  } else {
    // For other than IN operators, revert to standard minmax method
    int   n = 0, ndv = Colp->GetNdv();
    void *minp = NULL;
    void *maxp = NULL;
    ULONG btp;
    PVBLK dval = Colp->GetDval();

    for (i = 0; i < Nbm; i++)
      for (btp = 1; btp && n < ndv; btp <<= 1, n++)
        if (btp & bkmp[i]) {
          if (!minp)
            minp = dval->GetValPtrEx(n);

          maxp = dval->GetValPtrEx(n);
          } // endif btp

    Result = Arap->BlockTest(g, Opc, Opm, minp, maxp, Colp->IsSorted());
  } // endif Bitmap

  return Result;
  } // end of BlockEval
#endif // 0

/* ------------------------ Class BLKSPCIN --------------------------- */

/***********************************************************************/
/*  BLKSPCIN constructor.                                              */
/***********************************************************************/
BLKSPCIN::BLKSPCIN(PGLOBAL, PTDBDOS tdbp, int op, int opm,
                   PXOB *xp, int bsize)
        : BLOCKFILTER(tdbp, op)
  {
  if (op == OP_IN) {
    Opc = OP_EQ;
    Opm = 1;
  } else
    Opm = opm;

  Arap = (PARRAY)xp[1];
#if defined(_DEBUG)
  assert (Opm);
  assert (Arap->GetResultType() == TYPE_INT);
#endif
  Bsize = bsize;
  } // end of BLKSPCIN constructor

/***********************************************************************/
/*  Reset: have the sorted array reset its Bot value to -1 (bottom).   */
/***********************************************************************/
void BLKSPCIN::Reset(PGLOBAL)
  {
  Arap->Reset();
  } // end of Reset

/***********************************************************************/
/*  Evaluate block filter for a IN operator on a constant array.       */
/***********************************************************************/
int BLKSPCIN::BlockEval(PGLOBAL g)
  {
  int  n = Tdbp->GetCurBlk();
  int minrow = n * Bsize + 1;   // Minimum Rowid value for this block
  int maxrow = (n + 1) * Bsize; // Maximum Rowid value for this block

  Result = Arap->BlockTest(g, Opc, Opm, &minrow, &maxrow, TRUE);
  return Result;
  } // end of BlockEval

/* ------------------------------------------------------------------- */

#if 0
/***********************************************************************/
/*  Implementation of the BLOCKINDEX class.                            */
/***********************************************************************/
BLOCKINDEX::BLOCKINDEX(PBX nx, PDOSCOL cp, PKXBASE kp)
  {
  Next = nx;
  Tdbp = (cp) ? (PTDBDOS)cp->GetTo_Tdb() : NULL;
  Colp = cp;
  Kxp = kp;
  Type = (cp) ? cp->GetResultType() : TYPE_ERROR;
  Sorted = (cp) ? cp->IsSorted() > 0 : FALSE;
  Result = 0;
  } // end of BLOCKINDEX constructor

/***********************************************************************/
/*  Reset Bot and Top values of optimized Kindex blocks.               */
/***********************************************************************/
void BLOCKINDEX::Reset(void)
  {
  if (Next)
    Next->Reset();

  Kxp->Reset();
  } // end of Reset

/***********************************************************************/
/*  Evaluate block indexing test.                                      */
/***********************************************************************/
int BLOCKINDEX::BlockEval(PGLOBAL g)
  {
#if defined(_DEBUG)
  assert (Tdbp && Colp);
#endif
  int   n = Tdbp->GetCurBlk();
  void *minp = Colp->GetMin()->GetValPtr(n);
  void *maxp = Colp->GetMax()->GetValPtr(n);

  Result = Kxp->BlockTest(g, minp, maxp, Type, Sorted);
  return Result;
  } // end of BlockEval

/***********************************************************************/
/*  Make file output of BLOCKINDEX contents.                           */
/***********************************************************************/
void BLOCKINDEX::Printf(PGLOBAL g, FILE *f, UINT n)
  {
  char m[64];

  memset(m, ' ', n);                    // Make margin string
  m[n] = '\0';

  fprintf(f, "%sBLOCKINDEX: at %p next=%p col=%s kxp=%p result=%d\n",
    m, this, Next, (Colp) ? Colp->GetName() : "Rowid", Kxp, Result);

  if (Next)
    Next->Printf(g, f, n);

  } // end of Printf

/***********************************************************************/
/*  Make string output of BLOCKINDEX contents.                         */
/***********************************************************************/
void BLOCKINDEX::Prints(PGLOBAL g, char *ps, UINT z)
  {
  strncat(ps, "BlockIndex(es)", z);
  } // end of Prints

/* ------------------------------------------------------------------- */

/***********************************************************************/
/*  Implementation of the BLOCKINDX2 class.                            */
/***********************************************************************/
BLOCKINDX2::BLOCKINDX2(PBX nx, PDOSCOL cp, PKXBASE kp)
          : BLOCKINDEX(nx, cp, kp)
  {
  Nbm = Colp->GetNbm();
  Dval = Colp->GetDval();
  Bmap = Colp->GetBmap();
#if defined(_DEBUG)
  assert(Dval && Bmap);
#endif
  } // end of BLOCKINDX2 constructor

/***********************************************************************/
/*  Evaluate block indexing test.                                      */
/***********************************************************************/
int BLOCKINDX2::BlockEval(PGLOBAL g)
  {
  int   n = Tdbp->GetCurBlk();
  PUINT bmp = (PUINT)Bmap->GetValPtr(n * Nbm);

  Result = Kxp->BlockTst2(g, Dval, bmp, Nbm, Type, Sorted);
  return Result;
  } // end of BlockEval

/* ------------------------------------------------------------------- */

/***********************************************************************/
/*  Implementation of the BLKSPCINDX class.                            */
/***********************************************************************/
BLKSPCINDX::BLKSPCINDX(PBX nx, PTDBDOS tp, PKXBASE kp, int bsize)
          : BLOCKINDEX(nx, NULL, kp)
  {
  Tdbp = tp;
  Bsize = bsize;
  Type = TYPE_INT;
  Sorted = TRUE;
  } // end of BLKSPCINDX constructor

/***********************************************************************/
/*  Evaluate block indexing test.                                      */
/***********************************************************************/
int BLKSPCINDX::BlockEval(PGLOBAL g)
  {
  int  n = Tdbp->GetCurBlk();
  int minrow = n * Bsize + 1;   // Minimum Rowid value for this block
  int maxrow = (n + 1) * Bsize; // Maximum Rowid value for this block

  Result = Kxp->BlockTest(g, &minrow, &maxrow, TYPE_INT, TRUE);
  return Result;
  } // end of BlockEval
#endif // 0