diff --git a/server/src/main/java/org/elasticsearch/common/lucene/BytesRefs.java b/server/src/main/java/org/elasticsearch/common/lucene/BytesRefs.java index ed88c3a5a9c91..98b44710564d8 100644 --- a/server/src/main/java/org/elasticsearch/common/lucene/BytesRefs.java +++ b/server/src/main/java/org/elasticsearch/common/lucene/BytesRefs.java @@ -23,8 +23,8 @@ public static String toString(Object value) { if (value == null) { return null; } - if (value instanceof BytesRef) { - return ((BytesRef) value).utf8ToString(); + if (value instanceof BytesRef bytesRef) { + return bytesRef.utf8ToString(); } return value.toString(); } @@ -36,8 +36,8 @@ public static BytesRef toBytesRef(Object value) { if (value == null) { return null; } - if (value instanceof BytesRef) { - return (BytesRef) value; + if (value instanceof BytesRef bytesRef) { + return bytesRef; } return new BytesRef(value.toString()); } diff --git a/x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/Foldables.java b/x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/Foldables.java index 233113c3fe1b8..4052d41d7eb5f 100644 --- a/x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/Foldables.java +++ b/x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/Foldables.java @@ -6,6 +6,7 @@ */ package org.elasticsearch.xpack.esql.core.expression; +import org.apache.lucene.util.BytesRef; import org.elasticsearch.xpack.esql.core.QlIllegalArgumentException; public abstract class Foldables { @@ -16,4 +17,11 @@ public static Object valueOf(FoldContext ctx, Expression e) { } throw new QlIllegalArgumentException("Cannot determine value for {}", e); } + + public static String stringLiteralValueOf(Expression expression, String message) { + if (expression instanceof Literal literal && literal.value() instanceof BytesRef bytesRef) { + return bytesRef.utf8ToString(); + } + throw new QlIllegalArgumentException(message); + } } diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/enrich/EnrichPolicyResolver.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/enrich/EnrichPolicyResolver.java index d0a0843a234b0..ee68de83ad67e 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/enrich/EnrichPolicyResolver.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/enrich/EnrichPolicyResolver.java @@ -59,6 +59,7 @@ import java.util.Set; import java.util.stream.Collectors; +import static org.elasticsearch.xpack.esql.core.expression.Foldables.stringLiteralValueOf; import static org.elasticsearch.xpack.esql.session.EsqlCCSUtils.markClusterWithFinalStateAndNoShards; /** @@ -101,7 +102,9 @@ public EnrichPolicyResolver( } public record UnresolvedPolicy(String name, Enrich.Mode mode) { - + public static UnresolvedPolicy from(Enrich e) { + return new UnresolvedPolicy(stringLiteralValueOf(e.policyName(), "Enrich policy must be a constant string"), e.mode()); + } } /** diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/EsqlSession.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/EsqlSession.java index 34ece5661c8d7..f39e5d0cba0a1 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/EsqlSession.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/EsqlSession.java @@ -15,7 +15,6 @@ import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.action.support.SubscribableListener; import org.elasticsearch.common.collect.Iterators; -import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.compute.data.Block; import org.elasticsearch.compute.data.BlockUtils; import org.elasticsearch.compute.data.Page; @@ -46,7 +45,6 @@ import org.elasticsearch.xpack.esql.analysis.PreAnalyzer; import org.elasticsearch.xpack.esql.analysis.Verifier; import org.elasticsearch.xpack.esql.core.expression.Attribute; -import org.elasticsearch.xpack.esql.core.expression.FoldContext; import org.elasticsearch.xpack.esql.core.expression.ReferenceAttribute; import org.elasticsearch.xpack.esql.core.tree.Source; import org.elasticsearch.xpack.esql.core.type.DataType; @@ -88,6 +86,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static java.util.stream.Collectors.toSet; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.xpack.esql.core.tree.Source.EMPTY; import static org.elasticsearch.xpack.esql.plan.logical.join.InlineJoin.firstSubPlan; @@ -382,14 +381,7 @@ public void analyzedPlan( }; PreAnalyzer.PreAnalysis preAnalysis = preAnalyzer.preAnalyze(parsed); - var unresolvedPolicies = preAnalysis.enriches.stream() - .map( - e -> new EnrichPolicyResolver.UnresolvedPolicy( - BytesRefs.toString(e.policyName().fold(FoldContext.small() /* TODO remove me*/)), - e.mode() - ) - ) - .collect(Collectors.toSet()); + var unresolvedPolicies = preAnalysis.enriches.stream().map(EnrichPolicyResolver.UnresolvedPolicy::from).collect(toSet()); EsqlCCSUtils.initCrossClusterState(indicesExpressionGrouper, verifier.licenseState(), preAnalysis.indices, executionInfo); @@ -587,7 +579,7 @@ private IndexResolution checkSingleIndex( ) { // If all indices resolve to the same name, we can use that for BWC // Older clusters only can handle one name in LOOKUP JOIN - var localIndexNames = indexNames.stream().map(n -> RemoteClusterAware.splitIndexName(n)[1]).collect(Collectors.toSet()); + var localIndexNames = indexNames.stream().map(n -> RemoteClusterAware.splitIndexName(n)[1]).collect(toSet()); if (localIndexNames.size() == 1) { String indexName = localIndexNames.iterator().next(); EsIndex newIndex = new EsIndex(index, lookupIndexResolution.get().mapping(), Map.of(indexName, IndexMode.LOOKUP));