You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by GitBox <gi...@apache.org> on 2023/01/05 15:00:19 UTC

[GitHub] [drill] jnturton commented on a diff in pull request #2729: DRILL-8376: Add Distribution UDFs

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


##########
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);
+        }
+      }
+    }
+  }
+
+  @FunctionTemplate(
+      names = {"kendall_correlation","kendallCorrelation", "kendallTau", "kendall_tau"},
+      scope = FunctionScope.POINT_AGGREGATE,
+      nulls = NullHandling.INTERNAL
+  )
+  public static class KendallTauFunction implements DrillAggFunc {
+    @Param
+    Float8Holder xInput;
+
+    @Param
+    Float8Holder yInput;
+
+    @Workspace
+    Float8Holder prevXValue;
+
+    @Workspace
+    Float8Holder prevYValue;
+
+    @Workspace
+    IntHolder concordantPairs;
+
+    @Workspace
+    IntHolder discordantPairs;
+
+    @Workspace
+    IntHolder n;
+
+    @Output
+    Float8Holder tau;
+
+    @Override
+    public void add() {
+      double xValue = xInput.value;
+      double yValue = yInput.value;
+
+      if (n.value > 0) {
+        if ((xValue > prevXValue.value && yValue > prevYValue.value) || (xValue < prevXValue.value && yValue < prevYValue.value)) {
+          concordantPairs.value = concordantPairs.value + 1;
+        } else if ((xValue > prevXValue.value && yValue < prevYValue.value) || (xValue < prevXValue.value && yValue > prevYValue.value)) {
+          discordantPairs.value = discordantPairs.value + 1;
+        } else {
+          //Tie...
+        }
+
+        prevXValue.value = xInput.value;
+        prevYValue.value = yInput.value;
+        n.value = n.value + 1;

Review Comment:
   Given that xValue = xInput.value and yValue = yInput.value, I think this code is common to both branches of the parent if statement.



##########
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:
   ```suggestion
     // We define the buckets as the partition of the range by right half open intervals [ x, x + width )
     bucket.value = 1 + (inputValue.value - MinRangeValueHolder.value) / binWidth
     // TODO: decide on behaviour if the result above is less than 1 or more than bucketCount. Return it as is? Error? Null?
   ```



-- 
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-unsubscribe@drill.apache.org

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