arina-ielchiieva commented on a change in pull request #1680: Drill-7077: Add 
Function to Facilitate Time Series Analysis
URL: https://github.com/apache/drill/pull/1680#discussion_r263480921
 
 

 ##########
 File path: 
contrib/udfs/src/main/java/org/apache/drill/exec/udfs/NearestDateUtils.java
 ##########
 @@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.drill.exec.udfs;
+
+public class NearestDateUtils {
+  /**
+   * Specifies the time grouping to be used with the nearest date function
+   */
+  private enum TimeInterval {
+    YEAR,
+    QUARTER,
+    MONTH,
+    WEEK_SUNDAY,
+    WEEK_MONDAY,
+    DAY,
+    HOUR,
+    HALF_HOUR,
+    QUARTER_HOUR,
+    MINUTE,
+    HALF_MINUTE,
+    QUARTER_MINUTE,
+    SECOND
+  }
+  private static final org.slf4j.Logger logger = 
org.slf4j.LoggerFactory.getLogger(NearestDateUtils.class);
+
+  /**
+   * This function takes a Java LocalDateTime object, and an interval string 
and returns
+   * the nearest date closets to that time.  For instance, if you specified 
the date as 2018-05-04 and YEAR, the function
+   * will return 2018-01-01
+   * @param LocalDateTime d
+   * @param String interval The interval string to deduct from the supplied 
date
+   * @return the modified LocalDateTime
+   */
+  public final static java.time.LocalDateTime getDate(java.time.LocalDateTime 
d, String interval) {
+    java.time.LocalDateTime newDate = d;
+    int year = d.getYear();
+    int month = d.getMonth().getValue();
+    int day = d.getDayOfMonth();
+    int hour = d.getHour();
+    int minute = d.getMinute();
+    int second = d.getSecond();
+
+    try {
+      switch (TimeInterval.valueOf(interval.toUpperCase())) {
+        case YEAR:
+          newDate = java.time.LocalDateTime.of(year, 1, 1, 0, 0, 0);
+          break;
+        case QUARTER:
+          newDate = java.time.LocalDateTime.of(year, (month / 3) * 3 + 1, 1, 
0, 0, 0);
+          break;
+        case MONTH:
+          newDate = java.time.LocalDateTime.of(year, month, 1, 0, 0, 0);
+          break;
+        case WEEK_SUNDAY:
+          newDate = 
newDate.with(java.time.temporal.TemporalAdjusters.previousOrSame(java.time.DayOfWeek.SUNDAY))
+                  .truncatedTo(java.time.temporal.ChronoUnit.DAYS);
+          break;
+        case WEEK_MONDAY:
+          newDate = 
newDate.with(java.time.temporal.TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY))
+                  .truncatedTo(java.time.temporal.ChronoUnit.DAYS);
+          break;
+        case DAY:
+          newDate = java.time.LocalDateTime.of(year, month, day, 0, 0, 0);
+          break;
+        case HOUR:
+          newDate = java.time.LocalDateTime.of(year, month, day, hour, 0, 0);
+          break;
+        case HALF_HOUR:
+          if (minute >= 30) {
+            minute = 30;
+          } else {
+            minute = 0;
+          }
+          newDate = java.time.LocalDateTime.of(year, month, day, hour, minute, 
0);
+          break;
+        case QUARTER_HOUR:
+          if (minute >= 45) {
+            minute = 45;
+          } else if (minute >= 30) {
+            minute = 30;
+          } else if (minute >= 15) {
+            minute = 15;
+          } else {
+            minute = 0;
+          }
+          newDate = java.time.LocalDateTime.of(year, month, day, hour, minute, 
0);
+          break;
+        case MINUTE:
+          newDate = java.time.LocalDateTime.of(year, month, day, hour, minute, 
0);
+          break;
+        case HALF_MINUTE:
+          if (second >= 30) {
+            second = 30;
+          } else {
+            second = 0;
+          }
+          newDate = java.time.LocalDateTime.of(year, month, day, hour, minute, 
second);
+          break;
+        case QUARTER_MINUTE:
+          if (second >= 45) {
+            second = 45;
+          } else if (second >= 30) {
+            second = 30;
+          } else if (second >= 15) {
+            second = 15;
+          } else {
+            second = 0;
+          }
+          newDate = java.time.LocalDateTime.of(year, month, day, hour, minute, 
second);
+          break;
+        case SECOND:
+          newDate = java.time.LocalDateTime.of(year, month, day, hour, minute, 
second);
+          break;
+        default:
+          throw  
org.apache.drill.common.exceptions.UserException.validationError()
+                  .message(interval + " is not a valid time statement.")
+                  .build(logger);
+      }
+    } catch (Exception e) {
+      throw  org.apache.drill.common.exceptions.UserException.validationError()
 
 Review comment:
   1. Catch only specific exception.
   2. Since this class is static, i.e. utility, I think it's a bit awkward that 
it throws user exception, please change it to DrillRuntimeException.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to