Java Code Examples for io.airlift.slice.Slice#hasByteArray()

The following examples show how to use io.airlift.slice.Slice#hasByteArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JoniRegexpFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
@Description("Returns whether the pattern is contained within the string")
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.BOOLEAN)
public static boolean regexpLike(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern)
{
    Matcher matcher;
    int offset;
    if (source.hasByteArray()) {
        offset = source.byteArrayOffset();
        matcher = pattern.regex().matcher(source.byteArray(), offset, offset + source.length());
    }
    else {
        offset = 0;
        matcher = pattern.matcher(source.getBytes());
    }
    return matcher.search(offset, offset + source.length(), Option.DEFAULT) != -1;
}
 
Example 2
Source File: VarbinaryFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
@Description("Decode base64 encoded binary data")
@ScalarFunction("from_base64")
@LiteralParameters("x")
@SqlType(StandardTypes.VARBINARY)
public static Slice fromBase64Varchar(@SqlType("varchar(x)") Slice slice)
{
    try {
        if (slice.hasByteArray()) {
            return Slices.wrappedBuffer(Base64.getDecoder().decode(slice.toByteBuffer()));
        }
        return Slices.wrappedBuffer(Base64.getDecoder().decode(slice.getBytes()));
    }
    catch (IllegalArgumentException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
    }
}
 
Example 3
Source File: VarbinaryFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
@Description("Decode URL safe base64 encoded binary data")
@ScalarFunction("from_base64url")
@LiteralParameters("x")
@SqlType(StandardTypes.VARBINARY)
public static Slice fromBase64UrlVarchar(@SqlType("varchar(x)") Slice slice)
{
    try {
        if (slice.hasByteArray()) {
            return Slices.wrappedBuffer(Base64.getUrlDecoder().decode(slice.toByteBuffer()));
        }
        return Slices.wrappedBuffer(Base64.getUrlDecoder().decode(slice.getBytes()));
    }
    catch (IllegalArgumentException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
    }
}
 
Example 4
Source File: HmacFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
static Slice computeHash(HashFunction hash, Slice data)
{
    HashCode result;
    if (data.hasByteArray()) {
        result = hash.hashBytes(data.byteArray(), data.byteArrayOffset(), data.length());
    }
    else {
        result = hash.hashBytes(data.getBytes());
    }
    return wrappedBuffer(result.asBytes());
}
 
Example 5
Source File: VarbinaryFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Encode binary data as base64")
@ScalarFunction
@SqlType(StandardTypes.VARCHAR)
public static Slice toBase64(@SqlType(StandardTypes.VARBINARY) Slice slice)
{
    if (slice.hasByteArray()) {
        return Slices.wrappedBuffer(Base64.getEncoder().encode(slice.toByteBuffer()));
    }
    return Slices.wrappedBuffer(Base64.getEncoder().encode(slice.getBytes()));
}
 
Example 6
Source File: VarbinaryFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Decode base64 encoded binary data")
@ScalarFunction("from_base64")
@SqlType(StandardTypes.VARBINARY)
public static Slice fromBase64Varbinary(@SqlType(StandardTypes.VARBINARY) Slice slice)
{
    try {
        if (slice.hasByteArray()) {
            return Slices.wrappedBuffer(Base64.getDecoder().decode(slice.toByteBuffer()));
        }
        return Slices.wrappedBuffer(Base64.getDecoder().decode(slice.getBytes()));
    }
    catch (IllegalArgumentException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
    }
}
 
Example 7
Source File: VarbinaryFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Encode binary data as base64 using the URL safe alphabet")
@ScalarFunction("to_base64url")
@SqlType(StandardTypes.VARCHAR)
public static Slice toBase64Url(@SqlType(StandardTypes.VARBINARY) Slice slice)
{
    if (slice.hasByteArray()) {
        return Slices.wrappedBuffer(Base64.getUrlEncoder().encode(slice.toByteBuffer()));
    }
    return Slices.wrappedBuffer(Base64.getUrlEncoder().encode(slice.getBytes()));
}
 
Example 8
Source File: VarbinaryFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Decode URL safe base64 encoded binary data")
@ScalarFunction("from_base64url")
@SqlType(StandardTypes.VARBINARY)
public static Slice fromBase64UrlVarbinary(@SqlType(StandardTypes.VARBINARY) Slice slice)
{
    try {
        if (slice.hasByteArray()) {
            return Slices.wrappedBuffer(Base64.getUrlDecoder().decode(slice.toByteBuffer()));
        }
        return Slices.wrappedBuffer(Base64.getUrlDecoder().decode(slice.getBytes()));
    }
    catch (IllegalArgumentException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
    }
}
 
Example 9
Source File: VarbinaryFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Encode binary data as hex")
@ScalarFunction
@SqlType(StandardTypes.VARCHAR)
public static Slice toHex(@SqlType(StandardTypes.VARBINARY) Slice slice)
{
    String encoded;
    if (slice.hasByteArray()) {
        encoded = BaseEncoding.base16().encode(slice.byteArray(), slice.byteArrayOffset(), slice.length());
    }
    else {
        encoded = BaseEncoding.base16().encode(slice.getBytes());
    }
    return Slices.utf8Slice(encoded);
}
 
Example 10
Source File: BinaryDictionary.java    From presto with Apache License 2.0 5 votes vote down vote up
public BinaryDictionary(DictionaryPage dictionaryPage, Integer length)
        throws IOException
{
    super(dictionaryPage.getEncoding());
    content = new Binary[dictionaryPage.getDictionarySize()];

    byte[] dictionaryBytes;
    int offset;
    Slice dictionarySlice = dictionaryPage.getSlice();
    if (dictionarySlice.hasByteArray()) {
        dictionaryBytes = dictionarySlice.byteArray();
        offset = dictionarySlice.byteArrayOffset();
    }
    else {
        dictionaryBytes = dictionarySlice.getBytes();
        offset = 0;
    }

    if (length == null) {
        for (int i = 0; i < content.length; i++) {
            int len = readIntLittleEndian(dictionaryBytes, offset);
            offset += 4;
            content[i] = Binary.fromReusedByteArray(dictionaryBytes, offset, len);
            offset += len;
        }
    }
    else {
        checkArgument(length > 0, "Invalid byte array length: %s", length);
        for (int i = 0; i < content.length; i++) {
            content[i] = Binary.fromReusedByteArray(dictionaryBytes, offset, length);
            offset += length;
        }
    }
}