-
Notifications
You must be signed in to change notification settings - Fork 25.4k
Add optimized path for intermediate values aggregator #131390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
efd20d0
747d56e
a25e26e
30b0455
87bc050
90f892c
8df8fb8
faf0252
5c4d0a8
6845928
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 131390 | ||
summary: Add optimized path for intermediate values aggregator | ||
area: ES|QL | ||
type: enhancement | ||
issues: [] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,15 +28,7 @@ static GroupingAggregatorFunction.AddInput wrapAddInput( | |
if (valuesOrdinal == null) { | ||
return delegate; | ||
} | ||
BytesRefVector dict = valuesOrdinal.getDictionaryVector(); | ||
final IntVector hashIds; | ||
BytesRef spare = new BytesRef(); | ||
try (var hashIdsBuilder = values.blockFactory().newIntVectorFixedBuilder(dict.getPositionCount())) { | ||
for (int p = 0; p < dict.getPositionCount(); p++) { | ||
hashIdsBuilder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroup(state.bytes.add(dict.getBytesRef(p, spare))))); | ||
} | ||
hashIds = hashIdsBuilder.build(); | ||
} | ||
final IntVector hashIds = hashDict(state, valuesOrdinal.getDictionaryVector()); | ||
IntBlock ordinalIds = valuesOrdinal.getOrdinalsBlock(); | ||
return new GroupingAggregatorFunction.AddInput() { | ||
@Override | ||
|
@@ -85,17 +77,7 @@ public void add(int positionOffset, IntBigArrayBlock groupIds) { | |
|
||
@Override | ||
public void add(int positionOffset, IntVector groupIds) { | ||
for (int groupPosition = 0; groupPosition < groupIds.getPositionCount(); groupPosition++) { | ||
int groupId = groupIds.getInt(groupPosition); | ||
if (ordinalIds.isNull(groupPosition + positionOffset)) { | ||
continue; | ||
} | ||
int valuesStart = ordinalIds.getFirstValueIndex(groupPosition + positionOffset); | ||
int valuesEnd = valuesStart + ordinalIds.getValueCount(groupPosition + positionOffset); | ||
for (int v = valuesStart; v < valuesEnd; v++) { | ||
state.addValueOrdinal(groupId, hashIds.getInt(ordinalIds.getInt(v))); | ||
} | ||
} | ||
addOrdinalInputBlock(state, positionOffset, groupIds, ordinalIds, hashIds); | ||
} | ||
|
||
@Override | ||
|
@@ -114,15 +96,7 @@ static GroupingAggregatorFunction.AddInput wrapAddInput( | |
if (valuesOrdinal == null) { | ||
return delegate; | ||
} | ||
BytesRefVector dict = valuesOrdinal.getDictionaryVector(); | ||
final IntVector hashIds; | ||
BytesRef spare = new BytesRef(); | ||
try (var hashIdsBuilder = values.blockFactory().newIntVectorFixedBuilder(dict.getPositionCount())) { | ||
for (int p = 0; p < dict.getPositionCount(); p++) { | ||
hashIdsBuilder.appendInt(Math.toIntExact(BlockHash.hashOrdToGroup(state.bytes.add(dict.getBytesRef(p, spare))))); | ||
} | ||
hashIds = hashIdsBuilder.build(); | ||
} | ||
final IntVector hashIds = hashDict(state, valuesOrdinal.getDictionaryVector()); | ||
var ordinalIds = valuesOrdinal.getOrdinalsVector(); | ||
return new GroupingAggregatorFunction.AddInput() { | ||
@Override | ||
|
@@ -157,10 +131,7 @@ public void add(int positionOffset, IntBigArrayBlock groupIds) { | |
|
||
@Override | ||
public void add(int positionOffset, IntVector groupIds) { | ||
for (int groupPosition = 0; groupPosition < groupIds.getPositionCount(); groupPosition++) { | ||
int groupId = groupIds.getInt(groupPosition); | ||
state.addValueOrdinal(groupId, hashIds.getInt(ordinalIds.getInt(groupPosition + positionOffset))); | ||
} | ||
addOrdinalInputVector(state, positionOffset, groupIds, ordinalIds, hashIds); | ||
} | ||
|
||
@Override | ||
|
@@ -169,4 +140,86 @@ public void close() { | |
} | ||
}; | ||
} | ||
|
||
static IntVector hashDict(ValuesBytesRefAggregator.GroupingState state, BytesRefVector dict) { | ||
BytesRef scratch = new BytesRef(); | ||
try (var hashIdsBuilder = dict.blockFactory().newIntVectorFixedBuilder(dict.getPositionCount())) { | ||
for (int p = 0; p < dict.getPositionCount(); p++) { | ||
final long hashId = BlockHash.hashOrdToGroup(state.bytes.add(dict.getBytesRef(p, scratch))); | ||
hashIdsBuilder.appendInt(Math.toIntExact(hashId)); | ||
} | ||
return hashIdsBuilder.build(); | ||
} | ||
} | ||
|
||
static void addOrdinalInputBlock( | ||
ValuesBytesRefAggregator.GroupingState state, | ||
int positionOffset, | ||
IntVector groupIds, | ||
IntBlock ordinalIds, | ||
IntVector hashIds | ||
) { | ||
for (int p = 0; p < groupIds.getPositionCount(); p++) { | ||
final int valuePosition = p + positionOffset; | ||
final int groupId = groupIds.getInt(valuePosition); | ||
final int start = ordinalIds.getFirstValueIndex(valuePosition); | ||
final int end = start + ordinalIds.getValueCount(valuePosition); | ||
for (int i = start; i < end; i++) { | ||
int ord = ordinalIds.getInt(i); | ||
state.addValueOrdinal(groupId, hashIds.getInt(ord)); | ||
} | ||
} | ||
} | ||
|
||
static void addOrdinalInputVector( | ||
ValuesBytesRefAggregator.GroupingState state, | ||
int positionOffset, | ||
IntVector groupIds, | ||
IntVector ordinalIds, | ||
IntVector hashIds | ||
) { | ||
for (int p = 0; p < groupIds.getPositionCount(); p++) { | ||
int groupId = groupIds.getInt(p); | ||
int ord = ordinalIds.getInt(p + positionOffset); | ||
state.addValueOrdinal(groupId, hashIds.getInt(ord)); | ||
} | ||
} | ||
|
||
static void combineIntermediateInputValues( | ||
ValuesBytesRefAggregator.GroupingState state, | ||
int positionOffset, | ||
IntVector groupIds, | ||
BytesRefBlock values | ||
) { | ||
BytesRefVector dict = null; | ||
IntBlock ordinals = null; | ||
{ | ||
final OrdinalBytesRefBlock asOrdinals = values.asOrdinals(); | ||
if (asOrdinals != null) { | ||
dict = asOrdinals.getDictionaryVector(); | ||
ordinals = asOrdinals.getOrdinalsBlock(); | ||
} | ||
} | ||
if (dict != null && dict.getPositionCount() < groupIds.getPositionCount()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this use |
||
try (var hashIds = hashDict(state, dict)) { | ||
IntVector ordinalsVector = ordinals.asVector(); | ||
if (ordinalsVector != null) { | ||
addOrdinalInputVector(state, positionOffset, groupIds, ordinalsVector, hashIds); | ||
} else { | ||
addOrdinalInputBlock(state, positionOffset, groupIds, ordinals, hashIds); | ||
} | ||
} | ||
} else { | ||
final BytesRef scratch = new BytesRef(); | ||
for (int p = 0; p < groupIds.getPositionCount(); p++) { | ||
final int valuePosition = p + positionOffset; | ||
final int groupId = groupIds.getInt(valuePosition); | ||
final int start = values.getFirstValueIndex(valuePosition); | ||
final int end = start + values.getValueCount(valuePosition); | ||
for (int i = start; i < end; i++) { | ||
state.addValue(groupId, values.getBytesRef(i, scratch)); | ||
} | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.