use of io.trino.spi.type.Type in project trino by trinodb.
the class ArrayAggregationFunction method specialize.
@Override
public AggregationMetadata specialize(BoundSignature boundSignature) {
Type type = boundSignature.getArgumentTypes().get(0);
ArrayAggregationStateSerializer stateSerializer = new ArrayAggregationStateSerializer(type);
ArrayAggregationStateFactory stateFactory = new ArrayAggregationStateFactory(type);
MethodHandle inputFunction = INPUT_FUNCTION.bindTo(type);
MethodHandle combineFunction = COMBINE_FUNCTION.bindTo(type);
MethodHandle outputFunction = OUTPUT_FUNCTION.bindTo(type);
return new AggregationMetadata(inputFunction, Optional.empty(), Optional.of(combineFunction), outputFunction, ImmutableList.of(new AccumulatorStateDescriptor<>(ArrayAggregationState.class, stateSerializer, stateFactory)));
}
use of io.trino.spi.type.Type in project trino by trinodb.
the class RowToRowCast method getFunctionDependencies.
@Override
public FunctionDependencyDeclaration getFunctionDependencies(BoundSignature boundSignature) {
List<Type> toTypes = boundSignature.getReturnType().getTypeParameters();
List<Type> fromTypes = boundSignature.getArgumentType(0).getTypeParameters();
FunctionDependencyDeclarationBuilder builder = FunctionDependencyDeclaration.builder();
for (int i = 0; i < toTypes.size(); i++) {
Type fromElementType = fromTypes.get(i);
Type toElementType = toTypes.get(i);
builder.addCast(fromElementType, toElementType);
}
return builder.build();
}
use of io.trino.spi.type.Type in project trino by trinodb.
the class RowToRowCast method generateRowCast.
private static Class<?> generateRowCast(Type fromType, Type toType, FunctionDependencies functionDependencies) {
List<Type> toTypes = toType.getTypeParameters();
List<Type> fromTypes = fromType.getTypeParameters();
CallSiteBinder binder = new CallSiteBinder();
// Embed the MD5 hash code of input and output types into the generated class name instead of the raw type names,
// which could prevent the class name from hitting the length limitation and invalid characters.
byte[] md5Suffix = Hashing.md5().hashBytes((fromType + "$" + toType).getBytes(UTF_8)).asBytes();
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(Joiner.on("$").join("RowCast", BaseEncoding.base16().encode(md5Suffix))), type(Object.class));
Parameter session = arg("session", ConnectorSession.class);
Parameter row = arg("row", Block.class);
MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "castRow", type(Block.class), session, row);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
Variable wasNull = scope.declareVariable(boolean.class, "wasNull");
Variable blockBuilder = scope.createTempVariable(BlockBuilder.class);
Variable singleRowBlockWriter = scope.createTempVariable(BlockBuilder.class);
body.append(wasNull.set(constantBoolean(false)));
CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(definition, binder);
// create the row block builder
body.append(blockBuilder.set(constantType(binder, toType).invoke("createBlockBuilder", BlockBuilder.class, constantNull(BlockBuilderStatus.class), constantInt(1))));
body.append(singleRowBlockWriter.set(blockBuilder.invoke("beginBlockEntry", BlockBuilder.class)));
// loop through to append member blocks
for (int i = 0; i < toTypes.size(); i++) {
Type fromElementType = fromTypes.get(i);
Type toElementType = toTypes.get(i);
Type currentFromType = fromElementType;
if (currentFromType.equals(UNKNOWN)) {
body.append(singleRowBlockWriter.invoke("appendNull", BlockBuilder.class).pop());
continue;
}
MethodHandle castMethod = getNullSafeCast(functionDependencies, fromElementType, toElementType);
MethodHandle writeMethod = getNullSafeWrite(toElementType);
MethodHandle castAndWrite = collectArguments(writeMethod, 1, castMethod);
body.append(invokeDynamic(BOOTSTRAP_METHOD, ImmutableList.of(binder.bind(castAndWrite).getBindingId()), "castAndWriteField", castAndWrite.type(), singleRowBlockWriter, scope.getVariable("session"), row, constantInt(i)));
}
// call blockBuilder.closeEntry() and return the single row block
body.append(blockBuilder.invoke("closeEntry", BlockBuilder.class).pop());
body.append(constantType(binder, toType).invoke("getObject", Object.class, blockBuilder.cast(Block.class), constantInt(0)).cast(Block.class).ret());
// create constructor
MethodDefinition constructorDefinition = definition.declareConstructor(a(PUBLIC));
BytecodeBlock constructorBody = constructorDefinition.getBody();
Variable thisVariable = constructorDefinition.getThis();
constructorBody.comment("super();").append(thisVariable).invokeConstructor(Object.class);
cachedInstanceBinder.generateInitializations(thisVariable, constructorBody);
constructorBody.ret();
return defineClass(definition, Object.class, binder.getBindings(), RowToRowCast.class.getClassLoader());
}
use of io.trino.spi.type.Type in project trino by trinodb.
the class ZipFunction method specialize.
@Override
protected ScalarFunctionImplementation specialize(BoundSignature boundSignature) {
List<Type> types = boundSignature.getArgumentTypes().stream().map(ArrayType.class::cast).map(ArrayType::getElementType).collect(toImmutableList());
List<Class<?>> javaArgumentTypes = nCopies(types.size(), Block.class);
MethodHandle methodHandle = METHOD_HANDLE.bindTo(types).asVarargsCollector(Block[].class).asType(methodType(Block.class, javaArgumentTypes));
return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, nCopies(types.size(), NEVER_NULL), methodHandle);
}
use of io.trino.spi.type.Type in project trino by trinodb.
the class TryCastFunction method specialize.
@Override
public ScalarFunctionImplementation specialize(BoundSignature boundSignature, FunctionDependencies functionDependencies) {
Type fromType = boundSignature.getArgumentType(0);
Type toType = boundSignature.getReturnType();
Class<?> returnType = wrap(toType.getJavaType());
// the resulting method needs to return a boxed type
InvocationConvention invocationConvention = new InvocationConvention(ImmutableList.of(NEVER_NULL), NULLABLE_RETURN, true, false);
MethodHandle coercion = functionDependencies.getCastInvoker(fromType, toType, invocationConvention).getMethodHandle();
coercion = coercion.asType(methodType(returnType, coercion.type()));
MethodHandle exceptionHandler = dropArguments(constant(returnType, null), 0, RuntimeException.class);
MethodHandle tryCastHandle = catchException(coercion, RuntimeException.class, exceptionHandler);
return new ChoicesScalarFunctionImplementation(boundSignature, NULLABLE_RETURN, ImmutableList.of(NEVER_NULL), tryCastHandle);
}
Aggregations