Search in sources :

Example 31 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class SqlInvokedScalarFromAnnotationsParser method createSqlInvokedFunctions.

private static List<SqlInvokedFunction> createSqlInvokedFunctions(Method method, Optional<SqlInvokedScalarFunction> header, Optional<String> description) {
    SqlInvokedScalarFunction functionHeader = header.orElse(method.getAnnotation(SqlInvokedScalarFunction.class));
    String functionDescription = description.orElse(method.isAnnotationPresent(Description.class) ? method.getAnnotation(Description.class).value() : "");
    TypeSignature returnType = parseTypeSignature(method.getAnnotation(SqlType.class).value());
    // Parameter
    checkCondition(!method.isAnnotationPresent(SqlParameter.class) || !method.isAnnotationPresent(SqlParameters.class), FUNCTION_IMPLEMENTATION_ERROR, "Function-defining method [%s] is annotated with both @SqlParameter and @SqlParameters", method);
    List<Parameter> parameters;
    if (method.isAnnotationPresent(SqlParameter.class)) {
        parameters = ImmutableList.of(getParameterFromAnnotation(method.getAnnotation(SqlParameter.class)));
    } else if (method.isAnnotationPresent(SqlParameters.class)) {
        parameters = stream(method.getAnnotation(SqlParameters.class).value()).map(SqlInvokedScalarFromAnnotationsParser::getParameterFromAnnotation).collect(toImmutableList());
    } else {
        parameters = ImmutableList.of();
    }
    // Routine characteristics
    RoutineCharacteristics routineCharacteristics = RoutineCharacteristics.builder().setLanguage(RoutineCharacteristics.Language.SQL).setDeterminism(functionHeader.deterministic() ? DETERMINISTIC : NOT_DETERMINISTIC).setNullCallClause(functionHeader.calledOnNullInput() ? CALLED_ON_NULL_INPUT : RETURNS_NULL_ON_NULL_INPUT).build();
    String body;
    try {
        body = (String) method.invoke(null);
    } catch (ReflectiveOperationException e) {
        throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, format("Failed to get function body for method [%s]", method), e);
    }
    return Stream.concat(Stream.of(functionHeader.value()), stream(functionHeader.alias())).map(name -> new SqlInvokedFunction(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, name), parameters, returnType, functionDescription, routineCharacteristics, body, notVersioned())).collect(toImmutableList());
}
Also used : Parameter(com.facebook.presto.spi.function.Parameter) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) PrestoException(com.facebook.presto.spi.PrestoException) TypeSignature(com.facebook.presto.common.type.TypeSignature) SqlParameters(com.facebook.presto.spi.function.SqlParameters) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) CodegenScalarFunction(com.facebook.presto.spi.function.CodegenScalarFunction) DEFAULT_NAMESPACE(com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) ImmutableList(com.google.common.collect.ImmutableList) Description(com.facebook.presto.spi.function.Description) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) Method(java.lang.reflect.Method) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) SqlInvokedScalarFunction(com.facebook.presto.spi.function.SqlInvokedScalarFunction) FUNCTION_IMPLEMENTATION_ERROR(com.facebook.presto.spi.StandardErrorCode.FUNCTION_IMPLEMENTATION_ERROR) CALLED_ON_NULL_INPUT(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.CALLED_ON_NULL_INPUT) Failures.checkCondition(com.facebook.presto.util.Failures.checkCondition) FunctionsParserHelper.findPublicStaticMethods(com.facebook.presto.operator.annotations.FunctionsParserHelper.findPublicStaticMethods) SqlParameter(com.facebook.presto.spi.function.SqlParameter) ImmutableSet(com.google.common.collect.ImmutableSet) NOT_DETERMINISTIC(com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.NOT_DETERMINISTIC) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DETERMINISTIC(com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.DETERMINISTIC) Set(java.util.Set) String.format(java.lang.String.format) List(java.util.List) Stream(java.util.stream.Stream) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) RETURNS_NULL_ON_NULL_INPUT(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.RETURNS_NULL_ON_NULL_INPUT) FunctionVersion.notVersioned(com.facebook.presto.spi.function.FunctionVersion.notVersioned) Optional(java.util.Optional) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Arrays.stream(java.util.Arrays.stream) SqlType(com.facebook.presto.spi.function.SqlType) Description(com.facebook.presto.spi.function.Description) SqlParameter(com.facebook.presto.spi.function.SqlParameter) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) PrestoException(com.facebook.presto.spi.PrestoException) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) SqlParameters(com.facebook.presto.spi.function.SqlParameters) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) SqlInvokedScalarFunction(com.facebook.presto.spi.function.SqlInvokedScalarFunction) Parameter(com.facebook.presto.spi.function.Parameter) SqlParameter(com.facebook.presto.spi.function.SqlParameter)

