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


##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorNTile.java:
##########
@@ -86,18 +93,56 @@ 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;
+  }
+
+  /**
+   * Mirrors {@code GenericUDAFNTile}, which does not validate the number of 
buckets: 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 > 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 < -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 divides by zero, or for any other negative count loops until 
it runs out of
+      // memory.
+      throw new HiveException("NTILE requires a positive number of buckets, 
got: " + numBuckets);

Review Comment:
   Agreed. The number of buckets is now checked in one place, setUpBuckets, 
with a single message. readNumBuckets returns null for NULL instead of 
throwing. The constructor no longer re-checks a constant: the Vectorizer 
already sends non-positive constants to row mode, and any value that reached 
the evaluator would hit the same check. It can't move earlier, because a 
non-constant value is only known once the partition's first row is replayed. 
setUpBuckets runs at exactly that point, once per partition. The error text is 
unchanged, so the golden files didn't change.



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