Skip to content

Commit

Permalink
Add SpookyHashV2 hash functions
Browse files Browse the repository at this point in the history
  • Loading branch information
martint committed Aug 2, 2018
1 parent 0284f25 commit 5315bc5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<dep.antlr.version>4.6</dep.antlr.version>
<dep.airlift.version>0.172</dep.airlift.version>
<dep.packaging.version>${dep.airlift.version}</dep.packaging.version>
<dep.slice.version>0.35</dep.slice.version>
<dep.slice.version>0.36</dep.slice.version>
<dep.aws-sdk.version>1.11.293</dep.aws-sdk.version>
<dep.okhttp.version>3.9.0</dep.okhttp.version>
<dep.jdbi3.version>3.0.0</dep.jdbi3.version>
Expand Down
8 changes: 8 additions & 0 deletions presto-docs/src/main/sphinx/functions/binary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ Binary Functions

Computes the xxhash64 hash of ``binary``.

.. function:: spooky_hash_v2_32(binary) -> varbinary

Computes the 32-bit SpookyHashV2 hash of ``binary``.

.. function:: spooky_hash_v2_64(binary) -> varbinary

Computes the 64-bit SpookyHashV2 hash of ``binary``.

.. function:: hmac_md5(binary, key) -> varbinary

Computes HMAC with md5 of ``binary`` with the given ``key``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.primitives.Ints;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;
import io.airlift.slice.SpookyHashV2;
import io.airlift.slice.XxHash64;

import java.util.Base64;
Expand Down Expand Up @@ -276,6 +277,26 @@ public static Slice xxhash64(@SqlType(StandardTypes.VARBINARY) Slice slice)
return hash;
}

@Description("compute SpookyHashV2 32-bit hash")
@ScalarFunction
@SqlType(StandardTypes.VARBINARY)
public static Slice spookyHashV2_32(@SqlType(StandardTypes.VARBINARY) Slice slice)
{
Slice hash = Slices.allocate(Integer.BYTES);
hash.setInt(0, Integer.reverseBytes(SpookyHashV2.hash32(slice, 0, slice.length(), 0)));
return hash;
}

@Description("compute SpookyHashV2 64-bit hash")
@ScalarFunction
@SqlType(StandardTypes.VARBINARY)
public static Slice spookyHashV2_64(@SqlType(StandardTypes.VARBINARY) Slice slice)
{
Slice hash = Slices.allocate(Long.BYTES);
hash.setLong(0, Long.reverseBytes(SpookyHashV2.hash64(slice, 0, slice.length(), 0)));
return hash;
}

@Description("decode hex encoded binary data")
@ScalarFunction("from_hex")
@SqlType(StandardTypes.VARBINARY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,15 @@ public void testXxhash64()
assertFunction("xxhash64(CAST('hashme' AS VARBINARY))", VARBINARY, sqlVarbinaryHex("F9D96E0E1165E892"));
}

@Test
public void testSpookyHash()
{
assertFunction("spooky_hash_v2_32(CAST('' AS VARBINARY))", VARBINARY, sqlVarbinaryHex("6BF50919"));
assertFunction("spooky_hash_v2_32(CAST('hello' AS VARBINARY))", VARBINARY, sqlVarbinaryHex("D382E6CA"));
assertFunction("spooky_hash_v2_64(CAST('' AS VARBINARY))", VARBINARY, sqlVarbinaryHex("232706FC6BF50919"));
assertFunction("spooky_hash_v2_64(CAST('hello' AS VARBINARY))", VARBINARY, sqlVarbinaryHex("3768826AD382E6CA"));
}

@Test
public void testHashCode()
{
Expand Down

0 comments on commit 5315bc5

Please sign in to comment.