Skip to content

Commit 537919b

Browse files
committed
WIP: Migrate fold to Literal.value() in translatable functions
1 parent 734dd07 commit 537919b

File tree

14 files changed

+73
-23
lines changed

14 files changed

+73
-23
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ public static List<Object> fold(FoldContext ctx, List<? extends Expression> exps
116116
return folded;
117117
}
118118

119+
public static boolean literals(List<? extends Expression> exps) {
120+
for (Expression exp : exps) {
121+
if (exp instanceof Literal == false) {
122+
return false;
123+
}
124+
}
125+
return true;
126+
}
127+
119128
public static AttributeSet references(List<? extends Expression> exps) {
120129
if (exps.isEmpty()) {
121130
return AttributeSet.EMPTY;

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
@@ -16,4 +16,12 @@ public static Object valueOf(FoldContext ctx, Expression e) {
1616
}
1717
throw new QlIllegalArgumentException("Cannot determine value for {}", e);
1818
}
19+
20+
public static Object valueOfLiteral(Expression e) {
21+
if (e instanceof Literal literal) {
22+
return literal.value();
23+
}
24+
25+
throw new QlIllegalArgumentException("Expected a Literal but received [" + e + "]");
26+
}
1927
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/FullTextFunction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.xpack.esql.common.Failures;
2020
import org.elasticsearch.xpack.esql.core.expression.Expression;
2121
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
22+
import org.elasticsearch.xpack.esql.core.expression.Foldables;
2223
import org.elasticsearch.xpack.esql.core.expression.Nullability;
2324
import org.elasticsearch.xpack.esql.core.expression.TypeResolutions;
2425
import org.elasticsearch.xpack.esql.core.expression.function.Function;
@@ -114,7 +115,7 @@ public Expression query() {
114115
* @return query expression as an object
115116
*/
116117
public Object queryAsObject() {
117-
Object queryAsObject = query().fold(FoldContext.small() /* TODO remove me */);
118+
Object queryAsObject = Foldables.valueOfLiteral(query());
118119
return BytesRefs.toString(queryAsObject);
119120
}
120121

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/Match.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.xpack.esql.core.expression.Expression;
2323
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
2424
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
25+
import org.elasticsearch.xpack.esql.core.expression.Foldables;
2526
import org.elasticsearch.xpack.esql.core.expression.Literal;
2627
import org.elasticsearch.xpack.esql.core.expression.MapExpression;
2728
import org.elasticsearch.xpack.esql.core.querydsl.query.Query;
@@ -453,7 +454,7 @@ public BiConsumer<LogicalPlan, Failures> postAnalysisPlanVerification() {
453454

454455
@Override
455456
public Object queryAsObject() {
456-
Object queryAsObject = query().fold(FoldContext.small() /* TODO remove me */);
457+
Object queryAsObject = Foldables.valueOfLiteral(query());
457458

458459
// Convert BytesRef to string for string-based values
459460
if (queryAsObject instanceof BytesRef bytesRef) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.xpack.esql.core.expression.Expression;
2020
import org.elasticsearch.xpack.esql.core.expression.Expressions;
2121
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
22+
import org.elasticsearch.xpack.esql.core.expression.Foldables;
2223
import org.elasticsearch.xpack.esql.core.querydsl.query.Query;
2324
import org.elasticsearch.xpack.esql.core.querydsl.query.TermsQuery;
2425
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
@@ -35,6 +36,7 @@
3536
import org.elasticsearch.xpack.esql.planner.TranslatorHandler;
3637

3738
import java.io.IOException;
39+
import java.util.ArrayList;
3840
import java.util.Arrays;
3941
import java.util.LinkedHashSet;
4042
import java.util.List;
@@ -189,7 +191,11 @@ public Query asQuery(TranslatorHandler handler) {
189191
Check.isTrue(Expressions.foldable(matches), "Expected foldable matches, but got [{}]", matches);
190192

191193
String targetFieldName = handler.nameOf(fa.exactAttribute());
192-
Set<Object> set = new LinkedHashSet<>(Expressions.fold(FoldContext.small() /* TODO remove me */, matches));
194+
195+
Set<Object> set = new LinkedHashSet<>(matches.size());
196+
for (Expression exp : matches) {
197+
set.add(Foldables.valueOfLiteral(exp));
198+
}
193199

194200
return new TermsQuery(source(), targetFieldName, set);
195201
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.xpack.esql.core.expression.Expression;
2828
import org.elasticsearch.xpack.esql.core.expression.Expressions;
2929
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
30+
import org.elasticsearch.xpack.esql.core.expression.Foldables;
3031
import org.elasticsearch.xpack.esql.core.expression.TypedAttribute;
3132
import org.elasticsearch.xpack.esql.core.querydsl.query.Query;
3233
import org.elasticsearch.xpack.esql.core.tree.Source;
@@ -210,7 +211,10 @@ private Query translate(TranslatorHandler handler, Expression spatialExpression,
210211
String name = handler.nameOf(attribute);
211212

212213
try {
213-
Geometry shape = SpatialRelatesUtils.makeGeometryFromLiteral(FoldContext.small() /* TODO remove me */, constantExpression);
214+
Geometry shape = SpatialRelatesUtils.makeGeometryFromLiteralValue(
215+
Foldables.valueOfLiteral(constantExpression),
216+
constantExpression.dataType()
217+
);
214218
return new SpatialRelatesQuery(source(), name, queryRelation(), shape, attribute.dataType());
215219
} catch (IllegalArgumentException e) {
216220
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public static Geometry makeGeometryFromLiteral(FoldContext ctx, Expression expr)
175175
return makeGeometryFromLiteralValue(valueOf(ctx, expr), expr.dataType());
176176
}
177177

178-
private static Geometry makeGeometryFromLiteralValue(Object value, DataType dataType) {
178+
public static Geometry makeGeometryFromLiteralValue(Object value, DataType dataType) {
179179
if (value instanceof BytesRef bytesRef) {
180180
// Single value expression
181181
return SpatialCoordinateTypes.UNSPECIFIED.wkbToGeometry(bytesRef);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.elasticsearch.xpack.esql.core.expression.Expression;
2020
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
2121
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
22+
import org.elasticsearch.xpack.esql.core.expression.Foldables;
23+
import org.elasticsearch.xpack.esql.core.expression.Literal;
2224
import org.elasticsearch.xpack.esql.core.querydsl.query.Query;
2325
import org.elasticsearch.xpack.esql.core.querydsl.query.WildcardQuery;
2426
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
@@ -140,16 +142,14 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
140142

141143
@Override
142144
public boolean translatable(LucenePushdownPredicates pushdownPredicates) {
143-
return pushdownPredicates.isPushableAttribute(str) && suffix.foldable();
145+
return pushdownPredicates.isPushableAttribute(str) && suffix instanceof Literal;
144146
}
145147

146148
@Override
147149
public Query asQuery(TranslatorHandler handler) {
148150
LucenePushdownPredicates.checkIsPushableAttribute(str);
149151
var fieldName = handler.nameOf(str instanceof FieldAttribute fa ? fa.exactAttribute() : str);
150-
151-
// TODO: Get the real FoldContext here
152-
var wildcardQuery = "*" + QueryParser.escape(BytesRefs.toString(suffix.fold(FoldContext.small())));
152+
var wildcardQuery = "*" + QueryParser.escape(BytesRefs.toString(Foldables.valueOfLiteral(suffix)));
153153

154154
return new WildcardQuery(source(), fieldName, wildcardQuery);
155155
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.elasticsearch.xpack.esql.core.expression.Expression;
2020
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
2121
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
22+
import org.elasticsearch.xpack.esql.core.expression.Foldables;
23+
import org.elasticsearch.xpack.esql.core.expression.Literal;
2224
import org.elasticsearch.xpack.esql.core.querydsl.query.Query;
2325
import org.elasticsearch.xpack.esql.core.querydsl.query.WildcardQuery;
2426
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
@@ -137,16 +139,14 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
137139

138140
@Override
139141
public boolean translatable(LucenePushdownPredicates pushdownPredicates) {
140-
return pushdownPredicates.isPushableAttribute(str) && prefix.foldable();
142+
return pushdownPredicates.isPushableAttribute(str) && prefix instanceof Literal;
141143
}
142144

143145
@Override
144146
public Query asQuery(TranslatorHandler handler) {
145147
LucenePushdownPredicates.checkIsPushableAttribute(str);
146148
var fieldName = handler.nameOf(str instanceof FieldAttribute fa ? fa.exactAttribute() : str);
147-
148-
// TODO: Get the real FoldContext here
149-
var wildcardQuery = QueryParser.escape(BytesRefs.toString(prefix.fold(FoldContext.small()))) + "*";
149+
var wildcardQuery = QueryParser.escape(BytesRefs.toString(Foldables.valueOfLiteral(prefix))) + "*";
150150

151151
return new WildcardQuery(source(), fieldName, wildcardQuery);
152152
}

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

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

3232
import static java.util.Arrays.asList;
3333
import static org.elasticsearch.xpack.esql.core.expression.Foldables.valueOf;
34+
import static org.elasticsearch.xpack.esql.core.expression.Foldables.valueOfLiteral;
3435
import static org.elasticsearch.xpack.esql.core.type.DataType.IP;
3536
import static org.elasticsearch.xpack.esql.core.type.DataType.UNSIGNED_LONG;
3637
import static org.elasticsearch.xpack.esql.core.type.DataType.VERSION;
@@ -196,7 +197,7 @@ public boolean equals(Object obj) {
196197

197198
@Override
198199
public boolean translatable(LucenePushdownPredicates pushdownPredicates) {
199-
return pushdownPredicates.isPushableAttribute(value) && lower.foldable() && upper.foldable();
200+
return pushdownPredicates.isPushableAttribute(value) && lower instanceof Literal && upper instanceof Literal;
200201
}
201202

202203
@Override
@@ -205,8 +206,8 @@ public Query asQuery(TranslatorHandler handler) {
205206
}
206207

207208
private RangeQuery translate(TranslatorHandler handler) {
208-
Object l = valueOf(FoldContext.small() /* TODO remove me */, lower);
209-
Object u = valueOf(FoldContext.small() /* TODO remove me */, upper);
209+
Object l = valueOfLiteral(lower);
210+
Object u = valueOfLiteral(upper);
210211
String format = null;
211212

212213
DataType dataType = value.dataType();

0 commit comments

Comments
 (0)