Skip to content

Commit

Permalink
Add bitwise shift operations
Browse files Browse the repository at this point in the history
Allows bitwise shift operations including arithmetic shift right,
logical shift right, and shift left.
  • Loading branch information
gastonar committed Jun 6, 2019
1 parent 5e3ca2c commit afe74f4
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 2 deletions.
23 changes: 23 additions & 0 deletions presto-docs/src/main/sphinx/functions/bitwise.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,27 @@ Bitwise Functions

Returns the bitwise XOR of ``x`` and ``y`` in 2's complement representation.

.. function:: bitwise_left_shift(x, shift, bits) -> bigint

Left shift operation on ``x`` (treated as ``bits``-bit integer)
shifted by ``shift``.

SELECT bitwise_logical_left_shift(7, 2, 4); -- 12
SELECT bitwise_logical_left_shift(7, 2, 64); -- 28

.. function:: bitwise_shift_right_logical(x, shift, bits) -> bigint

Logical right shift operation on ``x`` (treated as ``bits``-bit integer)
shifted by ``shift``.

SELECT bitwise_logical_right_shift(7, 2, 4); -- 1
SELECT bitwise_logical_right_shift(-8, 2, 5); -- 6

.. function:: bitwise_shift_right_arithmetic(x, shift) -> bigint

Arithmetic right shift operation on ``x`` shifted by ``shift`` in 2's complement representation.

SELECT bitwise_arithmetic_right_shift(-8, 2); -- -2
SELECT bitwise_arithmetic_right_shift(7, 2); -- 1

See also :func:`bitwise_and_agg` and :func:`bitwise_or_agg`.
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@

public final class BitwiseFunctions
{
private static final int MAX_BITS = 64;

private BitwiseFunctions() {}

@Description("count number of set bits in 2's complement representation")
@ScalarFunction
@SqlType(StandardTypes.BIGINT)
public static long bitCount(@SqlType(StandardTypes.BIGINT) long num, @SqlType(StandardTypes.BIGINT) long bits)
{
if (bits == 64) {
if (bits == MAX_BITS) {
return Long.bitCount(num);
}
if (bits <= 1 || bits > 64) {
if (bits <= 1 || bits > MAX_BITS) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Bits specified in bit_count must be between 2 and 64, got " + bits);
}
long lowBitsMask = (1L << (bits - 1)) - 1; // set the least (bits - 1) bits
Expand Down Expand Up @@ -75,4 +77,60 @@ public static long bitwiseXor(@SqlType(StandardTypes.BIGINT) long left, @SqlType
{
return left ^ right;
}

@Description("shift left operation with specified bits")
@ScalarFunction
@SqlType(StandardTypes.BIGINT)
public static long bitwiseShiftLeft(@SqlType(StandardTypes.BIGINT) long number,
@SqlType(StandardTypes.BIGINT) long shift,
@SqlType(StandardTypes.BIGINT) long bits)
{
if (bits == MAX_BITS) {
return number << shift;
}

if (bits <= 1 || bits > MAX_BITS) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Bits specified must be between 2 and 64, got " + bits);
}

if (shift < 0) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Specified shift must be positive");
}

return number << shift & (long) (Math.pow(2, bits) - 1);
}

@Description("logical shift right operation with specified bits")
@ScalarFunction
@SqlType(StandardTypes.BIGINT)
public static long bitwiseLogicalShiftRight(@SqlType(StandardTypes.BIGINT) long number,
@SqlType(StandardTypes.BIGINT) long shift,
@SqlType(StandardTypes.BIGINT) long bits)
{
if (bits == MAX_BITS) {
return number >>> shift;
}

if (bits <= 1 || bits > MAX_BITS) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Bits specified in must be between 2 and 64, got " + bits);
}

if (shift < 0) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Specified shift must be positive");
}

return (number & (long) (Math.pow(2, bits) - 1)) >>> shift;
}

