-
Notifications
You must be signed in to change notification settings - Fork 25.2k
ES|QL: add local optimizations for constant_keyword #127549
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
base: main
Are you sure you want to change the base?
ES|QL: add local optimizations for constant_keyword #127549
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
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'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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we tend to use BytesRef
s 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, provide a more explanatory comment. I am looking for "reusing NameIDs is dangerous" explanation.
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
null
and constant check at the same time.
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.