SQL: use the correct data type for types conversion (#46574)

(cherry picked from commit 3e25db2f302c3aafe27e4d8d4fb1743401d85e6d)
This commit is contained in:
Andrei Stefan 2019-09-16 15:31:37 +03:00 committed by Andrei Stefan
parent fe8f33a8e1
commit 40e9353947
2 changed files with 31 additions and 3 deletions

View File

@ -34,6 +34,7 @@ import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
@ -1157,6 +1158,33 @@ public class ResultSetTestCase extends JdbcIntegrationTestCase {
assertEquals(expectedTimestamp, results.getObject("date", java.sql.Timestamp.class));
});
}
public void testGetDateTypeFromAggregation() throws Exception {
createIndex("test");
updateMapping("test", builder -> builder.startObject("test_date").field("type", "date").endObject());
// 1984-05-02 14:59:12 UTC
Long timeInMillis = 452357952000L;
index("test", "1", builder -> builder.field("test_date", timeInMillis));
doWithQueryAndTimezone("SELECT CONVERT(test_date, DATE) AS converted FROM test GROUP BY converted", "UTC", results -> {
results.next();
ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeInMillis), ZoneId.of("Z"))
.toLocalDate().atStartOfDay(ZoneId.of("Z"));
java.sql.Date expectedDate = new java.sql.Date(zdt.toInstant().toEpochMilli());
assertEquals(expectedDate, results.getDate("converted"));
assertEquals(expectedDate, results.getObject("converted", java.sql.Date.class));
java.sql.Time expectedTime = new java.sql.Time(0L);
assertEquals(expectedTime, results.getTime("converted"));
assertEquals(expectedTime, results.getObject("converted", java.sql.Time.class));
java.sql.Timestamp expectedTimestamp = new java.sql.Timestamp(zdt.toInstant().toEpochMilli());
assertEquals(expectedTimestamp, results.getTimestamp("converted"));
assertEquals(expectedTimestamp, results.getObject("converted", java.sql.Timestamp.class));
});
}
public void testGetTimeType() throws Exception {
createIndex("test");

View File

@ -405,7 +405,7 @@ public final class InternalSqlScriptUtils {
if (text == null || typeName == null) {
return null;
}
return new IntervalDayTime(Duration.parse(text), DataType.fromTypeName(typeName));
return new IntervalDayTime(Duration.parse(text), DataType.fromSqlOrEsType(typeName));
}
public static IntervalYearMonth intervalYearMonth(String text, String typeName) {
@ -413,7 +413,7 @@ public final class InternalSqlScriptUtils {
return null;
}
return new IntervalYearMonth(Period.parse(text), DataType.fromTypeName(typeName));
return new IntervalYearMonth(Period.parse(text), DataType.fromSqlOrEsType(typeName));
}
public static OffsetTime asTime(String time) {
@ -553,6 +553,6 @@ public final class InternalSqlScriptUtils {
public static Object cast(Object value, String typeName) {
// we call asDateTime here to make sure we handle JodaCompatibleZonedDateTime properly,
// since casting works for ZonedDateTime objects only
return DataTypeConversion.convert(asDateTime(value, true), DataType.fromTypeName(typeName));
return DataTypeConversion.convert(asDateTime(value, true), DataType.fromSqlOrEsType(typeName));
}
}