forked from manticoresoftware/manticoresearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchdsql.cpp
1988 lines (1634 loc) · 57.4 KB
/
searchdsql.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) 2017-2023, Manticore Software LTD (https://manticoresearch.com)
// 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 "searchdsql.h"
#include "sphinxint.h"
#include "sphinxplugin.h"
#include "searchdaemon.h"
#include "searchdddl.h"
#include "sphinxql_debug.h"
#include "sphinxql_second.h"
#include "sphinxql_extra.h"
extern int g_iAgentQueryTimeoutMs; // global (default). May be override by index-scope values, if one specified
void SqlNode_t::SetValueInt ( int64_t iValue )
{
m_uValue = abs(iValue);
m_bNegative = iValue<0;
}
void SqlNode_t::SetValueInt ( uint64_t uValue, bool bNegative )
{
m_uValue = uValue;
m_bNegative = bNegative;
}
int64_t SqlNode_t::GetValueInt() const
{
if ( m_bNegative )
{
if ( m_uValue > (uint64_t)LLONG_MAX )
return LLONG_MIN;
return -int64_t(m_uValue);
}
else
{
if ( m_uValue > (uint64_t)LLONG_MAX )
return LLONG_MAX;
return int64_t(m_uValue);
}
}
uint64_t SqlNode_t::GetValueUint() const
{
assert ( !m_bNegative );
return m_uValue;
}
void SqlNode_t::CopyValueInt ( const SqlNode_t & tRhs )
{
m_uValue = tRhs.m_uValue;
m_bNegative = tRhs.m_bNegative;
}
/////////////////////////////////////////////////////////////////////
void SqlInsert_t::SetValueInt ( uint64_t uValue, bool bNegative )
{
m_uValue = uValue;
m_bNegative = bNegative;
}
void SqlInsert_t::SetValueInt ( int64_t iValue )
{
m_uValue = abs(iValue);
m_bNegative = iValue<0;
}
int64_t SqlInsert_t::GetValueInt() const
{
if ( m_bNegative )
{
if ( m_uValue > (uint64_t)LLONG_MAX )
return LLONG_MIN;
return -int64_t(m_uValue);
}
else
{
if ( m_uValue > (uint64_t)LLONG_MAX )
return LLONG_MAX;
return int64_t(m_uValue);
}
}
uint64_t SqlInsert_t::GetValueUint() const
{
assert ( !m_bNegative );
return m_uValue;
}
void SqlInsert_t::CopyValueInt ( const SqlNode_t & tRhs )
{
m_uValue = tRhs.m_uValue;
m_bNegative = tRhs.m_bNegative;
}
/////////////////////////////////////////////////////////////////////
SqlStmt_t::SqlStmt_t()
{
m_tQuery.m_eMode = SPH_MATCH_EXTENDED2; // only new and shiny matching and sorting
m_tQuery.m_eSort = SPH_SORT_EXTENDED;
m_tQuery.m_sSortBy = "@weight desc"; // default order
m_tQuery.m_sOrderBy = "@weight desc";
m_tQuery.m_iAgentQueryTimeoutMs = g_iAgentQueryTimeoutMs;
m_tQuery.m_iRetryCount = -1;
m_tQuery.m_iRetryDelay = -1;
}
SqlStmt_t::~SqlStmt_t() = default;
bool SqlStmt_t::AddSchemaItem ( const char * psName )
{
m_dInsertSchema.Add ( psName );
CSphString & sAttr = m_dInsertSchema.Last();
sAttr.ToLower();
int iLen = sAttr.Length();
if ( iLen>1 && sAttr.cstr()[0] == '`' && sAttr.cstr()[iLen-1]=='`' )
sAttr = sAttr.SubString ( 1, iLen-2 );
m_iSchemaSz = m_dInsertSchema.GetLength();
return true; // stub; check if the given field actually exists in the schema
}
// check if the number of fields which would be inserted is in accordance to the given schema
bool SqlStmt_t::CheckInsertIntegrity()
{
// cheat: if no schema assigned, assume the size of schema as the size of the first row.
// (if it is wrong, it will be revealed later)
if ( !m_iSchemaSz )
m_iSchemaSz = m_dInsertValues.GetLength();
m_iRowsAffected++;
return m_dInsertValues.GetLength()==m_iRowsAffected*m_iSchemaSz;
}
//////////////////////////////////////////////////////////////////////////
SqlParserTraits_c::SqlParserTraits_c ( CSphVector<SqlStmt_t> & dStmt, const char* szQuery, CSphString* pError )
: m_pBuf ( szQuery )
, m_pParseError ( pError )
, m_dStmt ( dStmt )
{}
void SqlParserTraits_c::PushQuery()
{
assert ( m_dStmt.GetLength() || ( !m_pQuery && !m_pStmt ) );
// add new
m_dStmt.Add ();
m_pStmt = &m_dStmt.Last();
}
CSphString & SqlParserTraits_c::ToString ( CSphString & sRes, const SqlNode_t & tNode ) const
{
if ( tNode.m_iType>=0 )
sRes.SetBinary ( m_pBuf + tNode.m_iStart, tNode.m_iEnd - tNode.m_iStart );
else switch ( tNode.m_iType )
{
case SPHINXQL_TOK_COUNT: sRes = "@count"; break;
case SPHINXQL_TOK_GROUPBY: sRes = "@groupby"; break;
case SPHINXQL_TOK_WEIGHT: sRes = "@weight"; break;
default: assert ( 0 && "internal error: unknown parser ident code" );
}
return sRes;
}
CSphString SqlParserTraits_c::ToStringUnescape ( const SqlNode_t & tNode ) const
{
assert ( tNode.m_iType>=0 );
return SqlUnescape ( m_pBuf + tNode.m_iStart, tNode.m_iEnd - tNode.m_iStart );
}
void SqlParserTraits_c::ProcessParsingError ( const char* szMessage )
{
// 'wrong parser' is quite empiric - we fire it when from very beginning parser sees syntax error
// notice: szMessage here is NOT prefixed with "PXX:"
if ( ( m_pBuf == m_pLastTokenStart ) && ( strncmp ( szMessage, "syntax error", 12 ) == 0 ) )
m_bWrongParserSyntaxError = true;
m_pParseError->SetSprintf ( "%s %s near '%s'", m_sErrorHeader.cstr(), szMessage, m_pLastTokenStart ? m_pLastTokenStart : "(null)" );
// fixup TOK_xxx thingies
char* s = const_cast<char*> ( m_pParseError->cstr() );
char* d = s;
while ( *s )
{
if ( strncmp ( s, "TOK_", 4 ) == 0 )
s += 4;
else
*d++ = *s++;
}
*d = '\0';
}
bool SqlParserTraits_c::IsWrongSyntaxError() const noexcept
{
return m_bWrongParserSyntaxError;
}
void SqlParserTraits_c::DefaultOk ( std::initializer_list<const char*> sList )
{
for ( const char* sElem : sList )
m_pStmt->m_dInsertSchema.Add ( sElem );
m_pStmt->m_eStmt = STMT_DUMMY;
}
void SqlParserTraits_c::SetIndex ( const SqlNode_t& tNode ) const
{
ToString ( m_pStmt->m_sIndex, tNode );
// unquote index name
if ( ( tNode.m_iEnd - tNode.m_iStart ) > 2 && m_pStmt->m_sIndex.cstr()[0] == '\'' && m_pStmt->m_sIndex.cstr()[tNode.m_iEnd - tNode.m_iStart - 1] == '\'' )
m_pStmt->m_sIndex = m_pStmt->m_sIndex.SubString ( 1, m_pStmt->m_sIndex.Length() - 2 );
}
void SqlParserTraits_c::SetIndex ( const CSphString& sIndex ) const
{
auto iLen = sIndex.Length();
if ( iLen > 2 && sIndex.cstr()[0] == '\'' && sIndex.cstr()[iLen-1] == '\'' )
m_pStmt->m_sIndex = sIndex.SubString ( 1, iLen - 2 );
else
m_pStmt->m_sIndex = sIndex;
}
//////////////////////////////////////////////////////////////////////////
enum class Option_e : BYTE;
class SqlParser_c : public SqlParserTraits_c
{
public:
ESphCollation m_eCollation;
CSphVector<FilterTreeItem_t> m_dFilterTree;
CSphVector<int> m_dFiltersPerStmt;
bool m_bGotFilterOr = false;
public:
SqlParser_c ( CSphVector<SqlStmt_t> & dStmt, ESphCollation eCollation, const char* szQuery, CSphString* pError );
void PushQuery ();
void AddIndexHint ( SecondaryIndexType_e eType, bool bForce, const SqlNode_t & tValue );
void AddItem ( SqlNode_t * pExpr, ESphAggrFunc eFunc=SPH_AGGR_NONE, SqlNode_t * pStart=NULL, SqlNode_t * pEnd=NULL );
bool AddItem ( const char * pToken, SqlNode_t * pStart=NULL, SqlNode_t * pEnd=NULL );
bool AddCount ();
void AliasLastItem ( SqlNode_t * pAlias );
void AddInsval ( CSphVector<SqlInsert_t> & dVec, const SqlNode_t & tNode );
/// called on transition from an outer select to inner select
void ResetSelect();
/// called every time we capture a select list item
/// (i think there should be a simpler way to track these though)
void SetSelect ( SqlNode_t * pStart, SqlNode_t * pEnd=NULL );
bool AddSchemaItem ( SqlNode_t * pNode );
bool SetMatch ( const SqlNode_t & tValue );
void AddConst ( int iList, const SqlNode_t& tValue );
void SetLocalStatement ( const SqlNode_t & tName );
bool AddFloatRangeFilter ( const SqlNode_t & tAttr, float fMin, float fMax, bool bHasEqual, bool bExclude=false );
bool AddFloatFilterGreater ( const SqlNode_t & tAttr, float fVal, bool bHasEqual );
bool AddFloatFilterLesser ( const SqlNode_t & tAttr, float fVal, bool bHasEqual );
bool AddIntRangeFilter ( const SqlNode_t & tAttr, int64_t iMin, int64_t iMax, bool bExclude );
bool AddIntFilterGreater ( const SqlNode_t & tAttr, int64_t iVal, bool bHasEqual );
bool AddIntFilterLesser ( const SqlNode_t & tAttr, int64_t iVal, bool bHasEqual );
bool AddUservarFilter ( const SqlNode_t & tCol, const SqlNode_t & tVar, bool bExclude );
void AddGroupBy ( const SqlNode_t & tGroupBy );
CSphFilterSettings * AddFilter ( const SqlNode_t & tCol, ESphFilter eType );
bool AddStringFilter ( const SqlNode_t & tCol, const SqlNode_t & tVal, bool bExclude );
CSphFilterSettings * AddValuesFilter ( const SqlNode_t & tCol ) { return AddFilter ( tCol, SPH_FILTER_VALUES ); }
bool AddStringListFilter ( const SqlNode_t & tCol, SqlNode_t & tVal, StrList_e eType, bool bInverse=false );
bool AddNullFilter ( const SqlNode_t & tCol, bool bEqualsNull );
void AddHaving ();
bool AddDistinct ( SqlNode_t * pNewExpr, SqlNode_t * pStart, SqlNode_t * pEnd );
void AddDistinct ( SqlNode_t * pNewExpr );
bool MaybeAddFacetDistinct();
void FilterGroup ( SqlNode_t & tNode, SqlNode_t & tExpr );
void FilterOr ( SqlNode_t & tNode, const SqlNode_t & tLeft, const SqlNode_t & tRight );
void FilterAnd ( SqlNode_t & tNode, const SqlNode_t & tLeft, const SqlNode_t & tRight );
void SetOp ( SqlNode_t & tNode );
bool SetOldSyntax();
bool SetNewSyntax();
bool IsGoodSyntax();
bool IsDeprecatedSyntax() const;
int AllocNamedVec ();
CSphVector<CSphNamedInt> & GetNamedVec ( int iIndex );
void FreeNamedVec ( int iIndex );
void GenericStatement ( SqlNode_t * pNode );
void SwapSubkeys();
void AddUpdatedAttr ( const SqlNode_t & tName, ESphAttr eType ) const;
void UpdateMVAAttr ( const SqlNode_t & tName, const SqlNode_t& dValues );
void UpdateStringAttr ( const SqlNode_t & tCol, const SqlNode_t & tStr );
void SetGroupbyLimit ( int iLimit );
void SetLimit ( int iOffset, int iLimit );
float ToFloat ( const SqlNode_t & tNode ) const;
int64_t DotGetInt ( const SqlNode_t & tNode ) const;
void AddStringSubkey ( const SqlNode_t & tNode ) const;
void AddIntSubkey ( const SqlNode_t & tNode ) const;
void AddDotIntSubkey ( const SqlNode_t & tNode ) const;
void AddComment ( const SqlNode_t* tNode );
private:
bool m_bGotQuery = false;
BYTE m_uSyntaxFlags = 0;
bool m_bNamedVecBusy = false;
CSphVector<CSphNamedInt> m_dNamedVec;
void AutoAlias ( CSphQueryItem & tItem, SqlNode_t * pStart, SqlNode_t * pEnd );
bool CheckOption ( Option_e eOption ) const override;
SqlStmt_e GetSecondaryStmt () const;
};
#define YYSTYPE SqlNode_t
// unused parameter, simply to avoid type clash between all my yylex() functions
#define YY_DECL static int my_lex ( YYSTYPE * lvalp, void * yyscanner, SqlParser_c * pParser )
#if _WIN32
#define YY_NO_UNISTD_H 1
#endif
#include "flexsphinxql.c"
static void yyerror ( SqlParserTraits_c * pParser, const char * sMessage )
{
// flex put a zero at last token boundary; make it undo that
yylex_unhold ( pParser->m_pScanner );
pParser->ProcessParsingError(sMessage);
}
#ifndef NDEBUG
// using a proxy to be possible to debug inside yylex
static int yylex ( YYSTYPE * lvalp, SqlParser_c * pParser )
{
int res = my_lex ( lvalp, pParser->m_pScanner, pParser );
return res;
}
#else
static int yylex ( YYSTYPE * lvalp, SqlParser_c * pParser )
{
return my_lex ( lvalp, pParser->m_pScanner, pParser );
}
#endif
#include "bissphinxql.c"
//////////////////////////////////////////////////////////////////////////
SqlParser_c::SqlParser_c ( CSphVector<SqlStmt_t> & dStmt, ESphCollation eCollation, const char* szQuery, CSphString* pError )
: SqlParserTraits_c ( dStmt, szQuery, pError )
, m_eCollation ( eCollation )
{
assert ( m_dStmt.IsEmpty() );
PushQuery ();
m_sErrorHeader = "P01:";
}
void SqlParser_c::PushQuery ()
{
assert ( m_dStmt.GetLength() || ( !m_pQuery && !m_pStmt ) );
// post set proper result-set order
if ( m_dStmt.GetLength() && m_pQuery )
{
if ( m_pQuery->m_sGroupBy.IsEmpty() )
m_pQuery->m_sSortBy = m_pQuery->m_sOrderBy;
else
m_pQuery->m_sGroupSortBy = m_pQuery->m_sOrderBy;
m_dFiltersPerStmt.Add ( m_dFilterTree.GetLength() );
}
SqlParserTraits_c::PushQuery();
m_pQuery = &m_pStmt->m_tQuery;
m_pQuery->m_eCollation = m_eCollation;
m_bGotQuery = false;
}
static bool CheckInteger ( const CSphString & sOpt, const CSphString & sVal, CSphString & sError )
{
const char * p = sVal.cstr();
while ( sphIsInteger ( *p++ ) )
p++;
if ( *p )
{
sError.SetSprintf ( "%s value should be a number: '%s'", sOpt.cstr(), sVal.cstr() );
return false;
}
return true;
}
bool SqlParserTraits_c::CheckInteger ( const CSphString & sOpt, const CSphString & sVal ) const
{
return ::CheckInteger ( sOpt, sVal, *m_pParseError );
}
float SqlParser_c::ToFloat ( const SqlNode_t & tNode ) const
{
return (float) strtod ( m_pBuf+tNode.m_iStart, nullptr );
}
int64_t SqlParser_c::DotGetInt ( const SqlNode_t & tNode ) const
{
return (int64_t) strtoull ( m_pBuf+tNode.m_iStart+1, nullptr, 10 );
}
void SqlParser_c::AddStringSubkey ( const SqlNode_t & tNode ) const
{
auto& sKey = m_pStmt->m_dStringSubkeys.Add();
ToString ( sKey, tNode );
}
void SqlParser_c::AddIntSubkey ( const SqlNode_t & tNode ) const
{
m_pStmt->m_dIntSubkeys.Add ( tNode.GetValueInt() );
}
void SqlParser_c::AddDotIntSubkey ( const SqlNode_t & tNode ) const
{
m_pStmt->m_dIntSubkeys.Add ( DotGetInt ( tNode ) );
}
/// hashes for all options
enum class Option_e : BYTE
{
AGENT_QUERY_TIMEOUT = 0,
BOOLEAN_SIMPLIFY,
COLUMNS,
COMMENT,
CUTOFF,
DEBUG_NO_PAYLOAD, // fixme! document
EXPAND_KEYWORDS,
FIELD_WEIGHTS,
FORMAT,
GLOBAL_IDF,
IDF,
IGNORE_NONEXISTENT_COLUMNS,
IGNORE_NONEXISTENT_INDEXES, // fixme! document!
INDEX_WEIGHTS,
LOCAL_DF,
LOW_PRIORITY,
MAX_MATCHES,
MAX_PREDICTED_TIME,
MAX_QUERY_TIME,
MORPHOLOGY,
RAND_SEED,
RANKER,
RETRY_COUNT,
RETRY_DELAY,
REVERSE_SCAN,
SORT_METHOD,
STRICT_, // dash added because of windows
SYNC,
THREADS,
TOKEN_FILTER,
TOKEN_FILTER_OPTIONS,
NOT_ONLY_ALLOWED,
STORE,
ACCURATE_AGG,
MAXMATCH_THRESH,
DISTINCT_THRESH,
THREADS_EX,
SWITCHOVER,
INVALID_OPTION
};
static SmallStringHash_T<Option_e, (BYTE) Option_e::INVALID_OPTION * 2> g_hParseOption;
void InitParserOption()
{
const char * dOptions[(BYTE) Option_e::INVALID_OPTION] = { "agent_query_timeout", "boolean_simplify",
"columns", "comment", "cutoff", "debug_no_payload", "expand_keywords", "field_weights", "format", "global_idf",
"idf", "ignore_nonexistent_columns", "ignore_nonexistent_indexes", "index_weights", "local_df", "low_priority",
"max_matches", "max_predicted_time", "max_query_time", "morphology", "rand_seed", "ranker", "retry_count",
"retry_delay", "reverse_scan", "sort_method", "strict", "sync", "threads", "token_filter", "token_filter_options",
"not_terms_only_allowed", "store", "accurate_aggregation", "max_matches_increase_threshold", "distinct_precision_threshold",
"threads_ex", "switchover" };
for ( BYTE i = 0u; i<(BYTE) Option_e::INVALID_OPTION; ++i )
g_hParseOption.Add ( (Option_e) i, dOptions[i] );
}
static Option_e ParseOption ( const CSphString& sOpt )
{
auto * pCol = g_hParseOption ( sOpt );
return ( pCol ? *pCol : Option_e::INVALID_OPTION );
}
static bool CheckOption ( SqlStmt_e eStmt, Option_e eOption )
{
// trick! following vectors must be sorted, as BinarySearch used to determine presence of a value.
static Option_e dDeleteOptions[] = { Option_e::STORE };
static Option_e dUpdateOptions[] = { Option_e::AGENT_QUERY_TIMEOUT, Option_e::BOOLEAN_SIMPLIFY, Option_e::COMMENT,
Option_e::CUTOFF, Option_e::DEBUG_NO_PAYLOAD, Option_e::EXPAND_KEYWORDS, Option_e::FIELD_WEIGHTS,
Option_e::GLOBAL_IDF, Option_e::IDF, Option_e::IGNORE_NONEXISTENT_COLUMNS,
Option_e::IGNORE_NONEXISTENT_INDEXES, Option_e::INDEX_WEIGHTS, Option_e::LOCAL_DF, Option_e::LOW_PRIORITY,
Option_e::MAX_MATCHES, Option_e::MAX_PREDICTED_TIME, Option_e::MAX_QUERY_TIME, Option_e::MORPHOLOGY,
Option_e::RAND_SEED, Option_e::RANKER, Option_e::RETRY_COUNT, Option_e::RETRY_DELAY, Option_e::REVERSE_SCAN,
Option_e::SORT_METHOD, Option_e::STRICT_, Option_e::THREADS, Option_e::TOKEN_FILTER,
Option_e::NOT_ONLY_ALLOWED };
static Option_e dSelectOptions[] = { Option_e::AGENT_QUERY_TIMEOUT, Option_e::BOOLEAN_SIMPLIFY, Option_e::COLUMNS, Option_e::COMMENT,
Option_e::CUTOFF, Option_e::DEBUG_NO_PAYLOAD, Option_e::EXPAND_KEYWORDS, Option_e::FIELD_WEIGHTS, Option_e::FORMAT,
Option_e::GLOBAL_IDF, Option_e::IDF, Option_e::IGNORE_NONEXISTENT_INDEXES, Option_e::INDEX_WEIGHTS,
Option_e::LOCAL_DF, Option_e::LOW_PRIORITY, Option_e::MAX_MATCHES, Option_e::MAX_PREDICTED_TIME,
Option_e::MAX_QUERY_TIME, Option_e::MORPHOLOGY, Option_e::RAND_SEED, Option_e::RANKER,
Option_e::RETRY_COUNT, Option_e::RETRY_DELAY, Option_e::REVERSE_SCAN, Option_e::SORT_METHOD,
Option_e::THREADS, Option_e::TOKEN_FILTER, Option_e::NOT_ONLY_ALLOWED, Option_e::ACCURATE_AGG,
Option_e::MAXMATCH_THRESH, Option_e::DISTINCT_THRESH, Option_e::THREADS_EX};
static Option_e dInsertOptions[] = { Option_e::TOKEN_FILTER_OPTIONS };
static Option_e dOptimizeOptions[] = { Option_e::CUTOFF, Option_e::SYNC };
static Option_e dShowOptions[] = { Option_e::COLUMNS, Option_e::FORMAT };
static Option_e dReloadOptions[] = { Option_e::SWITCHOVER };
#define CHKOPT( _set, _val ) VecTraits_T<Option_e> (_set, sizeof(_set)).BinarySearch (_val)!=nullptr
switch ( eStmt )
{
case STMT_DELETE:
return CHKOPT( dDeleteOptions, eOption );
case STMT_UPDATE:
return CHKOPT( dUpdateOptions, eOption );
case STMT_SELECT:
return CHKOPT( dSelectOptions, eOption );
case STMT_INSERT:
case STMT_REPLACE:
return CHKOPT( dInsertOptions, eOption );
case STMT_OPTIMIZE_INDEX:
return CHKOPT( dOptimizeOptions, eOption );
case STMT_RELOAD_INDEX:
return CHKOPT( dReloadOptions, eOption );
case STMT_EXPLAIN:
case STMT_SHOW_PLAN:
case STMT_SHOW_THREADS:
return CHKOPT( dShowOptions, eOption );
default:
return false;
}
#undef CHKOPT
}
// if query is special, like 'select .. from @@system.threads', it can adopt options for 'show threads' also,
// so, provide stmt for extended validation of the option in this case.
SqlStmt_e SqlParser_c::GetSecondaryStmt () const
{
if ( m_pQuery->m_dStringSubkeys.any_of ([] (const CSphString& s) { return s==".threads"; }))
return STMT_SHOW_THREADS;
return STMT_PARSE_ERROR;
}
bool SqlParserTraits_c::CheckOption ( Option_e eOption ) const
{
assert ( m_pStmt );
return ::CheckOption ( m_pStmt->m_eStmt, eOption );
}
bool SqlParser_c::CheckOption ( Option_e eOption ) const
{
assert ( m_pStmt );
auto bRes = ::CheckOption ( m_pStmt->m_eStmt, eOption );
if ( bRes )
return true;
if ( m_pStmt->m_eStmt != STMT_SELECT )
return false;
return ::CheckOption ( GetSecondaryStmt(), eOption );
}
static auto fnFailer ( CSphString& sError )
{
return [&sError] ( const char* sTemplate, ... ) {
va_list ap;
va_start ( ap, sTemplate );
sError.SetSprintfVa ( sTemplate, ap );
va_end ( ap );
return AddOption_e::FAILED;
};
}
#ifdef FAILED
#undef FAILED
#endif
AddOption_e AddOption ( CSphQuery & tQuery, const CSphString & sOpt, const CSphString & sValue, int64_t iValue, SqlStmt_e eStmt, CSphString & sError )
{
auto FAILED = fnFailer (sError);
auto eOpt = ParseOption ( sOpt );
if ( !CheckOption ( eStmt, eOpt ) )
return FAILED ( "unknown option '%s'", sOpt.cstr () );
const Option_e dIntegerOptions[] =
{
Option_e::MAX_MATCHES, Option_e::CUTOFF, Option_e::MAX_QUERY_TIME, Option_e::RETRY_COUNT,
Option_e::RETRY_DELAY, Option_e::IGNORE_NONEXISTENT_COLUMNS, Option_e::AGENT_QUERY_TIMEOUT, Option_e::MAX_PREDICTED_TIME,
Option_e::BOOLEAN_SIMPLIFY, Option_e::GLOBAL_IDF, Option_e::LOCAL_DF, Option_e::IGNORE_NONEXISTENT_INDEXES,
Option_e::STRICT_, Option_e::COLUMNS, Option_e::RAND_SEED, Option_e::SYNC, Option_e::EXPAND_KEYWORDS,
Option_e::THREADS, Option_e::NOT_ONLY_ALLOWED, Option_e::LOW_PRIORITY, Option_e::DEBUG_NO_PAYLOAD,
Option_e::ACCURATE_AGG, Option_e::MAXMATCH_THRESH, Option_e::DISTINCT_THRESH, Option_e::SWITCHOVER,
};
bool bFound = ::any_of ( dIntegerOptions, [eOpt] ( auto i ) { return i == eOpt; } );
if ( !bFound )
return AddOption_e::NOT_FOUND;
if ( sValue.cstr() && !CheckInteger ( sOpt, sValue, sError ) )
return AddOption_e::FAILED;
switch ( eOpt )
{
case Option_e::MAX_MATCHES: // else if ( sOpt=="max_matches" )
tQuery.m_iMaxMatches = (int)iValue;
tQuery.m_bExplicitMaxMatches = true;
break;
case Option_e::DEBUG_NO_PAYLOAD:
if ( iValue )
tQuery.m_uDebugFlags |= QUERY_DEBUG_NO_PAYLOAD;
else
tQuery.m_uDebugFlags &= ~QUERY_DEBUG_NO_PAYLOAD;
break;
case Option_e::CUTOFF: tQuery.m_iCutoff = (int)iValue; break;
case Option_e::MAX_QUERY_TIME: tQuery.m_uMaxQueryMsec = (int)iValue; break;
case Option_e::RETRY_COUNT: tQuery.m_iRetryCount = (int)iValue; break;
case Option_e::RETRY_DELAY: tQuery.m_iRetryDelay = (int)iValue; break;
case Option_e::IGNORE_NONEXISTENT_COLUMNS: tQuery.m_bIgnoreNonexistent = iValue!=0; break;
case Option_e::AGENT_QUERY_TIMEOUT: tQuery.m_iAgentQueryTimeoutMs = (int)iValue; break;
case Option_e::MAX_PREDICTED_TIME: tQuery.m_iMaxPredictedMsec = int ( iValue > INT_MAX ? INT_MAX : iValue ); break;
case Option_e::BOOLEAN_SIMPLIFY: tQuery.m_bSimplify = iValue!=0; break;
case Option_e::GLOBAL_IDF: tQuery.m_bGlobalIDF = iValue!=0; break;
case Option_e::LOCAL_DF: tQuery.m_bLocalDF = iValue!=0; break;
case Option_e::IGNORE_NONEXISTENT_INDEXES: tQuery.m_bIgnoreNonexistentIndexes = iValue!=0; break;
case Option_e::STRICT_: tQuery.m_bStrict = iValue!=0; break;
case Option_e::SYNC: tQuery.m_bSync = iValue!=0; break;
case Option_e::EXPAND_KEYWORDS: tQuery.m_eExpandKeywords = ( iValue!=0 ? QUERY_OPT_ENABLED : QUERY_OPT_DISABLED ); break;
case Option_e::THREADS: tQuery.m_iCouncurrency = (int)iValue; break;
case Option_e::NOT_ONLY_ALLOWED: tQuery.m_bNotOnlyAllowed = iValue!=0; break;
case Option_e::RAND_SEED: tQuery.m_iRandSeed = int64_t(DWORD(iValue)); break;
case Option_e::LOW_PRIORITY: tQuery.m_bLowPriority = iValue!=0; break;
case Option_e::ACCURATE_AGG: tQuery.m_bAccurateAggregation = iValue!=0; tQuery.m_bExplicitAccurateAggregation = true; break;
case Option_e::MAXMATCH_THRESH: tQuery.m_iMaxMatchThresh = iValue; break;
case Option_e::DISTINCT_THRESH: tQuery.m_iDistinctThresh = iValue; tQuery.m_bExplicitDistinctThresh = true; break;
case Option_e::THREADS_EX: tQuery.m_iCouncurrency = (int)iValue; break;
default:
return AddOption_e::NOT_FOUND;
}
return AddOption_e::ADDED;
}
AddOption_e AddOption ( CSphQuery & tQuery, const CSphString & sOpt, const CSphString & sVal, const std::function<CSphString ()> & fnGetUnescaped, SqlStmt_e eStmt, CSphString & sError )
{
auto FAILED = fnFailer ( sError );
auto eOpt = ParseOption ( sOpt );
if ( !::CheckOption ( eStmt, eOpt ) )
return FAILED ( "unknown option '%s'", sOpt.cstr () );
// OPTIMIZE? hash possible sOpt choices?
switch ( eOpt )
{
case Option_e::RANKER:
tQuery.m_eRanker = SPH_RANK_TOTAL;
for ( int iRanker = SPH_RANK_PROXIMITY_BM25; iRanker<=SPH_RANK_SPH04; iRanker++ )
if ( sVal==sphGetRankerName ( ESphRankMode ( iRanker ) ) )
{
tQuery.m_eRanker = ESphRankMode ( iRanker );
break;
}
if ( tQuery.m_eRanker==SPH_RANK_TOTAL )
{
if ( sVal==sphGetRankerName ( SPH_RANK_EXPR ) || sVal==sphGetRankerName ( SPH_RANK_EXPORT ) )
return FAILED ( "missing ranker expression (use OPTION ranker=expr('1+2') for example)" );
else if ( sphPluginExists ( PLUGIN_RANKER, sVal.cstr() ) )
{
tQuery.m_eRanker = SPH_RANK_PLUGIN;
tQuery.m_sUDRanker = sVal;
}
return FAILED ( "unknown ranker '%s'", sVal.cstr() );
}
break;
case Option_e::TOKEN_FILTER: // tokfilter = hello.dll:hello:some_opts
{
StrVec_t dParams;
if ( !sphPluginParseSpec ( sVal, dParams, sError ) )
return AddOption_e::FAILED;
if ( !dParams.GetLength() )
return FAILED ( "missing token filter spec string" );
tQuery.m_sQueryTokenFilterLib = dParams[0];
tQuery.m_sQueryTokenFilterName = dParams[1];
tQuery.m_sQueryTokenFilterOpts = dParams[2];
}
break;
case Option_e::REVERSE_SCAN: //} else if ( sOpt=="reverse_scan" )
return FAILED ( "reverse_scan is deprecated" );
case Option_e::COMMENT: //} else if ( sOpt=="comment" )
tQuery.m_sComment = fnGetUnescaped();
break;
case Option_e::SORT_METHOD: //} else if ( sOpt=="sort_method" )
if ( sVal=="pq" ) tQuery.m_bSortKbuffer = false;
else if ( sVal=="kbuffer" ) tQuery.m_bSortKbuffer = true;
else
return FAILED ( "unknown sort_method=%s (known values are pq, kbuffer)", sVal.cstr() );
break;
case Option_e::IDF: //} else if ( sOpt=="idf" )
{
StrVec_t dOpts;
sphSplit ( dOpts, sVal.cstr() );
ARRAY_FOREACH ( i, dOpts )
{
if ( dOpts[i]=="normalized" )
tQuery.m_bPlainIDF = false;
else if ( dOpts[i]=="plain" )
tQuery.m_bPlainIDF = true;
else if ( dOpts[i]=="tfidf_normalized" )
tQuery.m_bNormalizedTFIDF = true;
else if ( dOpts[i]=="tfidf_unnormalized" )
tQuery.m_bNormalizedTFIDF = false;
else
return FAILED ( "unknown flag %s in idf=%s (known values are plain, normalized, tfidf_normalized, tfidf_unnormalized)", dOpts[i].cstr(), sVal.cstr() );
}
}
break;
case Option_e::MORPHOLOGY: //} else if ( sOpt=="morphology" )
if ( sVal=="none" )
tQuery.m_eExpandKeywords = QUERY_OPT_MORPH_NONE;
else
return FAILED ( "morphology could be only disabled with option none, got %s", sVal.cstr() );
break;
case Option_e::STORE: //} else if ( sOpt=="store" )
tQuery.m_sStore = sVal;
break;
case Option_e::THREADS_EX:
std::tie ( tQuery.m_tMainDispatcher, tQuery.m_tPseudoShardingDispatcher ) = Dispatcher::ParseTemplates ( sVal.cstr() );
break;
default:
return AddOption_e::NOT_FOUND;
}
return AddOption_e::ADDED;
}
AddOption_e AddOption ( CSphQuery & tQuery, const CSphString & sOpt, CSphVector<CSphNamedInt> & dNamed, SqlStmt_e eStmt, CSphString & sError )
{
auto FAILED = fnFailer ( sError );
auto eOpt = ParseOption ( sOpt );
if ( !::CheckOption ( eStmt, eOpt ) )
return FAILED ( "unknown option '%s'", sOpt.cstr () );
switch ( eOpt )
{
case Option_e::FIELD_WEIGHTS: tQuery.m_dFieldWeights.SwapData ( dNamed ); break;
case Option_e::INDEX_WEIGHTS: tQuery.m_dIndexWeights.SwapData ( dNamed ); break;
default:
return AddOption_e::NOT_FOUND;
}
return AddOption_e::ADDED;
}
AddOption_e AddOptionRanker ( CSphQuery & tQuery, const CSphString & sOpt, const CSphString & sVal, const std::function<CSphString ()> & fnGetUnescaped, SqlStmt_e eStmt, CSphString & sError )
{
auto FAILED = fnFailer ( sError );
auto eOpt = ParseOption ( sOpt );
if ( !::CheckOption ( eStmt, eOpt ) )
return FAILED ( "unknown option '%s'", sOpt.cstr () );
if ( eOpt==Option_e::RANKER )
{
if ( sVal=="expr" || sVal=="export" )
{
tQuery.m_eRanker = sVal=="expr" ? SPH_RANK_EXPR : SPH_RANK_EXPORT;
tQuery.m_sRankerExpr = fnGetUnescaped();
return AddOption_e::ADDED;
} else if ( sphPluginExists ( PLUGIN_RANKER, sVal.cstr() ) )
{
tQuery.m_eRanker = SPH_RANK_PLUGIN;
tQuery.m_sUDRanker = sVal;
tQuery.m_sUDRankerOpts = fnGetUnescaped();
return AddOption_e::ADDED;
}
}
return AddOption_e::NOT_FOUND;
}
bool SqlParserTraits_c::AddOption ( const SqlNode_t & tIdent, const SqlNode_t & tValue )
{
CSphString sOpt, sVal;
ToString ( sOpt, tIdent ).ToLower();
ToString ( sVal, tValue ).ToLower().Unquote();
auto eOpt = ParseOption ( sOpt );
if ( !CheckOption ( eOpt ) )
{
m_pParseError->SetSprintf ( "unknown option '%s'", sOpt.cstr () );
return false;
}
AddOption_e eAddRes;
eAddRes = ::AddOption ( *m_pQuery, sOpt, sVal, [this,tValue]{ return ToStringUnescape(tValue); }, m_pStmt->m_eStmt, *m_pParseError );
if ( eAddRes==AddOption_e::FAILED )
return false;
else if ( eAddRes==AddOption_e::ADDED )
return true;
eAddRes = ::AddOption ( *m_pQuery, sOpt, sVal, tValue.GetValueInt(), m_pStmt->m_eStmt, *m_pParseError );
if ( eAddRes==AddOption_e::FAILED )
return false;
else if ( eAddRes==AddOption_e::ADDED )
return true;
// OPTIMIZE? hash possible sOpt choices?
switch ( eOpt )
{
case Option_e::COLUMNS: //} else if ( sOpt=="columns" ) // for SHOW THREADS
if ( !CheckInteger ( sOpt, sVal ) )
return false;
m_pStmt->m_iThreadsCols = Max ( (int)tValue.GetValueInt(), 0 );
break;
case Option_e::FORMAT: //} else if ( sOpt=="format" ) // for SHOW THREADS
m_pStmt->m_sThreadFormat = sVal;
break;
case Option_e::TOKEN_FILTER_OPTIONS: //} else if ( sOpt=="token_filter_options" )
m_pStmt->m_sStringParam = sVal;
break;
case Option_e::SWITCHOVER:
m_pStmt->m_iIntParam = tValue.GetValueInt() ? 1 : 0;
break;
default: //} else
m_pParseError->SetSprintf ( "unknown option '%s' (or bad argument type)", sOpt.cstr() );
return false;
}
return true;
}
bool SqlParserTraits_c::AddOption ( const SqlNode_t & tIdent, const SqlNode_t & tValue, const SqlNode_t & tArg )
{
CSphString sOpt, sVal;
ToString ( sOpt, tIdent ).ToLower();
ToString ( sVal, tValue ).ToLower().Unquote();
auto eOpt = ParseOption ( sOpt );
if ( !CheckOption ( eOpt ) )
{
m_pParseError->SetSprintf ( "unknown option '%s'", sOpt.cstr () );
return false;
}
AddOption_e eAdd = ::AddOptionRanker ( *m_pQuery, sOpt, sVal, [this,tArg]{ return ToStringUnescape(tArg); }, m_pStmt->m_eStmt, *m_pParseError );
if ( eAdd==AddOption_e::NOT_FOUND )
m_pParseError->SetSprintf ( "unknown option '%s' (or bad argument type)", sOpt.cstr() );
return eAdd==AddOption_e::ADDED;
}
bool SqlParserTraits_c::AddOption ( const SqlNode_t & tIdent, CSphVector<CSphNamedInt> & dNamed )
{
CSphString sOpt;
ToString ( sOpt, tIdent ).ToLower ();
auto eOpt = ParseOption ( sOpt );
if ( !CheckOption ( eOpt ) )
{
m_pParseError->SetSprintf ( "unknown option '%s'", sOpt.cstr () );
return false;
}
AddOption_e eAdd = ::AddOption ( *m_pQuery, sOpt, dNamed, m_pStmt->m_eStmt, *m_pParseError );
if ( eAdd==AddOption_e::NOT_FOUND )
m_pParseError->SetSprintf ( "unknown option '%s' (or bad argument type)", sOpt.cstr() );
return eAdd==AddOption_e::ADDED;
}
void SqlParser_c::AddIndexHint ( SecondaryIndexType_e eType, bool bForce, const SqlNode_t & tValue )
{
CSphString sIndexes;
ToString ( sIndexes, tValue );
StrVec_t dIndexes;
sphSplit ( dIndexes, sIndexes.cstr() );
for ( const auto & i : dIndexes )
{
IndexHint_t & tHint = m_pQuery->m_dIndexHints.Add();
tHint.m_sIndex = i;
tHint.m_eType = eType;
tHint.m_bForce = bForce;
}
}
void SqlParser_c::AliasLastItem ( SqlNode_t * pAlias )
{
if ( pAlias )
{
CSphQueryItem & tItem = m_pQuery->m_dItems.Last();
tItem.m_sAlias.SetBinary ( m_pBuf + pAlias->m_iStart, pAlias->m_iEnd - pAlias->m_iStart );
tItem.m_sAlias.ToLower();
SetSelect ( pAlias );
}
}
void SqlParser_c::AddInsval ( CSphVector<SqlInsert_t> & dVec, const SqlNode_t & tNode )
{
SqlInsert_t & tIns = dVec.Add();
tIns.m_iType = tNode.m_iType;
tIns.CopyValueInt(tNode);
tIns.m_fVal = tNode.m_fValue;
if ( tIns.m_iType==TOK_QUOTED_STRING )
tIns.m_sVal = ToStringUnescape ( tNode );
tIns.m_pVals = tNode.m_pValues;
}
void SqlParser_c::ResetSelect()
{