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

2023-01-10 Thread GitBox


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

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



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

2023-01-09 Thread GitBox


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



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

2023-01-09 Thread GitBox


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

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