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


##########
ql/src/java/org/apache/hadoop/hive/ql/plan/VectorPTFDesc.java:
##########
@@ -222,6 +225,11 @@ public static VectorPTFEvaluatorBase 
getEvaluator(SupportedFunctionType function
     case CUME_DIST:
       evaluator = new VectorPTFEvaluatorCumeDist(windowFrameDef, 
outputColumnNum);
       break;
+    case NTILE:
+      // ntile(n) receives the number of buckets as a constant integer 
expression

Review Comment:
   please drop this comment , it requires input is obvious via use of 
`inputVectorExpression`



##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorNTile.java:
##########
@@ -0,0 +1,167 @@
+/*
+ * 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;
+  private int rowsLeftInPartition;

Review Comment:
   this can be dropped, the case it is checking 
https://github.com/apache/hive/pull/6861/changes#diff-8672208dc0cc7169c3fd2ece5a2319b7a6b470b13ebc5a54e3c539f27b1fb1c1R102
   can be checked via 
   `bucketSize<=0`
   as in other cases it can't exceed rows left in partition
   and can be returned early when` bucketSize` is evaluated  rather than in 
batch evaluation



##########
ql/src/test/org/apache/hadoop/hive/ql/exec/vector/ptf/TestVectorPTFEvaluatorNTile.java:
##########
@@ -0,0 +1,385 @@
+/*

Review Comment:
   the evaluation can be tested via q test this isn't needed IMHO



##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorNTile.java:
##########
@@ -0,0 +1,167 @@
+/*
+ * 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;
+  private int rowsLeftInPartition;
+
+  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;
+    resetEvaluator();
+  }
+
+  @Override
+  public boolean isGroupAggregatedStreamingEvaluator() {
+    return true;
+  }
+
+  @Override
+  public void addStreamingGroupResults(List<Integer> groupRowCounts) throws 
HiveException {
+    if (partitionSize < 0) {
+      throw new HiveException("Partition size must be set before computing 
ntile");
+    }
+    // Peer groups are irrelevant: buckets depend only on row position and 
partition size.
+    bucketSize = partitionSize / numBuckets;
+    largerBucketCount = partitionSize % numBuckets;
+    rowsLeftInPartition = partitionSize;
+    currentBucket = 1;
+    rowsLeftInBucket = rowsInBucket(currentBucket);
+  }
+
+  @Override
+  public void evaluateGroupBatch(VectorizedRowBatch batch) throws 
HiveException {
+    final int size = batch.size;
+    if (size > rowsLeftInPartition) {
+      throw new HiveException("ntile received " + size + " rows but only " + 
rowsLeftInPartition
+          + " remain in the partition (partitionSize=" + partitionSize + ")");
+    }
+    if (size == 0) {

Review Comment:
   this defensive check can also be dropped 



##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFOperator.java:
##########
@@ -435,10 +435,11 @@ private void initBufferedColumns() {
      * 1. order columns
      * 2. the non-key input columns
      * 3. any streaming columns that will already have their output values
-     * 4. scratch columns (as they're used by input expressions of evaluators)
+     * 4. scratch columns holding the results of evaluator input expressions
      */
+    final int[] bufferedScratchIndices = getEvaluatorInputScratchIndices();

Review Comment:
   why we need this?



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