Skip to content

Commit 3e25db2

Browse files
authored
SQL: use the correct data type for types conversion (#46574)
1 parent bd4d46c commit 3e25db2

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetTestCase.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.sql.Types;
3535
import java.time.Instant;
3636
import java.time.ZoneId;
37+
import java.time.ZonedDateTime;
3738
import java.util.Arrays;
3839
import java.util.Calendar;
3940
import java.util.Date;
@@ -1157,6 +1158,33 @@ public void testGetDateType() throws Exception {
11571158
assertEquals(expectedTimestamp, results.getObject("date", java.sql.Timestamp.class));
11581159
});
11591160
}
1161+
1162+
public void testGetDateTypeFromAggregation() throws Exception {
1163+
createIndex("test");
1164+
updateMapping("test", builder -> builder.startObject("test_date").field("type", "date").endObject());
1165+
1166+
// 1984-05-02 14:59:12 UTC
1167+
Long timeInMillis = 452357952000L;
1168+
index("test", "1", builder -> builder.field("test_date", timeInMillis));
1169+
1170+
doWithQueryAndTimezone("SELECT CONVERT(test_date, DATE) AS converted FROM test GROUP BY converted", "UTC", results -> {
1171+
results.next();
1172+
ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeInMillis), ZoneId.of("Z"))
1173+
.toLocalDate().atStartOfDay(ZoneId.of("Z"));
1174+
1175+
java.sql.Date expectedDate = new java.sql.Date(zdt.toInstant().toEpochMilli());
1176+
assertEquals(expectedDate, results.getDate("converted"));
1177+
assertEquals(expectedDate, results.getObject("converted", java.sql.Date.class));
1178+
1179+
java.sql.Time expectedTime = new java.sql.Time(0L);
1180+
assertEquals(expectedTime, results.getTime("converted"));
1181+
assertEquals(expectedTime, results.getObject("converted", java.sql.Time.class));
1182+
1183+
java.sql.Timestamp expectedTimestamp = new java.sql.Timestamp(zdt.toInstant().toEpochMilli());
1184+
assertEquals(expectedTimestamp, results.getTimestamp("converted"));
1185+
assertEquals(expectedTimestamp, results.getObject("converted", java.sql.Timestamp.class));
1186+
});
1187+
}
11601188

11611189
public void testGetTimeType() throws Exception {
11621190
createIndex("test");

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScriptUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,15 +405,15 @@ public static IntervalDayTime intervalDayTime(String text, String typeName) {
405405
if (text == null || typeName == null) {
406406
return null;
407407
}
408-
return new IntervalDayTime(Duration.parse(text), DataType.fromTypeName(typeName));
408+
return new IntervalDayTime(Duration.parse(text), DataType.fromSqlOrEsType(typeName));
409409
}
410410

411411
public static IntervalYearMonth intervalYearMonth(String text, String typeName) {
412412
if (text == null || typeName == null) {
413413
return null;
414414
}
415415

416-
return new IntervalYearMonth(Period.parse(text), DataType.fromTypeName(typeName));
416+
return new IntervalYearMonth(Period.parse(text), DataType.fromSqlOrEsType(typeName));
417417
}
418418

419419
public static OffsetTime asTime(String time) {
@@ -553,6 +553,6 @@ public static <T> GeoShape geoDocValue(Map<String, ScriptDocValues<T>> doc, Stri
553553
public static Object cast(Object value, String typeName) {
554554
// we call asDateTime here to make sure we handle JodaCompatibleZonedDateTime properly,
555555
// since casting works for ZonedDateTime objects only
556-
return DataTypeConversion.convert(asDateTime(value, true), DataType.fromTypeName(typeName));
556+
return DataTypeConversion.convert(asDateTime(value, true), DataType.fromSqlOrEsType(typeName));
557557
}
558558
}

0 commit comments

Comments
 (0)