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


##########
ql/src/java/org/apache/hadoop/hive/ql/exec/vector/ptf/VectorPTFEvaluatorNTile.java:
##########
@@ -86,18 +94,60 @@ 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(Integer 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.
+      buckets = 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.
+   *
+   * <p>The value is narrowed to an int as in row mode: a vectorized cast to 
int from a wider

Review Comment:
   nit: the identity cast is HIVE-19399; worth naming it here, so the (int) can 
go once that's fixed.  



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