ES|QL: add local optimizations for constant_keyword#127549
ES|QL: add local optimizations for constant_keyword#127549luigidellaquila merged 14 commits intoelastic:mainfrom
Conversation
|
After discussing it with @alex-spies, we decided to simplify it a bit (to the cost of some minor optimizations) and diverge a bit from what we do in |
|
Pinging @elastic/es-analytical-engine (Team:Analytics) |
|
With the following test data, things do not seem to work as they do on test1 test2 test3 Results in |
|
Hi @luigidellaquila, I've created a changelog YAML for you. |
|
My bad, a missing |
…ptimizations' into esql/constant_keyword_optimizations
nik9000
left a comment
There was a problem hiding this comment.
I'm good with it. It'd be nice if there were an easier way to get the constant value, but this'll do. If everyone else is good, I'm good.
| Object objVal = vals.size() == 1 ? vals.get(0) : null; | ||
| // we are considering only string values for now, since this can return "strange" things, | ||
| // see IndexModeFieldType | ||
| thisVal = objVal instanceof String ? (String) objVal : null; |
There was a problem hiding this comment.
I believe we tend to use BytesRefs encoded as utf-8.
No worries. |
|
|
||
| private LogicalPlan replaceAttributes(LogicalPlan plan, Map<Attribute, Expression> attrToValue) { | ||
| // This is slightly different from ReplaceMissingFieldWithNull. | ||
| // It's on purpose: reusing NameIDs is dangerous, and we have no evidence that adding an EVAL will actually lead to |
There was a problem hiding this comment.
Please, provide a more explanatory comment. I am looking for "reusing NameIDs is dangerous" explanation.
There was a problem hiding this comment.
Also, having an eval doing the value "replacement" is not about performance, but about making use of other mechanisms that already exists in the optimizer to naturally "move"/"flow" the EVAL through the Nodes tree (like constant folding, moving literals on the right hand side of boolean expressions etc).
There was a problem hiding this comment.
Thinking about this some more... this rule and ReplaceMissingFieldWithNull are, in essence, doing the same thing.
The only essential aspect that is different between them is
localLogicalOptimizerContext.searchStats().exists(f.fieldName())
and
localLogicalOptimizerContext.searchStats().constantValue(attribute.name())
Meaning, the "constant" is either null or is a value coming from a constant_keyword field. Try to see if you can abstract away this logic and either:
- have two rules that use common code and the only thing different is the
searchStats()check - have only one rule that does the
nulland constant check at the same time.
|
Thanks for the feedback @astefan, I unified the two rules. |
There was a problem hiding this comment.
I am ok with the idea behind the changes, but I believe the resulting code is not so nice.
For example, the transformExpressionsOnlyUp here is not clearly showing the two (distinct) use cases, ie what has to do shouldBeRetained with constants (the conditional links them together, but one has nothing to do with the other). I've tried being more explicit by splitting the conditional and adding comments, but I still don't feel good about it. The changes do show that the logic is split and things were just merged, is not a natural way to follow the code.
...ava/org/elasticsearch/xpack/esql/optimizer/rules/logical/local/ReplaceFieldWithConstant.java
Outdated
Show resolved
Hide resolved
...ava/org/elasticsearch/xpack/esql/optimizer/rules/logical/local/ReplaceFieldWithConstant.java
Outdated
Show resolved
Hide resolved
...ava/org/elasticsearch/xpack/esql/optimizer/rules/logical/local/ReplaceFieldWithConstant.java
Outdated
Show resolved
Hide resolved
...ava/org/elasticsearch/xpack/esql/optimizer/rules/logical/local/ReplaceFieldWithConstant.java
Outdated
Show resolved
Hide resolved
...ava/org/elasticsearch/xpack/esql/optimizer/rules/logical/local/ReplaceFieldWithConstant.java
Outdated
Show resolved
Hide resolved
...ava/org/elasticsearch/xpack/esql/optimizer/rules/logical/local/ReplaceFieldWithConstant.java
Outdated
Show resolved
Hide resolved
...ava/org/elasticsearch/xpack/esql/optimizer/rules/logical/local/ReplaceFieldWithConstant.java
Outdated
Show resolved
Hide resolved
...ava/org/elasticsearch/xpack/esql/optimizer/rules/logical/local/ReplaceFieldWithConstant.java
Outdated
Show resolved
Hide resolved
|
Thanks @astefan, I included your suggestions. |
💔 Backport failed
You can use sqren/backport to manually backport by running |
Adding local planning optimizations for constant field values (eg.
constant_keyword).The rule tries to get the value at planning time and replaces it with a literal, avoiding field extraction and allowing to trigger further optimizations.