abstractdog commented on code in PR #6861:
URL: https://github.com/apache/hive/pull/6861#discussion_r4269304308
##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/physical/Vectorizer.java:
##########
@@ -3011,6 +3012,13 @@ private boolean validatePTFOperator(PTFOperator op,
VectorizationContext vContex
throw new RuntimeException("Unexpected window type " +
windowFrameDef.getWindowType());
}
+ // Row mode also accepts a non-constant ntile argument (read from the
first row of the partition).
+ if (supportedFunctionType == SupportedFunctionType.NTILE &&
Review Comment:
please create a follow-up ticket for parity with non-vectorized mode
##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorNTile.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.Arrays;
+import java.util.List;
+
+import org.apache.hadoop.hive.ql.exec.vector.ColumnVector.Type;
+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.ConstantVectorExpression;
+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;
+
+/**
+ * Evaluates {@code ntile(n)} as a <b>group-aggregated streaming</b> evaluator
(see
+ * {@link VectorPTFEvaluatorBase}).
+ *
+ * <p>NTILE divides the rows of a partition into {@code n} buckets and returns
the 1-based bucket
+ * number of the current row. When the partition size is not divisible by
{@code n}, the first
+ * {@code partitionSize % n} buckets each get one extra row, matching {@code
GenericUDAFNTile}.
+ * The result depends only on row position and partition size, never on peer
groups.
+ *
+ * <p>The partition is buffered so {@link #setPartitionSize(int)} is known
before output is
+ * written. Bucket boundaries are then derived on the fly while the batches
are replayed, so the
+ * evaluator keeps O(1) state regardless of partition size, and a batch that
falls entirely within
+ * one bucket is emitted as a repeating vector.
+ *
+ * <p>The number of buckets must be a positive integer constant (e.g. {@code
ntile(4)}); the
+ * Vectorizer leaves any other form to row mode.
+ */
+public class VectorPTFEvaluatorNTile extends VectorPTFEvaluatorBase {
+
+ private final int numBuckets;
+
+ // Per-partition bucket layout, set up by addStreamingGroupResults.
+ private int bucketSize;
+ private int largerBucketCount;
+
+ // Replay position within the partition.
+ private int currentBucket;
+ private int rowsLeftInBucket;
+
+ public VectorPTFEvaluatorNTile(WindowFrameDef windowFrameDef,
+ VectorExpression numBucketsExpression, int outputColumnNum) {
+ super(windowFrameDef, outputColumnNum);
+ if (!(numBucketsExpression instanceof ConstantVectorExpression)) {
+ throw new RuntimeException(
+ "NTILE expects a constant integer expression for the number of
buckets, got: "
+ + (numBucketsExpression == null ? "null"
+ : numBucketsExpression.getClass().getName()));
+ }
+ long value = ((ConstantVectorExpression)
numBucketsExpression).getLongValue();
+ if (value <= 0 || value > Integer.MAX_VALUE) {
+ throw new RuntimeException(
+ "NTILE requires a positive integer number of buckets, got: " +
value);
+ }
+ this.numBuckets = (int) value;
Review Comment:
what about simply using `long numBuckets` field?
##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorNTile.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.Arrays;
+import java.util.List;
+
+import org.apache.hadoop.hive.ql.exec.vector.ColumnVector.Type;
+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.ConstantVectorExpression;
+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;
+
+/**
+ * Evaluates {@code ntile(n)} as a <b>group-aggregated streaming</b> evaluator
(see
+ * {@link VectorPTFEvaluatorBase}).
+ *
+ * <p>NTILE divides the rows of a partition into {@code n} buckets and returns
the 1-based bucket
+ * number of the current row. When the partition size is not divisible by
{@code n}, the first
+ * {@code partitionSize % n} buckets each get one extra row, matching {@code
GenericUDAFNTile}.
+ * The result depends only on row position and partition size, never on peer
groups.
+ *
+ * <p>The partition is buffered so {@link #setPartitionSize(int)} is known
before output is
+ * written. Bucket boundaries are then derived on the fly while the batches
are replayed, so the
+ * evaluator keeps O(1) state regardless of partition size, and a batch that
falls entirely within
+ * one bucket is emitted as a repeating vector.
+ *
+ * <p>The number of buckets must be a positive integer constant (e.g. {@code
ntile(4)}); the
+ * Vectorizer leaves any other form to row mode.
+ */
+public class VectorPTFEvaluatorNTile extends VectorPTFEvaluatorBase {
+
+ private final int numBuckets;
+
+ // Per-partition bucket layout, set up by addStreamingGroupResults.
+ private int bucketSize;
+ private int largerBucketCount;
+
+ // Replay position within the partition.
+ private int currentBucket;
+ private int rowsLeftInBucket;
+
+ public VectorPTFEvaluatorNTile(WindowFrameDef windowFrameDef,
+ VectorExpression numBucketsExpression, int outputColumnNum) {
+ super(windowFrameDef, outputColumnNum);
+ if (!(numBucketsExpression instanceof ConstantVectorExpression)) {
Review Comment:
an extra cast can be removed by:
```
if (!(numBucketsExpression instanceof ConstantVectorExpression
constantExpr)) {
```
##########
ql/src/test/results/clientpositive/llap/vector_windowing.q.out:
##########
@@ -3250,7 +3250,7 @@ STAGE PLANS:
Reduce Vectorization:
enabled: true
enableConditionsMet: hive.vectorized.execution.reduce.enabled
IS true, hive.execution.engine tez IN [tez] IS true
- notVectorizedReason: PTF operator: ntile not in supported
functions [avg, count, cume_dist, dense_rank, first_value, lag, last_value,
lead, max, min, percent_rank, rank, row_number, sum]
+ notVectorizedReason: PTF operator: stddev_pop not in supported
functions [avg, count, cume_dist, dense_rank, first_value, lag, last_value,
lead, max, min, ntile, percent_rank, rank, row_number, sum]
Review Comment:
consider creating follow-up ticket for `stddev_pop` vectorization
##########
ql/src/test/queries/clientpositive/sketches_rewrite_cume_dist.q:
##########
@@ -12,15 +12,11 @@ insert into table sketch_input values
select id,cume_dist() over (order by id) from sketch_input;
--- Vectorized PTF buffered path does not support complex-type passthrough
columns.
-set hive.vectorized.execution.ptf.enabled=false;
Review Comment:
this might also be related to the scratch col related change, isn't it? if
so, let's have same clarity about it in the PR desription
##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFOperator.java:
##########
@@ -474,13 +476,31 @@ private void initBufferedColumns() {
bufferedTypeInfos[bufferedMapIndex] =
outputTypeInfos[streamingEvaluatorNum];
}
- for (int i = 0; i < scratchColumnPositions.length; i++) {
+ for (int i = 0; i < bufferedScratchIndices.length; i++) {
+ final int scratchIndex = bufferedScratchIndices[i];
final int bufferedMapIndex = orderColumnCount + nonKeyInputColumnCount +
streamingEvaluatorCount + i;
- bufferedColumnMap[bufferedMapIndex] = scratchColumnPositions[i];
- bufferedTypeInfos[bufferedMapIndex] =
TypeInfoUtils.getTypeInfoFromTypeString(vOutContext.getScratchColumnTypeNames()[i]);
+ bufferedColumnMap[bufferedMapIndex] =
scratchColumnPositions[scratchIndex];
+ bufferedTypeInfos[bufferedMapIndex] =
+
TypeInfoUtils.getTypeInfoFromTypeString(vOutContext.getScratchColumnTypeNames()[scratchIndex]);
}
}
+ /**
+ * Returns the indices (into scratchColumnPositions) of the scratch columns
written by evaluator
+ * input expressions. Those are evaluated before a batch is buffered, so
their results must be
+ * buffered too. The other scratch columns belong to child operators, which
recompute them after
+ * the batch is forwarded; buffering them is wasted work and fails for
complex types.
+ */
+ private int[] getEvaluatorInputScratchIndices() {
Review Comment:
is the change related to introducing ntile function? I feel it's unrelated,
please clarify it in the PR description
--
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]