3030import org .elasticsearch .xpack .sql .expression .function .scalar .ScalarFunction ;
3131import org .elasticsearch .xpack .sql .expression .function .scalar .datetime .DateTimeFunction ;
3232import 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 ;
3533import org .elasticsearch .xpack .sql .expression .predicate .Range ;
3634import org .elasticsearch .xpack .sql .expression .predicate .fulltext .MatchQueryPredicate ;
3735import org .elasticsearch .xpack .sql .expression .predicate .fulltext .MultiMatchQueryPredicate ;
4038import org .elasticsearch .xpack .sql .expression .predicate .logical .Not ;
4139import org .elasticsearch .xpack .sql .expression .predicate .logical .Or ;
4240import org .elasticsearch .xpack .sql .expression .predicate .nulls .IsNotNull ;
41+ import org .elasticsearch .xpack .sql .expression .predicate .nulls .IsNull ;
4342import org .elasticsearch .xpack .sql .expression .predicate .operator .comparison .BinaryComparison ;
4443import org .elasticsearch .xpack .sql .expression .predicate .operator .comparison .Equals ;
4544import org .elasticsearch .xpack .sql .expression .predicate .operator .comparison .GreaterThan ;
9493import java .util .Map ;
9594import java .util .Map .Entry ;
9695import java .util .Optional ;
96+ import java .util .function .Supplier ;
9797
9898import static java .util .Collections .singletonList ;
9999import static org .elasticsearch .xpack .sql .expression .Foldables .doubleValuesOf ;
@@ -487,11 +487,8 @@ protected QueryTranslation asQuery(Not not, boolean onAggs) {
487487 if (onAggs ) {
488488 aggFilter = new AggFilter (not .id ().toString (), not .asScript ());
489489 } 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 ));
495492 }
496493
497494 return new QueryTranslation (query , aggFilter );
@@ -508,11 +505,8 @@ protected QueryTranslation asQuery(IsNotNull isNotNull, boolean onAggs) {
508505 if (onAggs ) {
509506 aggFilter = new AggFilter (isNotNull .id ().toString (), isNotNull .asScript ());
510507 } 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 ())));
516510 }
517511
518512 return new QueryTranslation (query , aggFilter );
@@ -529,11 +523,8 @@ protected QueryTranslation asQuery(IsNull isNull, boolean onAggs) {
529523 if (onAggs ) {
530524 aggFilter = new AggFilter (isNull .id ().toString (), isNull .asScript ());
531525 } 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 ()))));
537528 }
538529
539530 return new QueryTranslation (query , aggFilter );
@@ -564,12 +555,7 @@ protected QueryTranslation asQuery(BinaryComparison bc, boolean onAggs) {
564555 aggFilter = new AggFilter (at .id ().toString (), bc .asScript ());
565556 }
566557 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 ));
573559 }
574560 return new QueryTranslation (query , aggFilter );
575561 }
@@ -646,17 +632,11 @@ protected QueryTranslation asQuery(In in, boolean onAggs) {
646632 //
647633 // Agg context means HAVING -> PipelineAggs
648634 //
649- ScriptTemplate script = in .asScript ();
650635 if (onAggs ) {
651- aggFilter = new AggFilter (at .id ().toString (), script );
636+ aggFilter = new AggFilter (at .id ().toString (), in . asScript () );
652637 }
653638 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 ()));
660640 }
661641 return new QueryTranslation (query , aggFilter );
662642 }
@@ -687,16 +667,9 @@ protected QueryTranslation asQuery(Range r, boolean onAggs) {
687667 if (onAggs ) {
688668 aggFilter = new AggFilter (at .id ().toString (), r .asScript ());
689669 } 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 ())));
700673 }
701674 return new QueryTranslation (query , aggFilter );
702675 } else {
@@ -845,6 +818,14 @@ public QueryTranslation translate(Expression exp, boolean onAggs) {
845818
846819 protected abstract QueryTranslation asQuery (E e , boolean onAggs );
847820
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+
848829 protected static Query wrapIfNested (Query query , Expression exp ) {
849830 if (exp instanceof FieldAttribute ) {
850831 FieldAttribute fa = (FieldAttribute ) exp ;
0 commit comments