use of io.trino.spi.function.SqlType in project trino by trinodb.
the class ArrayToArrayCast method filterObject.
@TypeParameter("F")
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = Object.class)
@SqlType("array(T)")
public static Block filterObject(@TypeParameter("T") Type resultType, @CastDependency(fromType = "F", toType = "T", convention = @Convention(arguments = BLOCK_POSITION, result = NULLABLE_RETURN, session = true)) MethodHandle cast, ConnectorSession session, @SqlType("array(F)") Block array) throws Throwable {
int positionCount = array.getPositionCount();
BlockBuilder resultBuilder = resultType.createBlockBuilder(null, positionCount);
for (int position = 0; position < positionCount; position++) {
if (!array.isNull(position)) {
Object value = (Object) cast.invoke(session, array, position);
if (value != null) {
resultType.writeObject(resultBuilder, value);
continue;
}
}
resultBuilder.appendNull();
}
return resultBuilder.build();
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class ArrayToArrayCast method filterBoolean.
@TypeParameter("F")
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = boolean.class)
@SqlType("array(T)")
public static Block filterBoolean(@TypeParameter("T") Type resultType, @CastDependency(fromType = "F", toType = "T", convention = @Convention(arguments = BLOCK_POSITION, result = NULLABLE_RETURN, session = true)) MethodHandle cast, ConnectorSession session, @SqlType("array(F)") Block array) throws Throwable {
int positionCount = array.getPositionCount();
BlockBuilder resultBuilder = resultType.createBlockBuilder(null, positionCount);
for (int position = 0; position < positionCount; position++) {
if (!array.isNull(position)) {
Boolean value = (Boolean) cast.invokeExact(session, array, position);
if (value != null) {
resultType.writeBoolean(resultBuilder, value);
continue;
}
}
resultBuilder.appendNull();
}
return resultBuilder.build();
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class ArrayToArrayCast method filterDouble.
@TypeParameter("F")
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = double.class)
@SqlType("array(T)")
public static Block filterDouble(@TypeParameter("T") Type resultType, @CastDependency(fromType = "F", toType = "T", convention = @Convention(arguments = BLOCK_POSITION, result = NULLABLE_RETURN, session = true)) MethodHandle cast, ConnectorSession session, @SqlType("array(F)") Block array) throws Throwable {
int positionCount = array.getPositionCount();
BlockBuilder resultBuilder = resultType.createBlockBuilder(null, positionCount);
for (int position = 0; position < positionCount; position++) {
if (!array.isNull(position)) {
Double value = (Double) cast.invokeExact(session, array, position);
if (value != null) {
resultType.writeDouble(resultBuilder, value);
continue;
}
}
resultBuilder.appendNull();
}
return resultBuilder.build();
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class ArrayUnionFunction method union.
@TypeParameter("E")
@SqlType("array(E)")
public static Block union(@TypeParameter("E") Type type, @OperatorDependency(operator = EQUAL, argumentTypes = { "E", "E" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = NULLABLE_RETURN)) BlockPositionEqual elementEqual, @OperatorDependency(operator = HASH_CODE, argumentTypes = "E", convention = @Convention(arguments = BLOCK_POSITION, result = FAIL_ON_NULL)) BlockPositionHashCode elementHashCode, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
int leftArrayCount = leftArray.getPositionCount();
int rightArrayCount = rightArray.getPositionCount();
BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(null, leftArrayCount + rightArrayCount);
TypedSet typedSet = createEqualityTypedSet(type, elementEqual, elementHashCode, distinctElementBlockBuilder, leftArrayCount + rightArrayCount, "array_union");
for (int i = 0; i < leftArray.getPositionCount(); i++) {
typedSet.add(leftArray, i);
}
for (int i = 0; i < rightArray.getPositionCount(); i++) {
typedSet.add(rightArray, i);
}
return distinctElementBlockBuilder.build();
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
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('\u2588');
}
// 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