Repository navigation
Expand file tree
/
Copy pathsphinxexpr.cpp
More file actions
7216 lines (6150 loc) · 209 KB
/
Copy pathsphinxexpr.cpp
File metadata and controls
7216 lines (6150 loc) · 209 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// $Id$
//
//
// Copyright (c) 2001-2016, Andrew Aksyonoff
// Copyright (c) 2008-2016, Sphinx Technologies Inc
// 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 "sphinx.h"
#include "sphinxexpr.h"
#include "sphinxplugin.h"
#include "sphinxutils.h"
#include "sphinxint.h"
#include "sphinxjson.h"
#include <time.h>
#include <math.h>
#ifndef M_LOG2E
#define M_LOG2E 1.44269504088896340736
#endif
#ifndef M_LOG10E
#define M_LOG10E 0.434294481903251827651
#endif
// hack hack hack
UservarIntSet_c * ( *g_pUservarsHook )( const CSphString & sUservar );
//////////////////////////////////////////////////////////////////////////
// EVALUATION ENGINE
//////////////////////////////////////////////////////////////////////////
#if USE_WINDOWS
#ifndef NDEBUG
#define EXPR_CLASS_NAME(name) \
{\
const char * szFuncName = __FUNCTION__; \
const char * szClassNameEnd = strstr ( szFuncName, "::" ); \
assert ( szClassNameEnd ); \
const char * szTemplateNameEnd = strstr ( szFuncName, "<" ); \
if ( szTemplateNameEnd ) szClassNameEnd = szTemplateNameEnd; \
size_t iLen = szClassNameEnd-szFuncName; \
assert ( strlen(name)==iLen && "Wrong expression name specified in ::GetHash" ); \
assert ( !strncmp(name, szFuncName, iLen) && "Wrong expression name specified in ::GetHash" ); \
}\
const char * szClassName = name; \
uint64_t uHash = uPrevHash;
#else
#define EXPR_CLASS_NAME(name) \
const char * szClassName = name; \
uint64_t uHash = uPrevHash;
#endif
#else
#define EXPR_CLASS_NAME(name) \
const char * szClassName = name; \
uint64_t uHash = uPrevHash;
#endif
#define EXPR_CLASS_NAME_NOCHECK(name) \
const char * szClassName = name; \
uint64_t uHash = uPrevHash;
#define CALC_DEP_HASHES() sphCalcExprDepHash ( szClassName, this, tSorterSchema, uHash, bDisable );
#define CALC_DEP_HASHES_EX(hash) sphCalcExprDepHash ( szClassName, this, tSorterSchema, uHash^hash, bDisable );
#define CALC_PARENT_HASH() CalcHash ( szClassName, tSorterSchema, uHash, bDisable );
#define CALC_PARENT_HASH_EX(hash) CalcHash ( szClassName, tSorterSchema, uHash^hash, bDisable );
#define CALC_POD_HASH(value) uHash = sphFNV64 ( &value, sizeof(value), uHash );
#define CALC_POD_HASHES(values) uHash = sphFNV64 ( values.Begin(), values.GetLength()*sizeof(values[0]), uHash );
#define CALC_STR_HASH(str,len) uHash = sphFNV64 ( str.cstr(), len, uHash );
#define CALC_CHILD_HASH(child) if (child) uHash = child->GetHash ( tSorterSchema, uHash, bDisable );
#define CALC_CHILD_HASHES(children) ARRAY_FOREACH ( i, children ) if (children[i]) uHash = children[i]->GetHash ( tSorterSchema, uHash, bDisable );
struct ExprLocatorTraits_t
{
CSphAttrLocator m_tLocator;
int m_iLocator; // used by SPH_EXPR_GET_DEPENDENT_COLS
ExprLocatorTraits_t ( const CSphAttrLocator & tLocator, int iLocator ) : m_tLocator ( tLocator ), m_iLocator ( iLocator ) {}
void HandleCommand ( ESphExprCommand eCmd, void * pArg )
{
if ( eCmd==SPH_EXPR_GET_DEPENDENT_COLS && m_iLocator!=-1 )
static_cast < CSphVector<int>* >(pArg)->Add ( m_iLocator );
}
};
struct Expr_WithLocator_c : public ISphExpr, public ExprLocatorTraits_t
{
public:
Expr_WithLocator_c ( const CSphAttrLocator & tLocator, int iLocator )
: ExprLocatorTraits_t ( tLocator, iLocator )
{}
virtual void Command ( ESphExprCommand eCmd, void * pArg )
{
HandleCommand ( eCmd, pArg );
}
};
struct Expr_GetInt_c : public Expr_WithLocator_c
{
Expr_GetInt_c ( const CSphAttrLocator & tLocator, int iLocator ) : Expr_WithLocator_c ( tLocator, iLocator ) {}
virtual float Eval ( const CSphMatch & tMatch ) const { return (float) tMatch.GetAttr ( m_tLocator ); } // FIXME! OPTIMIZE!!! we can go the short route here
virtual int IntEval ( const CSphMatch & tMatch ) const { return (int)tMatch.GetAttr ( m_tLocator ); }
virtual int64_t Int64Eval ( const CSphMatch & tMatch ) const { return (int64_t)tMatch.GetAttr ( m_tLocator ); }
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_GetInt_c");
return CALC_DEP_HASHES();
}
};
struct Expr_GetBits_c : public Expr_WithLocator_c
{
Expr_GetBits_c ( const CSphAttrLocator & tLocator, int iLocator ) : Expr_WithLocator_c ( tLocator, iLocator ) {}
virtual float Eval ( const CSphMatch & tMatch ) const { return (float) tMatch.GetAttr ( m_tLocator ); }
virtual int IntEval ( const CSphMatch & tMatch ) const { return (int)tMatch.GetAttr ( m_tLocator ); }
virtual int64_t Int64Eval ( const CSphMatch & tMatch ) const { return (int64_t)tMatch.GetAttr ( m_tLocator ); }
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_GetBits_c");
return CALC_DEP_HASHES();
}
};
struct Expr_GetSint_c : public Expr_WithLocator_c
{
Expr_GetSint_c ( const CSphAttrLocator & tLocator, int iLocator ) : Expr_WithLocator_c ( tLocator, iLocator ) {}
virtual float Eval ( const CSphMatch & tMatch ) const { return (float)(int)tMatch.GetAttr ( m_tLocator ); }
virtual int IntEval ( const CSphMatch & tMatch ) const { return (int)tMatch.GetAttr ( m_tLocator ); }
virtual int64_t Int64Eval ( const CSphMatch & tMatch ) const { return (int)tMatch.GetAttr ( m_tLocator ); }
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_GetSint_c");
return CALC_DEP_HASHES();
}
};
struct Expr_GetFloat_c : public Expr_WithLocator_c
{
Expr_GetFloat_c ( const CSphAttrLocator & tLocator, int iLocator ) : Expr_WithLocator_c ( tLocator, iLocator ) {}
virtual float Eval ( const CSphMatch & tMatch ) const { return tMatch.GetAttrFloat ( m_tLocator ); }
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_GetFloat_c");
return CALC_DEP_HASHES();
}
};
struct Expr_GetString_c : public Expr_WithLocator_c
{
const BYTE * m_pStrings;
Expr_GetString_c ( const CSphAttrLocator & tLocator, int iLocator ) : Expr_WithLocator_c ( tLocator, iLocator ) {}
virtual float Eval ( const CSphMatch & ) const { assert ( 0 ); return 0; }
virtual void Command ( ESphExprCommand eCmd, void * pArg )
{
Expr_WithLocator_c::Command ( eCmd, pArg );
if ( eCmd==SPH_EXPR_SET_STRING_POOL )
m_pStrings = (const BYTE*)pArg;
}
virtual int StringEval ( const CSphMatch & tMatch, const BYTE ** ppStr ) const
{
SphAttr_t iOff = tMatch.GetAttr ( m_tLocator );
if ( iOff>0 )
return sphUnpackStr ( m_pStrings + iOff, ppStr );
*ppStr = NULL;
return 0;
}
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_GetString_c");
return CALC_DEP_HASHES();
}
};
struct Expr_GetMva_c : public Expr_WithLocator_c
{
const DWORD * m_pMva;
bool m_bArenaProhibit;
Expr_GetMva_c ( const CSphAttrLocator & tLocator, int iLocator ) : Expr_WithLocator_c ( tLocator, iLocator ), m_pMva ( NULL ), m_bArenaProhibit ( false ) {}
virtual float Eval ( const CSphMatch & ) const { assert ( 0 ); return 0; }
virtual void Command ( ESphExprCommand eCmd, void * pArg )
{
Expr_WithLocator_c::Command ( eCmd, pArg );
if ( eCmd==SPH_EXPR_SET_MVA_POOL )
{
const PoolPtrs_t * pPool = (const PoolPtrs_t *)pArg;
assert ( pPool );
m_pMva = pPool->m_pMva;
m_bArenaProhibit = pPool->m_bArenaProhibit;
}
}
virtual int IntEval ( const CSphMatch & tMatch ) const { return (int)tMatch.GetAttr ( m_tLocator ); }
virtual const DWORD * MvaEval ( const CSphMatch & tMatch ) const { return tMatch.GetAttrMVA ( m_tLocator, m_pMva, m_bArenaProhibit ); }
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_GetMva_c");
CALC_POD_HASH(m_bArenaProhibit);
return CALC_DEP_HASHES();
}
};
struct Expr_GetFactorsAttr_c : public Expr_WithLocator_c
{
Expr_GetFactorsAttr_c ( const CSphAttrLocator & tLocator, int iLocator ) : Expr_WithLocator_c ( tLocator, iLocator ) {}
virtual float Eval ( const CSphMatch & ) const { assert ( 0 ); return 0; }
virtual const DWORD * FactorEval ( const CSphMatch & tMatch ) const { return (DWORD *)tMatch.GetAttr ( m_tLocator ); }
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_GetFactorsAttr_c");
return CALC_DEP_HASHES();
}
};
struct Expr_GetConst_c : public ISphExpr
{
float m_fValue;
explicit Expr_GetConst_c ( float fValue ) : m_fValue ( fValue ) {}
virtual float Eval ( const CSphMatch & ) const { return m_fValue; }
virtual int IntEval ( const CSphMatch & ) const { return (int)m_fValue; }
virtual int64_t Int64Eval ( const CSphMatch & ) const { return (int64_t)m_fValue; }
virtual bool IsConst () const { return true; }
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_GetConst_c");
CALC_POD_HASH(m_fValue);
return CALC_DEP_HASHES();
}
};
struct Expr_GetIntConst_c : public ISphExpr
{
int m_iValue;
explicit Expr_GetIntConst_c ( int iValue ) : m_iValue ( iValue ) {}
virtual float Eval ( const CSphMatch & ) const { return (float) m_iValue; } // no assert() here cause generic float Eval() needs to work even on int-evaluator tree
virtual int IntEval ( const CSphMatch & ) const { return m_iValue; }
virtual int64_t Int64Eval ( const CSphMatch & ) const { return m_iValue; }
virtual bool IsConst () const { return true; }
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_GetIntConst_c");
CALC_POD_HASH(m_iValue);
return CALC_DEP_HASHES();
}
};
struct Expr_GetInt64Const_c : public ISphExpr
{
int64_t m_iValue;
explicit Expr_GetInt64Const_c ( int64_t iValue ) : m_iValue ( iValue ) {}
virtual float Eval ( const CSphMatch & ) const { return (float) m_iValue; } // no assert() here cause generic float Eval() needs to work even on int-evaluator tree
virtual int IntEval ( const CSphMatch & ) const { assert ( 0 ); return (int)m_iValue; }
virtual int64_t Int64Eval ( const CSphMatch & ) const { return m_iValue; }
virtual bool IsConst () const { return true; }
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_GetInt64Const_c");
CALC_POD_HASH(m_iValue);
return CALC_DEP_HASHES();
}
};
struct Expr_GetStrConst_c : public ISphStringExpr
{
CSphString m_sVal;
int m_iLen;
explicit Expr_GetStrConst_c ( const char * sVal, int iLen, bool bUnescape )
{
if ( iLen>0 )
{
if ( bUnescape )
SqlUnescape ( m_sVal, sVal, iLen );
else
m_sVal.SetBinary ( sVal, iLen );
}
m_iLen = m_sVal.Length();
}
virtual int StringEval ( const CSphMatch &, const BYTE ** ppStr ) const
{
*ppStr = (const BYTE*) m_sVal.cstr();
return m_iLen;
}
virtual float Eval ( const CSphMatch & ) const { assert ( 0 ); return 0; }
virtual int IntEval ( const CSphMatch & ) const { assert ( 0 ); return 0; }
virtual int64_t Int64Eval ( const CSphMatch & ) const { assert ( 0 ); return 0; }
virtual bool IsConst () const { return true; }
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_GetStrConst_c");
CALC_STR_HASH(m_sVal, m_iLen);
return CALC_DEP_HASHES();
}
};
class Expr_StrLength_c : public ISphExpr
{
public:
Expr_StrLength_c ( ISphExpr * pArg )
: m_pArg ( pArg )
{}
virtual int IntEval ( const CSphMatch & tMatch ) const
{
const BYTE * pStr;
return m_pArg->StringEval ( tMatch, &pStr );
}
virtual void Command ( ESphExprCommand eCmd, void * pArg )
{
m_pArg->Command ( eCmd, pArg );
}
virtual float Eval ( const CSphMatch & tMatch ) const { return (float)IntEval ( tMatch ); }
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_StrLength_c");
CALC_CHILD_HASH(m_pArg);
return CALC_DEP_HASHES();
}
protected:
ISphExpr * m_pArg;
};
struct Expr_GetZonespanlist_c : public ISphStringExpr
{
const CSphVector<int> * m_pData;
mutable CSphStringBuilder m_sBuilder;
explicit Expr_GetZonespanlist_c ()
: m_pData ( NULL )
{}
virtual int StringEval ( const CSphMatch & tMatch, const BYTE ** ppStr ) const
{
assert ( ppStr );
if ( !m_pData || !m_pData->GetLength() )
{
*ppStr = NULL;
return 0;
}
m_sBuilder.Clear();
const CSphVector<int> & dSpans = *m_pData;
int iStart = tMatch.m_iTag + 1; // spans[tag] contains the length, so the 1st data index is tag+1
int iEnd = iStart + dSpans [ tMatch.m_iTag ]; // [start,end) now covers all data indexes
for ( int i=iStart; i<iEnd; i+=2 )
m_sBuilder.Appendf ( " %d:%d", 1+dSpans[i], 1+dSpans[i+1] ); // convert our 0-based span numbers to human 1-based ones
*ppStr = (const BYTE *) CSphString ( m_sBuilder.cstr() ).Leak();
return m_sBuilder.Length();
}
virtual void Command ( ESphExprCommand eCmd, void * pArg )
{
if ( eCmd==SPH_EXPR_SET_EXTRA_DATA )
static_cast<ISphExtra*>(pArg)->ExtraData ( EXTRA_GET_DATA_ZONESPANS, (void**)&m_pData );
}
virtual bool IsStringPtr() const
{
return true;
}
virtual uint64_t GetHash ( const ISphSchema &, uint64_t, bool & bDisable )
{
bDisable = true; // disable caching for now, might add code to process if necessary
return 0;
}
};
struct Expr_GetRankFactors_c : public ISphStringExpr
{
/// hash type MUST BE IN SYNC with RankerState_Export_fn in sphinxsearch.cpp
CSphOrderedHash < CSphString, SphDocID_t, IdentityHash_fn, 256 > * m_pFactors;
explicit Expr_GetRankFactors_c ()
: m_pFactors ( NULL )
{}
virtual int StringEval ( const CSphMatch & tMatch, const BYTE ** ppStr ) const
{
assert ( ppStr );
if ( !m_pFactors )
{
*ppStr = NULL;
return 0;
}
CSphString * sVal = (*m_pFactors) ( tMatch.m_uDocID );
if ( !sVal )
{
*ppStr = NULL;
return 0;
}
int iLen = sVal->Length();
*ppStr = (const BYTE*)sVal->Leak();
m_pFactors->Delete ( tMatch.m_uDocID );
return iLen;
}
virtual void Command ( ESphExprCommand eCmd, void * pArg )
{
if ( eCmd==SPH_EXPR_SET_EXTRA_DATA )
static_cast<ISphExtra*>(pArg)->ExtraData ( EXTRA_GET_DATA_RANKFACTORS, (void**)&m_pFactors );
}
virtual bool IsStringPtr() const
{
return true;
}
virtual uint64_t GetHash ( const ISphSchema &, uint64_t, bool & bDisable )
{
bDisable = true; // disable caching for now, might add code to process if necessary
return 0;
}
};
struct Expr_GetPackedFactors_c : public ISphStringExpr
{
SphFactorHash_t * m_pHash;
explicit Expr_GetPackedFactors_c ()
: m_pHash ( NULL )
{}
virtual const DWORD * FactorEval ( const CSphMatch & tMatch ) const
{
if ( !m_pHash || !m_pHash->GetLength() )
return NULL;
SphFactorHashEntry_t * pEntry = (*m_pHash)[ (int)( tMatch.m_uDocID % m_pHash->GetLength() ) ];
assert ( pEntry );
while ( pEntry && pEntry->m_iId!=tMatch.m_uDocID )
pEntry = pEntry->m_pNext;
if ( !pEntry )
return NULL;
DWORD uDataLen = (BYTE *)pEntry - (BYTE *)pEntry->m_pData;
BYTE * pData = new BYTE[uDataLen];
memcpy ( pData, pEntry->m_pData, uDataLen );
return (DWORD *)pData;
}
virtual void Command ( ESphExprCommand eCmd, void * pArg )
{
if ( eCmd==SPH_EXPR_SET_EXTRA_DATA )
static_cast<ISphExtra*>(pArg)->ExtraData ( EXTRA_GET_DATA_PACKEDFACTORS, (void**)&m_pHash );
}
virtual bool IsStringPtr() const
{
return true;
}
virtual uint64_t GetHash ( const ISphSchema &, uint64_t, bool & bDisable )
{
bDisable = true; // disable caching for now, might add code to process if necessary
return 0;
}
};
struct Expr_BM25F_c : public ISphExpr
{
SphExtraDataRankerState_t m_tRankerState;
float m_fK1;
float m_fB;
float m_fWeightedAvgDocLen;
CSphVector<int> m_dWeights; ///< per field weights
SphFactorHash_t * m_pHash;
CSphVector<CSphNamedVariant> m_dFieldWeights;
Expr_BM25F_c ( float k1, float b, CSphVector<CSphNamedVariant> * pFieldWeights )
: m_pHash ( NULL )
{
// bind k1, b
m_fK1 = k1;
m_fB = b;
if ( pFieldWeights )
m_dFieldWeights.SwapData ( *pFieldWeights );
}
float Eval ( const CSphMatch & tMatch ) const
{
if ( !m_pHash || !m_pHash->GetLength() )
return 0.0f;
SphFactorHashEntry_t * pEntry = (*m_pHash)[ (int)( tMatch.m_uDocID % m_pHash->GetLength() ) ];
assert ( pEntry );
while ( pEntry && pEntry->m_iId!=tMatch.m_uDocID )
pEntry = pEntry->m_pNext;
if ( !pEntry )
return 0.0f;
SPH_UDF_FACTORS tUnpacked;
sphinx_factors_init ( &tUnpacked );
#ifndef NDEBUG
Verify ( sphinx_factors_unpack ( (const unsigned int*)pEntry->m_pData, &tUnpacked )==0 );
#else
sphinx_factors_unpack ( (const unsigned int*)pEntry->m_pData, &tUnpacked ); // fix MSVC Release warning
#endif
// compute document length
// OPTIMIZE? could precompute and store total dl in attrs, but at a storage cost
// OPTIMIZE? could at least share between multiple BM25F instances, if there are many
float dl = 0;
CSphAttrLocator tLoc = m_tRankerState.m_tFieldLensLoc;
if ( tLoc.m_iBitOffset>=0 )
{
for ( int i=0; i<m_tRankerState.m_iFields; i++ )
{
dl += tMatch.GetAttr ( tLoc ) * m_dWeights[i];
tLoc.m_iBitOffset += 32;
}
}
// compute (the current instance of) BM25F
float fRes = 0.0f;
for ( int iWord=0; iWord<m_tRankerState.m_iMaxQpos; iWord++ )
{
if ( !tUnpacked.term[iWord].keyword_mask )
continue;
// compute weighted TF
float tf = 0.0f;
for ( int i=0; i<m_tRankerState.m_iFields; i++ )
{
tf += tUnpacked.field_tf[ iWord + 1 + i * ( 1 + m_tRankerState.m_iMaxQpos ) ] * m_dWeights[i];
}
float idf = tUnpacked.term[iWord].idf; // FIXME? zeroed out for dupes!
fRes += tf / ( tf + m_fK1 * ( 1.0f - m_fB + m_fB * dl / m_fWeightedAvgDocLen ) ) * idf;
}
sphinx_factors_deinit ( &tUnpacked );
return fRes + 0.5f; // map to [0..1] range
}
virtual void Command ( ESphExprCommand eCmd, void * pArg )
{
if ( eCmd!=SPH_EXPR_SET_EXTRA_DATA )
return;
bool bGotHash = static_cast<ISphExtra*>(pArg)->ExtraData ( EXTRA_GET_DATA_PACKEDFACTORS, (void**)&m_pHash );
if ( !bGotHash )
return;
bool bGotState = static_cast<ISphExtra*>(pArg)->ExtraData ( EXTRA_GET_DATA_RANKER_STATE, (void**)&m_tRankerState );
if ( !bGotState )
return;
// bind weights
m_dWeights.Resize ( m_tRankerState.m_iFields );
m_dWeights.Fill ( 1 );
if ( m_dFieldWeights.GetLength() )
{
ARRAY_FOREACH ( i, m_dFieldWeights )
{
// FIXME? report errors if field was not found?
CSphString & sField = m_dFieldWeights[i].m_sKey;
int iField = m_tRankerState.m_pSchema->GetFieldIndex ( sField.cstr() );
if ( iField>=0 )
m_dWeights[iField] = m_dFieldWeights[i].m_iValue;
}
}
// compute weighted avgdl
m_fWeightedAvgDocLen = 1.0f;
if ( m_tRankerState.m_pFieldLens )
{
m_fWeightedAvgDocLen = 0.0f;
ARRAY_FOREACH ( i, m_dWeights )
m_fWeightedAvgDocLen += m_tRankerState.m_pFieldLens[i] * m_dWeights[i];
}
m_fWeightedAvgDocLen /= m_tRankerState.m_iTotalDocuments;
}
virtual uint64_t GetHash ( const ISphSchema &, uint64_t, bool & bDisable )
{
bDisable = true; // disable caching for now, might add code to process if necessary
return 0;
}
};
struct Expr_GetId_c : public ISphExpr
{
virtual float Eval ( const CSphMatch & tMatch ) const { return (float)tMatch.m_uDocID; }
virtual int IntEval ( const CSphMatch & tMatch ) const { return (int)tMatch.m_uDocID; }
virtual int64_t Int64Eval ( const CSphMatch & tMatch ) const { return (int64_t)tMatch.m_uDocID; }
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_GetId_c");
return CALC_DEP_HASHES();
}
};
struct Expr_GetWeight_c : public ISphExpr
{
virtual float Eval ( const CSphMatch & tMatch ) const { return (float)tMatch.m_iWeight; }
virtual int IntEval ( const CSphMatch & tMatch ) const { return (int)tMatch.m_iWeight; }
virtual int64_t Int64Eval ( const CSphMatch & tMatch ) const { return (int64_t)tMatch.m_iWeight; }
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME("Expr_GetWeight_c");
return CALC_DEP_HASHES();
}
};
//////////////////////////////////////////////////////////////////////////
struct Expr_Arglist_c : public ISphExpr
{
CSphVector<ISphExpr *> m_dArgs;
Expr_Arglist_c ( ISphExpr * pLeft, ISphExpr * pRight )
{
AddArgs ( pLeft );
AddArgs ( pRight );
}
~Expr_Arglist_c ()
{
ARRAY_FOREACH ( i, m_dArgs )
SafeRelease ( m_dArgs[i] );
}
void AddArgs ( ISphExpr * pExpr )
{
// not an arglist? just add it
if ( !pExpr->IsArglist() )
{
m_dArgs.Add ( pExpr );
return;
}
// arglist? take ownership of its args, and dismiss it
Expr_Arglist_c * pArgs = (Expr_Arglist_c *) pExpr;
ARRAY_FOREACH ( i, pArgs->m_dArgs )
{
m_dArgs.Add ( pArgs->m_dArgs[i] );
pArgs->m_dArgs[i] = NULL;
}
SafeRelease ( pExpr );
}
virtual bool IsArglist () const
{
return true;
}
virtual ISphExpr * GetArg ( int i ) const
{
if ( i>=m_dArgs.GetLength() )
return NULL;
return m_dArgs[i];
}
virtual int GetNumArgs() const
{
return m_dArgs.GetLength();
}
virtual float Eval ( const CSphMatch & ) const
{
assert ( 0 && "internal error: Eval() must not be explicitly called on arglist" );
return 0.0f;
}
virtual void Command ( ESphExprCommand eCmd, void * pArg )
{
ARRAY_FOREACH ( i, m_dArgs )
m_dArgs[i]->Command ( eCmd, pArg );
}
virtual uint64_t GetHash ( const ISphSchema &, uint64_t, bool & )
{
assert ( 0 && "internal error: GetHash() must not be explicitly called on arglist" );
return 0;
}
};
//////////////////////////////////////////////////////////////////////////
struct Expr_Unary_c : public ISphExpr
{
ISphExpr * m_pFirst;
const char * m_szExprName;
explicit Expr_Unary_c ( const char * szClassName, ISphExpr * pFirst )
: m_pFirst ( pFirst )
, m_szExprName ( szClassName )
{}
virtual ~Expr_Unary_c()
{
SafeRelease ( m_pFirst );
}
virtual void Command ( ESphExprCommand eCmd, void * pArg )
{
m_pFirst->Command ( eCmd, pArg );
}
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME_NOCHECK(m_szExprName);
CALC_CHILD_HASH(m_pFirst);
return CALC_DEP_HASHES();
}
};
struct Expr_Binary_c : public ISphExpr
{
ISphExpr * m_pFirst;
ISphExpr * m_pSecond;
const char * m_szExprName;
explicit Expr_Binary_c ( const char * szClassName, ISphExpr * pFirst, ISphExpr * pSecond )
: m_pFirst ( pFirst )
, m_pSecond ( pSecond )
, m_szExprName ( szClassName )
{}
virtual ~Expr_Binary_c()
{
SafeRelease ( m_pFirst );
SafeRelease ( m_pSecond );
}
virtual void Command ( ESphExprCommand eCmd, void * pArg )
{
m_pFirst->Command ( eCmd, pArg );
m_pSecond->Command ( eCmd, pArg );
}
virtual uint64_t GetHash ( const ISphSchema & tSorterSchema, uint64_t uPrevHash, bool & bDisable )
{
EXPR_CLASS_NAME_NOCHECK(m_szExprName);
CALC_CHILD_HASH(m_pFirst);
CALC_CHILD_HASH(m_pSecond);
return CALC_DEP_HASHES();
}
};
//////////////////////////////////////////////////////////////////////////
struct Expr_Crc32_c : public Expr_Unary_c
{
explicit Expr_Crc32_c ( ISphExpr * pFirst ) : Expr_Unary_c ( "Expr_Crc32_c", pFirst ) {}
virtual float Eval ( const CSphMatch & tMatch ) const { return (float)IntEval ( tMatch ); }
virtual int IntEval ( const CSphMatch & tMatch ) const
{
const BYTE * pStr;
int iLen = m_pFirst->StringEval ( tMatch, &pStr );
DWORD uCrc = sphCRC32 ( pStr, iLen );
if ( m_pFirst->IsStringPtr() )
SafeDeleteArray ( pStr );
return uCrc;
}
virtual int64_t Int64Eval ( const CSphMatch & tMatch ) const { return (int64_t)(DWORD)IntEval ( tMatch ); }
};
static inline int Fibonacci ( int i )
{
if ( i<0 )
return 0;
int f0 = 0;
int f1 = 1;
int j = 0;
for ( j=0; j+1<i; j+=2 )
{
f0 += f1; // f_j
f1 += f0; // f_{j+1}
}
return ( i & 1 ) ? f1 : f0;
}
struct Expr_Fibonacci_c : public Expr_Unary_c
{
explicit Expr_Fibonacci_c ( ISphExpr * pFirst ) : Expr_Unary_c ( "Expr_Fibonacci_c", pFirst ) {}
virtual float Eval ( const CSphMatch & tMatch ) const { return (float)IntEval ( tMatch ); }
virtual int IntEval ( const CSphMatch & tMatch ) const { return Fibonacci ( m_pFirst->IntEval ( tMatch ) ); }
virtual int64_t Int64Eval ( const CSphMatch & tMatch ) const { return IntEval ( tMatch ); }
};
struct Expr_ToString_c : public Expr_Unary_c
{
protected:
ESphAttr m_eArg;
mutable CSphStringBuilder m_sBuilder;
const BYTE * m_pStrings;
public:
Expr_ToString_c ( ISphExpr * pArg, ESphAttr eArg )
: Expr_Unary_c ( "Expr_ToString_c", pArg )
, m_eArg ( eArg )
, m_pStrings ( NULL )
{}
virtual float Eval ( const CSphMatch & ) const
{
assert ( 0 );
return 0.0f;
}
virtual int StringEval ( const CSphMatch & tMatch, const BYTE ** ppStr ) const
{
m_sBuilder.Clear();
int64_t iPacked = 0;
ESphJsonType eJson = JSON_NULL;
DWORD uOff = 0;
int iLen = 0;
switch ( m_eArg )
{
case SPH_ATTR_INTEGER: m_sBuilder.Appendf ( "%u", m_pFirst->IntEval ( tMatch ) ); break;
case SPH_ATTR_BIGINT: m_sBuilder.Appendf ( INT64_FMT, m_pFirst->Int64Eval ( tMatch ) ); break;
case SPH_ATTR_FLOAT: m_sBuilder.Appendf ( "%f", m_pFirst->Eval ( tMatch ) ); break;
case SPH_ATTR_UINT32SET:
case SPH_ATTR_INT64SET:
{
const DWORD * pValues = m_pFirst->MvaEval ( tMatch );
if ( !pValues || !*pValues )
break;
DWORD nValues = *pValues++;
assert (!( m_eArg==SPH_ATTR_INT64SET && ( nValues & 1 ) ));
// OPTIMIZE? minibuffer on stack, less allocs, manual formatting vs printf, etc
if ( m_eArg==SPH_ATTR_UINT32SET )
{
while ( nValues-- )
{
if ( m_sBuilder.Length() )
m_sBuilder += ",";
m_sBuilder.Appendf ( "%u", *pValues++ );
}
} else
{
for ( ; nValues; nValues-=2, pValues+=2 )
{
if ( m_sBuilder.Length() )
m_sBuilder += ",";
m_sBuilder.Appendf ( INT64_FMT, MVA_UPSIZE ( pValues ) );
}
}
}
break;
case SPH_ATTR_STRINGPTR:
return m_pFirst->StringEval ( tMatch, ppStr );
case SPH_ATTR_JSON_FIELD:
iPacked = m_pFirst->Int64Eval ( tMatch );
eJson = ESphJsonType ( iPacked>>32 );
uOff = (DWORD)iPacked;
if ( !uOff || eJson==JSON_NULL )
{
*ppStr = NULL;
iLen = 0;
} else
{
CSphVector<BYTE> dTmp;
sphJsonFieldFormat ( dTmp, m_pStrings+uOff, eJson, false );
if ( dTmp.GetLength() )
dTmp.Add ( '\0' );
iLen = dTmp.GetLength();
*ppStr = dTmp.LeakData();
}
return iLen;
default:
assert ( 0 && "unhandled arg type in TO_STRING()" );
break;
}
if ( !m_sBuilder.Length() )
{
*ppStr = NULL;
return 0;
}
*ppStr = (const BYTE *) CSphString ( m_sBuilder.cstr() ).Leak();
return m_sBuilder.Length();
}
virtual bool IsStringPtr() const
{
return true;
}
virtual void Command ( ESphExprCommand eCmd, void * pArg )
{
if ( eCmd==SPH_EXPR_SET_STRING_POOL )
m_pStrings = (const BYTE*)pArg;
m_pFirst->Command ( eCmd, pArg );
}
};
//////////////////////////////////////////////////////////////////////////
/// generic JSON value evaluation
/// can handle arbitrary stacks of jsoncol.key1.arr2[indexexpr3].key4[keynameexpr5]
/// m_dArgs holds the expressions that return actual accessors (either keynames or indexes)
/// m_dRetTypes holds their respective types
struct Expr_JsonField_c : public Expr_WithLocator_c
{
protected:
const BYTE * m_pStrings;
CSphVector<ISphExpr *> m_dArgs;
CSphVector<ESphAttr> m_dRetTypes;
public:
/// takes over the expressions
Expr_JsonField_c ( const CSphAttrLocator & tLocator, int iLocator, CSphVector<ISphExpr*> & dArgs, CSphVector<ESphAttr> & dRetTypes )
: Expr_WithLocator_c ( tLocator, iLocator )
, m_pStrings ( NULL )
{
assert ( dArgs.GetLength()==dRetTypes.GetLength() );
m_dArgs.SwapData ( dArgs );
m_dRetTypes.SwapData ( dRetTypes );
}
~Expr_JsonField_c ()
{
ARRAY_FOREACH ( i, m_dArgs )
SafeRelease ( m_dArgs[i] );
}
virtual void Command ( ESphExprCommand eCmd, void * pArg )
{
Expr_WithLocator_c::Command ( eCmd, pArg );
if ( eCmd==SPH_EXPR_SET_STRING_POOL )
m_pStrings = (const BYTE*)pArg;
else if ( eCmd==SPH_EXPR_GET_DEPENDENT_COLS && m_iLocator!=-1 )
static_cast < CSphVector<int>* > ( pArg )->Add ( m_iLocator );
ARRAY_FOREACH ( i, m_dArgs )
if ( m_dArgs[i] )
m_dArgs[i]->Command ( eCmd, pArg );