Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
idegtiarenko committed Dec 5, 2024
1 parent f063055 commit 062967f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
md5
hash
required_capability: hash_function

ROW value = "test" | EVAL hash = hash("md5", value);
FROM languages
| EVAL md5 = hash("md5", language_name), sha256 = hash("sha256", language_name)
| KEEP language_name, md5, sha256;

value:keyword | hash:keyword
test | 098f6bcd4621d373cade4e832627b4f6
;

sha256
required_capability: hash_function

ROW value = "test" | EVAL hash = hash("sha256", value);

value:keyword | hash:keyword
test | 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
language_name:keyword | md5:keyword | sha256:keyword
English | 78463a384a5aa4fad5fa73e2f506ecfc | ba118bf7fc9c1aedc1edb28a0aa86e0b43b681f222af6616e13c43be87815b06
French | ad225f707802ba118c22987186dd38e8 | 7458199fe97a184002bfd3c42bae81371f0ae2229b5c0a257a9bae77e4f4eda8
Spanish | cb5480c32e71778852b08ae1e8712775 | 3411059cb8e0660e29dd7a3737e65a28b08eb01524a8ebc3d4168932649f23e6
German | 86bc3115eb4e9873ac96904a4a68e19e | a659b60d246dce8d4865d45673963de83d0e49a57e8559da98581c2bc5d7d97f
;

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
Expand Up @@ -13,6 +13,7 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.compute.ann.Evaluator;
import org.elasticsearch.compute.ann.Fixed;
import org.elasticsearch.compute.operator.BreakingBytesRefBuilder;
import org.elasticsearch.compute.operator.EvalOperator;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
Expand Down Expand Up @@ -92,32 +93,51 @@ public boolean foldable() {
}

@Evaluator(warnExceptions = NoSuchAlgorithmException.class)
static BytesRef process(BytesRef alg, BytesRef input) throws NoSuchAlgorithmException {
return hash(MessageDigest.getInstance(alg.utf8ToString()), input);
static BytesRef process(@Fixed(includeInToString = false, build = true) BreakingBytesRefBuilder scratch, BytesRef alg, BytesRef input)
throws NoSuchAlgorithmException {
return hash(scratch, MessageDigest.getInstance(alg.utf8ToString()), input);
}

@Evaluator(extraName = "Constant")
static BytesRef processConstant(@Fixed(build = true) MessageDigest alg, BytesRef input) {
return hash(alg, input);
static BytesRef processConstant(
@Fixed(includeInToString = false, build = true) BreakingBytesRefBuilder scratch,
@Fixed(build = true) MessageDigest alg,
BytesRef input
) {
return hash(scratch, alg, input);
}

private static BytesRef hash(MessageDigest alg, BytesRef input) {
private static BytesRef hash(BreakingBytesRefBuilder scratch, MessageDigest alg, BytesRef input) {
alg.reset();
alg.update(input.bytes, input.offset, input.length);
var result = alg.digest();
return new BytesRef(HexFormat.of().formatHex(result));
var digest = alg.digest();
scratch.clear();
scratch.grow(digest.length * 2);
scratch.append(new BytesRef(HexFormat.of().formatHex(digest)));
return scratch.bytesRefView();
}

@Override
public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
if (alg.foldable() && alg.dataType() == DataType.KEYWORD) {
try {
var md = MessageDigest.getInstance(((BytesRef) alg.fold()).utf8ToString());
return new HashConstantEvaluator.Factory(source(), context -> md, toEvaluator.apply(input));
return new HashConstantEvaluator.Factory(
source(),
context -> new BreakingBytesRefBuilder(context.breaker(), "hash"),
context -> md,
toEvaluator.apply(input)
);
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException(e);
}
} else {
return new HashEvaluator.Factory(source(), toEvaluator.apply(alg), toEvaluator.apply(input));
return new HashEvaluator.Factory(
source(),
context -> new BreakingBytesRefBuilder(context.breaker(), "hash"),
toEvaluator.apply(alg),
toEvaluator.apply(input)
);
}
}

Expand Down

0 comments on commit 062967f

Please sign in to comment.