Skip to content

Commit 5a9a8c1

Browse files
committed
Add docs and examples, remove string conversion
1 parent 59782e7 commit 5a9a8c1

File tree

4 files changed

+50
-34
lines changed

4 files changed

+50
-34
lines changed

x-pack/plugin/esql/qa/testFixtures/src/main/resources/dense_vector.csv-spec

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,46 @@ id:l | new_vector:dense_vector
4545
2 | [9.0, 8.0, 7.0]
4646
3 | [0.054, 0.032, 0.012]
4747
;
48+
49+
convertIntsToDenseVector
50+
required_capability: dense_vector_field_type
51+
required_capability: to_dense_vector_function
52+
53+
// tag::to_dense_vector-ints[]
54+
row ints = [1, 2, 3]
55+
| eval vector = to_dense_vector(ints)
56+
| keep vector
57+
// end::to_dense_vector-ints[]
58+
;
59+
60+
// tag::to_dense_vector-ints-result[]
61+
vector:dense_vector
62+
[1.0, 2.0, 3.0]
63+
// end::to_dense_vector-ints-result[]
64+
;
65+
66+
convertLongsToDenseVector
67+
required_capability: dense_vector_field_type
68+
required_capability: to_dense_vector_function
69+
70+
row longs = [5013792, 2147483647, 501379200000]
71+
| eval vector = to_dense_vector(longs)
72+
| keep vector
73+
;
74+
75+
vector:dense_vector
76+
[5013792.0, 2147483647.0, 501379200000.0]
77+
;
78+
79+
convertDoublesToDenseVector
80+
required_capability: dense_vector_field_type
81+
required_capability: to_dense_vector_function
82+
83+
row doubles = [123.4, 567.8, 901.2]
84+
| eval vector = to_dense_vector(doubles)
85+
| keep vector
86+
;
87+
88+
vector:dense_vector
89+
[123.4, 567.8, 901.2]
90+
;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToDatePeriod;
7070
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToDatetime;
7171
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToDegrees;
72+
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToDenseVector;
7273
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToDouble;
7374
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToGeoPoint;
7475
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToGeoShape;
@@ -505,6 +506,7 @@ private static FunctionDefinition[][] snapshotFunctions() {
505506
def(Score.class, uni(Score::new), Score.NAME),
506507
def(Term.class, bi(Term::new), "term"),
507508
def(Knn.class, quad(Knn::new), "knn"),
509+
def(ToDenseVector.class, ToDenseVector::new, "to_dense_vector"),
508510
def(ToGeohash.class, ToGeohash::new, "to_geohash"),
509511
def(ToGeotile.class, ToGeotile::new, "to_geotile"),
510512
def(ToGeohex.class, ToGeohex::new, "to_geohex"),

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
package org.elasticsearch.xpack.esql.expression.function.scalar.convert;
99

10-
import org.apache.lucene.util.BytesRef;
1110
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1211
import org.elasticsearch.common.io.stream.StreamInput;
1312
import org.elasticsearch.compute.ann.ConvertEvaluator;
1413
import org.elasticsearch.xpack.esql.core.expression.Expression;
1514
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1615
import org.elasticsearch.xpack.esql.core.tree.Source;
1716
import org.elasticsearch.xpack.esql.core.type.DataType;
17+
import org.elasticsearch.xpack.esql.expression.function.Example;
1818
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
1919
import org.elasticsearch.xpack.esql.expression.function.Param;
2020

@@ -25,10 +25,7 @@
2525
import static org.elasticsearch.xpack.esql.core.type.DataType.DENSE_VECTOR;
2626
import static org.elasticsearch.xpack.esql.core.type.DataType.DOUBLE;
2727
import static org.elasticsearch.xpack.esql.core.type.DataType.INTEGER;
28-
import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD;
2928
import static org.elasticsearch.xpack.esql.core.type.DataType.LONG;
30-
import static org.elasticsearch.xpack.esql.core.type.DataType.TEXT;
31-
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.stringToDouble;
3229

3330
public class ToDenseVector extends AbstractConvertFunction {
3431
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
@@ -39,23 +36,22 @@ public class ToDenseVector extends AbstractConvertFunction {
3936

4037
private static final Map<DataType, BuildFactory> EVALUATORS = Map.ofEntries(
4138
Map.entry(DENSE_VECTOR, (source, fieldEval) -> fieldEval),
42-
Map.entry(KEYWORD, ToDenseVectorFromStringEvaluator.Factory::new),
43-
Map.entry(TEXT, ToDenseVectorFromStringEvaluator.Factory::new),
4439
Map.entry(LONG, ToDenseVectorFromLongEvaluator.Factory::new),
4540
Map.entry(INTEGER, ToDenseVectorFromIntEvaluator.Factory::new),
4641
Map.entry(DOUBLE, ToDenseVectorFromDoubleEvaluator.Factory::new)
4742
);
4843

4944
@FunctionInfo(
5045
returnType = "dense_vector",
51-
description = "Converts a multi-valued input of numbers or strings to a dense_vector."
46+
description = "Converts a multi-valued input of numbers to a dense_vector.",
47+
examples = @Example(file = "dense_vector", tag = "to_dense_vector-ints")
5248
)
5349
public ToDenseVector(
5450
Source source,
5551
@Param(
5652
name = "field",
57-
type = { "keyword", "text", "double", "long", "integer" },
58-
description = "Input multi-valued column or an expression."
53+
type = {"double", "long", "integer"},
54+
description = "multi-valued input of numbers to convert."
5955
) Expression field
6056
) {
6157
super(source, field);
@@ -90,11 +86,6 @@ protected NodeInfo<? extends Expression> info() {
9086
return NodeInfo.create(this, ToDenseVector::new, field());
9187
}
9288

93-
@ConvertEvaluator(extraName = "FromString", warnExceptions = { org.elasticsearch.xpack.esql.core.InvalidArgumentException.class })
94-
static float fromString(BytesRef in) {
95-
return (float) stringToDouble(in.utf8ToString());
96-
}
97-
9889
@ConvertEvaluator(extraName = "FromLong")
9990
static float fromLong(long l) {
10091
return l;

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToDenseVectorTests.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.carrotsearch.randomizedtesting.annotations.Name;
1111
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
1212

13-
import org.apache.lucene.util.BytesRef;
1413
import org.elasticsearch.test.ESTestCase;
1514
import org.elasticsearch.xpack.esql.core.expression.Expression;
1615
import org.elasticsearch.xpack.esql.core.tree.Source;
@@ -79,25 +78,6 @@ public static Iterable<Object[]> parameters() {
7978
)
8079
);
8180

82-
suppliers.add(
83-
new TestCaseSupplier(
84-
"mv_string",
85-
List.of(DataType.KEYWORD),
86-
() -> new TestCaseSupplier.TestCase(
87-
List.of(
88-
new TestCaseSupplier.TypedData(
89-
List.of(new BytesRef("1.1"), new BytesRef("2.2")),
90-
DataType.KEYWORD,
91-
"mv_string"
92-
)
93-
),
94-
evaluatorName("String", "in"),
95-
DataType.DENSE_VECTOR,
96-
equalTo(List.of(1.1f, 2.2f))
97-
)
98-
)
99-
);
100-
10181
return parameterSuppliersFromTypedDataWithDefaultChecksNoErrors(true, suppliers);
10282
}
10383

0 commit comments

Comments
 (0)