From 7ee299de5fcfdb92d869602235e10957bc27f221 Mon Sep 17 00:00:00 2001 From: carlosdelest Date: Fri, 24 Jan 2025 21:38:03 +0100 Subject: [PATCH 1/2] Fix class structure and serialization code for 8.x --- .../function/fulltext/MatchOperator.java | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/MatchOperator.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/MatchOperator.java index e3e4bc4678089..3e59103c53302 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/MatchOperator.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/MatchOperator.java @@ -7,8 +7,11 @@ package org.elasticsearch.xpack.esql.expression.function.fulltext; +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.index.query.QueryBuilder; import org.elasticsearch.xpack.esql.core.expression.Expression; import org.elasticsearch.xpack.esql.core.tree.NodeInfo; import org.elasticsearch.xpack.esql.core.tree.Source; @@ -23,7 +26,7 @@ /** * This class performs a {@link org.elasticsearch.xpack.esql.querydsl.query.MatchQuery} using an operator. */ -public class MatchOperator extends Match { +public class MatchOperator extends AbstractMatchFullTextFunction { public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry( Expression.class, @@ -60,15 +63,11 @@ public MatchOperator( description = "Value to find in the provided field." ) Expression matchQuery ) { - super(source, field, matchQuery); + this(source, field, matchQuery, null); } - private static Match readFrom(StreamInput in) throws IOException { - Source source = Source.readFrom((PlanStreamInput) in); - Expression field = in.readNamedWriteable(Expression.class); - Expression query = in.readNamedWriteable(Expression.class); - - return new MatchOperator(source, field, query); + private MatchOperator(Source source, Expression field, Expression matchQuery, QueryBuilder queryBuilder) { + super(source, matchQuery, List.of(field, matchQuery), queryBuilder, field); } @Override @@ -86,6 +85,27 @@ public String getWriteableName() { return ENTRY.name; } + private static MatchOperator readFrom(StreamInput in) throws IOException { + Source source = Source.readFrom((PlanStreamInput) in); + Expression field = in.readNamedWriteable(Expression.class); + Expression query = in.readNamedWriteable(Expression.class); + QueryBuilder queryBuilder = null; + if (in.getTransportVersion().onOrAfter(TransportVersions.ESQL_QUERY_BUILDER_IN_SEARCH_FUNCTIONS)) { + queryBuilder = in.readOptionalNamedWriteable(QueryBuilder.class); + } + return new MatchOperator(source, field, query, queryBuilder); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + source().writeTo(out); + out.writeNamedWriteable(field()); + out.writeNamedWriteable(query()); + if (out.getTransportVersion().onOrAfter(TransportVersions.ESQL_QUERY_BUILDER_IN_SEARCH_FUNCTIONS)) { + out.writeOptionalNamedWriteable(queryBuilder()); + } + } + @Override protected NodeInfo info() { return NodeInfo.create(this, MatchOperator::new, field(), query()); From bb8e2908ab7b1ba0e2d2437caf5f382081136ed1 Mon Sep 17 00:00:00 2001 From: carlosdelest Date: Fri, 24 Jan 2025 22:14:00 +0100 Subject: [PATCH 2/2] Fix tests --- .../esql/expression/function/fulltext/FullTextFunction.java | 2 +- .../elasticsearch/xpack/esql/analysis/AnalyzerTests.java | 4 ++-- .../xpack/esql/parser/StatementParserTests.java | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/FullTextFunction.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/FullTextFunction.java index d2e2135a4bf83..d8cd630c75431 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/FullTextFunction.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/FullTextFunction.java @@ -205,7 +205,7 @@ private static void checkFullTextQueryFunctions(LogicalPlan plan, Failures failu checkCommandsBeforeExpression( plan, condition, - Match.class, + AbstractMatchFullTextFunction.class, lp -> (lp instanceof Limit == false) && (lp instanceof Aggregate == false), m -> "[" + m.functionName() + "] " + m.functionType(), failures diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/AnalyzerTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/AnalyzerTests.java index 169ef9dd26f04..dd31d8145ebf4 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/AnalyzerTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/AnalyzerTests.java @@ -41,7 +41,7 @@ import org.elasticsearch.xpack.esql.expression.function.aggregate.Count; import org.elasticsearch.xpack.esql.expression.function.aggregate.Max; import org.elasticsearch.xpack.esql.expression.function.aggregate.Min; -import org.elasticsearch.xpack.esql.expression.function.fulltext.Match; +import org.elasticsearch.xpack.esql.expression.function.fulltext.MatchOperator; import org.elasticsearch.xpack.esql.expression.function.scalar.map.LogWithBaseInMap; import org.elasticsearch.xpack.esql.index.EsIndex; import org.elasticsearch.xpack.esql.index.IndexResolution; @@ -2584,7 +2584,7 @@ public void testFromEnrichAndMatchColonUsage() { """, "mapping-default.json"); var limit = as(plan, Limit.class); var filter = as(limit.child(), Filter.class); - var match = as(filter.condition(), Match.class); + var match = as(filter.condition(), MatchOperator.class); var enrich = as(filter.child(), Enrich.class); assertEquals(enrich.mode(), Enrich.Mode.ANY); assertEquals(enrich.policy().getMatchField(), "language_code"); diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java index 9c93557e022f7..10202d0cf9163 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java @@ -28,7 +28,7 @@ import org.elasticsearch.xpack.esql.expression.UnresolvedNamePattern; import org.elasticsearch.xpack.esql.expression.function.UnresolvedFunction; import org.elasticsearch.xpack.esql.expression.function.aggregate.FilteredExpression; -import org.elasticsearch.xpack.esql.expression.function.fulltext.Match; +import org.elasticsearch.xpack.esql.expression.function.fulltext.MatchOperator; import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToInteger; import org.elasticsearch.xpack.esql.expression.function.scalar.string.RLike; import org.elasticsearch.xpack.esql.expression.function.scalar.string.WildcardLike; @@ -2316,7 +2316,7 @@ public void testMetricWithGroupKeyAsAgg() { public void testMatchOperatorConstantQueryString() { var plan = statement("FROM test | WHERE field:\"value\""); var filter = as(plan, Filter.class); - var match = (Match) filter.condition(); + var match = (MatchOperator) filter.condition(); var matchField = (UnresolvedAttribute) match.field(); assertThat(matchField.name(), equalTo("field")); assertThat(match.query().fold(FoldContext.small()), equalTo("value")); @@ -2360,7 +2360,7 @@ public void testMatchFunctionFieldCasting() { public void testMatchOperatorFieldCasting() { var plan = statement("FROM test | WHERE field::int : \"value\""); var filter = as(plan, Filter.class); - var match = (Match) filter.condition(); + var match = (MatchOperator) filter.condition(); var toInteger = (ToInteger) match.field(); var matchField = (UnresolvedAttribute) toInteger.field(); assertThat(matchField.name(), equalTo("field"));