Skip to content

Commit afe3608

Browse files
committed
fine-grained
1 parent af6a820 commit afe3608

File tree

4 files changed

+64
-38
lines changed

4 files changed

+64
-38
lines changed

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/aggregation/blockhash/BytesRefBlockHash.java

Lines changed: 11 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/ValuesBytesRefAggregators.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,18 @@ static void addOrdinalInputVector(
180180
) {
181181
if (groupIds.isConstant() && hashIds.isConstant()) {
182182
state.addValueOrdinal(groupIds.getInt(0), hashIds.getInt(0));
183-
} else {
184-
for (int p = 0; p < groupIds.getPositionCount(); p++) {
185-
int groupId = groupIds.getInt(p);
186-
int ord = ordinalIds.getInt(p + positionOffset);
187-
state.addValueOrdinal(groupId, hashIds.getInt(ord));
183+
return;
184+
}
185+
int lastGroup = groupIds.getInt(0);
186+
int lastOrd = ordinalIds.getInt(positionOffset);
187+
state.addValueOrdinal(lastGroup, hashIds.getInt(lastOrd));
188+
for (int p = 1; p < groupIds.getPositionCount(); p++) {
189+
final int nextGroup = groupIds.getInt(p);
190+
final int nextOrd = ordinalIds.getInt(p + positionOffset);
191+
if (nextGroup != lastGroup || nextOrd != lastOrd) {
192+
lastGroup = nextGroup;
193+
lastOrd = nextOrd;
194+
state.addValueOrdinal(lastGroup, hashIds.getInt(lastOrd));
188195
}
189196
}
190197
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/blockhash/BytesRefLongBlockHash.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,29 +98,42 @@ public void add(IntBlock bytesHashes, LongBlock longsBlock, GroupingAggregatorFu
9898
}
9999

100100
public IntVector add(IntVector bytesHashes, LongVector longsVector) {
101+
int positions = bytesHashes.getPositionCount();
102+
final int[] ords = new int[positions];
103+
int lastByte = bytesHashes.getInt(0);
104+
long lastLong = longsVector.getLong(0);
105+
ords[0] = Math.toIntExact(hashOrdToGroup(finalHash.add(lastByte, lastLong)));
106+
boolean constant = true;
101107
if (bytesHashes.isConstant()) {
102-
boolean singleValue = true;
103-
// TODO: Should we also support num_runs? Should we add a constant flag to Vector and perform this check in EVAL instead?
104-
if (longsVector.isConstant() == false) {
105-
long firstValue = longsVector.getLong(0);
106-
for (int i = 1; i < longsVector.getPositionCount(); i++) {
107-
if (firstValue != longsVector.getLong(i)) {
108-
singleValue = false;
109-
break;
110-
}
108+
for (int i = 1; i < positions; i++) {
109+
final long nextLong = longsVector.getLong(i);
110+
if (nextLong == lastLong) {
111+
ords[i] = ords[i - 1];
112+
} else {
113+
ords[i] = Math.toIntExact(hashOrdToGroup(finalHash.add(lastByte, nextLong)));
114+
lastLong = nextLong;
115+
constant = false;
111116
}
112117
}
113-
if (singleValue) {
114-
int singleOrd = Math.toIntExact(hashOrdToGroup(finalHash.add(bytesHashes.getInt(0), longsVector.getLong(0))));
115-
return blockFactory.newConstantIntVector(singleOrd, longsVector.getPositionCount());
118+
} else {
119+
for (int i = 1; i < positions; i++) {
120+
final int nextByte = bytesHashes.getInt(i);
121+
final long nextLong = longsVector.getLong(i);
122+
if (nextByte == lastByte && nextLong == lastLong) {
123+
ords[i] = ords[i - 1];
124+
} else {
125+
ords[i] = Math.toIntExact(hashOrdToGroup(finalHash.add(nextByte, nextLong)));
126+
lastByte = nextByte;
127+
lastLong = nextLong;
128+
constant = false;
129+
}
116130
}
117131
}
118-
int positions = bytesHashes.getPositionCount();
119-
final int[] ords = new int[positions];
120-
for (int i = 0; i < positions; i++) {
121-
ords[i] = Math.toIntExact(hashOrdToGroup(finalHash.add(bytesHashes.getInt(i), longsVector.getLong(i))));
132+
if (constant) {
133+
return blockFactory.newConstantIntVector(ords[0], positions);
134+
} else {
135+
return blockFactory.newIntArrayVector(ords, positions);
122136
}
123-
return blockFactory.newIntArrayVector(ords, positions);
124137
}
125138

126139
@Override

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/blockhash/X-BlockHash.java.st

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,18 @@ $endif$
173173
$if(BytesRef)$
174174
private IntVector addOrdinalsVector(OrdinalBytesRefVector inputBlock) {
175175
IntVector inputOrds = inputBlock.getOrdinalsVector();
176-
try (
177-
var builder = blockFactory.newIntVectorBuilder(inputOrds.getPositionCount());
178-
var hashOrds = add(inputBlock.getDictionaryVector())
179-
) {
180-
for (int p = 0; p < inputOrds.getPositionCount(); p++) {
181-
int ord = hashOrds.getInt(inputOrds.getInt(p));
182-
builder.appendInt(ord);
176+
try (var hashOrds = add(inputBlock.getDictionaryVector())) {
177+
if (inputOrds.isConstant()) {
178+
int ord = hashOrds.getInt(inputOrds.getInt(0));
179+
return blockFactory.newConstantIntVector(ord, inputOrds.getPositionCount());
180+
}
181+
try (var builder = blockFactory.newIntVectorBuilder(inputOrds.getPositionCount())) {
182+
for (int p = 0; p < inputOrds.getPositionCount(); p++) {
183+
int ord = hashOrds.getInt(inputOrds.getInt(p));
184+
builder.appendInt(ord);
185+
}
186+
return builder.build();
183187
}
184-
return builder.build();
185188
}
186189
}
187190

0 commit comments

Comments
 (0)