-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
- Loading branch information
There are no files selected for viewing
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.
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.") | ||
public Md5(Source source, @Param(name = "input", type = { "keyword", "text" }, description = "Input to hash.") Expression input) { | ||
super(source, input); | ||
} | ||
|
||
public Md5(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
|
||
@Override | ||
protected HashFunction getHashFunction() { | ||
return MD5; | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return ENTRY.name; | ||
} | ||
|
||
@Override | ||
public Expression replaceChildren(List<Expression> newChildren) { | ||
return new Md5(source(), field); | ||
} | ||
|
||
@Override | ||
protected NodeInfo<? extends Expression> info() { | ||
return NodeInfo.create(this, Md5::new, field); | ||
} | ||
} |
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)); | ||
} | ||
} |