@Description("arithmetic shift right operation")
@ScalarFunction
@SqlType(StandardTypes.BIGINT)
public static long bitwiseArithmeticShiftRight(@SqlType(StandardTypes.BIGINT) long number, @SqlType(StandardTypes.BIGINT) long shift)
{
if (shift < 0) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Specified shift must be positive");
}

return number >> shift;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

import org.testng.annotations.Test;

import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
import static com.facebook.presto.spi.type.BigintType.BIGINT;
import static java.lang.String.format;

public class TestBitwiseFunctions
extends AbstractTestFunctions
Expand All @@ -33,6 +35,7 @@ public void testBitCount()
assertFunction("bit_count(-" + Long.MAX_VALUE + "-1, 64)", BIGINT, 1L); // bit_count(MIN_VALUE, 64)

assertFunction("bit_count(0, 32)", BIGINT, 0L);
assertFunction("bit_count(CAST (-8 AS SMALLINT), 6)", BIGINT, 3L);
assertFunction("bit_count(7, 32)", BIGINT, 3L);
assertFunction("bit_count(24, 32)", BIGINT, 2L);
assertFunction("bit_count(-8, 32)", BIGINT, 29L);
Expand Down Expand Up @@ -90,4 +93,42 @@ public void testBitwiseXor()
assertFunction("bitwise_xor(-4, 12)", BIGINT, -4L ^ 12L);
assertFunction("bitwise_xor(60, 21)", BIGINT, 60L ^ 21L);
}

@Test
public void testBitwiseSll()
{
assertFunction("bitwise_shift_left(7, 2, 4)", BIGINT, 12L);
assertFunction("bitwise_shift_left(7, 2, 64)", BIGINT, 7L << 2L);
assertFunction("bitwise_shift_left(-4, 6, 64)", BIGINT, -4L << 6L);
assertFunction("bitwise_shift_left(-4, 6, 5)", BIGINT, 0L);
assertFunction("bitwise_shift_left(-4, 6, 9)", BIGINT, 256L);

assertInvalidFunction("bitwise_shift_left(7, -3, 2)", INVALID_FUNCTION_ARGUMENT);
}

@Test
public void testBitwiseSrl()
{
assertFunction("bitwise_logical_shift_right(7, 2, 4)", BIGINT, 1L);
assertFunction("bitwise_logical_shift_right(7, 2, 64)", BIGINT, 7L >>> 2L);
assertFunction("bitwise_logical_shift_right(-4, 6, 64)", BIGINT, -4L >>> 6L);
assertFunction("bitwise_logical_shift_right(-8, 2, 5)", BIGINT, 6L);
assertFunction(format("bitwise_logical_shift_right(%s, 62, 64)", 0xF0000F0000F00000L), BIGINT, 3L);
assertFunction(format("bitwise_logical_shift_right(%s, 62, 4)", 0xF0000F0000F00000L), BIGINT, 0L);
assertFunction(format("bitwise_logical_shift_right(%s, 1, 4)", 0xF0000F0000F00000L), BIGINT, 0L);

assertInvalidFunction("bitwise_logical_shift_right(7, -3, 2)", INVALID_FUNCTION_ARGUMENT);
}

@Test
public void testBitwiseSra()
{
assertFunction("bitwise_arithmetic_shift_right(7, 2)", BIGINT, 7L >> 2L);
assertFunction("bitwise_arithmetic_shift_right(-4, 6)", BIGINT, -4L >> 6L);
assertFunction("bitwise_arithmetic_shift_right(-256, 3)", BIGINT, -32L);
assertFunction("bitwise_arithmetic_shift_right(-8, 2)", BIGINT, -2L);
assertFunction(format("bitwise_arithmetic_shift_right(%s, 62)", 0xF0000F0000F00000L), BIGINT, -1L);

assertInvalidFunction("bitwise_arithmetic_shift_right(7, -3)", INVALID_FUNCTION_ARGUMENT);
}
}

0 comments on commit afe74f4

Please sign in to comment.