-
Notifications
You must be signed in to change notification settings - Fork 26k
ES|QL: Fix KQL/QSTR with unmapped fields in NULLIFY mode #143399
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
Merged
luigidellaquila
merged 13 commits into
elastic:main
from
luigidellaquila:esql/fix_kql_nullify
Mar 9, 2026
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ca28232
ES|QL: Fix KQL/QSTR with unmapped fields in NULLIFY mode
luigidellaquila 1fc4dfa
Update docs/changelog/143399.yaml
luigidellaquila c8a28dc
Merge branch 'main' into esql/fix_kql_nullify
luigidellaquila f76b8bb
Merge branch 'main' into esql/fix_kql_nullify
luigidellaquila 7b2294e
Merge branch 'main' into esql/fix_kql_nullify
luigidellaquila 77c0744
Merge branch 'main' into esql/fix_kql_nullify
luigidellaquila 9e2df80
Merge branch 'main' into esql/fix_kql_nullify
luigidellaquila 26704c3
Merge branch 'main' into esql/fix_kql_nullify
bpintea dbb3699
Add MissingEsField
luigidellaquila adce0b1
Merge branch 'main' into esql/fix_kql_nullify
luigidellaquila f521f1b
Merge branch 'main' into esql/fix_kql_nullify
luigidellaquila 748a64a
Fix golden tests
luigidellaquila 3677225
Merge branch 'main' into esql/fix_kql_nullify
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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| area: ES|QL | ||
| issues: | ||
| - 142968 | ||
| - 142959 | ||
| pr: 143399 | ||
| summary: Fix KQL/QSTR with unmapped fields in NULLIFY mode | ||
| type: bug |
1 change: 1 addition & 0 deletions
1
server/src/main/resources/transport/definitions/referable/esql_missing_es_field.csv
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 9305000 |
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| inference_azure_openai_task_settings_headers,9304000 | ||
| esql_missing_es_field,9305000 |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -24,6 +24,9 @@ | |
| import org.elasticsearch.xpack.esql.core.expression.UnresolvedAttribute; | ||
| import org.elasticsearch.xpack.esql.core.expression.UnresolvedPattern; | ||
| import org.elasticsearch.xpack.esql.core.expression.UnresolvedTimestamp; | ||
| import org.elasticsearch.xpack.esql.core.type.DataType; | ||
| import org.elasticsearch.xpack.esql.core.type.EsField; | ||
| import org.elasticsearch.xpack.esql.core.type.MissingEsField; | ||
| import org.elasticsearch.xpack.esql.core.type.PotentiallyUnmappedKeywordEsField; | ||
| import org.elasticsearch.xpack.esql.core.util.Holder; | ||
| import org.elasticsearch.xpack.esql.plan.logical.EsRelation; | ||
|
|
@@ -97,19 +100,53 @@ private static LogicalPlan resolve(LogicalPlan plan, boolean load) { | |
| } | ||
|
|
||
| /** | ||
| * The method introduces {@code EVAL missing_field = NULL}-equivalent into the plan, on top of the source, for every attribute in | ||
| * {@code unresolved}. It also "patches" the introduced attributes through the plan, where needed (like through Fork/UntionAll). | ||
| * This method introduces null-typed field attributes for every attribute in {@code unresolved}, within the {@link EsRelation}s | ||
| * in the plan. The fields are added with {@link DataType#NULL} type, which causes {@code ReplaceFieldWithConstantOrNull} | ||
| * (in the local logical optimizer) to replace them with {@code Literal.NULL}. | ||
| * <p> | ||
| * For non-EsRelation sources (Row, LocalRelation), it falls back to inserting Eval nodes with null assignments. | ||
| * <p> | ||
| * It also "patches" the introduced attributes through the plan, where needed (like through Fork/UnionAll). | ||
| */ | ||
| private static LogicalPlan nullify(LogicalPlan plan, Set<UnresolvedAttribute> unresolved) { | ||
| // insert an Eval on top of every LeafPlan, if there's a UnaryPlan atop it | ||
| var transformed = plan.transformUp( | ||
| n -> n instanceof UnaryPlan unary && unary.child() instanceof LeafPlan, | ||
| // For EsRelation sources: add null-typed fields to the relation's output | ||
| var transformed = plan.transformUp(EsRelation.class, esr -> { | ||
| if (esr.indexMode() == IndexMode.LOOKUP) { | ||
| return esr; | ||
| } | ||
| List<FieldAttribute> fieldsToNullify = fieldsToNullify(unresolved, Expressions.names(esr.output())); | ||
| return fieldsToNullify.isEmpty() ? esr : esr.withAttributes(combine(esr.output(), fieldsToNullify)); | ||
| }); | ||
|
|
||
| // For non-EsRelation sources (Row, LocalRelation): insert Eval nodes with null assignments | ||
| // This handles cases like: ROW x = 1 | EVAL y = unmapped_field | ||
| transformed = transformed.transformUp( | ||
| n -> n instanceof UnaryPlan unary && unary.child() instanceof LeafPlan leaf && (leaf instanceof EsRelation == false), | ||
| p -> evalUnresolvedAtopUnary((UnaryPlan) p, nullAliases(unresolved)) | ||
| ); | ||
| // insert an Eval on top of those LeafPlan that are children of n-ary plans (could happen with UnionAll) | ||
| return transformed.transformUp( | ||
| n -> n instanceof UnaryPlan == false && n instanceof LeafPlan == false, | ||
| nAry -> evalUnresolvedAtopNary(nAry, nullAliases(unresolved)) | ||
| nAry -> evalUnresolvedAtopNaryNonEsRelation(nAry, nullAliases(unresolved)) | ||
| ); | ||
| } | ||
|
|
||
| private static List<FieldAttribute> fieldsToNullify(Set<UnresolvedAttribute> unresolved, List<String> exclude) { | ||
| List<FieldAttribute> nullified = new ArrayList<>(unresolved.size()); | ||
| for (var ua : unresolved) { | ||
| if (exclude.contains(ua.name()) == false) { | ||
| nullified.add(nullifyField(ua)); | ||
| } | ||
| } | ||
| return nullified; | ||
| } | ||
|
|
||
| private static FieldAttribute nullifyField(Attribute attribute) { | ||
| return new FieldAttribute( | ||
| attribute.source(), | ||
| null, | ||
| attribute.qualifier(), | ||
| attribute.name(), | ||
| new MissingEsField(attribute.name(), DataType.NULL, Map.of(), false, EsField.TimeSeriesFieldType.NONE) | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -222,6 +259,24 @@ private static LogicalPlan evalUnresolvedAtopNary(LogicalPlan nAry, List<Alias> | |
| return changed ? nAry.replaceChildren(newChildren) : nAry; | ||
| } | ||
|
|
||
| /** | ||
| * Inserts an Eval atop each child of the given {@code nAry}, if the child is a non-EsRelation LeafPlan. | ||
| * EsRelation sources are handled separately by adding fields to their output. | ||
| */ | ||
| private static LogicalPlan evalUnresolvedAtopNaryNonEsRelation(LogicalPlan nAry, List<Alias> nullAliases) { | ||
| List<LogicalPlan> newChildren = new ArrayList<>(nAry.children().size()); | ||
| boolean changed = false; | ||
| for (var child : nAry.children()) { | ||
| if (child instanceof LeafPlan source && (source instanceof EsRelation == false)) { | ||
|
Contributor
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. Also here, ( ) are redundant. |
||
| assertSourceType(source); | ||
| child = new Eval(source.source(), source, nullAliases); | ||
| changed = true; | ||
| } | ||
| newChildren.add(child); | ||
| } | ||
| return changed ? nAry.replaceChildren(newChildren) : nAry; | ||
| } | ||
|
|
||
| /** | ||
| * Inserts an Eval atop the given {@code unaryAtopSource}, if this isn't an Eval already. Otherwise it merges the nullAliases into it. | ||
| */ | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
54 changes: 54 additions & 0 deletions
54
x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/core/type/MissingEsField.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.esql.core.type; | ||
|
|
||
| import org.elasticsearch.TransportVersion; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
|
|
||
| public class MissingEsField extends EsField { | ||
|
|
||
| private static final TransportVersion ESQL_MISSING_ES_FIELD = TransportVersion.fromName("esql_missing_es_field"); | ||
|
|
||
| public MissingEsField( | ||
| String name, | ||
| DataType esDataType, | ||
| Map<String, EsField> properties, | ||
| boolean aggregatable, | ||
| TimeSeriesFieldType timeSeriesFieldType | ||
| ) { | ||
| super(name, esDataType, properties, aggregatable, timeSeriesFieldType); | ||
| } | ||
|
|
||
| public MissingEsField( | ||
| String name, | ||
| DataType esDataType, | ||
| Map<String, EsField> properties, | ||
| boolean aggregatable, | ||
| boolean isAlias, | ||
| TimeSeriesFieldType timeSeriesFieldType | ||
| ) { | ||
| super(name, esDataType, properties, aggregatable, isAlias, timeSeriesFieldType); | ||
| } | ||
|
|
||
| public MissingEsField(StreamInput in) throws IOException { | ||
| super(in); | ||
| } | ||
|
|
||
| public String getWriteableName(TransportVersion transportVersion) { | ||
| if (transportVersion.supports(ESQL_MISSING_ES_FIELD)) { | ||
| return "MissingEsField"; | ||
| } | ||
| // This was introduced because of https://github.com/elastic/elasticsearch/issues/142968 | ||
| // Things should still work fine with nodes that don't have MissingEsField, | ||
| // so for BWC we just fall back to EsField, which is the closest thing to MissingEsField that older nodes will understand. | ||
| return "EsField"; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Nit: no need for ( )