-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
ESQL: Method to convert BooleanBlock to a "mask" #112253
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.compute.data; | ||
|
||
import org.elasticsearch.core.Releasable; | ||
|
||
/** | ||
* Result from calling {@link BooleanBlock#toMask}. {@link #close closing} this will | ||
* close the contained {@link #mask()}. If you want to keep a reference to it then you'll | ||
* have to {@link Block#incRef()} it. | ||
*/ | ||
public record ToMask(BooleanVector mask, boolean hadMultivaluedFields) implements Releasable { | ||
alex-spies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Override | ||
public void close() { | ||
mask.close(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,6 +164,12 @@ public void testBooleanVector() throws IOException { | |
assertThat(block.getBoolean(i), equalTo(elements[i])); | ||
} | ||
assertKeepMask(block); | ||
try (ToMask mask = block.toMask()) { | ||
assertThat(mask.hadMultivaluedFields(), equalTo(false)); | ||
for (int p = 0; p < elements.length; p++) { | ||
assertThat(mask.mask().getBoolean(p), equalTo(elements[p])); | ||
} | ||
} | ||
try (var copy = serializeDeserializeBlock(block)) { | ||
assertThat(copy, instanceOf(BooleanVectorBlock.class)); | ||
assertThat(block.asVector(), instanceOf(BooleanArrayVector.class)); | ||
|
@@ -224,6 +230,12 @@ public void testBooleanBlock() throws IOException { | |
assertThat(block.getBoolean(i), equalTo(elements[i])); | ||
} | ||
assertKeepMask(block); | ||
try (ToMask mask = block.toMask()) { | ||
assertThat(mask.hadMultivaluedFields(), equalTo(true)); | ||
for (int p = 0; p < elements.length; p++) { | ||
assertThat(mask.mask().getBoolean(p), equalTo(false)); | ||
} | ||
} | ||
try (var copy = serializeDeserializeBlock(block)) { | ||
assertThat(copy, instanceOf(BooleanArrayBlock.class)); | ||
assertNull(copy.asVector()); | ||
|
@@ -253,6 +265,12 @@ public void testBooleanBlock() throws IOException { | |
assertThat(block.getBoolean(i), equalTo(elements[i])); | ||
} | ||
assertKeepMask(block); | ||
try (ToMask mask = block.toMask()) { | ||
assertThat(mask.hadMultivaluedFields(), equalTo(true)); | ||
for (int p = 0; p < elements.length; p++) { | ||
assertThat(mask.mask().getBoolean(p), equalTo(false)); | ||
} | ||
} | ||
try (var copy = serializeDeserializeBlock(block)) { | ||
assertThat(copy, instanceOf(BooleanBigArrayBlock.class)); | ||
assertNull(block.asVector()); | ||
|
@@ -266,4 +284,52 @@ public void testBooleanBlock() throws IOException { | |
} | ||
assertThat(blockFactory.breaker().getUsed(), equalTo(0L)); | ||
} | ||
|
||
/** | ||
* Tests a block with one value being multivalued and the rest are single valued. | ||
*/ | ||
public void testBooleanBlockOneMv() { | ||
int mvCount = between(2, 10); | ||
int positionCount = randomIntBetween(1000, 5000); | ||
blockFactory = new BlockFactory(blockFactory.breaker(), blockFactory.bigArrays(), ByteSizeValue.ofBytes(1)); | ||
try (var builder = blockFactory.newBooleanBlockBuilder(between(1, mvCount + positionCount))) { | ||
boolean[] elements = new boolean[positionCount + mvCount]; | ||
builder.beginPositionEntry(); | ||
for (int i = 0; i < mvCount; i++) { | ||
elements[i] = randomBoolean(); | ||
builder.appendBoolean(elements[i]); | ||
} | ||
builder.endPositionEntry(); | ||
for (int p = 1; p < positionCount; p++) { | ||
elements[mvCount + p] = randomBoolean(); | ||
builder.appendBoolean(elements[mvCount + p]); | ||
} | ||
try (var block = builder.build()) { | ||
assertThat(block, instanceOf(BooleanBigArrayBlock.class)); | ||
assertNull(block.asVector()); | ||
assertThat(block.getPositionCount(), equalTo(positionCount)); | ||
assertThat(block.getValueCount(0), equalTo(mvCount)); | ||
for (int i = 0; i < mvCount; i++) { | ||
assertThat(block.getBoolean(block.getFirstValueIndex(0) + i), equalTo(elements[i])); | ||
} | ||
for (int p = 1; p < positionCount; p++) { | ||
assertThat(block.getValueCount(p), equalTo(1)); | ||
assertThat(block.getBoolean(block.getFirstValueIndex(p)), equalTo(elements[mvCount + p])); | ||
} | ||
assertKeepMask(block); | ||
try (ToMask mask = block.toMask()) { | ||
/* | ||
* NOTE: this test is customized to the layout above where we don't make | ||
* any fields with 0 values. | ||
*/ | ||
assertThat(mask.hadMultivaluedFields(), equalTo(true)); | ||
assertThat(mask.mask().getBoolean(0), equalTo(false)); | ||
for (int p = 1; p < positionCount; p++) { | ||
assertThat(mask.mask().getBoolean(p), equalTo(elements[mvCount + p])); | ||
} | ||
} | ||
} | ||
} | ||
assertThat(blockFactory.breaker().getUsed(), equalTo(0L)); | ||
} | ||
} | ||
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. nit: there's 2 ways of having null values in an array block:
I'm not sure if the added tests cover 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. The test never makes 0 value count. Also! Those should mean the same thing. 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. That's my point though: for good measure, I think Last time I went digging in these parts of the code (when I added the invariant check for array blocks), there were rare instances where we actually made 0 value counts (by setting the first index for 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. I'll open an PR. I think at this point not setting the null mask but setting the value count is an error. All kinds of things will blow up. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this doesn't implement incref/RefCounted/AbstractNonThreadSafeRefCounted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Block
's ref counted - this is sort of like a single reference to it. Not sure if that's clear though.