Skip to content

Commit

Permalink
SQL: Fix issue with common type resolution (#46565)
Browse files Browse the repository at this point in the history
Many scalar functions try to find out the common type between their
arguments in order to set it as their return time, e.g.:
for `float + double` the common type which is set as the return type
of the + operation is `double`.

Previously, for data types TEXT and KEYWORD (string data types) there
was no common data type found and null was returned causing NPEs when
the function was trying to resolve the return data type.

Fixes: #46551
(cherry picked from commit 291017d)
  • Loading branch information
matriv committed Sep 11, 2019
1 parent d50ed0d commit 1e143f6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.elasticsearch.xpack.sql.type.DataType.DATETIME;
import static org.elasticsearch.xpack.sql.type.DataType.LONG;
import static org.elasticsearch.xpack.sql.type.DataType.NULL;
import static org.elasticsearch.xpack.sql.type.DataType.TEXT;
import static org.elasticsearch.xpack.sql.type.DataType.TIME;

/**
Expand Down Expand Up @@ -50,6 +51,12 @@ public static DataType commonType(DataType left, DataType right) {
if (DataTypes.isNull(right)) {
return left;
}
if (left.isString() && right.isString()) {
if (left == TEXT) {
return TEXT;
}
return right;
}
if (left.isNumeric() && right.isNumeric()) {
// if one is int
if (left.isInteger()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,12 +621,15 @@ public void testCommonType() {
assertEquals(NULL, commonType(NULL, NULL));
assertEquals(INTEGER, commonType(INTEGER, KEYWORD));
assertEquals(LONG, commonType(TEXT, LONG));
assertNull(commonType(TEXT, KEYWORD));
assertEquals(SHORT, commonType(SHORT, BYTE));
assertEquals(FLOAT, commonType(BYTE, FLOAT));
assertEquals(FLOAT, commonType(FLOAT, INTEGER));
assertEquals(DOUBLE, commonType(DOUBLE, FLOAT));

// strings
assertEquals(TEXT, commonType(TEXT, KEYWORD));
assertEquals(TEXT, commonType(KEYWORD, TEXT));

// numeric and intervals
assertEquals(INTERVAL_YEAR_TO_MONTH, commonType(INTERVAL_YEAR_TO_MONTH, LONG));
assertEquals(INTERVAL_HOUR_TO_MINUTE, commonType(INTEGER, INTERVAL_HOUR_TO_MINUTE));
Expand Down

0 comments on commit 1e143f6

Please sign in to comment.