ramitg254 commented on code in PR #6997:
URL: https://github.com/apache/hive/pull/6997#discussion_r4798136652
##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/physical/Vectorizer.java:
##########
@@ -3032,6 +3034,16 @@ private boolean validatePTFOperator(PTFOperator op,
VectorizationContext vContex
return false;
}
+ if ((supportedFunctionType == SupportedFunctionType.LAG
+ || supportedFunctionType == SupportedFunctionType.LEAD)
+ && exprNodeDescList.size() > 2
+ && !isSupportedLeadLagDefault(exprNodeDescList.get(2),
+ exprNodeDescList.get(0).getTypeInfo())) {
+ setOperatorIssue(functionName
+ + " default value must be a constant or have the same type as
its first argument");
Review Comment:
It looks like check to fallback to non vectorized because we are not
allowing type conversion in vectorized, if that's the case why can't we just
use the same type conversion mechanism from row mode to here as well to avoid
this fallback?
##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorLead.java:
##########
@@ -19,27 +19,98 @@
package org.apache.hadoop.hive.ql.exec.vector.ptf;
+import java.util.List;
+
+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.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.ptf.Range;
+import com.google.common.base.Preconditions;
+
+/**
+ * Evaluates lead() as a group-aggregated streaming evaluator: lead needs rows
that arrive after
+ * the current one, so the partition is buffered. The input values are
collected in
+ * {@link #onBufferGroupBatch} while buffering, and written into the output
column when the batches
+ * are forwarded. Row r of the partition is the lead of row r - amt, so the
first amt rows are
+ * never kept.
+ */
public class VectorPTFEvaluatorLead extends VectorPTFEvaluatorAbstractLeadLag {
+ // Larger storage is released at the end of the partition, so one big
partition does not pin its
+ // memory.
+ private static final int MAX_RETAINED_LEAD_VALUES = 64 *
VectorizedRowBatch.DEFAULT_SIZE;
+
+ // The lead value of row r of the partition is at index r.
+ private ColumnVector leadValues;
+ private int leadValueCount;
+ // Rows of the partition buffered so far.
+ private int bufferedRows;
+ // Rows of the partition forwarded so far.
+ private int forwardedRows;
+
public VectorPTFEvaluatorLead(WindowFrameDef windowFrameDef,
VectorExpression inputVecExpr,
int outputColumnNum, Type type, int amt, VectorExpression
defaultValueExpression) {
super(windowFrameDef, inputVecExpr, outputColumnNum, type, amt,
defaultValueExpression);
+ resetEvaluator();
+ }
+
+ @Override
+ public boolean isGroupAggregatedStreamingEvaluator() {
+ return true;
+ }
+
+ @Override
+ public void addStreamingGroupResults(List<Integer> groupRowCounts) throws
HiveException {
Review Comment:
please make the method in base as no op rather than overriding it here
##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/physical/Vectorizer.java:
##########
@@ -5397,6 +5424,32 @@ private static VectorPTFInfo
createVectorPTFInfo(Operator<? extends OperatorDesc
return vectorPTFInfo;
}
+ /*
+ * Returns argument argumentIndex of an evaluator. Row mode converts the
lead/lag default value to
+ * the type of the first argument, so a constant default is converted here
once.
+ */
+ private static ExprNodeDesc getEvaluatorArgument(SupportedFunctionType
functionType,
+ List<ExprNodeDesc> exprNodeDescList, int argumentIndex) {
+ final ExprNodeDesc exprNodeDesc = exprNodeDescList.get(argumentIndex);
+ if (argumentIndex == 2
+ && (functionType == SupportedFunctionType.LAG || functionType ==
SupportedFunctionType.LEAD)
+ && exprNodeDesc instanceof ExprNodeConstantDesc constantDesc
+ &&
!constantDesc.getTypeInfo().equals(exprNodeDescList.get(0).getTypeInfo())) {
+ return convertConstant(constantDesc,
exprNodeDescList.get(0).getTypeInfo());
Review Comment:
similar to https://github.com/apache/hive/pull/6997/changes#r4798136652
if we have similar conversion as we have in row mode then an incompatibility
between column type and expression type if not can throw earlier exception here
only
and it will be consistent with row mode as well
##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorAbstractLeadLag.java:
##########
@@ -50,77 +62,186 @@ public VectorPTFEvaluatorAbstractLeadLag(WindowFrameDef
windowFrameDef,
}
private void initDefaultValue(VectorExpression defaultValueExpression) {
- if (defaultValueExpression == null) {
- defaultValue = null;
+ if (defaultValueExpression == null || defaultValueExpression instanceof
ConstantVectorExpression) {
+ initConstantDefaultValue((ConstantVectorExpression)
defaultValueExpression);
return;
- } else if (defaultValueExpression instanceof ConstantVectorExpression) {
- // FIXME: always getLongValue()? check if other type is given in 3rd
argument
- long longValue = ((ConstantVectorExpression)
defaultValueExpression).getLongValue();
- switch (type) {
- case LONG:
- defaultValue = longValue;
- break;
- case DOUBLE:
- defaultValue = Double.valueOf(longValue);
- break;
- case DECIMAL:
- defaultValue = new HiveDecimalWritable(longValue);
- break;
- default:
- throw new RuntimeException("Unexpected column vector type " + type + "
for lag function");
- }
- } else if (defaultValueExpression instanceof IdentityExpression) {
- defaultValueColumn = ((IdentityExpression)
defaultValueExpression).getOutputColumnNum();
- } else {
+ }
+
+ // A column, or an expression that is evaluated together with the input
expression.
+ if (!(defaultValueExpression instanceof IdentityExpression)) {
+ defaultValueVecExpr = defaultValueExpression;
+ }
+ defaultValueColumn = defaultValueExpression.getOutputColumnNum();
+ }
+
+ private void initConstantDefaultValue(ConstantVectorExpression constant) {
+ // A single repeating row holding the default value.
+ defaultValueVector = createColumnVector(type, 1,
HiveDecimal.SYSTEM_DEFAULT_PRECISION,
Review Comment:
please cover the scenarios in which value doesn't fit into the precision or
scale or both
and does the results vary for row mode and vectorized mode to keep
consistency
--
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]