Skip to content

Commit c72a799

Browse files
committed
SQL: Don't allow inexact fields for MIN/MAX (#39563)
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
1 parent 52ecf18 commit c72a799

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/aggregate/Max.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.util.List;
1515

16+
import static org.elasticsearch.xpack.sql.expression.TypeResolutions.isExact;
1617
import static org.elasticsearch.xpack.sql.expression.TypeResolutions.isNumericOrDate;
1718

1819
/**
@@ -47,7 +48,7 @@ public String innerName() {
4748
@Override
4849
protected TypeResolution resolveType() {
4950
if (field().dataType().isString()) {
50-
return TypeResolution.TYPE_RESOLVED;
51+
return isExact(field(), sourceText(), ParamOrdinal.DEFAULT);
5152
} else {
5253
return isNumericOrDate(field(), sourceText(), ParamOrdinal.DEFAULT);
5354
}

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/aggregate/Min.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.util.List;
1515

16+
import static org.elasticsearch.xpack.sql.expression.TypeResolutions.isExact;
1617
import static org.elasticsearch.xpack.sql.expression.TypeResolutions.isNumericOrDate;
1718

1819
/**
@@ -50,7 +51,7 @@ public String innerName() {
5051
@Override
5152
protected TypeResolution resolveType() {
5253
if (field().dataType().isString()) {
53-
return TypeResolution.TYPE_RESOLVED;
54+
return isExact(field(), sourceText(), ParamOrdinal.DEFAULT);
5455
} else {
5556
return isNumericOrDate(field(), sourceText(), ParamOrdinal.DEFAULT);
5657
}

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,18 @@ public void testTopHitsGroupByHavingUnsupported() {
717717
error("SELECT FIRST(int) FROM test GROUP BY text HAVING FIRST(int) > 10"));
718718
}
719719

720+
public void testMinOnInexactUnsupported() {
721+
assertEquals("1:8: [MIN(text)] cannot operate on field of data type [text]: " +
722+
"No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead",
723+
error("SELECT MIN(text) FROM test"));
724+
}
725+
726+
public void testMaxOnInexactUnsupported() {
727+
assertEquals("1:8: [MAX(text)] cannot operate on field of data type [text]: " +
728+
"No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead",
729+
error("SELECT MAX(text) FROM test"));
730+
}
731+
720732
public void testMinOnKeywordGroupByHavingUnsupported() {
721733
assertEquals("1:52: HAVING filter is unsupported for function [MIN(keyword)]",
722734
error("SELECT MIN(keyword) FROM test GROUP BY text HAVING MIN(keyword) > 10"));

0 commit comments

Comments
 (0)