Example 32 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class WindowAnnotationsParser method parse.

private static SqlWindowFunction parse(Class<? extends WindowFunction> clazz, WindowFunctionSignature window) {
    List<TypeVariableConstraint> typeVariables = ImmutableList.of();
    if (!window.typeVariable().isEmpty()) {
        typeVariables = ImmutableList.of(typeVariable(window.typeVariable()));
    }
    List<TypeSignature> argumentTypes = Stream.of(window.argumentTypes()).map(TypeSignature::parseTypeSignature).collect(toImmutableList());
    Signature signature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, window.name()), WINDOW, typeVariables, ImmutableList.of(), parseTypeSignature(window.returnType()), argumentTypes, false);
    return new SqlWindowFunction(new ReflectionWindowFunctionSupplier<>(signature, clazz));
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) WindowFunctionSignature(com.facebook.presto.spi.function.WindowFunctionSignature) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) TypeVariableConstraint(com.facebook.presto.spi.function.TypeVariableConstraint)

Example 33 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature 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);
}
Also used : FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) JavaScalarFunctionImplementation(com.facebook.presto.spi.function.JavaScalarFunctionImplementation) TypeSignature(com.facebook.presto.common.type.TypeSignature) Signature(com.facebook.presto.spi.function.Signature) InvocationConvention(com.facebook.presto.spi.function.InvocationConvention) MethodHandle(java.lang.invoke.MethodHandle)

Example 34 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class PrestoPreparedStatement method getDescribeOutputColumnInfoList.

private static List<ColumnInfo> getDescribeOutputColumnInfoList(ResultSet resultSet) throws SQLException {
    ImmutableList.Builder<ColumnInfo> list = ImmutableList.builder();
    while (resultSet.next()) {
        String columnName = resultSet.getString("Column Name");
        String catalog = resultSet.getString("Catalog");
        String schema = resultSet.getString("Schema");
        String table = resultSet.getString("Table");
        TypeSignature signature = TypeSignature.parseTypeSignature(resultSet.getString("Type"));
        ColumnInfo.Builder builder = new ColumnInfo.Builder().setColumnName(columnName).setColumnLabel(columnName).setCatalogName(catalog).setSchemaName(schema).setTableName(table).setColumnTypeSignature(signature).setNullable(ColumnInfo.Nullable.UNKNOWN);
        setTypeInfo(builder, signature);
        list.add(builder.build());
    }
    return list.build();
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) ImmutableList(com.google.common.collect.ImmutableList)

Example 35 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class CreateFunctionTask method createSqlInvokedFunction.

