cgivre commented on code in PR #2729:
URL: https://github.com/apache/drill/pull/2729#discussion_r1064725653


##########
contrib/udfs/src/main/java/org/apache/drill/exec/udfs/DistributionFunctions.java:
##########
@@ -0,0 +1,335 @@
+/*
+ * 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.DrillAggFunc;
+import org.apache.drill.exec.expr.DrillSimpleFunc;
+import org.apache.drill.exec.expr.annotations.FunctionTemplate;
+import org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope;
+import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling;
+import org.apache.drill.exec.expr.annotations.Output;
+import org.apache.drill.exec.expr.annotations.Param;
+import org.apache.drill.exec.expr.annotations.Workspace;
+import org.apache.drill.exec.expr.holders.Float8Holder;
+import org.apache.drill.exec.expr.holders.IntHolder;
+
+public class DistributionFunctions {
+
+  @FunctionTemplate(names = {"width_bucket", "widthBucket"},
+      scope = FunctionScope.SIMPLE,
+      nulls = NullHandling.NULL_IF_NULL)
+  public static class WidthBucketFunction implements DrillSimpleFunc {
+
+    @Param
+    Float8Holder inputValue;
+
+    @Param
+    Float8Holder MinRangeValueHolder;
+
+    @Param
+    Float8Holder MaxRangeValueHolder;
+
+    @Param
+    IntHolder bucketCountHolder;
+
+    @Workspace
+    double binWidth;
+
+    @Output
+    IntHolder bucket;
+
+    @Override
+    public void setup() {
+      double max = MaxRangeValueHolder.value;
+      double min = MinRangeValueHolder.value;
+      int bucketCount = bucketCountHolder.value;
+      binWidth = (max - min) / bucketCount;
+    }
+
+    @Override
+    public void eval() {
+      // There is probably a more elegant way of doing this...
+      double binFloor = MinRangeValueHolder.value;
+      double binCeiling = binFloor + binWidth;
+
+      for (int i = 1; i <= bucketCountHolder.value; i++) {
+        if (inputValue.value <= binCeiling && inputValue.value > binFloor) {
+           bucket.value = i;
+           break;
+        } else {
+          binFloor = binCeiling;
+          binCeiling = binWidth * (i + 1);
+        }
+      }

Review Comment:
   @jnturton I looked at the docs for PostgreSQL (which is where this function 
is modeled after), and saw that in PostgreSQL, if the result is less than the 
bucket count, it goes into bucket `0` and if it is larger than the range, it 
goes into bucket `n+1`. 
   
   



-- 
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.

To unsubscribe, e-mail: dev-unsubscr...@drill.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to