use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.
the class MathFunctions method widthBucket.
@Description("The bucket number of a value given an array of bins")
@ScalarFunction("width_bucket")
@SqlType(StandardTypes.BIGINT)
public static long widthBucket(@SqlType(StandardTypes.DOUBLE) double operand, @SqlType("array(double)") Block bins) {
int numberOfBins = bins.getPositionCount();
checkCondition(numberOfBins > 0, INVALID_FUNCTION_ARGUMENT, "Bins cannot be an empty array");
checkCondition(!isNaN(operand), INVALID_FUNCTION_ARGUMENT, "Operand cannot be NaN");
int lower = 0;
int upper = numberOfBins;
int index;
double bin;
while (lower < upper) {
if (DOUBLE.getDouble(bins, lower) > DOUBLE.getDouble(bins, upper - 1)) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Bin values are not sorted in ascending order");
}
index = (lower + upper) / 2;
bin = DOUBLE.getDouble(bins, index);
checkCondition(isFinite(bin), INVALID_FUNCTION_ARGUMENT, format("Bin value must be finite, got %s", bin));
if (operand < bin) {
upper = index;
} else {
lower = index + 1;
}
}
return lower;
}
use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.
the class TimestampWithTimeZoneOperators method castToDate.
@ScalarFunction("date")
@ScalarOperator(CAST)
@SqlType(StandardTypes.DATE)
public static long castToDate(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long value) {
// round down the current timestamp to days
ISOChronology chronology = unpackChronology(value);
long date = chronology.dayOfYear().roundFloor(unpackMillisUtc(value));
// date is currently midnight in timezone of the original value
// convert to UTC
long millis = date + chronology.getZone().getOffset(date);
return TimeUnit.MILLISECONDS.toDays(millis);
}
use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.
the class TeradataStringFunctions method char2HexInt.
@Description("Returns the hexadecimal representation of the UTF-16BE encoding of the argument")
@ScalarFunction("char2hexint")
@SqlType(StandardTypes.VARCHAR)
public static Slice char2HexInt(@SqlType(StandardTypes.VARCHAR) Slice string) {
Slice utf16 = Slices.wrappedBuffer(UTF_16BE.encode(string.toStringUtf8()));
String encoded = BaseEncoding.base16().encode(utf16.getBytes());
return Slices.utf8Slice(encoded);
}
use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.
the class ArrayDistinctFunction method bigintDistinct.
@SqlType("array(bigint)")
public static Block bigintDistinct(@SqlType("array(bigint)") Block array) {
if (array.getPositionCount() == 0) {
return array;
}
boolean containsNull = false;
LongSet set = new LongOpenHashSet(array.getPositionCount());
BlockBuilder distinctElementBlockBuilder = BIGINT.createBlockBuilder(new BlockBuilderStatus(), array.getPositionCount());
for (int i = 0; i < array.getPositionCount(); i++) {
if (array.isNull(i)) {
if (!containsNull) {
containsNull = true;
distinctElementBlockBuilder.appendNull();
}
continue;
}
long value = BIGINT.getLong(array, i);
if (set.add(value)) {
BIGINT.writeLong(distinctElementBlockBuilder, value);
}
}
return distinctElementBlockBuilder.build();
}
use of com.facebook.presto.spi.function.SqlType in project presto by prestodb.
the class ArrayDistinctFunction method distinct.
@TypeParameter("E")
@SqlType("array(E)")
public static Block distinct(@TypeParameter("E") Type type, @SqlType("array(E)") Block array) {
if (array.getPositionCount() < 2) {
return array;
}
if (array.getPositionCount() == 2) {
if (type.equalTo(array, 0, array, 1)) {
return array.getSingleValueBlock(0);
} else {
return array;
}
}
TypedSet typedSet = new TypedSet(type, array.getPositionCount());
BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), array.getPositionCount());
for (int i = 0; i < array.getPositionCount(); i++) {
if (!typedSet.contains(array, i)) {
typedSet.add(array, i);
type.appendTo(array, i, distinctElementBlockBuilder);
}
}
return distinctElementBlockBuilder.build();
}
Aggregations