ihuzenko commented on a change in pull request #2033: DRILL-7652: Add 
time_bucket() function for time series analysis
URL: https://github.com/apache/drill/pull/2033#discussion_r395664288
 
 

 ##########
 File path: 
contrib/udfs/src/main/java/org/apache/drill/exec/udfs/TimeBucketFunctions.java
 ##########
 @@ -0,0 +1,105 @@
+/*
+ * 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;
+
+import org.apache.drill.exec.expr.DrillSimpleFunc;
+import org.apache.drill.exec.expr.annotations.FunctionTemplate;
+import org.apache.drill.exec.expr.annotations.Output;
+import org.apache.drill.exec.expr.annotations.Param;
+import org.apache.drill.exec.expr.holders.BigIntHolder;
+
+
+public class TimeBucketFunctions {
+
+  /**
+   * This function is used for facilitating time series analysis by creating 
buckets of time intervals.  See
+   * 
https://blog.timescale.com/blog/simplified-time-series-analytics-using-the-time_bucket-function/
 for usage. The function takes two arguments:
+   * 1. The timestamp in nanoseconds
+   * 2. The desired bucket interval IN MILLISECONDS
+   *
+   * The function returns a BIGINT of the nearest time bucket.
+   */
+  @FunctionTemplate(name = "time_bucket_ns",
+    scope = FunctionTemplate.FunctionScope.SIMPLE,
+    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
+  public static class TimeBucketNSFunction implements DrillSimpleFunc {
+
+    @Param
+    BigIntHolder inputDate;
+
+    @Param
+    BigIntHolder interval;
+
+    @Output
+    BigIntHolder out;
+
+    @Override
+    public void setup() {
+    }
+
+    @Override
+    public void eval() {
+      // Get the timestamp in nanoseconds
+      long timestamp = inputDate.value;
+
+      // Get the interval in milliseconds and convert to nanoseconds
+      long intervalToAdd = interval.value * 1000000;
+
+      out.value = (long) java.lang.Math.floor(timestamp / intervalToAdd) * 
intervalToAdd;
+    }
+  }
+
+  /**
+   * This function is used for facilitating time series analysis by creating 
buckets of time intervals.  See
+   * 
https://blog.timescale.com/blog/simplified-time-series-analytics-using-the-time_bucket-function/
 for usage. The function takes two arguments:
+   * 1. The timestamp in milliseconds
+   * 2. The desired bucket interval IN milliseconds
+   *
+   * The function returns a BIGINT of the nearest time bucket.
+   */
+  @FunctionTemplate(name = "time_bucket",
+    scope = FunctionTemplate.FunctionScope.SIMPLE,
+    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
+  public static class TimeBucketFunction implements DrillSimpleFunc {
+
+    @Param
+    BigIntHolder inputDate;
+
+    @Param
+    BigIntHolder interval;
+
+    @Output
+    BigIntHolder out;
+
+    @Override
+    public void setup() {
+    }
+
+    @Override
+    public void eval() {
+      // Get the timestamp in milliseconds
+      long timestamp = inputDate.value;
+
+      // Get the interval in milliseconds
+      long intervalToAdd = interval.value;
+
+      out.value = (long) java.lang.Math.floor(timestamp / intervalToAdd) * 
intervalToAdd;
 
 Review comment:
   no problems, you are a little bit in a rush I guess since in your second 
commit you left commented line. Also, it would be good to rerun your test just 
to be sure that the suggestion works fine. 

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to