Class BufferArrayGrouper
- java.lang.Object
-
- org.apache.druid.query.groupby.epinephelinae.BufferArrayGrouper
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Grouper<IntKey>
,IntGrouper
,VectorGrouper
public class BufferArrayGrouper extends Object implements VectorGrouper, IntGrouper
A buffer grouper for array-based aggregation. This grouper stores aggregated values in the buffer using the grouping key as the index.The buffer is divided into 2 separate regions, i.e., used flag buffer and value buffer. The used flag buffer is a bit set to represent which keys are valid. If a bit of an index is set, that key is valid. Finally, the value buffer is used to store aggregated values. The first index is reserved for
GroupByColumnSelectorStrategy.GROUP_BY_MISSING_VALUE
.This grouper is available only when the grouping key is a single indexed dimension of a known cardinality because it directly uses the dimension value as the index for array access. Since the cardinality for the grouping key across different segments cannot be currently retrieved, this grouper can be used only when performing per-segment query execution.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.druid.query.groupby.epinephelinae.Grouper
Grouper.BufferComparator, Grouper.Entry<T>, Grouper.KeySerde<T>, Grouper.KeySerdeFactory<T>
-
Nested classes/interfaces inherited from interface org.apache.druid.query.groupby.epinephelinae.IntGrouper
IntGrouper.IntGrouperHashFunction
-
-
Constructor Summary
Constructors Constructor Description BufferArrayGrouper(com.google.common.base.Supplier<ByteBuffer> bufferSupplier, AggregatorAdapters aggregators, int cardinality)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AggregateResult
aggregateKeyHash(int dimIndex)
AggregateResult
aggregateVector(org.apache.datasketches.memory.Memory keySpace, int startRow, int endRow)
Aggregate the current vector of rows from "startRow" to "endRow" using the provided keys.void
close()
Close the grouper and release associated resources.IntGrouper.IntGrouperHashFunction
hashFunction()
void
init()
Initialize the grouper.void
initVectorized(int maxVectorSize)
Initialize the grouper.boolean
isInitialized()
Check this grouper is initialized or not.CloseableIterator<Grouper.Entry<MemoryPointer>>
iterator()
Iterate through entry buckets.CloseableIterator<Grouper.Entry<IntKey>>
iterator(boolean sorted)
Iterate through entries.static long
requiredBufferCapacity(int cardinality, AggregatorFactory[] aggregatorFactories)
Computes required buffer capacity for a grouping key of the given cardinaltiy and aggregatorFactories.void
reset()
Reset the grouper to its initial state.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.druid.query.groupby.epinephelinae.IntGrouper
aggregate, aggregate, aggregate
-
-
-
-
Constructor Detail
-
BufferArrayGrouper
public BufferArrayGrouper(com.google.common.base.Supplier<ByteBuffer> bufferSupplier, AggregatorAdapters aggregators, int cardinality)
-
-
Method Detail
-
requiredBufferCapacity
public static long requiredBufferCapacity(int cardinality, AggregatorFactory[] aggregatorFactories)
Computes required buffer capacity for a grouping key of the given cardinaltiy and aggregatorFactories. This method assumes that the given cardinality doesn't count nulls. Returns -1 if cardinality + 1 (for null) > Integer.MAX_VALUE. Returns computed required buffer capacity otherwise.
-
init
public void init()
Description copied from interface:Grouper
Initialize the grouper. This method needs to be called before callingGrouper.aggregate(Object)
andGrouper.aggregate(Object, int)
.
-
initVectorized
public void initVectorized(int maxVectorSize)
Description copied from interface:VectorGrouper
Initialize the grouper. This method needs to be called before callingVectorGrouper.aggregateVector(org.apache.datasketches.memory.Memory, int, int)
.- Specified by:
initVectorized
in interfaceVectorGrouper
-
isInitialized
public boolean isInitialized()
Description copied from interface:Grouper
Check this grouper is initialized or not.- Specified by:
isInitialized
in interfaceGrouper<IntKey>
- Returns:
- true if the grouper is already initialized, otherwise false.
-
aggregateKeyHash
public AggregateResult aggregateKeyHash(int dimIndex)
- Specified by:
aggregateKeyHash
in interfaceIntGrouper
-
aggregateVector
public AggregateResult aggregateVector(org.apache.datasketches.memory.Memory keySpace, int startRow, int endRow)
Description copied from interface:VectorGrouper
Aggregate the current vector of rows from "startRow" to "endRow" using the provided keys.- Specified by:
aggregateVector
in interfaceVectorGrouper
- Parameters:
keySpace
- array holding keys, chunked into ints. First (endRow - startRow) keys must be valid.startRow
- row to start at (inclusive).endRow
- row to end at (exclusive).- Returns:
- result that indicates how many keys were aggregated (may be partial due to resource limits)
-
reset
public void reset()
Description copied from interface:VectorGrouper
Reset the grouper to its initial state.- Specified by:
reset
in interfaceGrouper<IntKey>
- Specified by:
reset
in interfaceVectorGrouper
-
hashFunction
public IntGrouper.IntGrouperHashFunction hashFunction()
- Specified by:
hashFunction
in interfaceGrouper<IntKey>
- Specified by:
hashFunction
in interfaceIntGrouper
-
close
public void close()
Description copied from interface:VectorGrouper
Close the grouper and release associated resources.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceGrouper<IntKey>
- Specified by:
close
in interfaceVectorGrouper
-
iterator
public CloseableIterator<Grouper.Entry<MemoryPointer>> iterator()
Description copied from interface:VectorGrouper
Iterate through entry buckets. Each bucket's key is aMemoryPointer
object in native byte order.After you are done with the iterator returned by this method, you should either call
VectorGrouper.close()
(if you are done with the VectorGrouper) orVectorGrouper.reset()
(if you want to reuse it).Callers must process and discard the returned
Grouper.Entry
s immediately, because objects may be reused.- Specified by:
iterator
in interfaceVectorGrouper
- Returns:
- entry iterator
-
iterator
public CloseableIterator<Grouper.Entry<IntKey>> iterator(boolean sorted)
Description copied from interface:Grouper
Iterate through entries.Some implementations allow writes even after this method is called. After you are done with the iterator returned by this method, you should either call
Grouper.close()
(if you are done with the Grouper) orGrouper.reset()
(if you want to reuse it). Some implementations allow callingGrouper.iterator(boolean)
again if you want another iterator. But, this method must not be called by multiple threads concurrently.If "sorted" is true then the iterator will return sorted results. It will use KeyType's natural ordering on deserialized objects, and will use the
Grouper.KeySerde.bufferComparator()
on serialized objects. Woe be unto you if these comparators are not equivalent.Callers must process and discard the returned
Grouper.Entry
s immediately because some implementations can reuse the key objects.
-
-