use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class DropFunctionTask method execute.
@Override
public ListenableFuture<?> execute(DropFunction statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.empty(), parameters, warningCollector);
analyzer.analyze(statement);
Optional<List<TypeSignature>> parameterTypes = statement.getParameterTypes().map(types -> types.stream().map(TypeSignature::parseTypeSignature).collect(toImmutableList()));
if (statement.isTemporary()) {
removeSessionFunction(session, new SqlFunctionId(QualifiedObjectName.valueOf(SESSION_NAMESPACE, statement.getFunctionName().getSuffix()), parameterTypes.orElse(emptyList())), statement.isExists());
} else {
metadata.getFunctionAndTypeManager().dropFunction(qualifyObjectName(statement.getFunctionName()), parameterTypes, statement.isExists());
}
return immediateFuture(null);
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class RowParametricType method createType.
@Override
public Type createType(List<TypeParameter> parameters) {
checkArgument(!parameters.isEmpty(), "Row type must have at least one parameter");
checkArgument(parameters.stream().allMatch(parameter -> parameter.getKind() == ParameterKind.NAMED_TYPE), "Expected only named types as a parameters, got %s", parameters);
List<TypeSignatureParameter> typeSignatureParameters = parameters.stream().map(TypeParameter::getNamedType).map(parameter -> TypeSignatureParameter.of(new NamedTypeSignature(parameter.getName(), parameter.getType().getTypeSignature()))).collect(toList());
List<RowType.Field> fields = parameters.stream().map(TypeParameter::getNamedType).map(parameter -> new RowType.Field(parameter.getName().map(RowFieldName::getName), parameter.getType())).collect(toList());
return RowType.createWithTypeSignature(new TypeSignature(StandardTypes.ROW, typeSignatureParameters), fields);
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class ScalarMethodHandles method generateUnbound.
/**
* Generates an unbound MethodHandle by {@code signature}.
* <p>
* For example, if {@code signature} describes a method like
* <pre>
* {@code
* IntegerType func(
* IntegerType i,
* VarcharType j)
* }
* </pre>,
* a method is generated as
* <pre>
* {@code
* static Long callEvaluate(
* ScalarFunctionInvoker invoker,
* Long input_0,
* Slice input_1) {
* return (Long) Invoker.evaluate(input_0, input_1);
* }
* }
* </pre>,
* and its MethodHandle returns.
*/
public static MethodHandle generateUnbound(Signature signature, TypeManager typeManager) {
Class<?> returnType = Primitives.wrap(typeManager.getType(signature.getReturnType()).getJavaType());
List<TypeSignature> argumentTypes = signature.getArgumentTypes();
List<Class<?>> argumentJavaTypes = argumentTypes.stream().map(t -> typeManager.getType(t).getJavaType()).map(Primitives::wrap).collect(toImmutableList());
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(CLASS_NAME), ParameterizedType.type(Object.class));
// Step 1: Declare default constructor
definition.declareDefaultConstructor(a(PRIVATE));
// Step 2: Declare method
Parameter[] declareParameters = new Parameter[argumentTypes.size() + 1];
declareParameters[0] = arg("invoker", ScalarFunctionInvoker.class);
for (int i = 0; i < argumentTypes.size(); i++) {
declareParameters[i + 1] = arg("input_" + i, argumentJavaTypes.get(i));
}
MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), METHOD_NAME, ParameterizedType.type(returnType), declareParameters);
// Step 3: Implement method body
BytecodeExpression[] evaluateInputs = new BytecodeExpression[argumentTypes.size()];
for (int i = 0; i < argumentTypes.size(); i++) {
evaluateInputs[i] = declareParameters[i + 1];
}
method.getBody().append(declareParameters[0].invoke("evaluate", Object.class, newArray(type(Object[].class), evaluateInputs)).cast(returnType).ret());
// Step 4: Generate class
Class<?> generatedClass = defineClass(definition, Object.class, Collections.emptyMap(), ScalarMethodHandles.class.getClassLoader());
// Step 5: Lookup MethodHandle
Class<?>[] lookupClasses = new Class[argumentTypes.size() + 1];
lookupClasses[0] = ScalarFunctionInvoker.class;
for (int i = 0; i < argumentTypes.size(); i++) {
lookupClasses[i + 1] = Primitives.wrap(typeManager.getType(argumentTypes.get(i)).getJavaType());
}
return methodHandle(generatedClass, METHOD_NAME, lookupClasses);
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class HiveScalarFunctionInvoker method createFunctionInvoker.
public static HiveScalarFunctionInvoker createFunctionInvoker(Class<?> cls, QualifiedObjectName name, List<TypeSignature> arguments, TypeManager typeManager) {
final List<Type> argumentTypes = arguments.stream().map(typeManager::getType).collect(Collectors.toList());
try {
// Step 1: Create function instance
final GenericUDF udf = createGenericUDF(name, cls);
// Step 2: Initialize function
ObjectInspector[] inputInspectors = argumentTypes.stream().map(argumentType -> ObjectInspectors.create(argumentType, typeManager)).toArray(ObjectInspector[]::new);
ObjectInspector resultInspector = udf.initialize(inputInspectors);
// Step 3: Create invoker
Type resultType = PrestoTypes.fromObjectInspector(resultInspector, typeManager);
ObjectInputDecoder[] argumentDecoders = argumentTypes.stream().map(argumentsType -> createDecoder(argumentsType, typeManager)).toArray(ObjectInputDecoder[]::new);
ObjectEncoder resultEncoder = createEncoder(resultType, resultInspector);
Signature signature = new Signature(name, SCALAR, resultType.getTypeSignature(), arguments);
// Step 4: Create ThreadLocal GenericUDF
final ThreadLocal<GenericUDF> genericUDFSupplier = ThreadLocal.withInitial(() -> {
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(cls.getClassLoader())) {
GenericUDF ret = createGenericUDF(name, cls);
ret.initialize(inputInspectors);
return ret;
} catch (Exception e) {
throw initializationError(e);
}
});
return new HiveScalarFunctionInvoker(signature, genericUDFSupplier::get, argumentDecoders, resultEncoder);
} catch (Exception e) {
throw initializationError(e);
}
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class PrestoTypes method fromMap.
private static Type fromMap(MapObjectInspector inspector, TypeManager typeManager) {
Type keyType = fromObjectInspector(inspector.getMapKeyObjectInspector(), typeManager);
Type valueType = fromObjectInspector(inspector.getMapValueObjectInspector(), typeManager);
return typeManager.getType(new TypeSignature(StandardTypes.MAP, TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature())));
}
Aggregations