Skip to content

Commit

Permalink
Refactor RunLengthEncodedBlock hash combination logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pettyjamesm committed Nov 18, 2023
1 parent 06eaf0f commit f9690fc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,16 +443,6 @@ private static MethodDefinition generateHashBlockVectorized(ClassDefinition defi
block,
position);

BytecodeExpression setHashExpression;
if (field.index() == 0) {
// hashes[index] = hash;
setHashExpression = hashes.setElement(index, hash);
}
else {
// hashes[index] = CombineHashFunction.getHash(hashes[index], hash);
setHashExpression = hashes.setElement(index, invokeStatic(CombineHashFunction.class, "getHash", long.class, hashes.getElement(index), hash));
}

BytecodeBlock rleHandling = new BytecodeBlock()
.append(new IfStatement("hash = block.isNull(position) ? NULL_HASH_CODE : hash(block, position)")
.condition(block.invoke("isNull", boolean.class, position))
Expand All @@ -463,11 +453,18 @@ private static MethodDefinition generateHashBlockVectorized(ClassDefinition defi
rleHandling.append(invokeStatic(Arrays.class, "fill", void.class, hashes, constantInt(0), length, hash));
}
else {
rleHandling.append(new ForLoop("for (int index = 0; index < length; index++) { hashes[index] = CombineHashFunction.getHash(hashes[index], hash); }")
.initialize(index.set(constantInt(0)))
.condition(lessThan(index, length))
.update(index.increment())
.body(setHashExpression));
// CombineHashFunction.combineAllHashesWithConstant(hashes, 0, length, hash)
rleHandling.append(invokeStatic(CombineHashFunction.class, "combineAllHashesWithConstant", void.class, hashes, constantInt(0), length, hash));
}

BytecodeExpression setHashExpression;
if (field.index() == 0) {
// hashes[index] = hash;
setHashExpression = hashes.setElement(index, hash);
}
else {
// hashes[index] = CombineHashFunction.getHash(hashes[index], hash);
setHashExpression = hashes.setElement(index, invokeStatic(CombineHashFunction.class, "getHash", long.class, hashes.getElement(index), hash));
}

BytecodeBlock computeHashLoop = new BytecodeBlock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
*/
package io.trino.operator.scalar;

import io.trino.annotation.UsedByGeneratedCode;
import io.trino.spi.function.ScalarFunction;
import io.trino.spi.function.SqlType;
import io.trino.spi.type.StandardTypes;

import static java.util.Objects.checkFromToIndex;

public final class CombineHashFunction
{
private CombineHashFunction() {}
Expand All @@ -27,4 +30,14 @@ public static long getHash(@SqlType(StandardTypes.BIGINT) long previousHashValue
{
return (31 * previousHashValue + value);
}

@UsedByGeneratedCode
public static void combineAllHashesWithConstant(long[] hashes, int fromIndex, int toIndex, long value)
{
checkFromToIndex(fromIndex, toIndex, hashes.length);

for (int i = 0; i < toIndex; i++) {
hashes[i] = (31 * hashes[i]) + value;
}
}
}

0 comments on commit f9690fc

Please sign in to comment.