Skip to content

Commit 1b46d3c

Browse files
Add support for more functions
1 parent cddf15a commit 1b46d3c

File tree

9 files changed

+58
-17
lines changed

9 files changed

+58
-17
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/SpatialRelatesFunction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.elasticsearch.xpack.esql.core.QlIllegalArgumentException;
2727
import org.elasticsearch.xpack.esql.core.expression.Expression;
2828
import org.elasticsearch.xpack.esql.core.expression.Expressions;
29-
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
3029
import org.elasticsearch.xpack.esql.core.expression.TypedAttribute;
3130
import org.elasticsearch.xpack.esql.core.querydsl.query.Query;
3231
import org.elasticsearch.xpack.esql.core.tree.Source;
@@ -213,7 +212,7 @@ private Query translate(TranslatorHandler handler, Expression spatialExpression,
213212
String name = handler.nameOf(attribute);
214213

215214
try {
216-
Geometry shape = SpatialRelatesUtils.makeGeometryFromLiteral(FoldContext.small() /* TODO remove me */, constantExpression);
215+
Geometry shape = SpatialRelatesUtils.makeGeometryFromLiteral(constantExpression);
217216
return new SpatialRelatesQuery(source(), name, queryRelation(), shape, attribute.dataType());
218217
} catch (IllegalArgumentException e) {
219218
throw new QlIllegalArgumentException(e.getMessage(), e);

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/SpatialRelatesUtils.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.List;
3939
import java.util.function.Function;
4040

41+
import static org.elasticsearch.xpack.esql.core.expression.Foldables.literalValueOf;
4142
import static org.elasticsearch.xpack.esql.core.expression.Foldables.valueOf;
4243

4344
public class SpatialRelatesUtils {
@@ -168,13 +169,20 @@ private static Geometry asGeometry(BytesRefBlock valueBlock, int position) {
168169
}
169170

170171
/**
171-
* This function is used in two places, when evaluating a spatial constant in the SpatialRelatesFunction, as well as when
172-
* we do lucene-pushdown of spatial functions.
172+
* This function is used when evaluating a spatial constant in the SpatialRelatesFunction
173173
*/
174174
public static Geometry makeGeometryFromLiteral(FoldContext ctx, Expression expr) {
175175
return makeGeometryFromLiteralValue(valueOf(ctx, expr), expr.dataType());
176176
}
177177

178+
/**
179+
* This function is used when we do lucene-pushdown of spatial functions.
180+
* The expression is expected to be folded already and a literal
181+
*/
182+
public static Geometry makeGeometryFromLiteral(Expression expr) {
183+
return makeGeometryFromLiteralValue(literalValueOf(expr), expr.dataType());
184+
}
185+
178186
private static Geometry makeGeometryFromLiteralValue(Object value, DataType dataType) {
179187
if (value instanceof BytesRef bytesRef) {
180188
// Single value expression

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/predicate/Range.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import java.util.Objects;
3434

3535
import static java.util.Arrays.asList;
36-
import static org.elasticsearch.xpack.esql.core.expression.Foldables.valueOf;
36+
import static org.elasticsearch.xpack.esql.core.expression.Foldables.literalValueOf;
3737
import static org.elasticsearch.xpack.esql.core.type.DataType.DATE_NANOS;
3838
import static org.elasticsearch.xpack.esql.core.type.DataType.IP;
3939
import static org.elasticsearch.xpack.esql.core.type.DataType.UNSIGNED_LONG;
@@ -232,8 +232,8 @@ public Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHand
232232
}
233233

234234
private RangeQuery translate(TranslatorHandler handler) {
235-
Object l = valueOf(FoldContext.small() /* TODO remove me */, lower);
236-
Object u = valueOf(FoldContext.small() /* TODO remove me */, upper);
235+
Object l = literalValueOf(lower);
236+
Object u = literalValueOf(upper);
237237
String format = null;
238238

239239
DataType dataType = value.dataType();

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/EsqlBinaryComparison.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
import java.util.Map;
5252

5353
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
54-
import static org.elasticsearch.xpack.esql.core.expression.Foldables.valueOf;
54+
import static org.elasticsearch.xpack.esql.core.expression.Foldables.extractLiteralOrReturnSelf;
55+
import static org.elasticsearch.xpack.esql.core.expression.Foldables.literalValueOf;
5556
import static org.elasticsearch.xpack.esql.core.type.DataType.DATETIME;
5657
import static org.elasticsearch.xpack.esql.core.type.DataType.DATE_NANOS;
5758
import static org.elasticsearch.xpack.esql.core.type.DataType.IP;
@@ -378,7 +379,7 @@ public Expression singleValueField() {
378379
private Query translate(TranslatorHandler handler) {
379380
TypedAttribute attribute = LucenePushdownPredicates.checkIsPushableAttribute(left());
380381
String name = handler.nameOf(attribute);
381-
Object value = valueOf(FoldContext.small() /* TODO remove me */, right());
382+
Object value = extractLiteralOrReturnSelf(right());
382383
String format = null;
383384
boolean isDateLiteralComparison = false;
384385

@@ -478,7 +479,7 @@ private Query translateOutOfRangeComparisons() {
478479
if ((left() instanceof FieldAttribute) == false || left().dataType().isNumeric() == false) {
479480
return null;
480481
}
481-
Object value = valueOf(FoldContext.small() /* TODO remove me */, right());
482+
Object value = literalValueOf(right());
482483

483484
// Comparisons with multi-values always return null in ESQL.
484485
if (value instanceof List<?>) {

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/InsensitiveEquals.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
import java.io.IOException;
3333

34-
import static org.elasticsearch.xpack.esql.core.expression.Foldables.valueOf;
34+
import static org.elasticsearch.xpack.esql.core.expression.Foldables.literalValueOf;
3535

3636
public class InsensitiveEquals extends InsensitiveBinaryComparison {
3737
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
@@ -125,7 +125,7 @@ private void checkInsensitiveComparison() {
125125

126126
private Query translate() {
127127
TypedAttribute attribute = LucenePushdownPredicates.checkIsPushableAttribute(left());
128-
BytesRef value = BytesRefs.toBytesRef(valueOf(FoldContext.small() /* TODO remove me */, right()));
128+
BytesRef value = BytesRefs.toBytesRef(literalValueOf(right()));
129129
String name = LucenePushdownPredicates.pushableAttributeName(attribute);
130130
return new TermQuery(source(), name, value.utf8ToString(), true);
131131
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/InsensitiveEqualsMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.util.List;
2525

26+
import static org.elasticsearch.xpack.esql.core.expression.Foldables.literalValueOf;
2627
import static org.elasticsearch.xpack.esql.evaluator.EvalMapper.toEvaluator;
2728

2829
public class InsensitiveEqualsMapper extends ExpressionMapper<InsensitiveEquals> {
@@ -44,7 +45,7 @@ public final ExpressionEvaluator.Factory map(
4445
var rightEval = toEvaluator(foldCtx, bc.right(), layout, shardContexts);
4546
if (DataType.isString(leftType)) {
4647
if (bc.right().foldable() && DataType.isString(rightType)) {
47-
BytesRef rightVal = BytesRefs.toBytesRef(bc.right().fold(FoldContext.small() /* TODO remove me */));
48+
BytesRef rightVal = BytesRefs.toBytesRef(literalValueOf(bc.right()));
4849
Automaton automaton = InsensitiveEquals.automaton(rightVal);
4950
return dvrCtx -> new InsensitiveEqualsConstantEvaluator(
5051
bc.source(),

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/Enrich.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.elasticsearch.xpack.esql.core.expression.AttributeSet;
2727
import org.elasticsearch.xpack.esql.core.expression.EmptyAttribute;
2828
import org.elasticsearch.xpack.esql.core.expression.Expression;
29-
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
3029
import org.elasticsearch.xpack.esql.core.expression.NameId;
3130
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
3231
import org.elasticsearch.xpack.esql.core.expression.ReferenceAttribute;
@@ -48,6 +47,7 @@
4847

4948
import static org.elasticsearch.xpack.esql.common.Failure.fail;
5049
import static org.elasticsearch.xpack.esql.core.expression.Expressions.asAttributes;
50+
import static org.elasticsearch.xpack.esql.core.expression.Foldables.literalValueOf;
5151
import static org.elasticsearch.xpack.esql.expression.NamedExpressions.mergeOutputAttributes;
5252

5353
public class Enrich extends UnaryPlan implements GeneratingPlan<Enrich>, PostAnalysisPlanVerificationAware, TelemetryAware, SortAgnostic {
@@ -152,7 +152,7 @@ public void writeTo(StreamOutput out) throws IOException {
152152
out.writeNamedWriteable(policyName());
153153
out.writeNamedWriteable(matchField());
154154
if (out.getTransportVersion().before(TransportVersions.V_8_13_0)) {
155-
out.writeString(BytesRefs.toString(policyName().fold(FoldContext.small() /* TODO remove me */))); // old policy name
155+
out.writeString(BytesRefs.toString(literalValueOf(policyName()))); // old policy name
156156
}
157157
policy().writeTo(out);
158158
if (out.getTransportVersion().onOrAfter(TransportVersions.V_8_13_0)) {

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/planner/mapper/MapperUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.elasticsearch.compute.aggregation.AggregatorMode;
1212
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
1313
import org.elasticsearch.xpack.esql.core.expression.Attribute;
14-
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
1514
import org.elasticsearch.xpack.esql.core.expression.MetadataAttribute;
1615
import org.elasticsearch.xpack.esql.plan.logical.Aggregate;
1716
import org.elasticsearch.xpack.esql.plan.logical.ChangePoint;
@@ -51,6 +50,8 @@
5150

5251
import java.util.List;
5352

53+
import static org.elasticsearch.xpack.esql.core.expression.Foldables.literalValueOf;
54+
5455
/**
5556
* Class for sharing code across Mappers.
5657
*/
@@ -113,7 +114,7 @@ static PhysicalPlan mapUnary(UnaryPlan p, PhysicalPlan child) {
113114
enrich.mode(),
114115
enrich.policy().getType(),
115116
enrich.matchField(),
116-
BytesRefs.toString(enrich.policyName().fold(FoldContext.small() /* TODO remove me */)),
117+
BytesRefs.toString(literalValueOf(enrich.policyName())),
117118
enrich.policy().getMatchField(),
118119
enrich.concreteIndices(),
119120
enrich.enrichFields()

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/esql/230_folding.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,3 +678,34 @@ CIDR_MATCH with non-foldable CIDRs:
678678
| WHERE CIDR_MATCH(ip, CONCAT(TO_STRING(ip), "/8"), "127.0.0.3/32") | LIMIT 5
679679
- length: { values: 3 }
680680

681+
---
682+
Employee name equals:
683+
- do:
684+
esql.query:
685+
body:
686+
query: |
687+
FROM employees
688+
| WHERE CONCAT("Alice"," Smith") == name
689+
| KEEP hire_date, salary, name
690+
| LIMIT 5
691+
- match: { columns.0.name: "hire_date" }
692+
- match: { columns.1.name: "salary" }
693+
- match: { columns.2.name: "name" }
694+
- length: { values: 1 }
695+
- match: { values.0.2: "Alice Smith" }
696+
697+
---
698+
Employee name in list:
699+
- do:
700+
esql.query:
701+
body:
702+
query: |
703+
FROM employees
704+
| WHERE name in (CONCAT("Alice"," Smith"), CONCAT("fake", " name"))
705+
| KEEP hire_date, salary, name
706+
| LIMIT 5
707+
- match: { columns.0.name: "hire_date" }
708+
- match: { columns.1.name: "salary" }
709+
- match: { columns.2.name: "name" }
710+
- length: { values: 1 }
711+
- match: { values.0.2: "Alice Smith" }

0 commit comments

Comments
 (0)