mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 02:14:54 +00:00
SQL: use the correct data type for types conversion (#46574)
(cherry picked from commit 3e25db2f302c3aafe27e4d8d4fb1743401d85e6d)
This commit is contained in:
parent
fe8f33a8e1
commit
40e9353947
@ -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");
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user