|
39 | 39 | import org.elasticsearch.xpack.sql.querydsl.agg.GroupByDateHistogram;
|
40 | 40 | import org.elasticsearch.xpack.sql.querydsl.query.BoolQuery;
|
41 | 41 | import org.elasticsearch.xpack.sql.querydsl.query.ExistsQuery;
|
| 42 | +import org.elasticsearch.xpack.sql.querydsl.query.GeoDistanceQuery; |
42 | 43 | import org.elasticsearch.xpack.sql.querydsl.query.NotQuery;
|
43 | 44 | import org.elasticsearch.xpack.sql.querydsl.query.Query;
|
44 | 45 | import org.elasticsearch.xpack.sql.querydsl.query.RangeQuery;
|
@@ -614,22 +615,41 @@ public void testTranslateStWktToSql() {
|
614 | 615 | assertEquals("[{v=keyword}, {v=point (10.0 20.0)}]", aggFilter.scriptTemplate().params().toString());
|
615 | 616 | }
|
616 | 617 |
|
617 |
| - public void testTranslateStDistance() { |
618 |
| - LogicalPlan p = plan("SELECT shape FROM test WHERE ST_Distance(shape, ST_WKTToSQL('point (10 20)')) > 20"); |
| 618 | + public void testTranslateStDistanceToScript() { |
| 619 | + String operator = randomFrom(">", ">="); |
| 620 | + String operatorFunction = operator.equalsIgnoreCase(">") ? "gt" : "gte"; |
| 621 | + LogicalPlan p = plan("SELECT shape FROM test WHERE ST_Distance(shape, ST_WKTToSQL('point (10 20)')) " + operator + " 20"); |
619 | 622 | assertThat(p, instanceOf(Project.class));
|
620 | 623 | assertThat(p.children().get(0), instanceOf(Filter.class));
|
621 | 624 | Expression condition = ((Filter) p.children().get(0)).condition();
|
622 | 625 | assertFalse(condition.foldable());
|
623 |
| - QueryTranslation translation = QueryTranslator.toQuery(condition, true); |
624 |
| - assertNull(translation.query); |
625 |
| - AggFilter aggFilter = translation.aggFilter; |
626 |
| - |
| 626 | + QueryTranslation translation = QueryTranslator.toQuery(condition, false); |
| 627 | + assertNull(translation.aggFilter); |
| 628 | + assertTrue(translation.query instanceof ScriptQuery); |
| 629 | + ScriptQuery sc = (ScriptQuery) translation.query; |
627 | 630 | assertEquals("InternalSqlScriptUtils.nullSafeFilter(" +
|
628 |
| - "InternalSqlScriptUtils.gt(" + |
| 631 | + "InternalSqlScriptUtils." + operatorFunction + "(" + |
629 | 632 | "InternalSqlScriptUtils.stDistance(" +
|
630 | 633 | "InternalSqlScriptUtils.geoDocValue(doc,params.v0),InternalSqlScriptUtils.stWktToSql(params.v1)),params.v2))",
|
631 |
| - aggFilter.scriptTemplate().toString()); |
632 |
| - assertEquals("[{v=shape}, {v=point (10.0 20.0)}, {v=20}]", aggFilter.scriptTemplate().params().toString()); |
| 634 | + sc.script().toString()); |
| 635 | + assertEquals("[{v=shape}, {v=point (10.0 20.0)}, {v=20}]", sc.script().params().toString()); |
| 636 | + } |
| 637 | + |
| 638 | + public void testTranslateStDistanceToQuery() { |
| 639 | + String operator = randomFrom("<", "<="); |
| 640 | + LogicalPlan p = plan("SELECT shape FROM test WHERE ST_Distance(shape, ST_WKTToSQL('point (10 20)')) " + operator + " 25"); |
| 641 | + assertThat(p, instanceOf(Project.class)); |
| 642 | + assertThat(p.children().get(0), instanceOf(Filter.class)); |
| 643 | + Expression condition = ((Filter) p.children().get(0)).condition(); |
| 644 | + assertFalse(condition.foldable()); |
| 645 | + QueryTranslation translation = QueryTranslator.toQuery(condition, false); |
| 646 | + assertNull(translation.aggFilter); |
| 647 | + assertTrue(translation.query instanceof GeoDistanceQuery); |
| 648 | + GeoDistanceQuery gq = (GeoDistanceQuery) translation.query; |
| 649 | + assertEquals("shape", gq.field()); |
| 650 | + assertEquals(20.0, gq.lat(), 0.00001); |
| 651 | + assertEquals(10.0, gq.lon(), 0.00001); |
| 652 | + assertEquals(25.0, gq.distance(), 0.00001); |
633 | 653 | }
|
634 | 654 |
|
635 | 655 | public void testTranslateCoalesce_GroupBy_Painless() {
|
|
0 commit comments