ramitg254 commented on code in PR #6898:
URL: https://github.com/apache/hive/pull/6898#discussion_r4346487986


##########
ql/src/java/org/apache/hadoop/hive/ql/plan/VectorPTFDesc.java:
##########
@@ -108,21 +111,40 @@ public enum SupportedFunctionType {
     LAST_VALUE,
     COUNT(true),
     LEAD,
-    LAG;
+    LAG,
+    VARIANCE(VarianceKind.VARIANCE),
+    VAR_POP(VarianceKind.VARIANCE),
+    VAR_SAMP(VarianceKind.VARIANCE_SAMPLE),
+    STD(VarianceKind.STANDARD_DEVIATION),
+    STDDEV(VarianceKind.STANDARD_DEVIATION),
+    STDDEV_POP(VarianceKind.STANDARD_DEVIATION),
+    STDDEV_SAMP(VarianceKind.STANDARD_DEVIATION_SAMPLE);
 
     private final boolean supportDistinct;
+    private final VarianceKind varianceKind;
 
     SupportedFunctionType() {
-      supportDistinct = false;
+      this(false);
     }
 
     SupportedFunctionType(boolean supportDistinct) {
       this.supportDistinct = supportDistinct;
+      this.varianceKind = null;
+    }
+
+    SupportedFunctionType(VarianceKind varianceKind) {
+      this.supportDistinct = false;

Review Comment:
   by default, I think this.supportDistinct should be false, we can remove this 
from constructor



##########
ql/src/java/org/apache/hadoop/hive/ql/plan/VectorPTFDesc.java:
##########
@@ -108,21 +111,40 @@ public enum SupportedFunctionType {
     LAST_VALUE,
     COUNT(true),
     LEAD,
-    LAG;
+    LAG,
+    VARIANCE(VarianceKind.VARIANCE),
+    VAR_POP(VarianceKind.VARIANCE),
+    VAR_SAMP(VarianceKind.VARIANCE_SAMPLE),
+    STD(VarianceKind.STANDARD_DEVIATION),
+    STDDEV(VarianceKind.STANDARD_DEVIATION),
+    STDDEV_POP(VarianceKind.STANDARD_DEVIATION),
+    STDDEV_SAMP(VarianceKind.STANDARD_DEVIATION_SAMPLE);
 
     private final boolean supportDistinct;
+    private final VarianceKind varianceKind;
 
     SupportedFunctionType() {
-      supportDistinct = false;
+      this(false);
     }
 
     SupportedFunctionType(boolean supportDistinct) {
       this.supportDistinct = supportDistinct;
+      this.varianceKind = null;

Review Comment:
   same 
https://github.com/apache/hive/pull/6898/changes#diff-fa58b977131685e0d39ee1419128aa0ef0b7a872f5b36aa3669967d1de340fe0R136
   this null assignment can be removed



##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorStreamingVariance.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.hadoop.hive.ql.exec.vector.ptf;
+
+import org.apache.hadoop.hive.ql.exec.vector.ColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.ColumnVector.Type;
+import org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.DoubleColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpression;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.ql.plan.ptf.WindowFrameDef;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFVariance.VarianceKind;
+import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
+
+import com.google.common.base.Preconditions;
+
+/**
+ * This class evaluates the variance family functions for a PTF group in a 
streaming fashion
+ * (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW).
+ *
+ * Each non-null value is added to the running aggregation and every row gets 
the running result.
+ * The result is only recalculated when a value is added, null rows reuse it.
+ */
+public class VectorPTFEvaluatorStreamingVariance extends 
VectorPTFEvaluatorAbstractVariance {
+
+  private boolean isCurrentResultNull;
+  private double currentResult;
+
+  public VectorPTFEvaluatorStreamingVariance(WindowFrameDef windowFrameDef,
+      VectorExpression inputVecExpr, int outputColumnNum, Type 
inputColumnVectorType,
+      VarianceKind varianceKind) {
+    super(windowFrameDef, inputVecExpr, outputColumnNum, 
inputColumnVectorType, varianceKind);
+    resetEvaluator();
+  }
+
+  @Override
+  public void evaluateGroupBatch(VectorizedRowBatch batch)
+      throws HiveException {
+
+    // We do not filter when PTF is in reducer.
+    Preconditions.checkState(!batch.selectedInUse);
+
+    final int size = batch.size;
+    if (size == 0) {
+      return;
+    }
+    ColumnVector colVector = batch.cols[inputColumnNum];
+    DoubleColumnVector outputColVector = (DoubleColumnVector) 
batch.cols[outputColumnNum];
+
+    if (colVector.isRepeating) {
+      if (colVector.noNulls || !colVector.isNull[0]) {
+        final double value = getDouble(colVector, 0);
+        for (int i = 0; i < size; i++) {
+          aggregate(value);
+          setOutput(outputColVector, i);
+        }
+      } else {
+
+        // All nulls, continue the previous result.
+        setOutput(outputColVector, 0);
+        outputColVector.isRepeating = true;
+      }
+      return;
+    }
+
+    switch (inputColumnVectorType) {
+    case LONG:
+      evaluateLongs((LongColumnVector) colVector, outputColVector, size);
+      break;
+    case DOUBLE:
+      evaluateDoubles((DoubleColumnVector) colVector, outputColVector, size);
+      break;
+    case DECIMAL:
+      evaluateDecimals((DecimalColumnVector) colVector, outputColVector, size);
+      break;
+    default:
+      throw new IllegalStateException("Unexpected column vector type " + 
inputColumnVectorType);
+    }
+  }
+
+  private void evaluateLongs(LongColumnVector colVector, DoubleColumnVector 
outputColVector,

Review Comment:
   same https://github.com/apache/hive/pull/6898/changes#r4346972436



##########
ql/src/java/org/apache/hadoop/hive/ql/plan/VectorPTFDesc.java:
##########
@@ -108,21 +111,40 @@ public enum SupportedFunctionType {
     LAST_VALUE,
     COUNT(true),
     LEAD,
-    LAG;
+    LAG,
+    VARIANCE(VarianceKind.VARIANCE),
+    VAR_POP(VarianceKind.VARIANCE),
+    VAR_SAMP(VarianceKind.VARIANCE_SAMPLE),
+    STD(VarianceKind.STANDARD_DEVIATION),
+    STDDEV(VarianceKind.STANDARD_DEVIATION),
+    STDDEV_POP(VarianceKind.STANDARD_DEVIATION),
+    STDDEV_SAMP(VarianceKind.STANDARD_DEVIATION_SAMPLE);
 
     private final boolean supportDistinct;
+    private final VarianceKind varianceKind;
 
     SupportedFunctionType() {
-      supportDistinct = false;
+      this(false);

Review Comment:
   same 
https://github.com/apache/hive/pull/6898/changes#diff-fa58b977131685e0d39ee1419128aa0ef0b7a872f5b36aa3669967d1de340fe0R136
   body can be empty



##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorVariance.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.hadoop.hive.ql.exec.vector.ptf;
+
+import org.apache.hadoop.hive.ql.exec.vector.ColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.ColumnVector.Type;
+import org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.DoubleColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpression;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.ql.plan.ptf.WindowFrameDef;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFVariance.VarianceKind;
+import org.apache.hadoop.hive.ql.udf.ptf.Range;
+import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
+
+import com.google.common.base.Preconditions;
+
+/**
+ * This class evaluates the variance family functions for a PTF group.
+ *
+ * The group result is calculated from the non-null values of the window 
range.  When a range
+ * starts where the previous one started and does not end before it (e.g. 
UNBOUNDED PRECEDING
+ * frames), the previous aggregation state is reused and only the new rows are 
iterated.  As the
+ * values are still iterated in row order, the result is the same as a full 
recalculation.
+ */
+public class VectorPTFEvaluatorVariance extends 
VectorPTFEvaluatorAbstractVariance {
+
+  private Range previousRange;
+  private long previousCount;
+  private double previousSum;
+  private double previousVariance;
+
+  public VectorPTFEvaluatorVariance(WindowFrameDef windowFrameDef, 
VectorExpression inputVecExpr,
+      int outputColumnNum, Type inputColumnVectorType, VarianceKind 
varianceKind) {
+    super(windowFrameDef, inputVecExpr, outputColumnNum, 
inputColumnVectorType, varianceKind);
+    resetEvaluator();
+  }
+
+  @Override
+  public void evaluateGroupBatch(VectorizedRowBatch batch)
+      throws HiveException {
+
+    // We do not filter when PTF is in reducer.
+    Preconditions.checkState(!batch.selectedInUse);
+
+    final int size = batch.size;
+    if (size == 0) {
+      return;
+    }
+    ColumnVector colVector = batch.cols[inputColumnNum];
+    if (colVector.isRepeating) {
+      if (colVector.noNulls || !colVector.isNull[0]) {
+        final double value = getDouble(colVector, 0);
+        for (int i = 0; i < size; i++) {
+          iterate(value);
+        }
+      }
+      return;
+    }
+
+    switch (inputColumnVectorType) {
+    case LONG:
+      iterateLongs((LongColumnVector) colVector, size);
+      break;
+    case DOUBLE:
+      iterateDoubles((DoubleColumnVector) colVector, size);
+      break;
+    case DECIMAL:
+      iterateDecimals((DecimalColumnVector) colVector, size);
+      break;
+    default:
+      throw new IllegalStateException("Unexpected column vector type " + 
inputColumnVectorType);
+    }
+  }
+
+  private void iterateLongs(LongColumnVector colVector, int size) {

Review Comment:
   all three variations of it can be combined to one to something like:
   ```
   final boolean noNulls = colVector.noNulls;
     final boolean[] isNull = colVector.isNull;
     for (int i = 0; i < size; i++) {
       if (noNulls || !isNull[i]) {
         iterate(getDouble(colVector, i));
       }
     }
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to