Skip to content
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

Refactor RunLengthEncodedBlock hash combination logic #19759

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Comment on lines +460 to +467
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this move?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, the same hash assignment expression was used in the RLE loop as well as the normal block loop, so it was declared above both of them (RLE loop was defined first). Now it's only used in the normal block loop, so I moved it closer to it's usage site.

}

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;
}
}
}