use of com.facebook.presto.spi.function.ScalarFunction in project presto by prestodb.
the class BenchmarkArraySort method oldArraySort.
@ScalarFunction
@SqlType("array(varchar)")
public static Block oldArraySort(@SqlType("array(varchar)") Block block) {
List<Integer> positions = Ints.asList(new int[block.getPositionCount()]);
for (int i = 0; i < block.getPositionCount(); i++) {
positions.set(i, i);
}
Collections.sort(positions, new Comparator<Integer>() {
@Override
public int compare(Integer p1, Integer p2) {
//TODO: This could be quite slow, it should use parametric equals
return VARCHAR.compareTo(block, p1, block, p2);
}
});
BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), block.getPositionCount());
for (int position : positions) {
VARCHAR.appendTo(block, position, blockBuilder);
}
return blockBuilder.build();
}
use of com.facebook.presto.spi.function.ScalarFunction in project presto by prestodb.
the class BenchmarkArrayDistinct method oldArrayDistinct.
@ScalarFunction
@SqlType("array(varchar)")
public static Block oldArrayDistinct(@SqlType("array(varchar)") Block array) {
if (array.getPositionCount() == 0) {
return array;
}
TypedSet typedSet = new TypedSet(VARCHAR, array.getPositionCount());
BlockBuilder distinctElementBlockBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), array.getPositionCount());
for (int i = 0; i < array.getPositionCount(); i++) {
if (!typedSet.contains(array, i)) {
typedSet.add(array, i);
VARCHAR.appendTo(array, i, distinctElementBlockBuilder);
}
}
return distinctElementBlockBuilder.build();
}
use of com.facebook.presto.spi.function.ScalarFunction in project presto by prestodb.
the class StringFunctions method substr.
@Description("substring of given length starting at an index")
@ScalarFunction
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice substr(@SqlType("varchar(x)") Slice utf8, @SqlType(StandardTypes.BIGINT) long start, @SqlType(StandardTypes.BIGINT) long length) {
if (start == 0 || (length <= 0) || (utf8.length() == 0)) {
return Slices.EMPTY_SLICE;
}
int startCodePoint = Ints.saturatedCast(start);
int lengthCodePoints = Ints.saturatedCast(length);
if (startCodePoint > 0) {
int indexStart = offsetOfCodePoint(utf8, startCodePoint - 1);
if (indexStart < 0) {
// before beginning of string
return Slices.EMPTY_SLICE;
}
int indexEnd = offsetOfCodePoint(utf8, indexStart, lengthCodePoints);
if (indexEnd < 0) {
// after end of string
indexEnd = utf8.length();
}
return utf8.slice(indexStart, indexEnd - indexStart);
}
// negative start is relative to end of string
int codePoints = countCodePoints(utf8);
startCodePoint += codePoints;
// before beginning of string
if (startCodePoint < 0) {
return Slices.EMPTY_SLICE;
}
int indexStart = offsetOfCodePoint(utf8, startCodePoint);
int indexEnd;
if (startCodePoint + lengthCodePoints < codePoints) {
indexEnd = offsetOfCodePoint(utf8, indexStart, lengthCodePoints);
} else {
indexEnd = utf8.length();
}
return utf8.slice(indexStart, indexEnd - indexStart);
}
use of com.facebook.presto.spi.function.ScalarFunction 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.ScalarFunction in project presto by prestodb.
the class ColorFunctions method bar.
@ScalarFunction
@SqlType(StandardTypes.VARCHAR)
public static Slice bar(@SqlType(StandardTypes.DOUBLE) double percent, @SqlType(StandardTypes.BIGINT) long width, @SqlType(ColorType.NAME) long lowColor, @SqlType(ColorType.NAME) long highColor) {
long count = (int) (percent * width);
count = Math.min(width, count);
count = Math.max(0, count);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < count; i++) {
float fraction = (float) (i * 1.0 / (width - 1));
int color = interpolate(fraction, lowColor, highColor);
builder.append(ansiColorEscape(color)).append('█');
}
// reset
builder.append(ANSI_RESET);
// pad to force column to be the requested width
for (long i = count; i < width; ++i) {
builder.append(' ');
}
return utf8Slice(builder.toString());
}
Aggregations