Skip to content

Commit 6ee83d1

Browse files
authored
Fix block loader for sorted ordinals (elastic#137231)
I broke the block loader for sorted ordinals in elastic#137076, and our tests didn’t catch it because we tested with the TestBuilder instead of the real builder. Relates elastic#137076.
1 parent 83f6f0b commit 6ee83d1

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/read/SingletonOrdinalsBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public BlockLoader.SingletonOrdinalsBuilder appendOrds(int ord, int length) {
7272
Arrays.fill(ords, count, count + length, ord);
7373
this.minOrd = Math.min(this.minOrd, ord);
7474
this.maxOrd = Math.max(this.maxOrd, ord);
75+
this.count += length;
7576
return this;
7677
}
7778

@@ -256,6 +257,10 @@ public long estimatedBytes() {
256257

257258
@Override
258259
public BytesRefBlock build() {
260+
if (count != ords.length) {
261+
assert false : "expected " + ords.length + " values but got " + count;
262+
throw new IllegalStateException("expected " + ords.length + " values but got " + count);
263+
}
259264
var constantBlock = tryBuildConstantBlock();
260265
if (constantBlock != null) {
261266
return constantBlock;

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/lucene/read/SingletonOrdinalsBuilderTests.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ public void testEmitOrdinalForHighCardinality() throws IOException {
191191
var b2 = new SingletonOrdinalsBuilder(factory, ctx.reader().getSortedDocValues("f"), batchSize, randomBoolean())
192192
) {
193193
for (int i = 0; i < batchSize; i++) {
194-
b1.appendOrd(ord);
195-
b2.appendOrd(ord);
194+
appendOrd(b1, ord);
195+
appendOrd(b2, ord);
196196
}
197197
try (BytesRefBlock block1 = b1.build(); BytesRefBlock block2 = b2.buildRegularBlock()) {
198198
assertThat(block1, equalTo(block2));
@@ -204,6 +204,20 @@ public void testEmitOrdinalForHighCardinality() throws IOException {
204204
}
205205
}
206206

207+
private void appendOrd(SingletonOrdinalsBuilder builder, int ord) {
208+
if (randomBoolean()) {
209+
builder.appendOrd(ord);
210+
} else if (randomBoolean()) {
211+
int prefix = between(0, 2);
212+
int suffix = between(0, 2);
213+
int[] ords = new int[prefix + 1 + suffix];
214+
ords[prefix] = ord;
215+
builder.appendOrds(ords, prefix, 1, ord, ord);
216+
} else {
217+
builder.appendOrds(ord, 1);
218+
}
219+
}
220+
207221
public void testEmitConstantBlocks() throws IOException {
208222
var config = new IndexWriterConfig();
209223
config.setIndexSort(new Sort(new SortField("f", SortField.Type.STRING)));

0 commit comments

Comments
 (0)