-
Notifications
You must be signed in to change notification settings - Fork 25.5k
ESQL: Speed up CASE for some parameters #112295
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
Changes from 4 commits
2ab7fc0
394b9d7
3979049
5fd2f66
0e1c8ac
19a8247
f3abaef
a4af922
3117287
deb5845
3c45129
9b1f6c4
b37d645
e6c0863
85c757e
15ea99c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 112295 | ||
summary: "ESQL: Speed up CASE for some parameters" | ||
area: ES|QL | ||
type: enhancement | ||
issues: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.compute.data.Block; | ||
import org.elasticsearch.compute.data.BooleanBlock; | ||
import org.elasticsearch.compute.data.BooleanVector; | ||
import org.elasticsearch.compute.data.ElementType; | ||
import org.elasticsearch.compute.data.Page; | ||
import org.elasticsearch.compute.operator.DriverContext; | ||
|
@@ -293,6 +294,12 @@ public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEv | |
return new ExpressionEvaluator.Factory() { | ||
@Override | ||
public ExpressionEvaluator get(DriverContext context) { | ||
if (conditionsFactories.size() == 1 | ||
&& conditionsFactories.get(0).value.safeToEvalInLazy() | ||
&& elseValueFactory.safeToEvalInLazy()) { | ||
|
||
return new EagerEvaluator(context, conditionsFactories.get(0).apply(context), elseValueFactory.get(context)); | ||
|
||
} | ||
return new CaseEvaluator( | ||
context, | ||
resultType, | ||
|
@@ -314,7 +321,7 @@ public String toString() { | |
}; | ||
} | ||
|
||
record ConditionEvaluatorSupplier(ExpressionEvaluator.Factory condition, ExpressionEvaluator.Factory value) | ||
private record ConditionEvaluatorSupplier(ExpressionEvaluator.Factory condition, ExpressionEvaluator.Factory value) | ||
implements | ||
Function<DriverContext, ConditionEvaluator> { | ||
@Override | ||
|
@@ -328,7 +335,9 @@ public String toString() { | |
} | ||
} | ||
|
||
record ConditionEvaluator(EvalOperator.ExpressionEvaluator condition, EvalOperator.ExpressionEvaluator value) implements Releasable { | ||
private record ConditionEvaluator(EvalOperator.ExpressionEvaluator condition, EvalOperator.ExpressionEvaluator value) | ||
implements | ||
Releasable { | ||
@Override | ||
public void close() { | ||
Releasables.closeExpectNoException(condition, value); | ||
|
@@ -393,4 +402,39 @@ public String toString() { | |
return "CaseEvaluator[resultType=" + resultType + ", conditions=" + conditions + ", elseVal=" + elseVal + ']'; | ||
} | ||
} | ||
|
||
private record EagerEvaluator(DriverContext driverContext, ConditionEvaluator condition, EvalOperator.ExpressionEvaluator elseVal) | ||
implements | ||
EvalOperator.ExpressionEvaluator { | ||
@Override | ||
public Block eval(Page page) { | ||
try ( | ||
BooleanVector lhsOrRhs = ((BooleanBlock) condition.condition.eval(page)).asVector(); | ||
// NOCOMMIT replace above with toMask | ||
Block lhs = condition.value.eval(page); | ||
Block rhs = elseVal.eval(page) | ||
) { | ||
try (Block.Builder builder = lhs.elementType().newBlockBuilder(lhs.getTotalValueCount(), driverContext.blockFactory())) { | ||
for (int p = 0; p < lhs.getPositionCount(); p++) { | ||
if (lhsOrRhs.getBoolean(p)) { | ||
builder.copyFrom(lhs, p, p + 1); | ||
} else { | ||
builder.copyFrom(rhs, p, p + 1); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
Releasables.closeExpectNoException(condition, elseVal); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CaseEvaluator[condition=" + condition + ", elseVal=" + elseVal + ']'; | ||
} | ||
} | ||
} |
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 suppose this is something that we could derive from the Expression - but it felt simpler to put it here. And it's just a boolean at this point - though maybe it should be a cost estimate at some point.
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 case of constants, the unvisited branches can be removed and the case simplified.
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.
That works if the expression is constant, but not if the values are constant. We still have to evaluate in that case. And when we do we can do it the fast way with something like this.