private SqlInvokedFunction createSqlInvokedFunction(CreateFunction statement, Metadata metadata, Analysis analysis) {
    QualifiedObjectName functionName = statement.isTemporary() ? QualifiedObjectName.valueOf(SESSION_NAMESPACE, statement.getFunctionName().getSuffix()) : qualifyObjectName(statement.getFunctionName());
    List<Parameter> parameters = statement.getParameters().stream().map(parameter -> new Parameter(parameter.getName().toString(), parseTypeSignature(parameter.getType()))).collect(toImmutableList());
    TypeSignature returnType = parseTypeSignature(statement.getReturnType());
    String description = statement.getComment().orElse("");
    RoutineCharacteristics routineCharacteristics = RoutineCharacteristics.builder().setLanguage(new RoutineCharacteristics.Language(statement.getCharacteristics().getLanguage().getLanguage())).setDeterminism(RoutineCharacteristics.Determinism.valueOf(statement.getCharacteristics().getDeterminism().name())).setNullCallClause(RoutineCharacteristics.NullCallClause.valueOf(statement.getCharacteristics().getNullCallClause().name())).build();
    RoutineBody body = statement.getBody();
    if (statement.getBody() instanceof Return) {
        Expression bodyExpression = ((Return) statement.getBody()).getExpression();
        Type bodyType = analysis.getType(bodyExpression);
        // Coerce expressions in body if necessary
        bodyExpression = ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Void>() {

            @Override
            public Expression rewriteExpression(Expression expression, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
                Expression rewritten = treeRewriter.defaultRewrite(expression, null);
                Type coercion = analysis.getCoercion(expression);
                if (coercion != null) {
                    return new Cast(rewritten, coercion.getTypeSignature().toString(), false, analysis.isTypeOnlyCoercion(expression));
                }
                return rewritten;
            }
        }, bodyExpression, null);
        if (!bodyType.equals(metadata.getType(returnType))) {
            // Casting is safe here, since we have verified at analysis time that the actual type of the body is coercible to declared return type.
            bodyExpression = new Cast(bodyExpression, statement.getReturnType());
        }
        body = new Return(bodyExpression);
    }
    return new SqlInvokedFunction(functionName, parameters, returnType, description, routineCharacteristics, formatSql(body, Optional.empty()), notVersioned());
}
Also used : ExpressionTreeRewriter(com.facebook.presto.sql.tree.ExpressionTreeRewriter) WarningCollector(com.facebook.presto.spi.WarningCollector) Parameter(com.facebook.presto.spi.function.Parameter) Analyzer(com.facebook.presto.sql.analyzer.Analyzer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) SESSION_NAMESPACE(com.facebook.presto.metadata.SessionFunctionHandle.SESSION_NAMESPACE) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) PrestoException(com.facebook.presto.spi.PrestoException) CreateFunction(com.facebook.presto.sql.tree.CreateFunction) TypeSignature(com.facebook.presto.common.type.TypeSignature) Inject(javax.inject.Inject) ALREADY_EXISTS(com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) Cast(com.facebook.presto.sql.tree.Cast) SqlFunctionHandle(com.facebook.presto.spi.function.SqlFunctionHandle) TransactionManager(com.facebook.presto.transaction.TransactionManager) FunctionAndTypeManager.qualifyObjectName(com.facebook.presto.metadata.FunctionAndTypeManager.qualifyObjectName) Type(com.facebook.presto.common.type.Type) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) Session(com.facebook.presto.Session) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) RoutineBody(com.facebook.presto.sql.tree.RoutineBody) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) String.format(java.lang.String.format) SqlParser(com.facebook.presto.sql.parser.SqlParser) ExpressionRewriter(com.facebook.presto.sql.tree.ExpressionRewriter) List(java.util.List) SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Expression(com.facebook.presto.sql.tree.Expression) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) Return(com.facebook.presto.sql.tree.Return) FunctionVersion.notVersioned(com.facebook.presto.spi.function.FunctionVersion.notVersioned) SqlFormatter.formatSql(com.facebook.presto.sql.SqlFormatter.formatSql) Analysis(com.facebook.presto.sql.analyzer.Analysis) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Metadata(com.facebook.presto.metadata.Metadata) AccessControl(com.facebook.presto.security.AccessControl) Cast(com.facebook.presto.sql.tree.Cast) ExpressionTreeRewriter(com.facebook.presto.sql.tree.ExpressionTreeRewriter) Return(com.facebook.presto.sql.tree.Return) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Type(com.facebook.presto.common.type.Type) ExpressionRewriter(com.facebook.presto.sql.tree.ExpressionRewriter) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) Expression(com.facebook.presto.sql.tree.Expression) Parameter(com.facebook.presto.spi.function.Parameter) RoutineBody(com.facebook.presto.sql.tree.RoutineBody)

Aggregations

TypeSignature (com.facebook.presto.common.type.TypeSignature)60 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)25 Signature (com.facebook.presto.spi.function.Signature)20 NamedTypeSignature (com.facebook.presto.common.type.NamedTypeSignature)19 ImmutableList (com.google.common.collect.ImmutableList)19 List (java.util.List)17 PrestoException (com.facebook.presto.spi.PrestoException)15 Type (com.facebook.presto.common.type.Type)14 Test (org.testng.annotations.Test)14 TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)13 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)13 Objects.requireNonNull (java.util.Objects.requireNonNull)12 RowFieldName (com.facebook.presto.common.type.RowFieldName)10 StandardTypes (com.facebook.presto.common.type.StandardTypes)9 Optional (java.util.Optional)9 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)8 DecimalType (com.facebook.presto.common.type.DecimalType)8 ImmutableSet (com.google.common.collect.ImmutableSet)8 RowType (com.facebook.presto.common.type.RowType)7 ArrayType (com.facebook.presto.common.type.ArrayType)6