Skip to content

Commit

Permalink
SQL: Don't allow inexact fields for MIN/MAX (#39563)
Browse files Browse the repository at this point in the history
MIN/MAX on strings are supported and are implemented with
TopAggs FIRST/LAST respectively, but they cannot operate on
`text` fields without underlying `keyword` fields => inexact.

Follows: #39427
  • Loading branch information
matriv authored Mar 4, 2019
1 parent ceb7051 commit 8eaa1fe
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.List;

import static org.elasticsearch.xpack.sql.expression.TypeResolutions.isExact;
import static org.elasticsearch.xpack.sql.expression.TypeResolutions.isNumericOrDate;

/**
Expand Down Expand Up @@ -47,7 +48,7 @@ public String innerName() {
@Override
protected TypeResolution resolveType() {
if (field().dataType().isString()) {
return TypeResolution.TYPE_RESOLVED;
return isExact(field(), sourceText(), ParamOrdinal.DEFAULT);
} else {
return isNumericOrDate(field(), sourceText(), ParamOrdinal.DEFAULT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.List;

import static org.elasticsearch.xpack.sql.expression.TypeResolutions.isExact;
import static org.elasticsearch.xpack.sql.expression.TypeResolutions.isNumericOrDate;

/**
Expand Down Expand Up @@ -50,7 +51,7 @@ public String innerName() {
@Override
protected TypeResolution resolveType() {
if (field().dataType().isString()) {
return TypeResolution.TYPE_RESOLVED;
return isExact(field(), sourceText(), ParamOrdinal.DEFAULT);
} else {
return isNumericOrDate(field(), sourceText(), ParamOrdinal.DEFAULT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,18 @@ public void testTopHitsGroupByHavingUnsupported() {
error("SELECT FIRST(int) FROM test GROUP BY text HAVING FIRST(int) > 10"));
}

public void testMinOnInexactUnsupported() {
assertEquals("1:8: [MIN(text)] cannot operate on field of data type [text]: " +
"No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead",
error("SELECT MIN(text) FROM test"));
}

public void testMaxOnInexactUnsupported() {
assertEquals("1:8: [MAX(text)] cannot operate on field of data type [text]: " +
"No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead",
error("SELECT MAX(text) FROM test"));
}

public void testMinOnKeywordGroupByHavingUnsupported() {
assertEquals("1:52: HAVING filter is unsupported for function [MIN(keyword)]",
error("SELECT MIN(keyword) FROM test GROUP BY text HAVING MIN(keyword) > 10"));
Expand Down

0 comments on commit 8eaa1fe

Please sign in to comment.