Skip to content

Commit e8c325b

Browse files
author
elasticsearchmachine
committed
Fix script translation
1 parent 33bc8f3 commit e8c325b

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/vector/Hamming.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1212
import org.elasticsearch.common.io.stream.StreamInput;
1313
import org.elasticsearch.xpack.esql.core.expression.Expression;
14+
import org.elasticsearch.xpack.esql.core.expression.Literal;
1415
import org.elasticsearch.xpack.esql.core.expression.function.scalar.BinaryScalarFunction;
1516
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1617
import org.elasticsearch.xpack.esql.core.tree.Source;
@@ -21,6 +22,7 @@
2122
import org.elasticsearch.xpack.esql.expression.function.Param;
2223

2324
import java.io.IOException;
25+
import java.util.List;
2426

2527
public class Hamming extends VectorSimilarityFunction {
2628

@@ -90,4 +92,14 @@ public static float calculateSimilarity(float[] leftScratch, float[] rightScratc
9092
protected String scriptFunctionName() {
9193
return "hamming";
9294
}
95+
96+
@Override
97+
protected String queryVectorAsScript(Expression queryVector) {
98+
// Need to convert to bytes so the appropriate method is called from script
99+
@SuppressWarnings("unchecked")
100+
List<Number> vector = (List<Number>) ((Literal) queryVector).value();
101+
List<Integer> intList = vector.stream().map(Number::intValue).toList();
102+
103+
return intList.toString();
104+
}
93105
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/vector/Magnitude.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.compute.operator.DriverContext;
1919
import org.elasticsearch.compute.operator.EvalOperator;
2020
import org.elasticsearch.xpack.esql.core.expression.Expression;
21+
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
2122
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
2223
import org.elasticsearch.xpack.esql.core.expression.TypeResolutions;
2324
import org.elasticsearch.xpack.esql.core.expression.function.scalar.UnaryScalarFunction;
@@ -134,7 +135,12 @@ public String toString() {
134135

135136
@Override
136137
public PushableOptions pushableOptions() {
137-
return PushableOptions.PREFERRED;
138+
return field() instanceof FieldAttribute ? PushableOptions.PREFERRED : PushableOptions.NOT_SUPPORTED;
139+
}
140+
141+
@Override
142+
public String asScript() {
143+
return "doc['" + ((FieldAttribute) field()).name() + "'].magnitude";
138144
}
139145

140146
private record ScalarEvaluator(

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/vector/VectorSimilarityFunction.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,19 @@ public PushableOptions pushableOptions() {
8989
// Can only push if one of the arguments is a literal, and the other a field name
9090
return left().foldable() && left().dataType() != NULL && right() instanceof FieldAttribute
9191
|| right().foldable() && right().dataType() != NULL && left() instanceof FieldAttribute
92-
? PushableOptions.PREFERRED
93-
: PushableOptions.NOT_SUPPORTED;
92+
? PushableOptions.PREFERRED
93+
: PushableOptions.NOT_SUPPORTED;
9494
}
9595

9696
@Override
9797
public final String asScript() {
98-
String queryVector = left().foldable() ? left().asScript() : right().asScript();
99-
String fieldName = left().foldable() ? ((FieldAttribute) right()).name() : ((FieldAttribute) left()).name();
100-
return "return doc['" + fieldName + "'].size() == 0 ? 0 : " + scriptFunctionName() + "(" + queryVector + ", '" + fieldName + "')";
98+
String queryVector = left().foldable() ? queryVectorAsScript(left()) : queryVectorAsScript(right());
99+
String fieldName = ((FieldAttribute) (left().foldable() ? right() : left())).name();
100+
return "doc['" + fieldName + "'].size() == 0 ? 0 : " + scriptFunctionName() + "(" + queryVector + ", '" + fieldName + "')";
101+
}
102+
103+
protected String queryVectorAsScript(Expression queryVector) {
104+
return queryVector.asScript();
101105
}
102106

103107
protected abstract String scriptFunctionName();

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/physical/local/PushTopNToSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ && canPushLimit(topNExec, plannerSettings)) {
220220
Alias expressionAlias = (Alias) pushableExpressions.get(resolvedAttribute.id());
221221

222222
// Create a script from the expression
223-
String scriptText = expressionAlias.child().asScript();
223+
String scriptText = "return " + expressionAlias.child().asScript();
224224
Script script = new Script(scriptText);
225225

226226
// Create a script sort that computes the value

0 commit comments

Comments
 (0)