use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class VarbinaryFunctions method spookyHashV2_64.
@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;
}
use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class VarbinaryFunctions method crc32.
@Description("compute CRC-32")
@ScalarFunction
@SqlType(StandardTypes.BIGINT)
public static long crc32(@SqlType(StandardTypes.VARBINARY) Slice slice) {
CRC32 crc32 = new CRC32();
crc32.update(slice.toByteBuffer());
return crc32.getValue();
}
use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class StringFunctions method concat.
// TODO: implement N arguments char concat
@Description("concatenates given character strings")
@ScalarFunction
@LiteralParameters({ "x", "y", "u" })
@Constraint(variable = "u", expression = "x + y")
@SqlType("char(u)")
public static Slice concat(@LiteralParameter("x") Long x, @SqlType("char(x)") Slice left, @SqlType("char(y)") Slice right) {
int rightLength = right.length();
if (rightLength == 0) {
return left;
}
Slice paddedLeft = padSpaces(left, x.intValue());
int leftLength = paddedLeft.length();
Slice result = Slices.allocate(leftLength + rightLength);
result.setBytes(0, paddedLeft);
result.setBytes(leftLength, right);
return result;
}
use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class StringFunctions method splitPart.
@SqlNullable
@Description("splits a string by a delimiter and returns the specified field (counting from one)")
@ScalarFunction
@LiteralParameters({ "x", "y" })
@SqlType("varchar(x)")
public static Slice splitPart(@SqlType("varchar(x)") Slice string, @SqlType("varchar(y)") Slice delimiter, @SqlType(StandardTypes.BIGINT) long index) {
checkCondition(index > 0, INVALID_FUNCTION_ARGUMENT, "Index must be greater than zero");
// Empty delimiter? Then every character will be a split
if (delimiter.length() == 0) {
int startCodePoint = toIntExact(index);
int indexStart = offsetOfCodePoint(string, startCodePoint - 1);
if (indexStart < 0) {
// index too big
return null;
}
int length = lengthOfCodePoint(string, indexStart);
if (indexStart + length > string.length()) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Invalid UTF-8 encoding");
}
return string.slice(indexStart, length);
}
int matchCount = 0;
int previousIndex = 0;
while (previousIndex < string.length()) {
int matchIndex = string.indexOf(delimiter, previousIndex);
// No match
if (matchIndex < 0) {
break;
}
// Reached the requested part?
if (++matchCount == index) {
return string.slice(previousIndex, matchIndex - previousIndex);
}
// Continue searching after the delimiter
previousIndex = matchIndex + delimiter.length();
}
if (matchCount == index - 1) {
// returns last section of the split
return string.slice(previousIndex, string.length() - previousIndex);
}
// index is too big, null is returned
return null;
}
use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class StringFunctions method fromUtf8.
@Description("decodes the UTF-8 encoded string")
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.VARCHAR)
public static Slice fromUtf8(@SqlType(StandardTypes.VARBINARY) Slice slice, @SqlType("varchar(x)") Slice replacementCharacter) {
int count = countCodePoints(replacementCharacter);
if (count > 1) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Replacement character string must empty or a single character");
}
OptionalInt replacementCodePoint;
if (count == 1) {
try {
replacementCodePoint = OptionalInt.of(getCodePointAt(replacementCharacter, 0));
} catch (InvalidUtf8Exception e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Invalid replacement character");
}
} else {
replacementCodePoint = OptionalInt.empty();
}
return SliceUtf8.fixInvalidUtf8(slice, replacementCodePoint);
}
Aggregations