NtileAnalytic

A NtileAnalytic is an AnalyticFunction that determines the bucket number over any values, returning the bucket number as a Long. Values range from 1 through n (inclusive). If p is the number of rows in the partition, and n is the number of buckets, then there are p/n rows in each bucket. If p is not divisible by n, then the first p % n buckets have p/n + 1 rows in them, e.g. "Ntile(4)" with 6 rows yields 1 1 2 2 3 4.

Usage

Create and use a NtileAnalytic, with one of the following methods:

  • AnalyticAggregator ana = new AnalyticAggregator.Builder()
        .setAnalyticFunction(new NtileAnalytic(4))
        .setPartition(new PartitionClause(Arrays.asList("category1")))
        .setOrderBy(new OrderByClause(Arrays.asList(new OrderByElement("value2", OrderByElement.SortDir.DESC))))
        .build();
    
  • AnalyticAggregator agg = AnalyticAggregator.getAnalytic("NtileAnalytic(4) partitionBy(category1) orderBy(value2 DESC)");
    

The only "property" that NtileAnalytic expects is the number of buckets.

The values range from 1 through the number of buckets specified, inclusive. The number of rows with the same bucket number value will vary by at most 1.

NtileAnalytic is a DependentAnalyticFunction that computes the bucket number based on Count(*) rows(, 0) and Count(*) range(), and it does not take a user-given window clause.