-
Notifications
You must be signed in to change notification settings - Fork 25.6k
ES|QL - Full text functions accept null as field parameter #137430
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
Open
carlosdelest
wants to merge
24
commits into
elastic:main
Choose a base branch
from
carlosdelest:bugfix/esql-full-text-functions-accept-null
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+558
−385
Open
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
1364419
Remove null check for fields in FTFs
bd00de1
First version - allow nulls as params, implement nullable() in Match,…
20b202d
Introduce base class for Match and MatchPhrase
c7bda0f
Make Knn a subclass of SingleFieldFullTextFunction, refactor
7f5f5c6
Refactor
8fa9556
Fix PushDownFilterAndLimitIntoUnionAllTests, now match can be pushed …
bd6e99d
Spotless
8ce4820
Refactor
1d98c74
Fix error tests
ec81181
Add check for single unique signature
336b33c
Update docs/changelog/137430.yaml
carlosdelest 959cac7
Fix tests and add null cases
21daf3d
Merge remote-tracking branch 'carlosdelest/bugfix/esql-full-text-func…
4ba054c
Merge remote-tracking branch 'origin/main' into bugfix/esql-full-text…
b737b03
Add test cases
f68b569
Fix tests
2436a22
Remove FoldNull as it is already applied in localOperators()
012378b
Apply suggestion from @bpintea
carlosdelest 30a8319
Apply suggestion from @bpintea
carlosdelest 569b084
Apply suggestion from @bpintea
carlosdelest 04b7dd9
Merge remote-tracking branch 'carlosdelest/bugfix/esql-full-text-func…
ad811e5
Feedback from @bpintea
845cd35
Add null test cases for Knn and MatchPhrase
48fa7c1
Merge branch 'main' into bugfix/esql-full-text-functions-accept-null
carlosdelest 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,6 @@ | ||
| pr: 137430 | ||
| summary: ES|QL - Full text functions accept null as field parameter | ||
| area: "ES|QL" | ||
| type: bug | ||
| issues: | ||
| - 136608 |
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 |
|---|---|---|
|
|
@@ -7,26 +7,20 @@ | |
|
|
||
| package org.elasticsearch.xpack.esql.expression.function.fulltext; | ||
|
|
||
| import org.apache.lucene.util.BytesRef; | ||
| import org.elasticsearch.TransportVersions; | ||
| import org.elasticsearch.common.io.stream.NamedWriteableRegistry; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.unit.Fuzziness; | ||
| import org.elasticsearch.index.query.QueryBuilder; | ||
| import org.elasticsearch.xpack.esql.capabilities.PostAnalysisPlanVerificationAware; | ||
| import org.elasticsearch.xpack.esql.common.Failures; | ||
| import org.elasticsearch.xpack.esql.core.InvalidArgumentException; | ||
| import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
| import org.elasticsearch.xpack.esql.core.expression.FieldAttribute; | ||
| import org.elasticsearch.xpack.esql.core.expression.MapExpression; | ||
| import org.elasticsearch.xpack.esql.core.querydsl.query.Query; | ||
| import org.elasticsearch.xpack.esql.core.tree.NodeInfo; | ||
| import org.elasticsearch.xpack.esql.core.tree.Source; | ||
| import org.elasticsearch.xpack.esql.core.type.DataType; | ||
| import org.elasticsearch.xpack.esql.core.util.Check; | ||
| import org.elasticsearch.xpack.esql.core.util.NumericUtils; | ||
| import org.elasticsearch.xpack.esql.expression.Foldables; | ||
| import org.elasticsearch.xpack.esql.expression.function.Example; | ||
| import org.elasticsearch.xpack.esql.expression.function.FunctionAppliesTo; | ||
| import org.elasticsearch.xpack.esql.expression.function.FunctionAppliesToLifecycle; | ||
|
|
@@ -37,18 +31,14 @@ | |
| import org.elasticsearch.xpack.esql.expression.function.Param; | ||
| import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput; | ||
| import org.elasticsearch.xpack.esql.optimizer.rules.physical.local.LucenePushdownPredicates; | ||
| import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; | ||
| import org.elasticsearch.xpack.esql.planner.TranslatorHandler; | ||
| import org.elasticsearch.xpack.esql.querydsl.query.MatchQuery; | ||
| import org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.function.BiConsumer; | ||
|
|
||
| import static java.util.Map.entry; | ||
| import static org.elasticsearch.index.query.AbstractQueryBuilder.BOOST_FIELD; | ||
|
|
@@ -62,11 +52,7 @@ | |
| import static org.elasticsearch.index.query.MatchQueryBuilder.OPERATOR_FIELD; | ||
| import static org.elasticsearch.index.query.MatchQueryBuilder.PREFIX_LENGTH_FIELD; | ||
| import static org.elasticsearch.index.query.MatchQueryBuilder.ZERO_TERMS_QUERY_FIELD; | ||
| import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST; | ||
| import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND; | ||
| import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.THIRD; | ||
| import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isNotNull; | ||
| import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isType; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.BOOLEAN; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.DATETIME; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.DATE_NANOS; | ||
|
|
@@ -76,20 +62,20 @@ | |
| import static org.elasticsearch.xpack.esql.core.type.DataType.IP; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.LONG; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.NULL; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.TEXT; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.UNSIGNED_LONG; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.VERSION; | ||
| import static org.elasticsearch.xpack.esql.expression.Foldables.TypeResolutionValidator.forPreOptimizationValidation; | ||
| import static org.elasticsearch.xpack.esql.expression.Foldables.resolveTypeQuery; | ||
| import static org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.EsqlBinaryComparison.formatIncompatibleTypesMessage; | ||
|
|
||
| /** | ||
| * Full text function that performs a {@link org.elasticsearch.xpack.esql.querydsl.query.MatchQuery} . | ||
| */ | ||
| public class Match extends FullTextFunction implements OptionalArgument, PostAnalysisPlanVerificationAware { | ||
| public class Match extends SingleFieldFullTextFunction implements OptionalArgument { | ||
|
|
||
| public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Match", Match::readFrom); | ||
| public static final Set<DataType> FIELD_DATA_TYPES = Set.of( | ||
| NULL, | ||
| KEYWORD, | ||
| TEXT, | ||
| BOOLEAN, | ||
|
|
@@ -115,11 +101,6 @@ public class Match extends FullTextFunction implements OptionalArgument, PostAna | |
| VERSION | ||
| ); | ||
|
|
||
| protected final Expression field; | ||
|
|
||
| // Options for match function. They don’t need to be serialized as the data nodes will retrieve them from the query builder | ||
| private final transient Expression options; | ||
|
|
||
| public static final Map<String, DataType> ALLOWED_OPTIONS = Map.ofEntries( | ||
| entry(ANALYZER_FIELD.getPreferredName(), KEYWORD), | ||
| entry(GENERATE_SYNONYMS_PHRASE_QUERY.getPreferredName(), BOOLEAN), | ||
|
|
@@ -266,9 +247,14 @@ public Match( | |
| } | ||
|
|
||
| public Match(Source source, Expression field, Expression matchQuery, Expression options, QueryBuilder queryBuilder) { | ||
| super(source, matchQuery, options == null ? List.of(field, matchQuery) : List.of(field, matchQuery, options), queryBuilder); | ||
| this.field = field; | ||
| this.options = options; | ||
| super( | ||
| source, | ||
| field, | ||
| matchQuery, | ||
| options, | ||
| options == null ? List.of(field, matchQuery) : List.of(field, matchQuery, options), | ||
| queryBuilder | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -300,47 +286,16 @@ public final void writeTo(StreamOutput out) throws IOException { | |
|
|
||
| @Override | ||
| protected TypeResolution resolveParams() { | ||
| return resolveField().and(resolveQuery()) | ||
|
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. Refactoring - all of these are moved up to |
||
| .and(Options.resolve(options(), source(), THIRD, ALLOWED_OPTIONS)) | ||
| .and(checkParamCompatibility()); | ||
| } | ||
|
|
||
| private TypeResolution resolveField() { | ||
| return isNotNull(field, sourceText(), FIRST).and( | ||
| isType( | ||
| field, | ||
| FIELD_DATA_TYPES::contains, | ||
| sourceText(), | ||
| FIRST, | ||
| "keyword, text, boolean, date, date_nanos, double, integer, ip, long, unsigned_long, version" | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| private TypeResolution resolveQuery() { | ||
| TypeResolution result = isType( | ||
| query(), | ||
| QUERY_DATA_TYPES::contains, | ||
| sourceText(), | ||
| SECOND, | ||
| "keyword, boolean, date, date_nanos, double, integer, ip, long, unsigned_long, version" | ||
| ).and(isNotNull(query(), sourceText(), SECOND)); | ||
| if (result.unresolved()) { | ||
| return result; | ||
| } | ||
| result = resolveTypeQuery(query(), sourceText(), forPreOptimizationValidation(query())); | ||
| if (result.equals(TypeResolution.TYPE_RESOLVED) == false) { | ||
| return result; | ||
| } | ||
| return TypeResolution.TYPE_RESOLVED; | ||
| return super.resolveParams().and(checkParamCompatibility()); | ||
| } | ||
|
|
||
| private TypeResolution checkParamCompatibility() { | ||
| DataType fieldType = field().dataType(); | ||
| DataType queryType = query().dataType(); | ||
|
|
||
| // Field and query types should match. If the query is a string, then it can match any field type. | ||
| if ((fieldType == queryType) || (queryType == KEYWORD)) { | ||
| // If the field is null, it will be folded to null. | ||
| if ((fieldType == queryType) || (queryType == KEYWORD) || fieldType == NULL) { | ||
| return TypeResolution.TYPE_RESOLVED; | ||
| } | ||
|
|
||
|
|
@@ -354,6 +309,21 @@ private TypeResolution checkParamCompatibility() { | |
| return new TypeResolution(formatIncompatibleTypesMessage(fieldType, queryType, sourceText())); | ||
| } | ||
|
|
||
| @Override | ||
| protected Set<DataType> getFieldDataTypes() { | ||
| return FIELD_DATA_TYPES; | ||
| } | ||
|
|
||
| @Override | ||
| protected Set<DataType> getQueryDataTypes() { | ||
| return QUERY_DATA_TYPES; | ||
| } | ||
|
|
||
| @Override | ||
| protected Map<String, DataType> getAllowedOptions() { | ||
| return ALLOWED_OPTIONS; | ||
| } | ||
|
|
||
| private Map<String, Object> matchQueryOptions() throws InvalidArgumentException { | ||
| if (options() == null) { | ||
| return Map.of(LENIENT_FIELD.getPreferredName(), true); | ||
|
|
@@ -367,14 +337,6 @@ private Map<String, Object> matchQueryOptions() throws InvalidArgumentException | |
| return matchOptions; | ||
| } | ||
|
|
||
| public Expression field() { | ||
| return field; | ||
| } | ||
|
|
||
| public Expression options() { | ||
| return options; | ||
| } | ||
|
|
||
| @Override | ||
| protected NodeInfo<? extends Expression> info() { | ||
| return NodeInfo.create(this, Match::new, field(), query(), options(), queryBuilder()); | ||
|
|
@@ -396,48 +358,6 @@ public Expression replaceQueryBuilder(QueryBuilder queryBuilder) { | |
| return new Match(source(), field, query(), options(), queryBuilder); | ||
| } | ||
|
|
||
| @Override | ||
| public BiConsumer<LogicalPlan, Failures> postAnalysisPlanVerification() { | ||
| return (plan, failures) -> { | ||
| super.postAnalysisPlanVerification().accept(plan, failures); | ||
| fieldVerifier(plan, this, field, failures); | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public BiConsumer<LogicalPlan, Failures> postOptimizationPlanVerification() { | ||
| // check plan again after predicates are pushed down into subqueries | ||
| return (plan, failures) -> { | ||
| super.postOptimizationPlanVerification().accept(plan, failures); | ||
| fieldVerifier(plan, this, field, failures); | ||
| }; | ||
| } | ||
|
|
||
| public Object queryAsObject() { | ||
| Object queryAsObject = Foldables.queryAsObject(query(), sourceText()); | ||
|
|
||
| // Convert BytesRef to string for string-based values | ||
| if (queryAsObject instanceof BytesRef bytesRef) { | ||
| return switch (query().dataType()) { | ||
| case IP -> EsqlDataTypeConverter.ipToString(bytesRef); | ||
| case VERSION -> EsqlDataTypeConverter.versionToString(bytesRef); | ||
| default -> bytesRef.utf8ToString(); | ||
| }; | ||
| } | ||
|
|
||
| // Converts specific types to the correct type for the query | ||
| if (query().dataType() == DataType.UNSIGNED_LONG) { | ||
| return NumericUtils.unsignedLongAsBigInteger((Long) queryAsObject); | ||
| } else if (query().dataType() == DataType.DATETIME && queryAsObject instanceof Long) { | ||
| // When casting to date and datetime, we get a long back. But Match query needs a date string | ||
| return EsqlDataTypeConverter.dateTimeToString((Long) queryAsObject); | ||
| } else if (query().dataType() == DATE_NANOS && queryAsObject instanceof Long) { | ||
| return EsqlDataTypeConverter.nanoTimeToString((Long) queryAsObject); | ||
| } | ||
|
|
||
| return queryAsObject; | ||
| } | ||
|
|
||
| @Override | ||
| protected Query translate(LucenePushdownPredicates pushdownPredicates, TranslatorHandler handler) { | ||
| var fieldAttribute = fieldAsFieldAttribute(); | ||
|
|
@@ -446,24 +366,4 @@ protected Query translate(LucenePushdownPredicates pushdownPredicates, Translato | |
| // Make query lenient so mixed field types can be queried when a field type is incompatible with the value provided | ||
| return new MatchQuery(source(), fieldName, queryAsObject(), matchQueryOptions()); | ||
| } | ||
|
|
||
| private FieldAttribute fieldAsFieldAttribute() { | ||
| return fieldAsFieldAttribute(field); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| // Match does not serialize options, as they get included in the query builder. We need to override equals and hashcode to | ||
| // ignore options when comparing two Match functions | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Match match = (Match) o; | ||
| return Objects.equals(field(), match.field()) | ||
| && Objects.equals(query(), match.query()) | ||
| && Objects.equals(queryBuilder(), match.queryBuilder()); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(field(), query(), queryBuilder()); | ||
| } | ||
| } | ||
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.
A somewhat lateral note: we could have an unpacking loop here, for the (more likely automated query generation) case of nested conversions, like:
field::INTEGER::LONG.