-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathqueryParser.ts
802 lines (723 loc) · 23.2 KB
/
queryParser.ts
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
import lucene from '@hyperdx/lucene';
import SqlString from 'sqlstring';
import { convertCHTypeToPrimitiveJSType, JSDataType } from '@/clickhouse';
import { Metadata } from '@/metadata';
import { splitAndTrimWithBracket } from '@/utils';
function encodeSpecialTokens(query: string): string {
return query
.replace(/\\\\/g, 'HDX_BACKSLASH_LITERAL')
.replace('http://', 'http_COLON_//')
.replace('https://', 'https_COLON_//')
.replace(/localhost:(\d{1,5})/, 'localhost_COLON_$1')
.replace(/\\:/g, 'HDX_COLON');
}
function decodeSpecialTokens(query: string): string {
return query
.replace(/\\"/g, '"')
.replace(/HDX_BACKSLASH_LITERAL/g, '\\')
.replace('http_COLON_//', 'http://')
.replace('https_COLON_//', 'https://')
.replace(/localhost_COLON_(\d{1,5})/, 'localhost:$1')
.replace(/HDX_COLON/g, ':');
}
export function parse(query: string): lucene.AST {
return lucene.parse(encodeSpecialTokens(query));
}
const IMPLICIT_FIELD = '<implicit>';
const CLICK_HOUSE_JSON_NUMBER_TYPES = [
'Int8',
'Int16',
'Int32',
'Int64',
'Int128',
'Int256',
'UInt8',
'UInt16',
'UInt32',
'UInt64',
'UInt128',
'UInt256',
'Float32',
'Float64',
];
interface Serializer {
operator(op: lucene.Operator): string;
eq(field: string, term: string, isNegatedField: boolean): Promise<string>;
isNotNull(field: string, isNegatedField: boolean): Promise<string>;
gte(field: string, term: string): Promise<string>;
lte(field: string, term: string): Promise<string>;
lt(field: string, term: string): Promise<string>;
gt(field: string, term: string): Promise<string>;
fieldSearch(
field: string,
term: string,
isNegatedField: boolean,
prefixWildcard: boolean,
suffixWildcard: boolean,
): Promise<string>;
range(
field: string,
start: string,
end: string,
isNegatedField: boolean,
): Promise<string>;
}
class EnglishSerializer implements Serializer {
private translateField(field: string) {
if (field === IMPLICIT_FIELD) {
return 'event';
}
return `'${field}'`;
}
operator(op: lucene.Operator) {
switch (op) {
case 'NOT':
case 'AND NOT':
return 'AND NOT';
case 'OR NOT':
return 'OR NOT';
// @ts-ignore TODO: Types need to be fixed upstream
case '&&':
case '<implicit>':
case 'AND':
return 'AND';
// @ts-ignore TODO: Types need to be fixed upstream
case '||':
case 'OR':
return 'OR';
default:
throw new Error(`Unexpected operator. ${op}`);
}
}
async eq(field: string, term: string, isNegatedField: boolean) {
return `${this.translateField(field)} ${
isNegatedField ? 'is not' : 'is'
} ${term}`;
}
async isNotNull(field: string, isNegatedField: boolean) {
return `${this.translateField(field)} ${
isNegatedField ? 'is null' : 'is not null'
}`;
}
async gte(field: string, term: string) {
return `${this.translateField(field)} is greater than or equal to ${term}`;
}
async lte(field: string, term: string) {
return `${this.translateField(field)} is less than or equal to ${term}`;
}
async lt(field: string, term: string) {
return `${this.translateField(field)} is less than ${term}`;
}
async gt(field: string, term: string) {
return `${this.translateField(field)} is greater than ${term}`;
}
// async fieldSearch(field: string, term: string, isNegatedField: boolean) {
// return `${this.translateField(field)} ${
// isNegatedField ? 'does not contain' : 'contains'
// } ${term}`;
// }
async fieldSearch(
field: string,
term: string,
isNegatedField: boolean,
prefixWildcard: boolean,
suffixWildcard: boolean,
) {
if (field === IMPLICIT_FIELD) {
return `${this.translateField(field)} ${
prefixWildcard && suffixWildcard
? isNegatedField
? 'does not contain'
: 'contains'
: prefixWildcard
? isNegatedField
? 'does not end with'
: 'ends with'
: suffixWildcard
? isNegatedField
? 'does not start with'
: 'starts with'
: isNegatedField
? 'does not have whole word'
: 'has whole word'
} ${term}`;
} else {
return `${this.translateField(field)} ${
isNegatedField ? 'does not contain' : 'contains'
} ${term}`;
}
}
async range(
field: string,
start: string,
end: string,
isNegatedField: boolean,
) {
return `${field} ${
isNegatedField ? 'is not' : 'is'
} between ${start} and ${end}`;
}
}
export abstract class SQLSerializer implements Serializer {
private NOT_FOUND_QUERY = '(1 = 0)';
abstract getColumnForField(field: string): Promise<{
column?: string;
columnJSON?: { string: string; number: string };
propertyType?: JSDataType;
found: boolean;
}>;
operator(op: lucene.Operator) {
switch (op) {
case 'NOT':
case 'AND NOT':
return 'AND NOT';
case 'OR NOT':
return 'OR NOT';
// @ts-ignore TODO: Types need to be fixed upstream
case '&&':
case '<implicit>':
case 'AND':
return 'AND';
// @ts-ignore TODO: Types need to be fixed upstream
case '||':
case 'OR':
return 'OR';
default:
throw new Error(`Unexpected operator. ${op}`);
}
}
// Only for exact string matches
async eq(field: string, term: string, isNegatedField: boolean) {
const { column, columnJSON, found, propertyType } =
await this.getColumnForField(field);
if (!found) {
return this.NOT_FOUND_QUERY;
}
if (propertyType === JSDataType.Bool) {
// numeric and boolean fields must be equality matched
const normTerm = `${term}`.trim().toLowerCase();
return SqlString.format(`(?? ${isNegatedField ? '!' : ''}= ?)`, [
column,
normTerm === 'true' ? 1 : normTerm === 'false' ? 0 : parseInt(normTerm),
]);
} else if (propertyType === JSDataType.Number) {
return SqlString.format(
`(${column} ${isNegatedField ? '!' : ''}= CAST(?, 'Float64'))`,
[term],
);
} else if (propertyType === JSDataType.JSON) {
return SqlString.format(
`(${columnJSON?.string} ${isNegatedField ? '!' : ''}= ?)`,
[term],
);
}
return SqlString.format(`(${column} ${isNegatedField ? '!' : ''}= ?)`, [
term,
]);
}
async isNotNull(field: string, isNegatedField: boolean) {
const { column, columnJSON, found, propertyType } =
await this.getColumnForField(field);
if (!found) {
return this.NOT_FOUND_QUERY;
}
if (propertyType === JSDataType.JSON) {
return `notEmpty(${columnJSON?.string}) ${isNegatedField ? '!' : ''}= 1`;
}
return `notEmpty(${column}) ${isNegatedField ? '!' : ''}= 1`;
}
async gte(field: string, term: string) {
const { column, columnJSON, found, propertyType } =
await this.getColumnForField(field);
if (!found) {
return this.NOT_FOUND_QUERY;
}
if (propertyType === JSDataType.JSON) {
return SqlString.format(`(${columnJSON?.number} >= ?)`, [term]);
}
return SqlString.format(`(${column} >= ?)`, [term]);
}
async lte(field: string, term: string) {
const { column, columnJSON, found, propertyType } =
await this.getColumnForField(field);
if (!found) {
return this.NOT_FOUND_QUERY;
}
if (propertyType === JSDataType.JSON) {
return SqlString.format(`(${columnJSON?.number} <= ?)`, [term]);
}
return SqlString.format(`(${column} <= ?)`, [term]);
}
async lt(field: string, term: string) {
const { column, columnJSON, found, propertyType } =
await this.getColumnForField(field);
if (!found) {
return this.NOT_FOUND_QUERY;
}
if (propertyType === JSDataType.JSON) {
return SqlString.format(`(${columnJSON?.number} < ?)`, [term]);
}
return SqlString.format(`(${column} < ?)`, [term]);
}
async gt(field: string, term: string) {
const { column, columnJSON, found, propertyType } =
await this.getColumnForField(field);
if (!found) {
return this.NOT_FOUND_QUERY;
}
if (propertyType === JSDataType.JSON) {
return SqlString.format(`(${columnJSON?.number} > ?)`, [term]);
}
return SqlString.format(`(${column} > ?)`, [term]);
}
// TODO: Not sure if SQL really needs this or if it'll coerce itself
private attemptToParseNumber(term: string): string | number {
const number = Number.parseFloat(term);
if (Number.isNaN(number)) {
return term;
}
return number;
}
// Ref: https://clickhouse.com/codebrowser/ClickHouse/src/Functions/HasTokenImpl.h.html#_ZN2DB12HasTokenImpl16isTokenSeparatorEDu
// Split by anything that's ascii 0-128, that's not a letter or a number
private tokenizeTerm(term: string): string[] {
return term.split(/[ -/:-@[-`{-~\t\n\r]+/).filter(t => t.length > 0);
}
private termHasSeperators(term: string): boolean {
return term.match(/[ -/:-@[-`{-~\t\n\r]+/) != null;
}
async fieldSearch(
field: string,
term: string,
isNegatedField: boolean,
prefixWildcard: boolean,
suffixWildcard: boolean,
) {
const isImplicitField = field === IMPLICIT_FIELD;
const { column, columnJSON, found, propertyType } =
await this.getColumnForField(field);
if (!found) {
return this.NOT_FOUND_QUERY;
}
// If it's a string field, we will always try to match with ilike
if (propertyType === JSDataType.Bool) {
// numeric and boolean fields must be equality matched
const normTerm = `${term}`.trim().toLowerCase();
return SqlString.format(`(?? ${isNegatedField ? '!' : ''}= ?)`, [
column,
normTerm === 'true' ? 1 : normTerm === 'false' ? 0 : parseInt(normTerm),
]);
} else if (propertyType === JSDataType.Number) {
return SqlString.format(
`(?? ${isNegatedField ? '!' : ''}= CAST(?, 'Float64'))`,
[column, term],
);
} else if (propertyType === JSDataType.JSON) {
return SqlString.format(
`(${columnJSON?.string} ${isNegatedField ? 'NOT ' : ''}ILIKE ?)`,
[`%${term}%`],
);
}
// // If the query is empty, or is a empty quoted string ex: ""
// // we should match all
if (term.length === 0) {
return '(1=1)';
}
if (isImplicitField) {
// For the _source column, we'll try to do whole word searches by default
// to utilize the token bloom filter unless a prefix/sufix wildcard is specified
if (prefixWildcard || suffixWildcard) {
return SqlString.format(
`(lower(?) ${isNegatedField ? 'NOT ' : ''}LIKE lower(?))`,
[
SqlString.raw(column ?? ''),
`${prefixWildcard ? '%' : ''}${term}${suffixWildcard ? '%' : ''}`,
],
);
} else {
// We can't search multiple tokens with `hasToken`, so we need to split up the term into tokens
const hasSeperators = this.termHasSeperators(term);
if (hasSeperators) {
const tokens = this.tokenizeTerm(term);
return `(${isNegatedField ? 'NOT (' : ''}${[
...tokens.map(token =>
SqlString.format(`hasTokenCaseInsensitive(?, ?)`, [
SqlString.raw(column ?? ''),
token,
]),
),
// If there are symbols in the term, we'll try to match the whole term as well (ex. Scott!)
SqlString.format(`(lower(?) LIKE lower(?))`, [
SqlString.raw(column ?? ''),
`%${term}%`,
]),
].join(' AND ')}${isNegatedField ? ')' : ''})`;
} else {
return SqlString.format(
`(${isNegatedField ? 'NOT ' : ''}hasTokenCaseInsensitive(?, ?))`,
[SqlString.raw(column ?? ''), term],
);
}
}
} else {
const shoudUseTokenBf = isImplicitField;
return SqlString.format(
`(${column} ${isNegatedField ? 'NOT ' : ''}? ?)`,
[SqlString.raw(shoudUseTokenBf ? 'LIKE' : 'ILIKE'), `%${term}%`],
);
}
}
async range(
field: string,
start: string,
end: string,
isNegatedField: boolean,
) {
const { column, found } = await this.getColumnForField(field);
if (!found) {
return this.NOT_FOUND_QUERY;
}
return SqlString.format(
`(${column} ${isNegatedField ? 'NOT ' : ''}BETWEEN ? AND ?)`,
[this.attemptToParseNumber(start), this.attemptToParseNumber(end)],
);
}
}
export type CustomSchemaConfig = {
databaseName: string;
implicitColumnExpression?: string;
tableName: string;
connectionId: string;
};
export class CustomSchemaSQLSerializerV2 extends SQLSerializer {
private metadata: Metadata;
private tableName: string;
private databaseName: string;
private implicitColumnExpression?: string;
private connectionId: string;
constructor({
metadata,
databaseName,
tableName,
connectionId,
implicitColumnExpression,
}: { metadata: Metadata } & CustomSchemaConfig) {
super();
this.metadata = metadata;
this.databaseName = databaseName;
this.tableName = tableName;
this.implicitColumnExpression = implicitColumnExpression;
this.connectionId = connectionId;
}
/**
* Translate field from user ex. column.property.subproperty to SQL expression
* Supports:
* - Materialized Columns
* - Map
* - JSON Strings (via JSONExtract)
* TODO:
* - Nested Map
* - JSONExtract for non-string types
*/
private async buildColumnExpressionFromField(field: string) {
const exactMatch = await this.metadata.getColumn({
databaseName: this.databaseName,
tableName: this.tableName,
column: field,
connectionId: this.connectionId,
});
if (exactMatch) {
return {
found: true,
columnType: exactMatch.type,
columnExpression: exactMatch.name,
// TODO
// Add JSON excatMatch if want to support whole json compare in future, ex: json:"{a: 1234}""
};
}
const fieldPrefix = field.split('.')[0];
const prefixMatch = await this.metadata.getColumn({
databaseName: this.databaseName,
tableName: this.tableName,
column: fieldPrefix,
connectionId: this.connectionId,
});
if (prefixMatch) {
const fieldPostfix = field.split('.').slice(1).join('.');
if (prefixMatch.type.startsWith('Map')) {
const valueType = prefixMatch.type.match(/,\s+(\w+)\)$/)?.[1];
return {
found: true,
columnExpression: SqlString.format(`??[?]`, [
prefixMatch.name,
fieldPostfix,
]),
columnType: valueType ?? 'Unknown',
};
} else if (prefixMatch.type.startsWith('JSON')) {
// ignore original column expression at here
// need to know the term to decide which expression to read
// TODO: add real columnExpression when CH update JSON data type
return {
found: true,
columnExpression: '',
columnExpressionJSON: {
string: SqlString.format(`toString(??)`, [field]),
number: SqlString.format(`dynamicType(??) in (?) and ??`, [
field,
CLICK_HOUSE_JSON_NUMBER_TYPES,
field,
]),
},
columnType: 'JSON',
};
} else if (prefixMatch.type === 'String') {
// TODO: Support non-strings
const nestedPaths = fieldPostfix.split('.');
return {
found: true,
columnExpression: SqlString.format(
`JSONExtractString(??, ${Array(nestedPaths.length)
.fill('?')
.join(',')})`,
[prefixMatch.name, ...nestedPaths],
),
columnType: 'String',
};
}
// TODO: Support arrays and tuples
throw new Error('Unsupported column type for prefix match');
}
// It might be an alias, let's just try the column
// TODO: Verify aliases
return {
found: true,
columnExpression: field,
columnType: 'Unknown',
};
// throw new Error(`Column not found: ${field}`);
}
async getColumnForField(field: string) {
if (field === IMPLICIT_FIELD) {
if (!this.implicitColumnExpression) {
throw new Error(
'Can not search bare text without an implicit column set.',
);
}
const expressions = splitAndTrimWithBracket(
this.implicitColumnExpression,
);
return {
column:
expressions.length > 1
? `concatWithSeparator(';',${expressions.join(',')})`
: this.implicitColumnExpression,
columnJSON: undefined,
propertyType: JSDataType.String,
found: true,
};
}
const expression = await this.buildColumnExpressionFromField(field);
return {
column: expression.columnExpression,
columnJSON: expression?.columnExpressionJSON,
propertyType:
convertCHTypeToPrimitiveJSType(expression.columnType) ?? undefined,
found: expression.found,
};
}
}
async function nodeTerm(
node: lucene.Node,
serializer: Serializer,
): Promise<string> {
const field = node.field[0] === '-' ? node.field.slice(1) : node.field;
let isNegatedField = node.field[0] === '-';
const isImplicitField = node.field === IMPLICIT_FIELD;
// NodeTerm
if ((node as lucene.NodeTerm).term != null) {
const nodeTerm = node as lucene.NodeTerm;
let term = decodeSpecialTokens(nodeTerm.term);
// We should only negate the search for negated bare terms (ex. '-5')
// This meeans the field is implicit and the prefix is -
if (isImplicitField && nodeTerm.prefix === '-') {
isNegatedField = true;
}
// Otherwise, if we have a negated term for a field (ex. 'level:-5')
// we should not negate the search, and search for -5
if (!isImplicitField && nodeTerm.prefix === '-') {
term = nodeTerm.prefix + decodeSpecialTokens(nodeTerm.term);
}
// TODO: Decide if this is good behavior
// If the term is quoted, we should search for the exact term in a property (ex. foo:"bar")
// Implicit field searches should still use substring matching (ex. "foo bar")
if (nodeTerm.quoted && !isImplicitField) {
return serializer.eq(field, term, isNegatedField);
}
if (!nodeTerm.quoted && term === '*') {
return serializer.isNotNull(field, isNegatedField);
}
if (!nodeTerm.quoted && term.substring(0, 2) === '>=') {
if (isNegatedField) {
return serializer.lt(field, term.slice(2));
}
return serializer.gte(field, term.slice(2));
}
if (!nodeTerm.quoted && term.substring(0, 2) === '<=') {
if (isNegatedField) {
return serializer.gt(field, term.slice(2));
}
return serializer.lte(field, term.slice(2));
}
if (!nodeTerm.quoted && term[0] === '>') {
if (isNegatedField) {
return serializer.lte(field, term.slice(1));
}
return serializer.gt(field, term.slice(1));
}
if (!nodeTerm.quoted && term[0] === '<') {
if (isNegatedField) {
return serializer.gte(field, term.slice(1));
}
return serializer.lt(field, term.slice(1));
}
let prefixWildcard = false;
let suffixWildcard = false;
if (!nodeTerm.quoted && term[0] === '*') {
prefixWildcard = true;
term = term.slice(1);
}
if (!nodeTerm.quoted && term[term.length - 1] === '*') {
suffixWildcard = true;
term = term.slice(0, -1);
}
return serializer.fieldSearch(
field,
term,
isNegatedField,
prefixWildcard,
suffixWildcard,
);
// TODO: Handle regex, similarity, boost, prefix
}
// NodeRangedTerm
if ((node as lucene.NodeRangedTerm).inclusive != null) {
const rangedTerm = node as lucene.NodeRangedTerm;
return serializer.range(
field,
rangedTerm.term_min,
rangedTerm.term_max,
isNegatedField,
);
}
throw new Error(`Unexpected Node type. ${node}`);
}
async function serialize(
ast: lucene.AST | lucene.Node,
serializer: Serializer,
): Promise<string> {
// Node Scenarios:
// 1. NodeTerm: Single term ex. "foo:bar"
// 2. NodeRangedTerm: Two terms ex. "foo:[bar TO qux]"
if ((ast as lucene.NodeTerm).term != null) {
return await nodeTerm(ast as lucene.NodeTerm, serializer);
}
if ((ast as lucene.NodeRangedTerm).inclusive != null) {
return await nodeTerm(ast as lucene.NodeTerm, serializer);
}
// AST Scenarios:
// 1. BinaryAST: Two terms ex. "foo:bar AND baz:qux"
// 2. LeftOnlyAST: Single term ex. "foo:bar"
if ((ast as lucene.BinaryAST).right != null) {
const binaryAST = ast as lucene.BinaryAST;
const operator = serializer.operator(binaryAST.operator);
const parenthesized = binaryAST.parenthesized;
return `${parenthesized ? '(' : ''}${await serialize(
binaryAST.left,
serializer,
)} ${operator} ${await serialize(binaryAST.right, serializer)}${
parenthesized ? ')' : ''
}`;
}
if ((ast as lucene.LeftOnlyAST).left != null) {
const leftOnlyAST = ast as lucene.LeftOnlyAST;
const parenthesized = leftOnlyAST.parenthesized;
// start is used when ex. "NOT foo:bar"
return `${parenthesized ? '(' : ''}${
leftOnlyAST.start != undefined ? `${leftOnlyAST.start} ` : ''
}${await serialize(leftOnlyAST.left, serializer)}${
parenthesized ? ')' : ''
}`;
}
// Blank AST, means no text was parsed
return '';
}
// TODO: can just inline this within getSearchQuery
export async function genWhereSQL(
ast: lucene.AST,
serializer: Serializer,
): Promise<string> {
return await serialize(ast, serializer);
}
export class SearchQueryBuilder {
private readonly searchQ: string;
private readonly conditions: string[];
private serializer: SQLSerializer;
constructor(searchQ: string, serializer: SQLSerializer) {
this.conditions = [];
this.searchQ = searchQ;
// init default serializer
this.serializer = serializer;
}
setSerializer(serializer: SQLSerializer) {
this.serializer = serializer;
return this;
}
getSerializer() {
return this.serializer;
}
private async genSearchQuery() {
if (!this.searchQ) {
return '';
}
// const implicitColumn = await this.serializer.getColumnForField(
// IMPLICIT_FIELD,
// );
// let querySql = this.searchQ
// .split(/\s+/)
// .map(queryToken =>
// SqlString.format(`lower(??) LIKE lower(?)`, [
// implicitColumn.column,
// `%${queryToken}%`,
// ]),
// )
// .join(' AND ');
const parsedQ = parse(this.searchQ);
return await genWhereSQL(parsedQ, this.serializer);
}
and(condition: string) {
if (condition && condition.trim()) {
this.conditions.push(`(${condition})`);
}
return this;
}
async build() {
const searchQuery = await this.genSearchQuery();
if (this.searchQ) {
this.and(searchQuery);
}
return this.conditions.join(' AND ');
}
}
export async function genEnglishExplanation(query: string): Promise<string> {
try {
const parsedQ = parse(query);
if (parsedQ) {
const serializer = new EnglishSerializer();
return await serialize(parsedQ, serializer);
}
} catch (e) {
console.warn('Parse failure', query, e);
}
return `Message containing ${query}`;
}