use of com.facebook.presto.spi.function.JavaScalarFunctionImplementation in project presto by prestodb.
the class RowToRowCast method generateRowCast.
private static Class<?> generateRowCast(Type fromType, Type toType, FunctionAndTypeManager functionAndTypeManager) {
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()).asBytes();
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(Joiner.on("$").join("RowCast", BaseEncoding.base16().encode(md5Suffix))), type(Object.class));
Parameter properties = arg("properties", SqlFunctionProperties.class);
Parameter value = arg("value", Block.class);
MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "castRow", type(Block.class), properties, value);
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++) {
FunctionHandle functionHandle = functionAndTypeManager.lookupCast(CastType.CAST, fromTypes.get(i).getTypeSignature(), toTypes.get(i).getTypeSignature());
JavaScalarFunctionImplementation function = functionAndTypeManager.getJavaScalarFunctionImplementation(functionHandle);
Type currentFromType = fromTypes.get(i);
if (currentFromType.equals(UNKNOWN)) {
body.append(singleRowBlockWriter.invoke("appendNull", BlockBuilder.class).pop());
continue;
}
BytecodeExpression fromElement = constantType(binder, currentFromType).getValue(value, constantInt(i));
BytecodeExpression toElement = invokeFunction(scope, cachedInstanceBinder, CAST.name(), function, fromElement);
IfStatement ifElementNull = new IfStatement("if the element in the row type is null...");
ifElementNull.condition(value.invoke("isNull", boolean.class, constantInt(i))).ifTrue(singleRowBlockWriter.invoke("appendNull", BlockBuilder.class).pop()).ifFalse(constantType(binder, toTypes.get(i)).writeValue(singleRowBlockWriter, toElement));
body.append(ifElementNull);
}
// 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 com.facebook.presto.spi.function.JavaScalarFunctionImplementation in project presto by prestodb.
the class HiveScalarFunction method createHiveScalarFunction.
public static HiveScalarFunction createHiveScalarFunction(Class<?> cls, QualifiedObjectName name, List<TypeSignature> argumentTypes, TypeManager typeManager) {
HiveScalarFunctionInvoker invoker = createFunctionInvoker(cls, name, argumentTypes, typeManager);
MethodHandle methodHandle = ScalarMethodHandles.generateUnbound(invoker.getSignature(), typeManager).bindTo(invoker);
Signature signature = invoker.getSignature();
FunctionMetadata functionMetadata = new FunctionMetadata(name, signature.getArgumentTypes(), signature.getReturnType(), SCALAR, FunctionImplementationType.JAVA, true, true);
InvocationConvention invocationConvention = new InvocationConvention(signature.getArgumentTypes().stream().map(t -> BOXED_NULLABLE).collect(toImmutableList()), InvocationConvention.InvocationReturnConvention.NULLABLE_RETURN, false);
JavaScalarFunctionImplementation implementation = new HiveScalarFunctionImplementation(methodHandle, invocationConvention);
return new HiveScalarFunction(functionMetadata, signature, name.getObjectName(), implementation);
}
use of com.facebook.presto.spi.function.JavaScalarFunctionImplementation in project presto by prestodb.
the class FunctionCallCodeGenerator method generateCall.
public BytecodeNode generateCall(FunctionHandle functionHandle, BytecodeGeneratorContext context, Type returnType, List<RowExpression> arguments, Optional<Variable> outputBlockVariable) {
FunctionAndTypeManager functionAndTypeManager = context.getFunctionManager();
ScalarFunctionImplementation function = functionAndTypeManager.getScalarFunctionImplementation(functionHandle);
checkArgument(function instanceof JavaScalarFunctionImplementation, format("FunctionCallCodeGenerator only handles JavaScalarFunctionImplementation, get %s", function.getClass().getName()));
JavaScalarFunctionImplementation javaFunction = (JavaScalarFunctionImplementation) function;
List<BytecodeNode> argumentsBytecode = new ArrayList<>();
ScalarFunctionImplementationChoice choice = getAllScalarFunctionImplementationChoices(javaFunction).get(0);
for (int i = 0; i < arguments.size(); i++) {
RowExpression argument = arguments.get(i);
ArgumentProperty argumentProperty = choice.getArgumentProperty(i);
if (argumentProperty.getArgumentType() == VALUE_TYPE) {
argumentsBytecode.add(context.generate(argument, Optional.empty()));
} else {
argumentsBytecode.add(context.generate(argument, Optional.empty(), Optional.of(argumentProperty.getLambdaInterface())));
}
}
return context.generateCall(functionAndTypeManager.getFunctionMetadata(functionHandle).getName().getObjectName(), javaFunction, argumentsBytecode, outputBlockVariable.map(variable -> new OutputBlockVariableAndType(variable, returnType)));
}
use of com.facebook.presto.spi.function.JavaScalarFunctionImplementation in project presto by prestodb.
the class FunctionInvokerProvider method createFunctionInvoker.
public FunctionInvoker createFunctionInvoker(FunctionHandle functionHandle, Optional<InvocationConvention> invocationConvention) {
JavaScalarFunctionImplementation functionImplementation = functionAndTypeManager.getJavaScalarFunctionImplementation(functionHandle);
for (ScalarFunctionImplementationChoice choice : getAllScalarFunctionImplementationChoices(functionImplementation)) {
if (checkChoice(choice.getArgumentProperties(), choice.isNullable(), choice.hasProperties(), invocationConvention)) {
return new FunctionInvoker(choice.getMethodHandle());
}
}
checkState(invocationConvention.isPresent());
throw new PrestoException(FUNCTION_NOT_FOUND, format("Dependent function implementation (%s) with convention (%s) is not available", functionHandle, invocationConvention.toString()));
}
use of com.facebook.presto.spi.function.JavaScalarFunctionImplementation in project presto by prestodb.
the class MapToMapCast method buildProcessor.
/**
* The signature of the returned MethodHandle is (Block fromMap, int position, SqlFunctionProperties properties, BlockBuilder mapBlockBuilder)void.
* The processor will get the value from fromMap, cast it and write to toBlock.
*/
private MethodHandle buildProcessor(FunctionAndTypeManager functionAndTypeManager, Type fromType, Type toType, boolean isKey) {
MethodHandle getter = nativeValueGetter(fromType);
// Adapt cast that takes ([SqlFunctionProperties,] ?) to one that takes (?, SqlFunctionProperties), where ? is the return type of getter.
JavaScalarFunctionImplementation castImplementation = functionAndTypeManager.getJavaScalarFunctionImplementation(functionAndTypeManager.lookupCast(CastType.CAST, fromType.getTypeSignature(), toType.getTypeSignature()));
MethodHandle cast = castImplementation.getMethodHandle();
if (cast.type().parameterArray()[0] != SqlFunctionProperties.class) {
cast = MethodHandles.dropArguments(cast, 0, SqlFunctionProperties.class);
}
cast = permuteArguments(cast, methodType(cast.type().returnType(), cast.type().parameterArray()[1], cast.type().parameterArray()[0]), 1, 0);
MethodHandle target = compose(cast, getter);
// If the key cast function is nullable, check the result is not null.
if (isKey && castImplementation.isNullable()) {
target = compose(nullChecker(target.type().returnType()), target);
}
MethodHandle writer = nativeValueWriter(toType);
writer = permuteArguments(writer, methodType(void.class, writer.type().parameterArray()[1], writer.type().parameterArray()[0]), 1, 0);
return compose(writer, target.asType(methodType(unwrap(target.type().returnType()), target.type().parameterArray())));
}
Aggregations