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 2022/12/25 04:37:42 UTC

[GitHub] [drill] cgivre opened a new pull request, #2729: DRILL-8376: Add Distribution UDFs

cgivre opened a new pull request, #2729:
URL: https://github.com/apache/drill/pull/2729

   # [DRILL-8376](https://issues.apache.org/jira/browse/DRILL-8376): Add Distribution UDFs
   
   ## Description
   This PR adds several new UDFs to help with statistical analysis.  They are `width_bucket` which mirrors the functionality of the POSTGRES function of the same name. (https://www.oreilly.com/library/view/sql-in-a/9780596155322/re91.html).  This function is useful for building histograms of data.
   
   This also adds the `kendall_correlation` and `pearson_correlation` functions which are two function for calculating correlation coefficients of two columns.
   
   ## Documentation
   Updated README.
   
   ## Testing
   Added unit tests.


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


[GitHub] [drill] cgivre commented on pull request #2729: DRILL-8376: Add Distribution UDFs

Posted by GitBox <gi...@apache.org>.
cgivre commented on PR #2729:
URL: https://github.com/apache/drill/pull/2729#issuecomment-1375739889

   @jnturton Thanks for the review.  I believe I've addressed your comments.


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


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

Posted by GitBox <gi...@apache.org>.
cgivre commented on code in PR #2729:
URL: https://github.com/apache/drill/pull/2729#discussion_r1064724101


##########
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:
   Fixed.



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


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

Posted by GitBox <gi...@apache.org>.
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-unsubscribe@drill.apache.org

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


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

Posted by GitBox <gi...@apache.org>.
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


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

Posted by GitBox <gi...@apache.org>.
jnturton commented on code in PR #2729:
URL: https://github.com/apache/drill/pull/2729#discussion_r1065424637


##########
contrib/udfs/src/main/java/org/apache/drill/exec/udfs/DistributionFunctions.java:
##########
@@ -51,31 +51,29 @@ public static class WidthBucketFunction implements DrillSimpleFunc {
     @Workspace
     double binWidth;
 
+    @Workspace
+    int bucketCount;
+
     @Output
     IntHolder bucket;
 
     @Override
     public void setup() {
       double max = MaxRangeValueHolder.value;
       double min = MinRangeValueHolder.value;
-      int bucketCount = bucketCountHolder.value;
+      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);
-        }
+      if (inputValue.value < MinRangeValueHolder.value) {
+        bucket.value = 0;
+      } else if (inputValue.value > MaxRangeValueHolder.value) {
+        bucket.value = bucketCount + 1;
+      } else {
+        double f = (1 + (inputValue.value - MinRangeValueHolder.value) / binWidth);

Review Comment:
   It looks like `f` is recomputed rather than used in what follows.



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


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

Posted by GitBox <gi...@apache.org>.
cgivre commented on code in PR #2729:
URL: https://github.com/apache/drill/pull/2729#discussion_r1065940539


##########
contrib/udfs/src/main/java/org/apache/drill/exec/udfs/DistributionFunctions.java:
##########
@@ -51,31 +51,29 @@ public static class WidthBucketFunction implements DrillSimpleFunc {
     @Workspace
     double binWidth;
 
+    @Workspace
+    int bucketCount;
+
     @Output
     IntHolder bucket;
 
     @Override
     public void setup() {
       double max = MaxRangeValueHolder.value;
       double min = MinRangeValueHolder.value;
-      int bucketCount = bucketCountHolder.value;
+      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);
-        }
+      if (inputValue.value < MinRangeValueHolder.value) {
+        bucket.value = 0;
+      } else if (inputValue.value > MaxRangeValueHolder.value) {
+        bucket.value = bucketCount + 1;
+      } else {
+        double f = (1 + (inputValue.value - MinRangeValueHolder.value) / binWidth);

Review Comment:
   Oops... That was a test variable.  Removed. 



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


[GitHub] [drill] cgivre merged pull request #2729: DRILL-8376: Add Distribution UDFs

Posted by GitBox <gi...@apache.org>.
cgivre merged PR #2729:
URL: https://github.com/apache/drill/pull/2729


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