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


##########
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) {
+    final long[] vector = colVector.vector;

Review Comment:
   can we remove these explicit vars defined here and the other similar iterate 
methods?, they are used for a single occurrence only so can be used directly



##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorAbstractVariance.java:
##########
@@ -0,0 +1,111 @@
+/*
+ * 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 java.util.EnumSet;
+import java.util.Set;
+
+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.expressions.VectorExpression;
+import org.apache.hadoop.hive.ql.plan.ptf.WindowFrameDef;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFVariance;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFVariance.VarianceKind;
+
+import com.google.common.base.Preconditions;
+
+/**
+ * Base class of the variance family evaluators: variance/var_pop, var_samp,
+ * std/stddev/stddev_pop and stddev_samp.
+ *
+ * The aggregation state (count, sum and variance) is updated value by value 
in row order, exactly
+ * like GenericUDAFVarianceEvaluator.iterate does in row mode, so the 
vectorized results are
+ * identical to the row mode ones.  LONG, DOUBLE and DECIMAL input values are 
converted to double
+ * the same way row mode does.
+ */
+public abstract class VectorPTFEvaluatorAbstractVariance extends 
VectorPTFEvaluatorBase {
+
+  private static final Set<Type> SUPPORTED_INPUT_TYPES =
+      EnumSet.of(Type.LONG, Type.DOUBLE, Type.DECIMAL);
+
+  protected final Type inputColumnVectorType;
+  protected final VarianceKind varianceKind;
+
+  protected long count;
+  protected double sum;
+  // sum[(x - avg)^2], this is count times the population variance.
+  protected double variance;
+
+  protected VectorPTFEvaluatorAbstractVariance(WindowFrameDef windowFrameDef,
+      VectorExpression inputVecExpr, int outputColumnNum, Type 
inputColumnVectorType,
+      VarianceKind varianceKind) {
+    super(windowFrameDef, inputVecExpr, outputColumnNum);
+    
Preconditions.checkArgument(SUPPORTED_INPUT_TYPES.contains(inputColumnVectorType),
+        "Unexpected column vector type %s for %s", inputColumnVectorType, 
varianceKind);
+    this.inputColumnVectorType = inputColumnVectorType;
+    this.varianceKind = varianceKind;
+  }
+
+  protected final void iterate(double value) {
+    count++;
+    sum += value;
+    if (count > 1) {
+      variance = GenericUDAFVariance.calculateIntermediate(count, sum, value, 
variance);
+    }
+  }
+
+  protected final double getDouble(ColumnVector colVector, int i) {
+    switch (inputColumnVectorType) {
+    case LONG:
+      return (double) ((LongColumnVector) colVector).vector[i];
+    case DOUBLE:
+      return ((DoubleColumnVector) colVector).vector[i];
+    case DECIMAL:
+      return ((DecimalColumnVector) colVector).vector[i].doubleValue();
+    default:
+      throw new IllegalStateException("Unexpected column vector type " + 
inputColumnVectorType);
+    }
+  }
+
+  // SQL standard: null for zero elements (zero or one element for the sample 
variants).

Review Comment:
   can we drop this comment, seems obvious



##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorAbstractVariance.java:
##########
@@ -0,0 +1,111 @@
+/*
+ * 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 java.util.EnumSet;
+import java.util.Set;
+
+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.expressions.VectorExpression;
+import org.apache.hadoop.hive.ql.plan.ptf.WindowFrameDef;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFVariance;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFVariance.VarianceKind;
+
+import com.google.common.base.Preconditions;
+
+/**
+ * Base class of the variance family evaluators: variance/var_pop, var_samp,
+ * std/stddev/stddev_pop and stddev_samp.
+ *
+ * The aggregation state (count, sum and variance) is updated value by value 
in row order, exactly
+ * like GenericUDAFVarianceEvaluator.iterate does in row mode, so the 
vectorized results are
+ * identical to the row mode ones.  LONG, DOUBLE and DECIMAL input values are 
converted to double
+ * the same way row mode does.
+ */
+public abstract class VectorPTFEvaluatorAbstractVariance extends 
VectorPTFEvaluatorBase {
+
+  private static final Set<Type> SUPPORTED_INPUT_TYPES =
+      EnumSet.of(Type.LONG, Type.DOUBLE, Type.DECIMAL);
+
+  protected final Type inputColumnVectorType;
+  protected final VarianceKind varianceKind;
+
+  protected long count;
+  protected double sum;
+  // sum[(x - avg)^2], this is count times the population variance.
+  protected double variance;
+
+  protected VectorPTFEvaluatorAbstractVariance(WindowFrameDef windowFrameDef,
+      VectorExpression inputVecExpr, int outputColumnNum, Type 
inputColumnVectorType,
+      VarianceKind varianceKind) {
+    super(windowFrameDef, inputVecExpr, outputColumnNum);
+    
Preconditions.checkArgument(SUPPORTED_INPUT_TYPES.contains(inputColumnVectorType),
+        "Unexpected column vector type %s for %s", inputColumnVectorType, 
varianceKind);
+    this.inputColumnVectorType = inputColumnVectorType;
+    this.varianceKind = varianceKind;
+  }
+
+  protected final void iterate(double value) {
+    count++;
+    sum += value;
+    if (count > 1) {
+      variance = GenericUDAFVariance.calculateIntermediate(count, sum, value, 
variance);
+    }
+  }
+
+  protected final double getDouble(ColumnVector colVector, int i) {
+    switch (inputColumnVectorType) {
+    case LONG:
+      return (double) ((LongColumnVector) colVector).vector[i];
+    case DOUBLE:
+      return ((DoubleColumnVector) colVector).vector[i];
+    case DECIMAL:
+      return ((DecimalColumnVector) colVector).vector[i].doubleValue();
+    default:
+      throw new IllegalStateException("Unexpected column vector type " + 
inputColumnVectorType);
+    }
+  }
+
+  // SQL standard: null for zero elements (zero or one element for the sample 
variants).
+  protected final boolean isResultNull() {
+    return GenericUDAFVariance.isVarianceNull(count, varianceKind);
+  }
+
+  // Valid only when isResultNull() is false.  For one element the population 
variants return 0.

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



##########
ql/src/test/queries/clientpositive/vector_ptf_variance.q:
##########
@@ -0,0 +1,333 @@
+--! qt:dataset:alltypesorc

Review Comment:
   just a generic comment: I see in this test case couple of scenarios are 
covered
   not mandatory but if possible can we combine some cases in one 
   like 1 &2  can be combine to single one if possible



##########
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,
+      int size) {
+    final long[] vector = colVector.vector;

Review Comment:
   same can we remove these extra var definitions here as well as in similar 
evaluate methods



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