Skip to content

Commit 33d5473

Browse files
committed
unit tests
1 parent fabaabb commit 33d5473

File tree

3 files changed

+88
-13
lines changed

3 files changed

+88
-13
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,7 @@ private static FunctionDefinition[][] functions() {
406406
// fulltext functions
407407
new FunctionDefinition[] { def(Match.class, Match::new, "match"), def(QueryString.class, QueryString::new, "qstr") },
408408
// hash
409-
new FunctionDefinition[] {
410-
def(Hash.class, Hash::new, "hash")
411-
}
412-
};
409+
new FunctionDefinition[] { def(Hash.class, Hash::new, "hash") } };
413410
}
414411

415412
private static FunctionDefinition[][] snapshotFunctions() {

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ public class Hash extends EsqlScalarFunction {
3636
private final Expression alg;
3737
private final Expression input;
3838

39-
@FunctionInfo(
40-
returnType = "keyword",
41-
description = "Computes the hash of the input keyword."
42-
)
39+
@FunctionInfo(returnType = "keyword", description = "Computes the hash of the input keyword.")
4340
public Hash(
4441
Source source,
4542
@Param(name = "alg", type = { "keyword", "text" }, description = "Hash algorithm to use.") Expression alg,
@@ -51,11 +48,7 @@ public Hash(
5148
}
5249

5350
private Hash(StreamInput in) throws IOException {
54-
this(
55-
Source.readFrom((PlanStreamInput) in),
56-
in.readNamedWriteable(Expression.class),
57-
in.readNamedWriteable(Expression.class)
58-
);
51+
this(Source.readFrom((PlanStreamInput) in), in.readNamedWriteable(Expression.class), in.readNamedWriteable(Expression.class));
5952
}
6053

6154
@Override
@@ -75,6 +68,11 @@ public DataType dataType() {
7568
return DataType.KEYWORD;
7669
}
7770

71+
@Override
72+
public boolean foldable() {
73+
return alg.foldable() && input.foldable();
74+
}
75+
7876
@Evaluator(warnExceptions = NoSuchAlgorithmException.class)
7977
static BytesRef process(BytesRef alg, BytesRef input) throws NoSuchAlgorithmException {
8078
return hash(MessageDigest.getInstance(alg.utf8ToString()), input);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.expression.function.scalar.hash;
9+
10+
import com.carrotsearch.randomizedtesting.annotations.Name;
11+
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
12+
13+
import org.apache.lucene.util.BytesRef;
14+
import org.elasticsearch.xpack.esql.core.expression.Expression;
15+
import org.elasticsearch.xpack.esql.core.tree.Source;
16+
import org.elasticsearch.xpack.esql.core.type.DataType;
17+
import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase;
18+
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
19+
20+
import java.security.MessageDigest;
21+
import java.security.NoSuchAlgorithmException;
22+
import java.util.ArrayList;
23+
import java.util.HexFormat;
24+
import java.util.List;
25+
import java.util.function.Supplier;
26+
27+
import static org.hamcrest.Matchers.equalTo;
28+
29+
public class HashTests extends AbstractScalarFunctionTestCase {
30+
31+
public HashTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
32+
this.testCase = testCaseSupplier.get();
33+
}
34+
35+
@ParametersFactory
36+
public static Iterable<Object[]> parameters() {
37+
List<TestCaseSupplier> cases = new ArrayList<>();
38+
for (String alg : List.of("MD5", "SHA", "SHA-224", "SHA-256", "SHA-384", "SHA-512")) {
39+
cases.addAll(createTestCases(alg));
40+
}
41+
return parameterSuppliersFromTypedData(cases);
42+
}
43+
44+
private static List<TestCaseSupplier> createTestCases(String alg) {
45+
return List.of(
46+
createTestCase(alg, DataType.KEYWORD, DataType.KEYWORD),
47+
createTestCase(alg, DataType.KEYWORD, DataType.TEXT),
48+
createTestCase(alg, DataType.TEXT, DataType.KEYWORD),
49+
createTestCase(alg, DataType.TEXT, DataType.TEXT)
50+
);
51+
}
52+
53+
private static TestCaseSupplier createTestCase(String alg, DataType algType, DataType inputType) {
54+
return new TestCaseSupplier(alg, List.of(algType, inputType), () -> {
55+
var input = randomAlphaOfLength(10);
56+
return new TestCaseSupplier.TestCase(
57+
List.of(
58+
new TestCaseSupplier.TypedData(new BytesRef(alg), algType, "alg"),
59+
new TestCaseSupplier.TypedData(input, inputType, "input")
60+
),
61+
"HashEvaluator[alg=Attribute[channel=0], input=Attribute[channel=1]]",
62+
DataType.KEYWORD,
63+
equalTo(new BytesRef(hash(alg, input)))
64+
);
65+
});
66+
}
67+
68+
private static String hash(String alg, String input) {
69+
try {
70+
return HexFormat.of().formatHex(MessageDigest.getInstance(alg).digest(input.getBytes()));
71+
} catch (NoSuchAlgorithmException e) {
72+
throw new IllegalArgumentException("Unknown algorithm: " + alg);
73+
}
74+
}
75+
76+
@Override
77+
protected Expression build(Source source, List<Expression> args) {
78+
return new Hash(source, args.get(0), args.get(1));
79+
}
80+
}

0 commit comments

Comments
 (0)