ggangadharan commented on code in PR #6946:
URL: https://github.com/apache/hive/pull/6946#discussion_r4678262698
##########
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:
Confirmed. A vectorized integer-family cast to int is an identity, so the
vector kept 4294967297 while row mode narrows it to 1. The evaluator now holds
the bucket count as an int, as GenericUDAFNTile does, and readNumBuckets
narrows the value, once per partition. I added a bigint-to-int regression with
4294967297, 4294967299 and -4294967294; it now matches row mode.
--
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]