-
Notifications
You must be signed in to change notification settings - Fork 25k
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: add driverContext to conversion function evaluators and use blockFactory to instantiate blocks #100016
Merged
elasticsearchmachine
merged 7 commits into
elastic:main
from
luigidellaquila:esql/convert_functions_block_factory
Sep 29, 2023
Merged
ESQL: add driverContext to conversion function evaluators and use blockFactory to instantiate blocks #100016
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
34dbcf4
Add block factory to conversion functions evaluators
luigidellaquila ccdcde8
Use the block factory where possible
luigidellaquila 5fd2af6
Avoid using block and vector constructors
luigidellaquila 7548a04
Use block builders instead of manipulating BytesRefArrays manually
luigidellaquila 9ad53c9
Merge branch 'main' into esql/convert_functions_block_factory
elasticmachine 4f17472
Merge branch 'main' into esql/convert_functions_block_factory
luigidellaquila 4ba19f0
Removed semicolon
luigidellaquila File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,15 +6,12 @@ | |
|
||
import java.lang.Override; | ||
import java.lang.String; | ||
import java.util.BitSet; | ||
import org.elasticsearch.compute.data.Block; | ||
import org.elasticsearch.compute.data.BooleanArrayBlock; | ||
import org.elasticsearch.compute.data.BooleanArrayVector; | ||
import org.elasticsearch.compute.data.BooleanBlock; | ||
import org.elasticsearch.compute.data.ConstantBooleanVector; | ||
import org.elasticsearch.compute.data.DoubleBlock; | ||
import org.elasticsearch.compute.data.DoubleVector; | ||
import org.elasticsearch.compute.data.Vector; | ||
import org.elasticsearch.compute.operator.DriverContext; | ||
import org.elasticsearch.compute.operator.EvalOperator; | ||
import org.elasticsearch.xpack.ql.tree.Source; | ||
|
||
|
@@ -23,8 +20,12 @@ | |
* This class is generated. Do not edit it. | ||
*/ | ||
public final class ToBooleanFromDoubleEvaluator extends AbstractConvertFunction.AbstractEvaluator { | ||
public ToBooleanFromDoubleEvaluator(EvalOperator.ExpressionEvaluator field, Source source) { | ||
private final DriverContext driverContext; | ||
|
||
public ToBooleanFromDoubleEvaluator(EvalOperator.ExpressionEvaluator field, Source source, | ||
DriverContext driverContext) { | ||
super(field, source); | ||
this.driverContext = driverContext; | ||
} | ||
|
||
@Override | ||
|
@@ -38,29 +39,22 @@ public Block evalVector(Vector v) { | |
int positionCount = v.getPositionCount(); | ||
if (vector.isConstant()) { | ||
try { | ||
return new ConstantBooleanVector(evalValue(vector, 0), positionCount).asBlock(); | ||
return driverContext.blockFactory().newConstantBooleanBlockWith(evalValue(vector, 0), positionCount); | ||
} catch (Exception e) { | ||
registerException(e); | ||
return Block.constantNullBlock(positionCount); | ||
return Block.constantNullBlock(positionCount, driverContext.blockFactory()); | ||
} | ||
} | ||
BitSet nullsMask = null; | ||
boolean[] values = new boolean[positionCount]; | ||
BooleanBlock.Builder builder = BooleanBlock.newBlockBuilder(positionCount, driverContext.blockFactory()); | ||
for (int p = 0; p < positionCount; p++) { | ||
try { | ||
values[p] = evalValue(vector, p); | ||
builder.appendBoolean(evalValue(vector, p)); | ||
} catch (Exception e) { | ||
registerException(e); | ||
if (nullsMask == null) { | ||
nullsMask = new BitSet(positionCount); | ||
} | ||
nullsMask.set(p); | ||
builder.appendNull(); | ||
} | ||
} | ||
return nullsMask == null | ||
? new BooleanArrayVector(values, positionCount).asBlock() | ||
// UNORDERED, since whatever ordering there is, it isn't necessarily preserved | ||
: new BooleanArrayBlock(values, positionCount, null, nullsMask, Block.MvOrdering.UNORDERED); | ||
return builder.build(); | ||
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. This makes sense to me. :+!: 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. ++ |
||
} | ||
|
||
private static boolean evalValue(DoubleVector container, int index) { | ||
|
@@ -72,7 +66,7 @@ private static boolean evalValue(DoubleVector container, int index) { | |
public Block evalBlock(Block b) { | ||
DoubleBlock block = (DoubleBlock) b; | ||
int positionCount = block.getPositionCount(); | ||
BooleanBlock.Builder builder = BooleanBlock.newBlockBuilder(positionCount); | ||
BooleanBlock.Builder builder = BooleanBlock.newBlockBuilder(positionCount, driverContext.blockFactory()); | ||
for (int p = 0; p < positionCount; p++) { | ||
int valueCount = block.getValueCount(p); | ||
int start = block.getFirstValueIndex(p); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
All this logic (nullMasks, BytesRefArrays) is already managed by BlockBuilders... or maybe I'm missing something?
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.
Assuming that the direct usage of arrays here is a deliberate optimization, I tried to minimize the impact and just replace
with
The change seems logic, but CsvTests don't seem to appreciate it:
and then
Is it worth further investigation to keep the optimizations in place or should we just go with the builders?
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'd switch to the builders to be honest. The builders are going to be easier to easier to manage the memory of.
The direct usage of arrays was an optimization for sure. I think when we don't have to handle null we can use the
Vector.FixedBuilder
thing I wrote. When we do have to handle null we could probably use the regular block builder. Later I think we could makeBlock.FixedBuilder
which gets the same optimizations back.The goal with the
FixedBuilder
s is that the JVM can inline all the method calls on it to be "the same" as when we operate on the arrays directly.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.
In this case we'll have to handle nulls (conversions can fail), so I guess what this PR does now is good enough. We can review it when we have a bit more time and fine-tune some specific cases (eg. maybe it's safe to assume that conversions to string never fail)