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 ;
3433import org .elasticsearch .xpack .sql .expression .predicate .nulls .IsNotNull ;
3534import org .elasticsearch .xpack .sql .expression .predicate .Range ;
3635import org .elasticsearch .xpack .sql .expression .predicate .fulltext .MatchQueryPredicate ;
3938import org .elasticsearch .xpack .sql .expression .predicate .logical .And ;
4039import org .elasticsearch .xpack .sql .expression .predicate .logical .Not ;
4140import org .elasticsearch .xpack .sql .expression .predicate .logical .Or ;
41+ import org .elasticsearch .xpack .sql .expression .predicate .nulls .IsNotNull ;
4242import org .elasticsearch .xpack .sql .expression .predicate .nulls .IsNull ;
4343import org .elasticsearch .xpack .sql .expression .predicate .operator .comparison .BinaryComparison ;
4444import org .elasticsearch .xpack .sql .expression .predicate .operator .comparison .Equals ;
9494import java .util .Map ;
9595import java .util .Map .Entry ;
9696import java .util .Optional ;
97+ import java .util .function .Supplier ;
9798
9899import static java .util .Collections .singletonList ;
99100import static org .elasticsearch .xpack .sql .expression .Foldables .doubleValuesOf ;
@@ -487,11 +488,8 @@ protected QueryTranslation asQuery(Not not, boolean onAggs) {
487488 if (onAggs ) {
488489 aggFilter = new AggFilter (not .id ().toString (), not .asScript ());
489490 } 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- }
491+ query = handleQuery (not , not .field (),
492+ () -> new NotQuery (not .location (), toQuery (not .field (), false ).query ));
495493 }
496494
497495 return new QueryTranslation (query , aggFilter );
@@ -508,11 +506,8 @@ protected QueryTranslation asQuery(IsNotNull isNotNull, boolean onAggs) {
508506 if (onAggs ) {
509507 aggFilter = new AggFilter (isNotNull .id ().toString (), isNotNull .asScript ());
510508 } 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- }
509+ query = handleQuery (isNotNull , isNotNull .field (),
510+ () -> new ExistsQuery (isNotNull .location (), nameOf (isNotNull .field ())));
516511 }
517512
518513 return new QueryTranslation (query , aggFilter );
@@ -529,11 +524,8 @@ protected QueryTranslation asQuery(IsNull isNull, boolean onAggs) {
529524 if (onAggs ) {
530525 aggFilter = new AggFilter (isNull .id ().toString (), isNull .asScript ());
531526 } 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- }
527+ query = handleQuery (isNull , isNull .field (),
528+ () -> new NotQuery (isNull .location (), new ExistsQuery (isNull .location (), nameOf (isNull .field ()))));
537529 }
538530
539531 return new QueryTranslation (query , aggFilter );
@@ -564,12 +556,7 @@ protected QueryTranslation asQuery(BinaryComparison bc, boolean onAggs) {
564556 aggFilter = new AggFilter (at .id ().toString (), bc .asScript ());
565557 }
566558 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- }
559+ query = handleQuery (bc , ne , () -> translateQuery (bc ));
573560 }
574561 return new QueryTranslation (query , aggFilter );
575562 }
@@ -646,17 +633,11 @@ protected QueryTranslation asQuery(In in, boolean onAggs) {
646633 //
647634 // Agg context means HAVING -> PipelineAggs
648635 //
649- ScriptTemplate script = in .asScript ();
650636 if (onAggs ) {
651- aggFilter = new AggFilter (at .id ().toString (), script );
637+ aggFilter = new AggFilter (at .id ().toString (), in . asScript () );
652638 }
653639 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- }
640+ query = handleQuery (in , ne , () -> new TermsQuery (in .location (), ne .name (), in .list ()));
660641 }
661642 return new QueryTranslation (query , aggFilter );
662643 }
@@ -687,16 +668,9 @@ protected QueryTranslation asQuery(Range r, boolean onAggs) {
687668 if (onAggs ) {
688669 aggFilter = new AggFilter (at .id ().toString (), r .asScript ());
689670 } 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- }
671+ query = handleQuery (r , r .value (),
672+ () -> new RangeQuery (r .location (), nameOf (r .value ()), valueOf (r .lower ()), r .includeLower (),
673+ valueOf (r .upper ()), r .includeUpper (), dateFormat (r .value ())));
700674 }
701675 return new QueryTranslation (query , aggFilter );
702676 } else {
@@ -845,6 +819,14 @@ public QueryTranslation translate(Expression exp, boolean onAggs) {
845819
846820 protected abstract QueryTranslation asQuery (E e , boolean onAggs );
847821
822+
823+ protected static Query handleQuery (ScalarFunction sf , Expression field , Supplier <Query > query ) {
824+ if (field instanceof FieldAttribute ) {
825+ return wrapIfNested (query .get (), field );
826+ }
827+ return new ScriptQuery (sf .location (), sf .asScript ());
828+ }
829+
848830 protected static Query wrapIfNested (Query query , Expression exp ) {
849831 if (exp instanceof FieldAttribute ) {
850832 FieldAttribute fa = (FieldAttribute ) exp ;
0 commit comments