Skip to content

Commit 2353ec6

Browse files
authored
Remove unnecessary fold in enrich policy name (#132239)
1 parent 78b63ff commit 2353ec6

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

server/src/main/java/org/elasticsearch/common/lucene/BytesRefs.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public static String toString(Object value) {
2323
if (value == null) {
2424
return null;
2525
}
26-
if (value instanceof BytesRef) {
27-
return ((BytesRef) value).utf8ToString();
26+
if (value instanceof BytesRef bytesRef) {
27+
return bytesRef.utf8ToString();
2828
}
2929
return value.toString();
3030
}
@@ -36,8 +36,8 @@ public static BytesRef toBytesRef(Object value) {
3636
if (value == null) {
3737
return null;
3838
}
39-
if (value instanceof BytesRef) {
40-
return (BytesRef) value;
39+
if (value instanceof BytesRef bytesRef) {
40+
return bytesRef;
4141
}
4242
return new BytesRef(value.toString());
4343
}

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/Foldables.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package org.elasticsearch.xpack.esql.core.expression;
88

9+
import org.apache.lucene.util.BytesRef;
910
import org.elasticsearch.xpack.esql.core.QlIllegalArgumentException;
1011

1112
public abstract class Foldables {
@@ -16,4 +17,11 @@ public static Object valueOf(FoldContext ctx, Expression e) {
1617
}
1718
throw new QlIllegalArgumentException("Cannot determine value for {}", e);
1819
}
20+
21+
public static String stringLiteralValueOf(Expression expression, String message) {
22+
if (expression instanceof Literal literal && literal.value() instanceof BytesRef bytesRef) {
23+
return bytesRef.utf8ToString();
24+
}
25+
throw new QlIllegalArgumentException(message);
26+
}
1927
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/enrich/EnrichPolicyResolver.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import java.util.Set;
6060
import java.util.stream.Collectors;
6161

62+
import static org.elasticsearch.xpack.esql.core.expression.Foldables.stringLiteralValueOf;
6263
import static org.elasticsearch.xpack.esql.session.EsqlCCSUtils.markClusterWithFinalStateAndNoShards;
6364

6465
/**
@@ -101,7 +102,9 @@ public EnrichPolicyResolver(
101102
}
102103

103104
public record UnresolvedPolicy(String name, Enrich.Mode mode) {
104-
105+
public static UnresolvedPolicy from(Enrich e) {
106+
return new UnresolvedPolicy(stringLiteralValueOf(e.policyName(), "Enrich policy must be a constant string"), e.mode());
107+
}
105108
}
106109

107110
/**

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/EsqlSession.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.action.search.ShardSearchFailure;
1616
import org.elasticsearch.action.support.SubscribableListener;
1717
import org.elasticsearch.common.collect.Iterators;
18-
import org.elasticsearch.common.lucene.BytesRefs;
1918
import org.elasticsearch.compute.data.Block;
2019
import org.elasticsearch.compute.data.BlockUtils;
2120
import org.elasticsearch.compute.data.Page;
@@ -46,7 +45,6 @@
4645
import org.elasticsearch.xpack.esql.analysis.PreAnalyzer;
4746
import org.elasticsearch.xpack.esql.analysis.Verifier;
4847
import org.elasticsearch.xpack.esql.core.expression.Attribute;
49-
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
5048
import org.elasticsearch.xpack.esql.core.expression.ReferenceAttribute;
5149
import org.elasticsearch.xpack.esql.core.tree.Source;
5250
import org.elasticsearch.xpack.esql.core.type.DataType;
@@ -88,6 +86,7 @@
8886
import java.util.stream.Collectors;
8987
import java.util.stream.Stream;
9088

89+
import static java.util.stream.Collectors.toSet;
9190
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
9291
import static org.elasticsearch.xpack.esql.core.tree.Source.EMPTY;
9392
import static org.elasticsearch.xpack.esql.plan.logical.join.InlineJoin.firstSubPlan;
@@ -382,14 +381,7 @@ public void analyzedPlan(
382381
};
383382

384383
PreAnalyzer.PreAnalysis preAnalysis = preAnalyzer.preAnalyze(parsed);
385-
var unresolvedPolicies = preAnalysis.enriches.stream()
386-
.map(
387-
e -> new EnrichPolicyResolver.UnresolvedPolicy(
388-
BytesRefs.toString(e.policyName().fold(FoldContext.small() /* TODO remove me*/)),
389-
e.mode()
390-
)
391-
)
392-
.collect(Collectors.toSet());
384+
var unresolvedPolicies = preAnalysis.enriches.stream().map(EnrichPolicyResolver.UnresolvedPolicy::from).collect(toSet());
393385

394386
EsqlCCSUtils.initCrossClusterState(indicesExpressionGrouper, verifier.licenseState(), preAnalysis.indices, executionInfo);
395387

@@ -587,7 +579,7 @@ private IndexResolution checkSingleIndex(
587579
) {
588580
// If all indices resolve to the same name, we can use that for BWC
589581
// Older clusters only can handle one name in LOOKUP JOIN
590-
var localIndexNames = indexNames.stream().map(n -> RemoteClusterAware.splitIndexName(n)[1]).collect(Collectors.toSet());
582+
var localIndexNames = indexNames.stream().map(n -> RemoteClusterAware.splitIndexName(n)[1]).collect(toSet());
591583
if (localIndexNames.size() == 1) {
592584
String indexName = localIndexNames.iterator().next();
593585
EsIndex newIndex = new EsIndex(index, lookupIndexResolution.get().mapping(), Map.of(indexName, IndexMode.LOOKUP));

0 commit comments

Comments
 (0)