Skip to content

Commit 9db5bf0

Browse files
authored
Fix getBytesRef in ConstantBytesRefVector (#136445) (#136457)
We should not return the BytesRef directly from ConstantBytesRefBlock, but instead copy its slice to a scratch buffer. Although current usage does not modify the offset and length, the contract should allow callers to change these fields, as long as the content of the bytes array remains unchanged. The usage pattern below should be fine, but currently it can change ConstantBytesRefBlock.
1 parent 8a4af9a commit 9db5bf0

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/ConstantBytesRefVector.java

Lines changed: 5 additions & 2 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/data/X-ConstantVector.java.st

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ $endif$
4040

4141
@Override
4242
$if(BytesRef)$
43-
public BytesRef getBytesRef(int position, BytesRef ignore) {
43+
public BytesRef getBytesRef(int position, BytesRef scratch) {
44+
scratch.bytes = value.bytes;
45+
scratch.offset = value.offset;
46+
scratch.length = value.length;
47+
return scratch;
4448
$else$
4549
public $type$ get$Type$(int position) {
46-
$endif$
4750
return value;
51+
$endif$
4852
}
4953

5054
@Override

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/BasicBlockTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,12 @@ public void testConstantBytesRefBlock() {
818818
assertInsertNulls(block);
819819
assertDeepCopy(block);
820820
releaseAndAssertBreaker(block);
821+
// modify the offset
822+
var v0 = block.getBytesRef(randomInt(positionCount), new BytesRef());
823+
var v1 = block.getBytesRef(randomInt(positionCount), new BytesRef());
824+
v1.length = 0;
825+
var v2 = block.getBytesRef(randomInt(positionCount), new BytesRef());
826+
assertThat(v2, equalTo(v0));
821827
}
822828
}
823829

0 commit comments

Comments
 (0)