-
Notifications
You must be signed in to change notification settings - Fork 25.2k
[ES|QL] Assign new id to alias created by ReplaceMissingFieldWithNull when there is lookup join #125462
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
[ES|QL] Assign new id to alias created by ReplaceMissingFieldWithNull when there is lookup join #125462
Changes from all commits
13b35e6
45553bb
4f5fcc6
95a461f
f705ed4
2a21697
e6a055b
3723510
25dc590
92985fa
a825a5b
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,6 @@ | ||
pr: 125462 | ||
summary: Assign new id to alias created by `ReplaceMissingFieldWithNull` when there | ||
is lookup join | ||
area: ES|QL | ||
type: bug | ||
issues: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,12 @@ | |
import org.elasticsearch.common.util.Maps; | ||
import org.elasticsearch.index.IndexMode; | ||
import org.elasticsearch.xpack.esql.core.expression.Alias; | ||
import org.elasticsearch.xpack.esql.core.expression.Attribute; | ||
import org.elasticsearch.xpack.esql.core.expression.AttributeSet; | ||
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute; | ||
import org.elasticsearch.xpack.esql.core.expression.Literal; | ||
import org.elasticsearch.xpack.esql.core.expression.NamedExpression; | ||
import org.elasticsearch.xpack.esql.core.expression.ReferenceAttribute; | ||
import org.elasticsearch.xpack.esql.core.type.DataType; | ||
import org.elasticsearch.xpack.esql.core.type.PotentiallyUnmappedKeywordEsField; | ||
import org.elasticsearch.xpack.esql.optimizer.LocalLogicalOptimizerContext; | ||
|
@@ -22,6 +24,7 @@ | |
import org.elasticsearch.xpack.esql.plan.logical.Eval; | ||
import org.elasticsearch.xpack.esql.plan.logical.Filter; | ||
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; | ||
import org.elasticsearch.xpack.esql.plan.logical.MvExpand; | ||
import org.elasticsearch.xpack.esql.plan.logical.OrderBy; | ||
import org.elasticsearch.xpack.esql.plan.logical.Project; | ||
import org.elasticsearch.xpack.esql.plan.logical.RegexExtract; | ||
|
@@ -81,7 +84,11 @@ else if (plan instanceof Project project) { | |
Alias nullAlias = nullLiteral.get(f.dataType()); | ||
// save the first field as null (per datatype) | ||
if (nullAlias == null) { | ||
Alias alias = new Alias(f.source(), f.name(), Literal.of(f, null), f.id()); | ||
// In case of batch executions on data nodes and join exists, SearchStats may not always be available for all | ||
// fields, creating a new alias for null with the same id as the field id can potentially cause planEval to add a | ||
// duplicated ChannelSet to a layout, and Layout.builder().build() could throw a NullPointerException. | ||
// As a workaround, assign a new alias id to the null alias when join exists and SearchStats is not available. | ||
Alias alias = new Alias(f.source(), f.name(), Literal.of(f, null), joinAttributes.isEmpty() ? f.id() : null); | ||
nullLiteral.put(dt, alias); | ||
projection = alias.toAttribute(); | ||
} | ||
|
@@ -113,14 +120,40 @@ else if (plan instanceof Project project) { | |
? f | ||
: Literal.of(f, null) | ||
); | ||
} else if (plan instanceof MvExpand m) { | ||
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. There can also be other downstream commands that will have the same problem, though, like CHANGE_POINT. |
||
NamedExpression target = m.target(); | ||
AttributeSet joinAttributes = joinAttributes(m); | ||
if (joinAttributes.isEmpty() == false // rewrite only when there is join, TODO do we want to rewrite when there is no join? | ||
&& target instanceof FieldAttribute f | ||
&& stats.exists(f.fieldName()) == false | ||
&& joinAttributes.contains(f) == false | ||
&& f.field() instanceof PotentiallyUnmappedKeywordEsField == false) { | ||
// Replace missing target field with null. | ||
Alias alias = new Alias(f.source(), f.name(), Literal.of(f, null)); | ||
NamedExpression nullTarget = alias.toAttribute(); | ||
plan = new Eval(m.source(), m.child(), List.of(alias)); | ||
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 think this will change the position of the expanded field in the output because EVAL always places new attributes on the right. This may be fine when there's a projection some time later, but it may also create another bug. |
||
// The expanded reference is built on top of target field with the same name, and the parent plans all reference to the | ||
// expanded reference other than the target field, keep expanded's id unchanged, otherwise the parent plans cannot find | ||
// it. | ||
Attribute nullExpanded = new ReferenceAttribute( | ||
nullTarget.source(), | ||
nullTarget.name(), | ||
nullTarget.dataType(), | ||
nullTarget.nullable(), | ||
m.expanded().id(), | ||
false | ||
); | ||
plan = new MvExpand(m.source(), plan, nullTarget, nullExpanded); | ||
} | ||
} | ||
|
||
return plan; | ||
} | ||
|
||
private AttributeSet joinAttributes(Project project) { | ||
private AttributeSet joinAttributes(LogicalPlan plan) { | ||
var attributes = new AttributeSet(); | ||
project.forEachDown(Join.class, j -> j.right().forEachDown(EsRelation.class, p -> attributes.addAll(p.output()))); | ||
if (plan instanceof Project || plan instanceof MvExpand) { | ||
plan.forEachDown(Join.class, j -> j.right().forEachDown(EsRelation.class, p -> attributes.addAll(p.output()))); | ||
} | ||
return attributes; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks a lot @fang-xing-esql for getting to the bottom of this!
I think this approach unfortunately still has problems. There could be downstream commands that still reference the old name id, but now the attribute with the old id is gone for good (projected away). For instance, there might be another
LOOKUP JOIN
command after the projection, or anMV_EXPAND
, and I think this one will just break due to the name id being completely absent from the previous commands' layouts. The only subsequent commands that are OK to use downstream are those taken care of in the nextelse if
branch below, namely Eval, Filter, OrderBy, RegexExtract and TopN.I guess there are 2 approaches of eliminating missing fields: either, define a new attribute pointing to a null literal with an own id - and update all downstream references to point to the new attribute; OR update the attribute in place by defining a null literal with the same name id + make sure we never extract the original attribute in the first place; the latter is sketchy because, as we can see here, it can lead to re-defining the same name id multiple times.
Now, I think a part of the correct long-term solution is that we avoid the lookup join when the left hand side join key is missing. But that's not sufficient: I can see that we're introducing multiple Projects for the same missing attribute even if it is not a join key, like so (taken from your csv test):
The problem is that there are 2 Projects for
birth_date
, which is locally missing, andReplaceMissingFieldWithNull
will lead to the addition of the null attributebirth_date
with the id 15 twice. Your fix gives that a new id each time, but ifbirth_date{f}#15
is used anywhere after the secondProject
(in a more complex query), it will be missing.Therefore, I think the second, more crucial, part of a correct solution is that we avoid replacing attributes by other attributes with the same name id, at all. Instead, I think we should update
ReplaceMissingFieldWithNull
so that it always assigns new ids - and updates any downstream references to the missing field attribute with a reference attribute to the newly defined null attribute.