Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
idegtiarenko committed Dec 4, 2024
1 parent fabaabb commit 33d5473
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,7 @@ private static FunctionDefinition[][] functions() {
// fulltext functions
new FunctionDefinition[] { def(Match.class, Match::new, "match"), def(QueryString.class, QueryString::new, "qstr") },
// hash
new FunctionDefinition[] {
def(Hash.class, Hash::new, "hash")
}
};
new FunctionDefinition[] { def(Hash.class, Hash::new, "hash") } };
}

private static FunctionDefinition[][] snapshotFunctions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ public class Hash extends EsqlScalarFunction {
private final Expression alg;
private final Expression input;

@FunctionInfo(
returnType = "keyword",
description = "Computes the hash of the input keyword."
)
@FunctionInfo(returnType = "keyword", description = "Computes the hash of the input keyword.")
public Hash(
Source source,
@Param(name = "alg", type = { "keyword", "text" }, description = "Hash algorithm to use.") Expression alg,
Expand All @@ -51,11 +48,7 @@ public Hash(
}

private Hash(StreamInput in) throws IOException {
this(
Source.readFrom((PlanStreamInput) in),
in.readNamedWriteable(Expression.class),
in.readNamedWriteable(Expression.class)
);
this(Source.readFrom((PlanStreamInput) in), in.readNamedWriteable(Expression.class), in.readNamedWriteable(Expression.class));
}

@Override
Expand All @@ -75,6 +68,11 @@ public DataType dataType() {
return DataType.KEYWORD;
}

@Override
public boolean foldable() {
return alg.foldable() && input.foldable();
}

@Evaluator(warnExceptions = NoSuchAlgorithmException.class)
static BytesRef process(BytesRef alg, BytesRef input) throws NoSuchAlgorithmException {
return hash(MessageDigest.getInstance(alg.utf8ToString()), input);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.expression.function.scalar.hash;

import com.carrotsearch.randomizedtesting.annotations.Name;
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;

import org.apache.lucene.util.BytesRef;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase;
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HexFormat;
import java.util.List;
import java.util.function.Supplier;

import static org.hamcrest.Matchers.equalTo;

public class HashTests extends AbstractScalarFunctionTestCase {

public HashTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
this.testCase = testCaseSupplier.get();
}

@ParametersFactory
public static Iterable<Object[]> parameters() {
List<TestCaseSupplier> cases = new ArrayList<>();
for (String alg : List.of("MD5", "SHA", "SHA-224", "SHA-256", "SHA-384", "SHA-512")) {
cases.addAll(createTestCases(alg));
}
return parameterSuppliersFromTypedData(cases);
}

private static List<TestCaseSupplier> createTestCases(String alg) {
return List.of(
createTestCase(alg, DataType.KEYWORD, DataType.KEYWORD),
createTestCase(alg, DataType.KEYWORD, DataType.TEXT),
createTestCase(alg, DataType.TEXT, DataType.KEYWORD),
createTestCase(alg, DataType.TEXT, DataType.TEXT)
);
}

private static TestCaseSupplier createTestCase(String alg, DataType algType, DataType inputType) {
return new TestCaseSupplier(alg, List.of(algType, inputType), () -> {
var input = randomAlphaOfLength(10);
return new TestCaseSupplier.TestCase(
List.of(
new TestCaseSupplier.TypedData(new BytesRef(alg), algType, "alg"),
new TestCaseSupplier.TypedData(input, inputType, "input")
),
"HashEvaluator[alg=Attribute[channel=0], input=Attribute[channel=1]]",
DataType.KEYWORD,
equalTo(new BytesRef(hash(alg, input)))
);
});
}

private static String hash(String alg, String input) {
try {
return HexFormat.of().formatHex(MessageDigest.getInstance(alg).digest(input.getBytes()));
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException("Unknown algorithm: " + alg);
}
}

@Override
protected Expression build(Source source, List<Expression> args) {
return new Hash(source, args.get(0), args.get(1));
}
}

0 comments on commit 33d5473

Please sign in to comment.