|
12 | 12 | import org.elasticsearch.xpack.esql.core.type.DataType;
|
13 | 13 |
|
14 | 14 | import java.time.Instant;
|
| 15 | +import java.util.ArrayList; |
15 | 16 | import java.util.Arrays;
|
| 17 | +import java.util.Collections; |
| 18 | +import java.util.HashSet; |
16 | 19 | import java.util.List;
|
| 20 | +import java.util.Set; |
17 | 21 |
|
| 22 | +import static org.elasticsearch.xpack.esql.core.type.DataType.AGGREGATE_METRIC_DOUBLE; |
18 | 23 | import static org.elasticsearch.xpack.esql.core.type.DataType.BOOLEAN;
|
19 | 24 | import static org.elasticsearch.xpack.esql.core.type.DataType.BYTE;
|
20 | 25 | import static org.elasticsearch.xpack.esql.core.type.DataType.CARTESIAN_POINT;
|
|
47 | 52 | import static org.elasticsearch.xpack.esql.core.type.DataType.isDateTime;
|
48 | 53 | import static org.elasticsearch.xpack.esql.core.type.DataType.isDateTimeOrNanosOrTemporal;
|
49 | 54 | import static org.elasticsearch.xpack.esql.core.type.DataType.isString;
|
| 55 | +import static org.elasticsearch.xpack.esql.core.type.DataType.suggestedCast; |
50 | 56 | import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.commonType;
|
51 | 57 |
|
52 | 58 | public class EsqlDataTypeConverterTests extends ESTestCase {
|
@@ -186,4 +192,33 @@ private static void assertNullCommonType(DataType dataType1, DataType dataType2)
|
186 | 192 | assertNull("Expected null for " + dataType1 + " and " + dataType2, commonType(dataType1, dataType2));
|
187 | 193 | assertNull("Expected null for " + dataType1 + " and " + dataType2, commonType(dataType2, dataType1));
|
188 | 194 | }
|
| 195 | + |
| 196 | + public void testSuggestedCast() { |
| 197 | + // date |
| 198 | + { |
| 199 | + assertEquals(DATETIME, DataType.suggestedCast(Set.of(DATETIME, DATE_NANOS))); |
| 200 | + DataType randomType = DataType.values()[random().nextInt(DataType.values().length)]; |
| 201 | + DataType suggested = DataType.suggestedCast(Set.of(DATETIME, DATE_NANOS, randomType)); |
| 202 | + if (randomType != DATETIME && randomType != DATE_NANOS) { |
| 203 | + assertEquals(KEYWORD, suggested); |
| 204 | + } else { |
| 205 | + assertEquals(DATETIME, suggested); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + // aggregate metric double |
| 210 | + { |
| 211 | + List<DataType> NUMERICS = new ArrayList<>(Arrays.stream(DataType.values()).filter(DataType::isNumeric).toList()); |
| 212 | + Collections.shuffle(NUMERICS, random()); |
| 213 | + Set<DataType> subset = new HashSet<>(NUMERICS.subList(0, random().nextInt(NUMERICS.size()))); |
| 214 | + subset.add(AGGREGATE_METRIC_DOUBLE); |
| 215 | + assertEquals(AGGREGATE_METRIC_DOUBLE, suggestedCast(subset)); |
| 216 | + } |
| 217 | + |
| 218 | + // unsupported tests |
| 219 | + { |
| 220 | + assertNull(DataType.suggestedCast(Set.of())); |
| 221 | + assertNull(DataType.suggestedCast(Set.of(UNSUPPORTED, DataType.values()[random().nextInt(DataType.values().length)]))); |
| 222 | + } |
| 223 | + } |
189 | 224 | }
|
0 commit comments