Skip to content

Commit e837c57

Browse files
committed
use common component
1 parent be9bd3a commit e837c57

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
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: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.elasticsearch.cluster.service.ClusterService;
1818
import org.elasticsearch.common.io.stream.StreamInput;
1919
import org.elasticsearch.common.io.stream.StreamOutput;
20-
import org.elasticsearch.common.lucene.BytesRefs;
2120
import org.elasticsearch.common.util.CollectionUtils;
2221
import org.elasticsearch.common.util.Maps;
2322
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
@@ -38,11 +37,8 @@
3837
import org.elasticsearch.xpack.core.ClientHelper;
3938
import org.elasticsearch.xpack.core.enrich.EnrichMetadata;
4039
import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
41-
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
4240
import org.elasticsearch.xpack.esql.action.EsqlExecutionInfo;
4341
import org.elasticsearch.xpack.esql.analysis.EnrichResolution;
44-
import org.elasticsearch.xpack.esql.core.expression.Expression;
45-
import org.elasticsearch.xpack.esql.core.expression.Literal;
4642
import org.elasticsearch.xpack.esql.core.type.DataType;
4743
import org.elasticsearch.xpack.esql.core.type.EsField;
4844
import org.elasticsearch.xpack.esql.core.util.StringUtils;
@@ -63,7 +59,7 @@
6359
import java.util.Set;
6460
import java.util.stream.Collectors;
6561

66-
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
62+
import static org.elasticsearch.xpack.esql.core.expression.Foldables.stringLiteralValueOf;
6763
import static org.elasticsearch.xpack.esql.session.EsqlCCSUtils.markClusterWithFinalStateAndNoShards;
6864

6965
/**
@@ -107,14 +103,7 @@ public EnrichPolicyResolver(
107103

108104
public record UnresolvedPolicy(String name, Enrich.Mode mode) {
109105
public static UnresolvedPolicy from(Enrich e) {
110-
return new UnresolvedPolicy(asString(e.policyName()), e.mode());
111-
}
112-
113-
private static String asString(Expression expression) {
114-
if (expression instanceof Literal literal) {
115-
return BytesRefs.toString(literal.value());
116-
}
117-
throw new EsqlIllegalArgumentException(format(null, "Enrich policy must be a constant string"));
106+
return new UnresolvedPolicy(stringLiteralValueOf(e.policyName(), "Enrich policy must be a constant string"), e.mode());
118107
}
119108
}
120109

0 commit comments

Comments
 (0)