SQL: Handle the bwc Joda ZonedDateTime scripting class in Painless (#37024)

* Handle the bwc Joda ZonedDateTime scripting class in Painless

* Integrated the types checking in the already existent method
This commit is contained in:
Andrei Stefan 2019-01-04 09:19:24 +02:00 committed by GitHub
parent 0ff2707c9f
commit 3578e69669
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 5 deletions

View File

@ -125,6 +125,40 @@ SELECT WEEK(birth_date) week, birth_date FROM test_emp WHERE WEEK(birth_date) >
// Aggregate // Aggregate
// //
castedDateTimeWithGroupBy1
SELECT CONVERT(birth_date, DOUBLE) AS date FROM test_emp GROUP BY date ORDER BY date LIMIT 10;
date:d
---------------
null
-5.631552E8
-5.586624E8
-5.56416E8
-5.539104E8
-5.517504E8
-5.492448E8
-5.406912E8
-5.371488E8
-5.359392E8
;
castedDateTimeWithGroupBy2
SELECT CAST(hire_date AS INTEGER) AS date FROM test_emp GROUP BY date ORDER BY date LIMIT 10;
date:i
---------------
477532800
478051200
484790400
489715200
495763200
498096000
498614400
501206400
501292800
501379200
;
dateTimeAggByIsoDayOfWeekWithFilter dateTimeAggByIsoDayOfWeekWithFilter
SELECT IDOW(birth_date) day, DAY_NAME(birth_date) name, COUNT(*) c FROM test_emp WHERE IDOW(birth_date) < 6 GROUP BY day, name ORDER BY day desc; SELECT IDOW(birth_date) day, DAY_NAME(birth_date) name, COUNT(*) c FROM test_emp WHERE IDOW(birth_date) < 6 GROUP BY day, name ORDER BY day desc;

View File

@ -346,6 +346,10 @@ public final class InternalSqlScriptUtils {
} }
public static ZonedDateTime asDateTime(Object dateTime) { public static ZonedDateTime asDateTime(Object dateTime) {
return (ZonedDateTime) asDateTime(dateTime, false);
}
private static Object asDateTime(Object dateTime, boolean lenient) {
if (dateTime == null) { if (dateTime == null) {
return null; return null;
} }
@ -355,12 +359,15 @@ public final class InternalSqlScriptUtils {
if (dateTime instanceof ZonedDateTime) { if (dateTime instanceof ZonedDateTime) {
return (ZonedDateTime) dateTime; return (ZonedDateTime) dateTime;
} }
if (false == lenient) {
if (dateTime instanceof Number) { if (dateTime instanceof Number) {
return DateUtils.of(((Number) dateTime).longValue()); return DateUtils.of(((Number) dateTime).longValue());
} }
throw new SqlIllegalArgumentException("Invalid date encountered [{}]", dateTime); throw new SqlIllegalArgumentException("Invalid date encountered [{}]", dateTime);
} }
return dateTime;
}
public static IntervalDayTime intervalDayTime(String text, String typeName) { public static IntervalDayTime intervalDayTime(String text, String typeName) {
if (text == null || typeName == null) { if (text == null || typeName == null) {
@ -468,6 +475,8 @@ public final class InternalSqlScriptUtils {
// Casting // Casting
// //
public static Object cast(Object value, String typeName) { public static Object cast(Object value, String typeName) {
return DataTypeConversion.convert(value, DataType.fromTypeName(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));
} }
} }