From 3578e69669c7ece8ca7f31f3be018b359c7f8a49 Mon Sep 17 00:00:00 2001 From: Andrei Stefan Date: Fri, 4 Jan 2019 09:19:24 +0200 Subject: [PATCH] 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 --- .../qa/src/main/resources/datetime.csv-spec | 34 +++++++++++++++++++ .../whitelist/InternalSqlScriptUtils.java | 19 ++++++++--- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec b/x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec index d966c8c822e..5e51ae69bf3 100644 --- a/x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec +++ b/x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec @@ -125,6 +125,40 @@ SELECT WEEK(birth_date) week, birth_date FROM test_emp WHERE WEEK(birth_date) > // 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 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; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScriptUtils.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScriptUtils.java index 6d39fa6fbc2..01d56188ed2 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScriptUtils.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScriptUtils.java @@ -346,6 +346,10 @@ public final class InternalSqlScriptUtils { } public static ZonedDateTime asDateTime(Object dateTime) { + return (ZonedDateTime) asDateTime(dateTime, false); + } + + private static Object asDateTime(Object dateTime, boolean lenient) { if (dateTime == null) { return null; } @@ -355,11 +359,14 @@ public final class InternalSqlScriptUtils { if (dateTime instanceof ZonedDateTime) { return (ZonedDateTime) dateTime; } - if (dateTime instanceof Number) { - return DateUtils.of(((Number) dateTime).longValue()); + if (false == lenient) { + if (dateTime instanceof Number) { + 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) { @@ -468,6 +475,8 @@ public final class InternalSqlScriptUtils { // Casting // 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)); } }