-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Hash functions #118938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hash functions #118938
Changes from 5 commits
c3e18d5
3eb01a9
8d52bc7
dfc536d
961eda1
e2877ec
9a9bc75
00fce7f
d62705a
60834e8
13fbbfa
5efa383
89be7d0
8caebb5
4cba449
500658f
016aacb
4d7c381
1ba0d75
f0b0deb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 118938 | ||
summary: Hash functions | ||
area: ES|QL | ||
type: enhancement | ||
issues: [] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add some extra test cases, to double check both the:
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.string; | ||
|
||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.compute.operator.BreakingBytesRefBuilder; | ||
import org.elasticsearch.compute.operator.DriverContext; | ||
import org.elasticsearch.compute.operator.EvalOperator; | ||
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.scalar.UnaryScalarFunction; | ||
import org.elasticsearch.xpack.esql.expression.function.scalar.string.Hash.HashFunction; | ||
|
||
import java.io.IOException; | ||
import java.util.function.Function; | ||
|
||
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.DEFAULT; | ||
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isString; | ||
|
||
public abstract class AbstractHashFunction extends UnaryScalarFunction { | ||
|
||
protected AbstractHashFunction(Source source, Expression field) { | ||
super(source, field); | ||
} | ||
|
||
protected AbstractHashFunction(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
|
||
protected abstract HashFunction getHashFunction(); | ||
|
||
@Override | ||
public DataType dataType() { | ||
return DataType.KEYWORD; | ||
} | ||
|
||
@Override | ||
protected TypeResolution resolveType() { | ||
if (childrenResolved() == false) { | ||
return new TypeResolution("Unresolved children"); | ||
} | ||
return isString(field, sourceText(), DEFAULT); | ||
} | ||
|
||
@Override | ||
public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) { | ||
return new HashConstantEvaluator.Factory( | ||
source(), | ||
context -> new BreakingBytesRefBuilder(context.breaker(), "hash"), | ||
new Function<>() { | ||
@Override | ||
public HashFunction apply(DriverContext context) { | ||
return getHashFunction().copy(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getHashFunction().toString(); | ||
} | ||
}, | ||
toEvaluator.apply(field) | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,56 @@ | ||||||
/* | ||||||
* 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.string; | ||||||
|
||||||
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; | ||||||
import org.elasticsearch.common.io.stream.StreamInput; | ||||||
import org.elasticsearch.xpack.esql.core.expression.Expression; | ||||||
import org.elasticsearch.xpack.esql.core.tree.NodeInfo; | ||||||
import org.elasticsearch.xpack.esql.core.tree.Source; | ||||||
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo; | ||||||
import org.elasticsearch.xpack.esql.expression.function.Param; | ||||||
import org.elasticsearch.xpack.esql.expression.function.scalar.string.Hash.HashFunction; | ||||||
|
||||||
import java.io.IOException; | ||||||
import java.util.List; | ||||||
|
||||||
public class Md5 extends AbstractHashFunction { | ||||||
|
||||||
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "MD5", Md5::new); | ||||||
|
||||||
private static final HashFunction MD5 = HashFunction.create("MD5"); | ||||||
|
||||||
@FunctionInfo(returnType = "keyword", description = "Computes MD5 hash of the input.") | ||||||
|
@FunctionInfo(returnType = "keyword", description = "Computes MD5 hash of the input.") | |
@FunctionInfo(returnType = "keyword", description = "Computes the MD5 hash of the input.") |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return new Md5(source(), field); | |
return new Md5(source(), newChildren.get(0)); |
Edit: Oh, looks like EsqlNodeSubclassTests.testReplaceChildren
failed successfully with this in CI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,12 +87,28 @@ private static TestCaseSupplier createTestCase(String algorithm, boolean forceLi | |
}); | ||
} | ||
|
||
static List<TestCaseSupplier> createHashFunctionTestCases(String algorithm) { | ||
return List.of(createHashFunctionTestCase(algorithm, DataType.KEYWORD), createHashFunctionTestCase(algorithm, DataType.TEXT)); | ||
|
||
} | ||
|
||
private static TestCaseSupplier createHashFunctionTestCase(String algorithm, DataType inputType) { | ||
return new TestCaseSupplier(algorithm + " with " + inputType, List.of(inputType), () -> { | ||
var input = randomFrom(TestCaseSupplier.stringCases(inputType)).get(); | ||
return new TestCaseSupplier.TestCase( | ||
List.of(input), | ||
"HashConstantEvaluator[algorithm=" + algorithm + ", input=Attribute[channel=0]]", | ||
DataType.KEYWORD, | ||
equalTo(new BytesRef(HashTests.hash(algorithm, BytesRefs.toString(input.data())))) | ||
); | ||
}); | ||
} | ||
|
||
private static TestCaseSupplier.TypedData createTypedData(String value, boolean forceLiteral, DataType type, String name) { | ||
var data = new TestCaseSupplier.TypedData(new BytesRef(value), type, name); | ||
return forceLiteral ? data.forceLiteral() : data; | ||
} | ||
|
||
private static String hash(String algorithm, String input) { | ||
static String hash(String algorithm, String input) { | ||
try { | ||
return HexFormat.of().formatHex(MessageDigest.getInstance(algorithm).digest(input.getBytes(StandardCharsets.UTF_8))); | ||
} catch (NoSuchAlgorithmException e) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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.string; | ||
|
||
import org.elasticsearch.xpack.esql.expression.AbstractExpressionSerializationTests; | ||
|
||
import java.io.IOException; | ||
|
||
public class Md5SerializationTests extends AbstractExpressionSerializationTests<Md5> { | ||
|
||
@Override | ||
protected Md5 createTestInstance() { | ||
return new Md5(randomSource(), randomChild()); | ||
} | ||
|
||
@Override | ||
protected Md5 mutateInstance(Md5 instance) throws IOException { | ||
return new Md5(instance.source(), mutateExpression(instance.field())); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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.string; | ||
|
||
import com.carrotsearch.randomizedtesting.annotations.Name; | ||
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; | ||
|
||
import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
import org.elasticsearch.xpack.esql.core.tree.Source; | ||
import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase; | ||
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
public class Md5Tests extends AbstractScalarFunctionTestCase { | ||
|
||
public Md5Tests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) { | ||
this.testCase = testCaseSupplier.get(); | ||
} | ||
|
||
@ParametersFactory | ||
public static Iterable<Object[]> parameters() { | ||
List<TestCaseSupplier> cases = new ArrayList<>(); | ||
cases.addAll(HashTests.createHashFunctionTestCases("MD5")); | ||
return parameterSuppliersFromTypedDataWithDefaultChecks(true, cases, (v, p) -> "string"); | ||
} | ||
|
||
@Override | ||
protected Expression build(Source source, List<Expression> args) { | ||
return new Md5(source, args.get(0)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you link the other new functions?