forked from manticoresoftware/manticoresearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secondaryindex.cpp
1470 lines (1172 loc) · 43 KB
/
secondaryindex.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
//
//
// Copyright (c) 2018-2023, Manticore Software LTD (https://manticoresearch.com)
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org/
//
#include "secondaryindex.h"
#include <algorithm>
#include "histogram.h"
#include "sphinxint.h"
#include "killlist.h"
#include "attribute.h"
#include "columnarfilter.h"
#include <queue>
#include "util/util.h"
#include "secondarylib.h"
//////////////////////////////////////////////////////////////////////////
bool ReturnIteratorResult ( RowID_t * pRowID, RowID_t * pRowIdStart, RowIdBlock_t & dRowIdBlock )
{
if ( pRowID==pRowIdStart )
return false;
dRowIdBlock = RowIdBlock_t(pRowIdStart, pRowID-pRowIdStart);
return true;
}
//////////////////////////////////////////////////////////////////////////
class SecondaryIndexIterator_c : public RowidIterator_i
{
protected:
static const int MAX_COLLECTED = 1024;
CSphFixedVector<RowID_t> m_dCollected {MAX_COLLECTED};
};
//////////////////////////////////////////////////////////////////////////
static RowIdBlock_t DoRowIdFiltering ( const RowIdBlock_t & dRowIdBlock, const RowIdBoundaries_t & tBoundaries, CSphVector<RowID_t> & dCollected )
{
RowID_t tMinSpanRowID = dRowIdBlock.First();
RowID_t tMaxSpanRowID = dRowIdBlock.Last();
if ( tMaxSpanRowID < tBoundaries.m_tMinRowID || tMinSpanRowID > tBoundaries.m_tMaxRowID )
return {};
dCollected.Resize ( dRowIdBlock.GetLength() );
RowID_t * pRowIdStart = dCollected.Begin();
RowID_t * pRowID = pRowIdStart;
for ( auto i : dRowIdBlock )
{
if ( i>=tBoundaries.m_tMinRowID && i<=tBoundaries.m_tMaxRowID )
*pRowID++ = i;
}
return { pRowIdStart, pRowID-pRowIdStart };
}
//////////////////////////////////////////////////////////////////////////
template <typename T, bool ROWID_LIMITS>
class IteratorState_T
{
public:
std::unique_ptr<T> m_pIterator;
const RowID_t * m_pRowID = nullptr;
const RowID_t * m_pRowIDMax = nullptr;
RowIdBoundaries_t m_tBoundaries;
IteratorState_T() = default;
IteratorState_T ( T * pIterator ) : m_pIterator(pIterator) {}
FORCE_INLINE bool RewindTo ( RowID_t tRowID );
FORCE_INLINE bool WarmupDocs();
FORCE_INLINE bool WarmupDocs ( RowID_t tRowID );
void Stop();
FORCE_INLINE bool IsStopped() const { return m_bStopped; }
static inline bool IsLess ( const IteratorState_T * pA, const IteratorState_T * pB ) { return ( *pA->m_pRowID < *pB->m_pRowID ); }
private:
bool m_bStopped = false;
CSphVector<RowID_t> m_dCollected;
};
template <typename T, bool ROWID_LIMITS>
bool IteratorState_T<T, ROWID_LIMITS>::WarmupDocs()
{
assert(m_pIterator);
assert ( !ROWID_LIMITS ); // we assume that underlying iterators do the filtering
RowIdBlock_t dRowIdBlock;
if ( !m_pIterator->GetNextRowIdBlock(dRowIdBlock) )
{
Stop();
return false;
}
m_pRowID = dRowIdBlock.Begin();
m_pRowIDMax = m_pRowID+dRowIdBlock.GetLength();
return true;
}
template <>
bool IteratorState_T<common::BlockIterator_i,true>::WarmupDocs()
{
assert(m_pIterator);
RowIdBlock_t dRowIdBlock;
do
{
util::Span_T<uint32_t> dSpan;
if ( !m_pIterator->GetNextRowIdBlock(dSpan) )
{
Stop();
return false;
}
RowID_t tMinSpanRowID = dSpan.front();
RowID_t tMaxSpanRowID = dSpan.back();
dRowIdBlock = { (RowID_t *)dSpan.begin(), (int64_t)dSpan.size() };
// we need additional filtering only on first and last blocks
// per-block filtering is performed inside MCL
if ( tMinSpanRowID < m_tBoundaries.m_tMinRowID || tMaxSpanRowID > m_tBoundaries.m_tMaxRowID )
dRowIdBlock = DoRowIdFiltering ( dRowIdBlock, m_tBoundaries, m_dCollected );
}
while ( !dRowIdBlock.GetLength() );
m_pRowID = (const RowID_t *)dRowIdBlock.begin();
m_pRowIDMax = (const RowID_t *)dRowIdBlock.end();
return true;
}
template <>
bool IteratorState_T<common::BlockIterator_i,false>::WarmupDocs()
{
assert(m_pIterator);
util::Span_T<uint32_t> dRowIdBlock;
if ( !m_pIterator->GetNextRowIdBlock(dRowIdBlock) )
{
Stop();
return false;
}
m_pRowID = (const RowID_t *)dRowIdBlock.begin();
m_pRowIDMax = (const RowID_t *)dRowIdBlock.end();
return true;
}
template <typename T, bool ROWID_LIMITS>
bool IteratorState_T<T,ROWID_LIMITS>::WarmupDocs ( RowID_t tRowID )
{
if ( !m_pIterator->HintRowID(tRowID) )
{
Stop();
return false;
}
return WarmupDocs();
}
template <typename T, bool ROWID_LIMITS>
void IteratorState_T<T,ROWID_LIMITS>::Stop()
{
m_pRowID = m_pRowIDMax = nullptr;
m_bStopped = true;
}
template <typename T, bool ROWID_LIMITS>
bool IteratorState_T<T,ROWID_LIMITS>::RewindTo ( RowID_t tRowID )
{
assert ( !m_bStopped );
if ( tRowID>*(m_pRowIDMax-1) && !WarmupDocs(tRowID) )
return false;
const RowID_t * pRowID = m_pRowID;
while ( true )
{
while ( pRowID < m_pRowIDMax && *pRowID < tRowID )
pRowID++;
if ( pRowID<m_pRowIDMax )
break;
if ( !WarmupDocs() )
return false;
pRowID = m_pRowID;
}
m_pRowID = pRowID;
assert(pRowID);
return true;
}
//////////////////////////////////////////////////////////////////////////
template <typename T, bool ROWID_LIMITS>
class RowidIterator_Base_T : public SecondaryIndexIterator_c
{
public:
RowidIterator_Base_T ( T ** ppIterators, int iNumIterators, const RowIdBoundaries_t * pBoundaries );
int64_t GetNumProcessed() const override;
protected:
using IteratorState_t = IteratorState_T<T,ROWID_LIMITS>;
CSphFixedVector<IteratorState_t> m_dIterators;
};
template <typename T, bool ROWID_LIMITS>
RowidIterator_Base_T<T,ROWID_LIMITS>::RowidIterator_Base_T ( T ** ppIterators, int iNumIterators, const RowIdBoundaries_t * pBoundaries )
: m_dIterators ( iNumIterators )
{
for ( int i = 0; i < iNumIterators; i++ )
{
m_dIterators[i] = ppIterators[i];
if ( pBoundaries )
m_dIterators[i].m_tBoundaries = *pBoundaries;
}
}
template <typename T, bool ROWID_LIMITS>
int64_t RowidIterator_Base_T<T,ROWID_LIMITS>::GetNumProcessed() const
{
int64_t iTotal = 0;
for ( auto & i : m_dIterators )
iTotal += i.m_pIterator->GetNumProcessed();
return iTotal;
}
//////////////////////////////////////////////////////////////////////////
template <typename T, bool ROWID_LIMITS>
class RowidIterator_Intersect_T : public RowidIterator_Base_T<T,ROWID_LIMITS>
{
using BASE = RowidIterator_Base_T<T,ROWID_LIMITS>;
public:
RowidIterator_Intersect_T ( T ** ppIterators, int iNumIterators, const RowIdBoundaries_t * pBoundaries = nullptr );
bool HintRowID ( RowID_t tRowID ) override;
bool GetNextRowIdBlock ( RowIdBlock_t & dRowIdBlock ) override;
void SetCutoff ( int iCutoff ) override { m_iRowsLeft = iCutoff; }
bool WasCutoffHit() const override { return !m_iRowsLeft; }
void AddDesc ( CSphVector<IteratorDesc_t> & dDesc ) const override;
private:
int m_iRowsLeft = INT_MAX;
FORCE_INLINE bool AdvanceIterators();
FORCE_INLINE bool Advance ( int iIterator, RowID_t tRowID );
};
template <typename T, bool ROWID_LIMITS>
RowidIterator_Intersect_T<T,ROWID_LIMITS>::RowidIterator_Intersect_T ( T ** ppIterators, int iNumIterators, const RowIdBoundaries_t * pBoundaries )
: BASE ( ppIterators, iNumIterators, pBoundaries )
{
BASE::m_dIterators[0].WarmupDocs();
}
template <typename T, bool ROWID_LIMITS>
bool RowidIterator_Intersect_T<T,ROWID_LIMITS>::HintRowID ( RowID_t tRowID )
{
if ( BASE::m_dIterators[0].IsStopped() )
return false;
return BASE::m_dIterators[0].RewindTo(tRowID);
}
template <typename T, bool ROWID_LIMITS>
bool RowidIterator_Intersect_T<T,ROWID_LIMITS>::GetNextRowIdBlock ( RowIdBlock_t & dRowIdBlock )
{
RowID_t * pRowIdStart = BASE::m_dCollected.Begin();
RowID_t * pRowIdMax = pRowIdStart + Min ( BASE::m_dCollected.GetLength()-1, m_iRowsLeft );
RowID_t * pRowID = pRowIdStart;
auto & tFirst = BASE::m_dIterators[0];
// we assume that iterators are sorted from most selective to least selective
while ( pRowID<pRowIdMax )
{
if ( !tFirst.m_pRowID )
break;
if ( !AdvanceIterators() )
{
tFirst.Stop();
break;
}
*pRowID++ = *tFirst.m_pRowID;
tFirst.m_pRowID++;
if ( tFirst.m_pRowID>=tFirst.m_pRowIDMax && !tFirst.WarmupDocs() )
break;
}
if ( m_iRowsLeft!=INT_MAX )
{
m_iRowsLeft -= pRowID-pRowIdStart;
assert ( m_iRowsLeft>=0 );
}
return ReturnIteratorResult ( pRowID, pRowIdStart, dRowIdBlock );
}
template <typename T, bool ROWID_LIMITS>
bool RowidIterator_Intersect_T<T,ROWID_LIMITS>::AdvanceIterators()
{
auto & tFirst = BASE::m_dIterators[0];
RowID_t tMaxRowID = *tFirst.m_pRowID;
for ( int i=1; i < BASE::m_dIterators.GetLength(); i++ )
{
auto & tState = BASE::m_dIterators[i];
if ( !tState.m_pRowID && !tState.WarmupDocs(tMaxRowID) )
return false;
if ( *tState.m_pRowID==tMaxRowID )
continue;
if ( !tState.RewindTo(tMaxRowID) )
return false;
if ( *tState.m_pRowID>tMaxRowID )
{
if ( !tFirst.RewindTo( *tState.m_pRowID ) )
return false;
tMaxRowID = *tFirst.m_pRowID;
i = 0;
}
}
return true;
}
template <typename T, bool ROWID_LIMITS>
void RowidIterator_Intersect_T<T,ROWID_LIMITS>::AddDesc ( CSphVector<IteratorDesc_t> & dDesc ) const
{
std::vector<common::IteratorDesc_t> dIteratorDesc;
for ( const auto & i : BASE::m_dIterators )
i.m_pIterator->AddDesc(dIteratorDesc);
for ( const auto & i : dIteratorDesc )
dDesc.Add ( { i.m_sAttr.c_str(), i.m_sType.c_str() } );
}
template <>
void RowidIterator_Intersect_T<RowidIterator_i,true>::AddDesc ( CSphVector<IteratorDesc_t> & dDesc ) const
{
for ( const auto & i : BASE::m_dIterators )
i.m_pIterator->AddDesc(dDesc);
}
template <>
void RowidIterator_Intersect_T<RowidIterator_i,false>::AddDesc ( CSphVector<IteratorDesc_t> & dDesc ) const
{
for ( const auto & i : BASE::m_dIterators )
i.m_pIterator->AddDesc(dDesc);
}
/////////////////////////////////////////////////////////////////////
template <typename T, bool ROWID_LIMITS>
class RowidIterator_Union_T : public RowidIterator_Base_T<T,ROWID_LIMITS>
{
using BASE = RowidIterator_Base_T<T,ROWID_LIMITS>;
public:
RowidIterator_Union_T ( T ** ppIterators, int iNumIterators, const RowIdBoundaries_t * pBoundaries = nullptr );
bool HintRowID ( RowID_t tRowID ) override;
bool GetNextRowIdBlock ( RowIdBlock_t & dRowIdBlock ) override;
void SetCutoff ( int iCutoff ) override { m_iRowsLeft = iCutoff; }
bool WasCutoffHit() const override { return !m_iRowsLeft; }
void AddDesc ( CSphVector<IteratorDesc_t> & dDesc ) const override;
private:
CSphQueue<typename BASE::IteratorState_t*, typename BASE::IteratorState_t> m_tMerge;
int m_iRowsLeft = INT_MAX;
FORCE_INLINE void AdvanceQueue ( typename BASE::IteratorState_t * pState );
};
template <typename T, bool ROWID_LIMITS>
RowidIterator_Union_T<T,ROWID_LIMITS>::RowidIterator_Union_T ( T ** ppIterators, int iNumIterators, const RowIdBoundaries_t * pBoundaries )
: BASE ( ppIterators, iNumIterators, pBoundaries )
, m_tMerge ( iNumIterators )
{
for ( auto & i : BASE::m_dIterators )
if ( i.WarmupDocs() )
m_tMerge.Push(&i);
}
template <typename T, bool ROWID_LIMITS>
bool RowidIterator_Union_T<T,ROWID_LIMITS>::HintRowID ( RowID_t tRowID )
{
if ( !m_tMerge.GetLength() )
return false;
if ( tRowID<=*m_tMerge.Last()->m_pRowID )
return true;
m_tMerge.Clear();
for ( auto & i : BASE::m_dIterators )
if ( i.m_pRowID && i.RewindTo(tRowID) )
m_tMerge.Push(&i);
return m_tMerge.GetLength()>0;
}
template <typename T, bool ROWID_LIMITS>
void RowidIterator_Union_T<T,ROWID_LIMITS>::AdvanceQueue ( typename BASE::IteratorState_t * pState )
{
pState->m_pRowID++;
if ( pState->m_pRowID<pState->m_pRowIDMax || pState->WarmupDocs() )
m_tMerge.Push(pState);
}
template <typename T, bool ROWID_LIMITS>
bool RowidIterator_Union_T<T,ROWID_LIMITS>::GetNextRowIdBlock ( RowIdBlock_t & dRowIdBlock )
{
RowID_t * pRowIdStart = BASE::m_dCollected.Begin();
RowID_t * pRowIdMax = pRowIdStart + Min ( BASE::m_dCollected.GetLength()-1, m_iRowsLeft );
RowID_t * pRowID = pRowIdStart;
RowID_t tLastRowID = INVALID_ROWID;
while ( pRowID<pRowIdMax && m_tMerge.GetLength()>1 )
{
auto pState = m_tMerge.Root();
m_tMerge.Pop();
RowID_t tCurRowID = *pState->m_pRowID;
if ( tCurRowID!=tLastRowID ) // skip all items with the same row-id
*pRowID++ = tCurRowID;
tLastRowID = tCurRowID;
AdvanceQueue(pState);
}
if ( m_tMerge.GetLength()==1 )
{
auto pState = m_tMerge.Root();
do
{
while ( pRowID<pRowIdMax && pState->m_pRowID<pState->m_pRowIDMax )
*pRowID++ = *pState->m_pRowID++;
}
while ( pState->m_pRowID>=pState->m_pRowIDMax && pState->WarmupDocs() );
if ( !pState->m_pRowID )
m_tMerge.Pop();
}
if ( m_iRowsLeft!=INT_MAX )
{
m_iRowsLeft -= pRowID-pRowIdStart;
assert ( m_iRowsLeft>=0 );
}
return ReturnIteratorResult ( pRowID, pRowIdStart, dRowIdBlock );
}
template <typename T, bool ROWID_LIMITS>
void RowidIterator_Union_T<T,ROWID_LIMITS>::AddDesc ( CSphVector<IteratorDesc_t> & dDesc ) const
{
std::vector<common::IteratorDesc_t> dIteratorDesc;
BASE::m_dIterators[0].m_pIterator->AddDesc(dIteratorDesc);
dDesc.Add ( { dIteratorDesc[0].m_sAttr.c_str(), dIteratorDesc[0].m_sType.c_str() } );
}
template <>
void RowidIterator_Union_T<RowidIterator_i,true>::AddDesc ( CSphVector<IteratorDesc_t> & dDesc ) const
{
BASE::m_dIterators[0].m_pIterator->AddDesc(dDesc);
}
template <>
void RowidIterator_Union_T<RowidIterator_i,false>::AddDesc ( CSphVector<IteratorDesc_t> & dDesc ) const
{
BASE::m_dIterators[0].m_pIterator->AddDesc(dDesc);
}
/////////////////////////////////////////////////////////////////////
template <bool ROWID_LIMITS>
class RowidIterator_Wrapper_T : public RowidIterator_i
{
public:
RowidIterator_Wrapper_T ( common::BlockIterator_i * pIterator, const RowIdBoundaries_t * pBoundaries = nullptr );
bool HintRowID ( RowID_t tRowID ) override { return m_pIterator->HintRowID(tRowID); }
bool GetNextRowIdBlock ( RowIdBlock_t & dRowIdBlock ) override;
int64_t GetNumProcessed() const override { return m_pIterator->GetNumProcessed(); }
void SetCutoff ( int iCutoff ) override { m_pIterator->SetCutoff(iCutoff); }
bool WasCutoffHit() const override { return false; }
void AddDesc ( CSphVector<IteratorDesc_t> & dDesc ) const override;
private:
std::unique_ptr<common::BlockIterator_i> m_pIterator;
RowIdBoundaries_t m_tBoundaries;
CSphVector<RowID_t> m_dCollected;
};
template <bool ROWID_LIMITS>
RowidIterator_Wrapper_T<ROWID_LIMITS>::RowidIterator_Wrapper_T ( common::BlockIterator_i * pIterator, const RowIdBoundaries_t * pBoundaries )
: m_pIterator ( pIterator )
{
if ( pBoundaries )
m_tBoundaries = *pBoundaries;
}
template <>
bool RowidIterator_Wrapper_T<true>::GetNextRowIdBlock ( RowIdBlock_t & dRowIdBlock )
{
do
{
util::Span_T<uint32_t> dSpan;
if ( !m_pIterator->GetNextRowIdBlock(dSpan) )
return false;
dRowIdBlock = { (RowID_t *)dSpan.begin(), (int64_t)dSpan.size() };
if ( !dSpan.size() )
return true;
RowID_t tMinSpanRowID = dSpan.front();
RowID_t tMaxSpanRowID = dSpan.back();
// we need additional filtering only on first and last blocks
// per-block filtering is performed inside MCL
if ( tMinSpanRowID < m_tBoundaries.m_tMinRowID || tMaxSpanRowID > m_tBoundaries.m_tMaxRowID )
dRowIdBlock = DoRowIdFiltering ( dRowIdBlock, m_tBoundaries, m_dCollected );
}
while ( !dRowIdBlock.GetLength() );
return true;
}
template <>
bool RowidIterator_Wrapper_T<false>::GetNextRowIdBlock ( RowIdBlock_t & dRowIdBlock )
{
util::Span_T<uint32_t> dSpan;
if ( !m_pIterator->GetNextRowIdBlock(dSpan) )
return false;
dRowIdBlock = { (RowID_t *)dSpan.begin(), (int64_t)dSpan.size() };
return true;
}
template <bool ROWID_LIMITS>
void RowidIterator_Wrapper_T<ROWID_LIMITS>::AddDesc ( CSphVector<IteratorDesc_t> & dDesc ) const
{
assert(m_pIterator);
std::vector<common::IteratorDesc_t> dIteratorDesc;
m_pIterator->AddDesc(dIteratorDesc);
for ( const auto & i : dIteratorDesc )
dDesc.Add ( { i.m_sAttr.c_str(), i.m_sType.c_str() } );
}
//////////////////////////////////////////////////////////////////////////
static bool NextSet ( CSphVector<int> & dSet, const CSphVector<SecondaryIndexInfo_t> & dSecondaryIndexes )
{
for ( int i = 0; i < dSet.GetLength(); i++ )
{
int iNumCapabilities = dSecondaryIndexes[i].m_dCapabilities.GetLength();
if ( !iNumCapabilities )
continue;
dSet[i]++;
if ( dSet[i] >= iNumCapabilities )
dSet[i] = 0;
else
return true;
}
return false;
}
static SIDefault_e g_eSIState = SIDefault_e::ENABLED;
void SetSecondaryIndexDefault ( SIDefault_e eState )
{
g_eSIState = eState;
}
SIDefault_e GetSecondaryIndexDefault ()
{
return g_eSIState;
}
static bool CheckIndexHint ( const CSphFilterSettings & tFilter, const CSphVector<IndexHint_t> & dHints, SecondaryIndexType_e eType, bool & bForce )
{
bForce = false;
for ( const auto & i : dHints )
if ( i.m_sIndex==tFilter.m_sAttrName && i.m_eType==eType )
{
bForce = i.m_bForce;
return bForce;
}
return true;
}
static bool HaveSI ( const CSphFilterSettings & tFilter, const SelectIteratorCtx_t & tCtx, bool & bForce )
{
if ( !tCtx.IsEnabled_SI(tFilter) )
return false;
if ( !CheckIndexHint ( tFilter, tCtx.m_tQuery.m_dIndexHints, SecondaryIndexType_e::INDEX, bForce ) )
return false;
if ( !IsSecondaryLibLoaded() || GetSecondaryIndexDefault()==SIDefault_e::DISABLED )
return false;
return true;
}
static bool HaveAnalyzer ( const CSphFilterSettings & tFilter, const SelectIteratorCtx_t & tCtx, bool & bForce )
{
if ( !tCtx.IsEnabled_Analyzer(tFilter) )
return false;
return CheckIndexHint ( tFilter, tCtx.m_tQuery.m_dIndexHints, SecondaryIndexType_e::ANALYZER, bForce );
}
static bool HaveLookup ( const CSphFilterSettings & tFilter, const CSphVector<IndexHint_t> & dHints, bool & bForce )
{
if ( tFilter.m_sAttrName!=sphGetDocidName() )
return false;
return CheckIndexHint ( tFilter, dHints, SecondaryIndexType_e::LOOKUP, bForce );
}
static void FetchHistogramInfo ( CSphVector<SecondaryIndexInfo_t> & dSIInfo, const SelectIteratorCtx_t & tCtx )
{
if ( !tCtx.m_pHistograms )
return;
ARRAY_FOREACH ( i, tCtx.m_tQuery.m_dFilters )
{
const CSphFilterSettings & tFilter = tCtx.m_tQuery.m_dFilters[i];
const Histogram_i * pHistogram = tCtx.m_pHistograms->Get ( tFilter.m_sAttrName );
if ( !pHistogram )
continue;
HistogramRset_t tEstimate;
auto & tSIInfo = dSIInfo[i];
tSIInfo.m_bUsable = pHistogram->EstimateRsetSize ( tFilter, tEstimate );
tSIInfo.m_iRsetEstimate = tEstimate.m_iTotal;
tSIInfo.m_iTotalValues = pHistogram->GetNumValues();
tSIInfo.m_bHasHistograms = true;
}
}
static void MarkAvailableLookup ( CSphVector<SecondaryIndexInfo_t> & dSIInfo, const SelectIteratorCtx_t & tCtx )
{
ARRAY_FOREACH ( i, tCtx.m_tQuery.m_dFilters )
{
if ( !dSIInfo[i].m_bUsable )
continue;
bool bForce = false;
if ( !HaveLookup( tCtx.m_tQuery.m_dFilters[i], tCtx.m_tQuery.m_dIndexHints, bForce ) )
continue;
dSIInfo[i].m_dCapabilities.Add ( SecondaryIndexType_e::LOOKUP );
if ( bForce )
dSIInfo[i].m_eForce = SecondaryIndexType_e::LOOKUP;
}
}
static void MarkAvailableSI ( CSphVector<SecondaryIndexInfo_t> & dSIInfo, const SelectIteratorCtx_t & tCtx, bool bCheckUsable = true )
{
if ( !tCtx.m_pHistograms )
return;
ARRAY_FOREACH ( i, tCtx.m_tQuery.m_dFilters )
{
if ( bCheckUsable && !dSIInfo[i].m_bUsable )
continue;
bool bForce = false;
if ( !HaveSI ( tCtx.m_tQuery.m_dFilters[i], tCtx, bForce ) )
continue;
if ( bForce )
dSIInfo[i].m_eForce = SecondaryIndexType_e::INDEX;
dSIInfo[i].m_dCapabilities.Add ( SecondaryIndexType_e::INDEX );
}
}
static void MarkAvailableAnalyzers ( CSphVector<SecondaryIndexInfo_t> & dSIInfo, const SelectIteratorCtx_t & tCtx )
{
ARRAY_FOREACH ( i, tCtx.m_tQuery.m_dFilters )
{
if ( !dSIInfo[i].m_bUsable )
continue;
bool bForce = false;
if ( !HaveAnalyzer ( tCtx.m_tQuery.m_dFilters[i], tCtx, bForce ) )
continue;
if ( bForce )
dSIInfo[i].m_eForce = SecondaryIndexType_e::ANALYZER;
dSIInfo[i].m_dCapabilities.Add ( SecondaryIndexType_e::ANALYZER );
// this belongs in the CBO, but for now let's just remove the option to evaluate FILTER if ANALYZER is present
// as ANALYZERs are always faster than FILTERs
dSIInfo[i].m_dCapabilities.RemoveValue ( SecondaryIndexType_e::FILTER );
}
}
static void ForceSI ( CSphVector<SecondaryIndexInfo_t> & dSIInfo )
{
for ( auto & i : dSIInfo )
if ( i.m_eForce!=SecondaryIndexType_e::NONE )
{
i.m_dCapabilities.Resize(0);
i.m_dCapabilities.Add ( i.m_eForce );
i.m_eType = i.m_eForce;
}
}
static void DisableRowidFilters ( CSphVector<SecondaryIndexInfo_t> & dSIInfo, const SelectIteratorCtx_t & tCtx )
{
ARRAY_FOREACH ( i, dSIInfo )
if ( tCtx.m_tQuery.m_dFilters[i].m_sAttrName=="@rowid" )
dSIInfo[i].m_dCapabilities.Resize(0);
}
static void FetchPartialColumnarMinMax ( CSphVector<SecondaryIndexInfo_t> & dSIInfo, const SelectIteratorCtx_t & tCtx )
{
ARRAY_FOREACH ( i, dSIInfo )
{
auto & tSIInfo = dSIInfo[i];
auto & tFilter = tCtx.m_tQuery.m_dFilters[i];
bool bHaveAnalyzers = tSIInfo.m_dCapabilities.any_of ( []( auto eCapability ){ return eCapability==SecondaryIndexType_e::ANALYZER; } );
bool bHaveSI = tSIInfo.m_dCapabilities.any_of ( []( auto eCapability ){ return eCapability==SecondaryIndexType_e::INDEX; } );
bool bHaveLookups = tSIInfo.m_dCapabilities.any_of ( []( auto eCapability ){ return eCapability==SecondaryIndexType_e::LOOKUP; } );
if ( bHaveAnalyzers && ( bHaveSI || bHaveLookups ) )
{
// create a single filter and run it through partial columnar minmax
VecTraits_T<CSphFilterSettings> dFilter = { &tFilter, 1 };
CreateFilterContext_t tCFCtx;
tCFCtx.m_pFilters = &dFilter;
tCFCtx.m_pSchema = &tCtx.m_tSchema;
tCFCtx.m_pColumnar = tCtx.m_pColumnar;
tCFCtx.m_eCollation = tCtx.m_tQuery.m_eCollation;
tCFCtx.m_bScan = true;
tCFCtx.m_pHistograms= tCtx.m_pHistograms;
tCFCtx.m_iTotalDocs = tCtx.m_iTotalDocs;
CSphString sError, sWarning;
if ( !sphCreateFilters ( tCFCtx, sError, sWarning ) )
continue;
common::Filter_t tColumnarFilter;
if ( !ToColumnarFilter ( tColumnarFilter, tFilter, tCtx.m_tQuery.m_eCollation, tCtx.m_tSchema, sWarning ) )
continue;
tSIInfo.m_iPartialColumnarMinMax = tCtx.m_pColumnar->EstimateMinMax ( tColumnarFilter, *tCFCtx.m_pFilter );
}
}
}
static uint32_t CalcNumSIIterators ( const CSphFilterSettings & tFilter, int64_t iDocs, const SelectIteratorCtx_t & tCtx )
{
uint32_t uNumIterators = 1;
if ( !tCtx.m_pSI )
return uNumIterators;
common::Filter_t tColumnarFilter;
CSphString sWarning;
if ( !ToColumnarFilter ( tColumnarFilter, tFilter, tCtx.m_tQuery.m_eCollation, tCtx.m_tSchema, sWarning ) )
return 0;
return tCtx.m_pSI->GetNumIterators(tColumnarFilter);
}
static void FetchNumSIIterators ( CSphVector<SecondaryIndexInfo_t> & dSIInfo, const SelectIteratorCtx_t & tCtx )
{
ARRAY_FOREACH ( i, dSIInfo )
{
auto & tSIInfo = dSIInfo[i];
tSIInfo.m_uNumSIIterators = CalcNumSIIterators ( tCtx.m_tQuery.m_dFilters[i], tSIInfo.m_iRsetEstimate, tCtx );
}
}
static void CheckHint ( const IndexHint_t & tHint, const CSphFilterSettings & tFilter, const SecondaryIndexInfo_t & tSIInfo, const SelectIteratorCtx_t & tCtx, StrVec_t & dWarnings )
{
CSphString sWarning;
const auto * pAttr = tCtx.m_tSchema.GetAttr ( tHint.m_sIndex.cstr() );
if ( !pAttr )
{
sWarning.SetSprintf ( "hint error: '%s' attribute not found", tHint.m_sIndex.cstr() );
dWarnings.Add (sWarning);
return;
}
if ( !tSIInfo.m_bHasHistograms )
{
sWarning.SetSprintf ( "hint error: histogram not found for attribute '%s'", tHint.m_sIndex.cstr() );
dWarnings.Add (sWarning);
}
else if ( !tSIInfo.m_bUsable )
{
sWarning.SetSprintf ( "hint error: histogram unusable for attribute '%s'", tHint.m_sIndex.cstr() );
dWarnings.Add (sWarning);
}
switch ( tHint.m_eType )
{
case SecondaryIndexType_e::LOOKUP:
if ( tHint.m_sIndex!=sphGetDocidName() )
dWarnings.Add ( "hint error: DocidIndex can only be applied to 'id' attribute" );
break;
case SecondaryIndexType_e::ANALYZER:
if ( tHint.m_bForce )
{
if ( !IsColumnarLibLoaded() )
dWarnings.Add ( "hint error: columnar library not loaded" );
else if ( !tCtx.m_pColumnar )
dWarnings.Add ( "hint error: no columnar storage" );
else if ( pAttr->m_eAttrType==SPH_ATTR_STRING && tCtx.m_tQuery.m_eCollation!=SPH_COLLATION_DEFAULT )
{
sWarning.SetSprintf ( "hint error: unsupported collation; ColumnarScan might be slow for '%s'", tHint.m_sIndex.cstr() );
dWarnings.Add(sWarning);
}
else
{
const auto * pAttr = tCtx.m_tSchema.GetAttr ( tHint.m_sIndex.cstr()) ;
if ( !pAttr->IsColumnar() && !pAttr->IsColumnarExpr() )
{
sWarning.SetSprintf ( "hint error: attribute '%s' is not columnar", tHint.m_sIndex.cstr() );
dWarnings.Add(sWarning);
}
}
}
break;
case SecondaryIndexType_e::INDEX:
if ( tHint.m_bForce )
{
if ( !IsSecondaryLibLoaded() )
dWarnings.Add ( "hint error: secondary library not loaded" );
else if ( GetSecondaryIndexDefault()==SIDefault_e::DISABLED )
dWarnings.Add ( "hint error: secondary indexes are disabled" );
else if ( !tCtx.m_pSI )
dWarnings.Add ( "hint error: table has no secondary indexes" );
else if ( !tCtx.m_pSI->IsEnabled ( tHint.m_sIndex.cstr() ) )
{
sWarning.SetSprintf ( "hint error: secondary index disabled for '%s' (attribute was updated?)", tHint.m_sIndex.cstr() );
dWarnings.Add(sWarning);
}
else if ( pAttr->m_eAttrType==SPH_ATTR_STRING && tCtx.m_tQuery.m_eCollation!=SPH_COLLATION_DEFAULT )
{
sWarning.SetSprintf ( "hint error: unsupported collation; secondary index disabled for '%s'", tHint.m_sIndex.cstr() );
dWarnings.Add(sWarning);
}
else if ( pAttr->m_pExpr.Ptr() && !pAttr->IsColumnarExpr() )
{
sWarning.SetSprintf ( "hint error: attribute is an expression; secondary index disabled for '%s'", tHint.m_sIndex.cstr() );
dWarnings.Add(sWarning);
}
else if ( tFilter.m_eType!=SPH_FILTER_VALUES && tFilter.m_eType!=SPH_FILTER_STRING && tFilter.m_eType!=SPH_FILTER_STRING_LIST && tFilter.m_eType!=SPH_FILTER_RANGE && tFilter.m_eType!=SPH_FILTER_FLOATRANGE )
{
sWarning.SetSprintf ( "hint error: unsupported filter type; secondary index disabled for '%s'", tHint.m_sIndex.cstr() );
dWarnings.Add(sWarning);
}
else if ( tFilter.m_eMvaFunc==SPH_MVAFUNC_ALL )
{
sWarning.SetSprintf ( "hint error: unsupported mva eval type; secondary index disabled for '%s'", tHint.m_sIndex.cstr() );
dWarnings.Add(sWarning);
}
}
break;
default:
break;
}
}
static void CheckHints ( const CSphVector<SecondaryIndexInfo_t> & dSIInfo, const SelectIteratorCtx_t & tCtx, StrVec_t & dWarnings )
{
const auto & dFilters = tCtx.m_tQuery.m_dFilters;
for ( auto & tHint : tCtx.m_tQuery.m_dIndexHints )
{
int iFilter = -1;
ARRAY_FOREACH ( i, dFilters )
if ( dFilters[i].m_sAttrName==tHint.m_sIndex )
{
iFilter = i;
break;
}
if ( iFilter==-1 )
{
CSphString sWarning;
sWarning.SetSprintf ( "hint error: filter not found for attribute '%s'", tHint.m_sIndex.cstr() );
dWarnings.Add(sWarning);
}
else
CheckHint ( tHint, dFilters[iFilter], dSIInfo[iFilter], tCtx, dWarnings );
}
ARRAY_FOREACH ( i, dFilters )
for ( auto & tHint : tCtx.m_tQuery.m_dIndexHints )
if ( tHint.m_sIndex==dFilters[i].m_sAttrName && tHint.m_bForce )
if ( !dSIInfo[i].m_dCapabilities.any_of ( [&tHint]( auto eSupported ){ return tHint.m_eType==eSupported; } ) )
{
CSphString sWarning;
sWarning.SetSprintf ( "hint error: requested hint type not supported for attribute '%s'", tHint.m_sIndex.cstr() );
dWarnings.Add(sWarning);
}
}
/////////////////////////////////////////////////////////////////////
CSphVector<SecondaryIndexInfo_t> SelectIterators ( const SelectIteratorCtx_t & tCtx, float & fBestCost, StrVec_t & dWarnings )
{
fBestCost = FLT_MAX;
CSphVector<SecondaryIndexInfo_t> dSIInfo ( tCtx.m_tQuery.m_dFilters.GetLength() );
ARRAY_FOREACH ( i, dSIInfo )
dSIInfo[i].m_dCapabilities.Add ( SecondaryIndexType_e::FILTER );
if ( !tCtx.m_pHistograms )
{
if ( tCtx.m_tQuery.m_dIndexHints.GetLength() )
dWarnings.Add ( "index has no histograms; secondary indexes are unavailable" );
return dSIInfo;
}
// no iterators with OR queries
if ( !tCtx.m_tQuery.m_dFilterTree.IsEmpty() )
{
if ( tCtx.m_tQuery.m_dIndexHints.GetLength() )
dWarnings.Add ( "secondary indexes are not available when using the OR operator between filters" );
return dSIInfo;
}
FetchHistogramInfo ( dSIInfo, tCtx );
MarkAvailableLookup ( dSIInfo, tCtx );
MarkAvailableSI ( dSIInfo, tCtx );
MarkAvailableAnalyzers ( dSIInfo, tCtx );
ForceSI(dSIInfo);
DisableRowidFilters ( dSIInfo, tCtx );
FetchPartialColumnarMinMax ( dSIInfo, tCtx );
FetchNumSIIterators ( dSIInfo, tCtx );
CheckHints ( dSIInfo, tCtx, dWarnings );