SQL: Translate MIN/MAX on keyword fields as FIRST/LAST (#41247)

Although the translation rule was implemented in the `Optimizer`,
the rule was not added in the list of rules to be executed.

Relates to #41195
Follows #37936


(cherry picked from commit f426a339b77af6008d41cc000c9199fe384e9269)
This commit is contained in:
Marios Trivyzas 2019-04-16 20:39:54 +03:00
parent a01dd96afe
commit c4ffd75805
3 changed files with 45 additions and 0 deletions

View File

@ -414,6 +414,26 @@ SELECT COUNT(ALL last_name)=COUNT(ALL first_name) AS areEqual, COUNT(ALL first_n
false |90 |100
;
topHitsAsMinAndMax
schema::min:s|max:s|first:s|last:s
SELECT MIN(first_name) as min, MAX(first_name) as max, FIRST(first_name) as first, LAST(first_name) as last FROM test_emp;
min | max | first | last
---------------+---------------+--------------+----------
Alejandro | Zvonko | Alejandro | Zvonko
;
topHitsAsMinAndMaxAndGroupBy
schema::gender:s|min:s|max:s|first:s|last:s
SELECT gender, MIN(first_name) as min, MAX(first_name) as max, FIRST(first_name) as first, LAST(first_name) as last FROM test_emp GROUP BY gender ORDER BY gender;
gender | min | max | first | last
---------------+---------------+--------------+---------------+----------
null | Berni | Patricio | Berni | Patricio
F | Alejandro | Xinglin | Alejandro | Xinglin
M | Amabile | Zvonko | Amabile | Zvonko
;
topHitsWithOneArgAndGroupBy
schema::gender:s|first:s|last:s
SELECT gender, FIRST(first_name) as first, LAST(first_name) as last FROM test_emp GROUP BY gender ORDER BY gender;

View File

@ -149,6 +149,7 @@ public class Optimizer extends RuleExecutor<LogicalPlan> {
Batch aggregate = new Batch("Aggregation Rewrite",
//new ReplaceDuplicateAggsWithReferences(),
new ReplaceMinMaxWithTopHits(),
new ReplaceAggsWithMatrixStats(),
new ReplaceAggsWithExtendedStats(),
new ReplaceAggsWithStats(),

View File

@ -763,6 +763,18 @@ public class QueryTranslatorTests extends ESTestCase {
"\"explain\":false,\"docvalue_fields\":[{\"field\":\"keyword\"}]," +
"\"sort\":[{\"keyword\":{\"order\":\"asc\",\"missing\":\"_last\",\"unmapped_type\":\"keyword\"}}]}}}}}"));
}
{
PhysicalPlan p = optimizeAndPlan("SELECT MIN(keyword) FROM test");
assertEquals(EsQueryExec.class, p.getClass());
EsQueryExec eqe = (EsQueryExec) p;
assertEquals(1, eqe.output().size());
assertEquals("MIN(keyword)", eqe.output().get(0).qualifiedName());
assertEquals(DataType.KEYWORD, eqe.output().get(0).dataType());
assertThat(eqe.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""),
endsWith("\"top_hits\":{\"from\":0,\"size\":1,\"version\":false,\"seq_no_primary_term\":false," +
"\"explain\":false,\"docvalue_fields\":[{\"field\":\"keyword\"}]," +
"\"sort\":[{\"keyword\":{\"order\":\"asc\",\"missing\":\"_last\",\"unmapped_type\":\"keyword\"}}]}}}}}"));
}
{
PhysicalPlan p = optimizeAndPlan("SELECT LAST(date) FROM test");
assertEquals(EsQueryExec.class, p.getClass());
@ -775,6 +787,18 @@ public class QueryTranslatorTests extends ESTestCase {
"\"explain\":false,\"docvalue_fields\":[{\"field\":\"date\",\"format\":\"epoch_millis\"}]," +
"\"sort\":[{\"date\":{\"order\":\"desc\",\"missing\":\"_last\",\"unmapped_type\":\"date\"}}]}}}}}"));
}
{
PhysicalPlan p = optimizeAndPlan("SELECT MAX(keyword) FROM test");
assertEquals(EsQueryExec.class, p.getClass());
EsQueryExec eqe = (EsQueryExec) p;
assertEquals(1, eqe.output().size());
assertEquals("MAX(keyword)", eqe.output().get(0).qualifiedName());
assertEquals(DataType.KEYWORD, eqe.output().get(0).dataType());
assertThat(eqe.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""),
endsWith("\"top_hits\":{\"from\":0,\"size\":1,\"version\":false,\"seq_no_primary_term\":false," +
"\"explain\":false,\"docvalue_fields\":[{\"field\":\"keyword\"}]," +
"\"sort\":[{\"keyword\":{\"order\":\"desc\",\"missing\":\"_last\",\"unmapped_type\":\"keyword\"}}]}}}}}"));
}
}
public void testTopHitsAggregationWithTwoArgs() {