Skip to content

Commit

Permalink
SQL: Translate MIN/MAX on keyword fields as FIRST/LAST (#41247)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
matriv authored Apr 16, 2019
1 parent ab2613c commit f426a33
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
20 changes: 20 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/agg.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ protected Iterable<RuleExecutor<LogicalPlan>.Batch> batches() {

Batch aggregate = new Batch("Aggregation Rewrite",
//new ReplaceDuplicateAggsWithReferences(),
new ReplaceMinMaxWithTopHits(),
new ReplaceAggsWithMatrixStats(),
new ReplaceAggsWithExtendedStats(),
new ReplaceAggsWithStats(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,18 @@ public void testTopHitsAggregationWithOneArg() {
"\"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());
Expand All @@ -775,6 +787,18 @@ public void testTopHitsAggregationWithOneArg() {
"\"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() {
Expand Down

0 comments on commit f426a33

Please sign in to comment.