1
0
Эх сурвалжийг харах

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
Andrei Stefan 6 жил өмнө
parent
commit
3578e69669

+ 34 - 0
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;
 

+ 14 - 5
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));
     }
 }