Copilot commented on code in PR #6946:
URL: https://github.com/apache/hive/pull/6946#discussion_r4677278673


##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorNTile.java:
##########
@@ -44,38 +46,44 @@
  * 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.
+ * <p>The number of buckets is either a positive integer constant (e.g. {@code 
ntile(4)}) or any
+ * other int expression (e.g. {@code ntile(col)}), which, as in {@code 
GenericUDAFNTile}, is read
+ * from the first row of each partition. The Vectorizer leaves a non-positive 
constant to row mode.
  */
 public class VectorPTFEvaluatorNTile extends VectorPTFEvaluatorBase {
 
-  private final long numBuckets;
+  // Number of buckets of a constant argument, or null when it is read from 
the partition's first
+  // row.
+  private final Long constantNumBuckets;
 
-  // Per-partition bucket layout, set up by addStreamingGroupResults.
+  // Per-partition bucket layout, set up by setUpBuckets once the first batch 
is replayed.
   private int bucketSize;
   private int largerBucketCount;
 
-  // Replay position within the partition.
+  // Replay position within the partition; 0 until the bucket layout is set up.
   private int currentBucket;
   private int rowsLeftInBucket;
 
   public VectorPTFEvaluatorNTile(WindowFrameDef windowFrameDef,
       VectorExpression numBucketsExpression, int outputColumnNum) {
-    super(windowFrameDef, outputColumnNum);
-    if (!(numBucketsExpression instanceof ConstantVectorExpression 
constantExpr)) {
-      throw new RuntimeException(
-          "NTILE expects a constant integer expression for the number of 
buckets, got: "
-              + (numBucketsExpression == null ? "null"
-                  : numBucketsExpression.getClass().getName()));
-    }
-    numBuckets = constantExpr.getLongValue();
-    if (numBuckets <= 0) {
-      throw new RuntimeException(
-          "NTILE requires a positive number of buckets, got: " + numBuckets);
-    }
+    super(windowFrameDef, getInputExpression(numBucketsExpression), 
outputColumnNum);

Review Comment:
   The newly accepted evaluator input expressions are not transient-initialized 
in `VectorPTFOperator.initializeOp`, so `ntile(cast(string_col as int))` 
converts even valid counts such as '3' to NULL and fails. In the evaluator 
initialization loop, call 
`VectorExpression.doTransientInit(evaluator.inputVecExpr, hconf)` to initialize 
each expression and its children before processing batches. Add a string-to-int 
argument to the row/vector regression comparisons.



##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorNTile.java:
##########
@@ -86,18 +94,57 @@ public void addStreamingGroupResults(List<Integer> 
groupRowCounts) throws HiveEx
     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.
-    // With more buckets than rows every row gets its own bucket, which is 
ntile(partitionSize);
-    // capping the bucket count keeps every bucket non-empty (and fits it in 
an int).
-    final int buckets = (int) Math.min(numBuckets, partitionSize);
+    // Peer groups are irrelevant: buckets depend only on row position and 
partition size. The
+    // layout is set up on the first replayed batch, which holds the row a 
non-constant number of
+    // buckets is read from.
+    currentBucket = 0;
+  }
+
+  /**
+   * Validates the number of buckets (null for NULL), once per partition, and 
sets up the bucket
+   * layout. This is the only place it is validated, as a non-constant one is 
known only once the
+   * partition's first row is replayed. Mirrors {@code GenericUDAFNTile}, 
which does not validate
+   * it: wherever row mode produces a result this produces the same one, and 
wherever row mode
+   * fails this fails fast.
+   */
+  private void setUpBuckets(Long numBuckets) throws HiveException {
+    final int buckets;
+    if (numBuckets != null && numBuckets > 0) {
+      // With more buckets than rows every row gets its own bucket, which is 
ntile(partitionSize);
+      // capping the bucket count keeps every bucket non-empty (and fits it in 
an int).
+      buckets = (int) Math.min(numBuckets, partitionSize);
+    } else if (numBuckets != null && numBuckets < -partitionSize) {
+      // Row mode's truncating division yields an empty base bucket size, so 
every row gets its own
+      // bucket, as for a positive count larger than the partition.
+      buckets = partitionSize;
+    } else {
+      // Row mode throws on NULL, divides by zero on 0, and for any other 
negative count loops until
+      // it runs out of memory.
+      throw new HiveException("NTILE requires a positive number of buckets, 
got: "
+          + (numBuckets == null ? "NULL" : numBuckets));
+    }
     bucketSize = partitionSize / buckets;
     largerBucketCount = partitionSize % buckets;
     currentBucket = 1;
     rowsLeftInBucket = rowsInBucket(currentBucket);
   }
 
+  /**
+   * Reads the number of buckets, or null for NULL, from the first row of the 
partition, held by the
+   * first replayed batch. Replayed batches never use a selection vector, so 
that row is at index 0,
+   * which is also where a repeating vector (e.g. a partition key column) 
keeps its value.
+   */
+  private Long readNumBuckets(VectorizedRowBatch batch) {
+    LongColumnVector numBucketsColVector = (LongColumnVector) 
batch.cols[inputColumnNum];
+    return !numBucketsColVector.noNulls && numBucketsColVector.isNull[0]
+        ? null : numBucketsColVector.vector[0];

Review Comment:
   Narrow the non-null value to `int` before using it as the bucket count: 
integer-family vector casts can leave a value outside the SQL INT range in the 
backing `LongColumnVector`. With three rows and a first-row BIGINT value of 
4294967297, `ntile(cast(nb as int))` uses one bucket in row mode but produces 
three buckets here. Cast through `int` before widening to `long`, and add a 
row/vector regression for this case.



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