30
30
import org .elasticsearch .xpack .sql .expression .function .scalar .ScalarFunction ;
31
31
import org .elasticsearch .xpack .sql .expression .function .scalar .datetime .DateTimeFunction ;
32
32
import org .elasticsearch .xpack .sql .expression .function .scalar .datetime .DateTimeHistogramFunction ;
33
- import org .elasticsearch .xpack .sql .expression .gen .script .ScriptTemplate ;
34
- import org .elasticsearch .xpack .sql .expression .predicate .nulls .IsNull ;
35
33
import org .elasticsearch .xpack .sql .expression .predicate .Range ;
36
34
import org .elasticsearch .xpack .sql .expression .predicate .fulltext .MatchQueryPredicate ;
37
35
import org .elasticsearch .xpack .sql .expression .predicate .fulltext .MultiMatchQueryPredicate ;
40
38
import org .elasticsearch .xpack .sql .expression .predicate .logical .Not ;
41
39
import org .elasticsearch .xpack .sql .expression .predicate .logical .Or ;
42
40
import org .elasticsearch .xpack .sql .expression .predicate .nulls .IsNotNull ;
41
+ import org .elasticsearch .xpack .sql .expression .predicate .nulls .IsNull ;
43
42
import org .elasticsearch .xpack .sql .expression .predicate .operator .comparison .BinaryComparison ;
44
43
import org .elasticsearch .xpack .sql .expression .predicate .operator .comparison .Equals ;
45
44
import org .elasticsearch .xpack .sql .expression .predicate .operator .comparison .GreaterThan ;
94
93
import java .util .Map ;
95
94
import java .util .Map .Entry ;
96
95
import java .util .Optional ;
96
+ import java .util .function .Supplier ;
97
97
98
98
import static java .util .Collections .singletonList ;
99
99
import static org .elasticsearch .xpack .sql .expression .Foldables .doubleValuesOf ;
@@ -487,11 +487,8 @@ protected QueryTranslation asQuery(Not not, boolean onAggs) {
487
487
if (onAggs ) {
488
488
aggFilter = new AggFilter (not .id ().toString (), not .asScript ());
489
489
} else {
490
- query = new NotQuery (not .location (), toQuery (not .field (), false ).query );
491
- // query directly on the field
492
- if (not .field () instanceof FieldAttribute ) {
493
- query = wrapIfNested (query , not .field ());
494
- }
490
+ query = handleQuery (not , not .field (),
491
+ () -> new NotQuery (not .location (), toQuery (not .field (), false ).query ));
495
492
}
496
493
497
494
return new QueryTranslation (query , aggFilter );
@@ -508,11 +505,8 @@ protected QueryTranslation asQuery(IsNotNull isNotNull, boolean onAggs) {
508
505
if (onAggs ) {
509
506
aggFilter = new AggFilter (isNotNull .id ().toString (), isNotNull .asScript ());
510
507
} else {
511
- query = new ExistsQuery (isNotNull .location (), nameOf (isNotNull .field ()));
512
- // query directly on the field
513
- if (isNotNull .field () instanceof NamedExpression ) {
514
- query = wrapIfNested (query , isNotNull .field ());
515
- }
508
+ query = handleQuery (isNotNull , isNotNull .field (),
509
+ () -> new ExistsQuery (isNotNull .location (), nameOf (isNotNull .field ())));
516
510
}
517
511
518
512
return new QueryTranslation (query , aggFilter );
@@ -529,11 +523,8 @@ protected QueryTranslation asQuery(IsNull isNull, boolean onAggs) {
529
523
if (onAggs ) {
530
524
aggFilter = new AggFilter (isNull .id ().toString (), isNull .asScript ());
531
525
} else {
532
- query = new NotQuery (isNull .location (), new ExistsQuery (isNull .location (), nameOf (isNull .field ())));
533
- // query directly on the field
534
- if (isNull .field () instanceof NamedExpression ) {
535
- query = wrapIfNested (query , isNull .field ());
536
- }
526
+ query = handleQuery (isNull , isNull .field (),
527
+ () -> new NotQuery (isNull .location (), new ExistsQuery (isNull .location (), nameOf (isNull .field ()))));
537
528
}
538
529
539
530
return new QueryTranslation (query , aggFilter );
@@ -564,12 +555,7 @@ protected QueryTranslation asQuery(BinaryComparison bc, boolean onAggs) {
564
555
aggFilter = new AggFilter (at .id ().toString (), bc .asScript ());
565
556
}
566
557
else {
567
- // query directly on the field
568
- if (at instanceof FieldAttribute ) {
569
- query = wrapIfNested (translateQuery (bc ), ne );
570
- } else {
571
- query = new ScriptQuery (at .location (), bc .asScript ());
572
- }
558
+ query = handleQuery (bc , ne , () -> translateQuery (bc ));
573
559
}
574
560
return new QueryTranslation (query , aggFilter );
575
561
}
@@ -646,17 +632,11 @@ protected QueryTranslation asQuery(In in, boolean onAggs) {
646
632
//
647
633
// Agg context means HAVING -> PipelineAggs
648
634
//
649
- ScriptTemplate script = in .asScript ();
650
635
if (onAggs ) {
651
- aggFilter = new AggFilter (at .id ().toString (), script );
636
+ aggFilter = new AggFilter (at .id ().toString (), in . asScript () );
652
637
}
653
638
else {
654
- // query directly on the field
655
- if (at instanceof FieldAttribute ) {
656
- query = wrapIfNested (new TermsQuery (in .location (), ne .name (), in .list ()), ne );
657
- } else {
658
- query = new ScriptQuery (at .location (), script );
659
- }
639
+ query = handleQuery (in , ne , () -> new TermsQuery (in .location (), ne .name (), in .list ()));
660
640
}
661
641
return new QueryTranslation (query , aggFilter );
662
642
}
@@ -687,16 +667,9 @@ protected QueryTranslation asQuery(Range r, boolean onAggs) {
687
667
if (onAggs ) {
688
668
aggFilter = new AggFilter (at .id ().toString (), r .asScript ());
689
669
} else {
690
- // typical range; no scripting involved
691
- if (at instanceof FieldAttribute ) {
692
- RangeQuery rangeQuery = new RangeQuery (r .location (), nameOf (r .value ()), valueOf (r .lower ()), r .includeLower (),
693
- valueOf (r .upper ()), r .includeUpper (), dateFormat (r .value ()));
694
- query = wrapIfNested (rangeQuery , r .value ());
695
- }
696
- // scripted query
697
- else {
698
- query = new ScriptQuery (at .location (), r .asScript ());
699
- }
670
+ query = handleQuery (r , r .value (),
671
+ () -> new RangeQuery (r .location (), nameOf (r .value ()), valueOf (r .lower ()), r .includeLower (),
672
+ valueOf (r .upper ()), r .includeUpper (), dateFormat (r .value ())));
700
673
}
701
674
return new QueryTranslation (query , aggFilter );
702
675
} else {
@@ -845,6 +818,14 @@ public QueryTranslation translate(Expression exp, boolean onAggs) {
845
818
846
819
protected abstract QueryTranslation asQuery (E e , boolean onAggs );
847
820
821
+
822
+ protected static Query handleQuery (ScalarFunction sf , Expression field , Supplier <Query > query ) {
823
+ if (field instanceof FieldAttribute ) {
824
+ return wrapIfNested (query .get (), field );
825
+ }
826
+ return new ScriptQuery (sf .location (), sf .asScript ());
827
+ }
828
+
848
829
protected static Query wrapIfNested (Query query , Expression exp ) {
849
830
if (exp instanceof FieldAttribute ) {
850
831
FieldAttribute fa = (FieldAttribute ) exp ;
0 commit comments