|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.inference.bulk; |
| 9 | + |
| 10 | +import org.elasticsearch.compute.data.Block; |
| 11 | +import org.elasticsearch.compute.data.DoubleBlock; |
| 12 | +import org.elasticsearch.compute.data.Page; |
| 13 | +import org.elasticsearch.compute.test.ComputeTestCase; |
| 14 | +import org.elasticsearch.compute.test.RandomBlock; |
| 15 | +import org.elasticsearch.core.Releasables; |
| 16 | +import org.elasticsearch.logging.LogManager; |
| 17 | +import org.elasticsearch.xpack.core.inference.action.InferenceAction; |
| 18 | +import org.elasticsearch.xpack.core.inference.results.RankedDocsResults; |
| 19 | +import org.elasticsearch.xpack.esql.inference.rerank.RerankOperatorOutputBuilder; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import static org.hamcrest.Matchers.equalTo; |
| 25 | + |
| 26 | +public class RerankOperatorOutputBuilderTests extends ComputeTestCase { |
| 27 | + |
| 28 | + public void testBuildSmallOutput() { |
| 29 | + assertBuildOutput(between(1, 100)); |
| 30 | + } |
| 31 | + |
| 32 | + public void testBuildLargeOutput() { |
| 33 | + assertBuildOutput(between(10_000, 100_000)); |
| 34 | + } |
| 35 | + |
| 36 | + private void assertBuildOutput(int size) { |
| 37 | + final Page inputPage = randomInputPage(size, between(1, 20)); |
| 38 | + final int scoreChannel = randomIntBetween(0, inputPage.getBlockCount()); |
| 39 | + try ( |
| 40 | + RerankOperatorOutputBuilder outputBuilder = new RerankOperatorOutputBuilder( |
| 41 | + blockFactory().newDoubleBlockBuilder(size), |
| 42 | + inputPage, |
| 43 | + scoreChannel |
| 44 | + ) |
| 45 | + ) { |
| 46 | + int batchSize = randomIntBetween(1, size); |
| 47 | + for (int currentPos = 0; currentPos < inputPage.getPositionCount();) { |
| 48 | + List<RankedDocsResults.RankedDoc> rankedDocs = new ArrayList<>(); |
| 49 | + for (int rankedDocIndex = 0; rankedDocIndex < batchSize && currentPos < inputPage.getPositionCount(); rankedDocIndex++) { |
| 50 | + rankedDocs.add(new RankedDocsResults.RankedDoc(rankedDocIndex, relevanceScore(currentPos), randomIdentifier())); |
| 51 | + currentPos++; |
| 52 | + } |
| 53 | + |
| 54 | + outputBuilder.addInferenceResponse(new InferenceAction.Response(new RankedDocsResults(rankedDocs))); |
| 55 | + } |
| 56 | + |
| 57 | + final Page outputPage = outputBuilder.buildOutput(); |
| 58 | + try { |
| 59 | + assertThat(outputPage.getPositionCount(), equalTo(inputPage.getPositionCount())); |
| 60 | + LogManager.getLogger(RerankOperatorOutputBuilderTests.class) |
| 61 | + .info( |
| 62 | + "{} , {}, {}, {}", |
| 63 | + scoreChannel, |
| 64 | + inputPage.getBlockCount(), |
| 65 | + outputPage.getBlockCount(), |
| 66 | + Math.max(scoreChannel + 1, inputPage.getBlockCount()) |
| 67 | + ); |
| 68 | + assertThat(outputPage.getBlockCount(), equalTo(Integer.max(scoreChannel + 1, inputPage.getBlockCount()))); |
| 69 | + assertOutputContent(outputPage.getBlock(scoreChannel)); |
| 70 | + } finally { |
| 71 | + outputPage.releaseBlocks(); |
| 72 | + } |
| 73 | + |
| 74 | + } finally { |
| 75 | + inputPage.releaseBlocks(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private float relevanceScore(int position) { |
| 80 | + return (float) 1 / (1 + position); |
| 81 | + } |
| 82 | + |
| 83 | + private void assertOutputContent(DoubleBlock block) { |
| 84 | + for (int currentPos = 0; currentPos < block.getPositionCount(); currentPos++) { |
| 85 | + assertThat(block.isNull(currentPos), equalTo(false)); |
| 86 | + assertThat(block.getValueCount(currentPos), equalTo(1)); |
| 87 | + assertThat(block.getDouble(block.getFirstValueIndex(currentPos)), equalTo((double) relevanceScore(currentPos))); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private Page randomInputPage(int positionCount, int columnCount) { |
| 92 | + final Block[] blocks = new Block[columnCount]; |
| 93 | + try { |
| 94 | + for (int i = 0; i < columnCount; i++) { |
| 95 | + blocks[i] = RandomBlock.randomBlock( |
| 96 | + blockFactory(), |
| 97 | + RandomBlock.randomElementType(), |
| 98 | + positionCount, |
| 99 | + randomBoolean(), |
| 100 | + 0, |
| 101 | + 0, |
| 102 | + randomInt(10), |
| 103 | + randomInt(10) |
| 104 | + ).block(); |
| 105 | + } |
| 106 | + |
| 107 | + return new Page(blocks); |
| 108 | + } catch (Exception e) { |
| 109 | + Releasables.close(blocks); |
| 110 | + throw (e); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments