ggangadharan commented on code in PR #6898: URL: https://github.com/apache/hive/pull/6898#discussion_r4386556112
########## 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: Done. I checked it with the same JMH benchmark first. The JIT hoists these field reads out of the loop on its own, so removing the locals makes no measurable difference. With all 3 input types in one JVM: buffered −4% / −5%, streaming +3% / +5% (no NULLs / 10% NULLs), with overlapping error bars. The only local I kept is value in the repeating-value branch: inlining it there would rerun getDouble(), and for decimals the conversion, on every row instead of once per batch. -- 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]
