Search in sources :

Example 1 with INVALID_FUNCTION_ARGUMENT

use of io.prestosql.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT in project hetu-core by openlookeng.

the class ArrayJoin method specializeArrayJoin.

private static BuiltInScalarFunctionImplementation specializeArrayJoin(Map<String, Type> types, FunctionAndTypeManager functionAndTypeManager, List<Boolean> nullableArguments, MethodHandle methodHandle) {
    Type type = types.get("T");
    List<ArgumentProperty> argumentProperties = nullableArguments.stream().map(nullable -> nullable ? valueTypeArgumentProperty(USE_BOXED_TYPE) : valueTypeArgumentProperty(RETURN_NULL_ON_NULL)).collect(toImmutableList());
    if (type instanceof UnknownType) {
        return new BuiltInScalarFunctionImplementation(false, argumentProperties, methodHandle.bindTo(null), Optional.of(STATE_FACTORY));
    } else {
        try {
            BuiltInScalarFunctionImplementation castFunction = functionAndTypeManager.getBuiltInScalarFunctionImplementation(functionAndTypeManager.lookupCast(CastType.CAST, type.getTypeSignature(), VARCHAR_TYPE_SIGNATURE));
            MethodHandle getter;
            Class<?> elementType = type.getJavaType();
            if (elementType == boolean.class) {
                getter = GET_BOOLEAN;
            } else if (elementType == double.class) {
                getter = GET_DOUBLE;
            } else if (elementType == long.class) {
                getter = GET_LONG;
            } else if (elementType == Slice.class) {
                getter = GET_SLICE;
            } else {
                throw new UnsupportedOperationException("Unsupported type: " + elementType.getName());
            }
            MethodHandle cast = castFunction.getMethodHandle();
            // if the cast doesn't take a ConnectorSession, create an adapter that drops the provided session
            if (cast.type().parameterArray()[0] != ConnectorSession.class) {
                cast = MethodHandles.dropArguments(cast, 0, ConnectorSession.class);
            }
            // Adapt a target cast that takes (ConnectorSession, ?) to one that takes (Block, int, ConnectorSession), which will be invoked by the implementation
            // The first two arguments (Block, int) are filtered through the element type's getXXX method to produce the underlying value that needs to be passed to
            // the cast.
            cast = MethodHandles.permuteArguments(cast, MethodType.methodType(Slice.class, cast.type().parameterArray()[1], cast.type().parameterArray()[0]), 1, 0);
            cast = MethodHandles.dropArguments(cast, 1, int.class);
            cast = MethodHandles.dropArguments(cast, 1, Block.class);
            cast = MethodHandles.foldArguments(cast, getter.bindTo(type));
            MethodHandle target = MethodHandles.insertArguments(methodHandle, 0, cast);
            return new BuiltInScalarFunctionImplementation(false, argumentProperties, target, Optional.of(STATE_FACTORY));
        } catch (PrestoException e) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, String.format("Input type %s not supported", type), e);
        }
    }
}
Also used : BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) UnknownType(io.prestosql.spi.type.UnknownType) MethodHandle(java.lang.invoke.MethodHandle) Slice(io.airlift.slice.Slice) FunctionKind(io.prestosql.spi.function.FunctionKind) StandardTypes(io.prestosql.spi.type.StandardTypes) ArgumentProperty(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty) INVALID_FUNCTION_ARGUMENT(io.prestosql.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) BoundVariables(io.prestosql.metadata.BoundVariables) DEFAULT_NAMESPACE(io.prestosql.spi.connector.CatalogSchemaName.DEFAULT_NAMESPACE) USE_BOXED_TYPE(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.NullConvention.USE_BOXED_TYPE) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) VARCHAR(io.prestosql.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) Map(java.util.Map) RETURN_NULL_ON_NULL(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.NullConvention.RETURN_NULL_ON_NULL) Type(io.prestosql.spi.type.Type) Signature(io.prestosql.spi.function.Signature) Block(io.prestosql.spi.block.Block) PrestoException(io.prestosql.spi.PrestoException) CastType(io.prestosql.metadata.CastType) SqlScalarFunction(io.prestosql.metadata.SqlScalarFunction) BlockBuilder(io.prestosql.spi.block.BlockBuilder) MethodHandles(java.lang.invoke.MethodHandles) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Reflection.methodHandle(io.prestosql.spi.util.Reflection.methodHandle) PageBuilder(io.prestosql.spi.PageBuilder) Signature.typeVariable(io.prestosql.spi.function.Signature.typeVariable) List(java.util.List) MethodType(java.lang.invoke.MethodType) GENERIC_INTERNAL_ERROR(io.prestosql.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR) ArgumentProperty.valueTypeArgumentProperty(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty.valueTypeArgumentProperty) Optional(java.util.Optional) TypeSignature(io.prestosql.spi.type.TypeSignature) FunctionAndTypeManager(io.prestosql.metadata.FunctionAndTypeManager) ArgumentProperty(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty) ArgumentProperty.valueTypeArgumentProperty(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty.valueTypeArgumentProperty) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) PrestoException(io.prestosql.spi.PrestoException) UnknownType(io.prestosql.spi.type.UnknownType) UnknownType(io.prestosql.spi.type.UnknownType) Type(io.prestosql.spi.type.Type) CastType(io.prestosql.metadata.CastType) MethodType(java.lang.invoke.MethodType) Slice(io.airlift.slice.Slice) Block(io.prestosql.spi.block.Block) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with INVALID_FUNCTION_ARGUMENT

use of io.prestosql.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT in project hetu-core by openlookeng.

the class HanaRowExpressionConverter method handleOperatorFunction.

private String handleOperatorFunction(CallExpression call, FunctionMetadata functionMetadata, JdbcConverterContext context) {
    Optional<OperatorType> operatorTypeOptional = functionMetadata.getOperatorType();
    OperatorType type = operatorTypeOptional.get();
    if (type.equals(OperatorType.CAST)) {
        return handleCastOperator(call.getArguments().get(0), call.getType(), context);
    }
    List<String> argumentList = call.getArguments().stream().map(expr -> expr.accept(this, context)).collect(Collectors.toList());
    if (type.isArithmeticOperator()) {
        if (type.equals(OperatorType.MODULUS)) {
            return format("MOD(%s, %s)", argumentList.get(0), argumentList.get(1));
        } else {
            return format("(%s %s %s)", argumentList.get(0), type.getOperator(), argumentList.get(1));
        }
    }
    if (type.isComparisonOperator()) {
        final String[] hanaCompareOperators = new String[] { "=", ">", "<", ">=", "<=", "!=", "<>" };
        if (Arrays.asList(hanaCompareOperators).contains(type.getOperator())) {
            return format("(%s %s %s)", argumentList.get(0), type.getOperator(), argumentList.get(1));
        } else {
            String exceptionInfo = "Hana Connector does not support comparison operator " + type.getOperator();
            throw new PrestoException(NOT_SUPPORTED, exceptionInfo);
        }
    }
    if (type.equals(OperatorType.SUBSCRIPT)) {
        if (call.getArguments().size() == 2) {
            return format("MEMBER_AT(%s, %s)", argumentList.get(0), argumentList.get(1));
        }
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Illegal argument num of function " + type.getOperator());
    }
    if (call.getArguments().size() == 1 && type.equals(OperatorType.NEGATION)) {
        String value = argumentList.get(0);
        String separator = value.startsWith("-") ? " " : "";
        return format("-%s%s", separator, value);
    }
    throw new PrestoException(NOT_SUPPORTED, String.format("Unknown operator %s in push down", type.getOperator()));
}
Also used : Arrays(java.util.Arrays) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) INVALID_FUNCTION_ARGUMENT(io.prestosql.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) DeterminismEvaluator(io.prestosql.spi.relation.DeterminismEvaluator) CallExpression(io.prestosql.spi.relation.CallExpression) DateTimeFunctionCallRewriter(io.hetu.core.plugin.hana.rewrite.functioncall.DateTimeFunctionCallRewriter) ConfigSupplier(io.prestosql.configmanager.ConfigSupplier) OperatorType(io.prestosql.spi.function.OperatorType) Map(java.util.Map) FunctionMetadataManager(io.prestosql.spi.function.FunctionMetadataManager) FunctionMetadata(io.prestosql.spi.function.FunctionMetadata) Type(io.prestosql.spi.type.Type) FromBase64CallRewriter(io.prestosql.sql.builder.functioncall.functions.base.FromBase64CallRewriter) ENGLISH(java.util.Locale.ENGLISH) PrestoException(io.prestosql.spi.PrestoException) TIME(io.prestosql.spi.type.TimeType.TIME) Set(java.util.Set) FunctionCallRewriter(io.prestosql.sql.builder.functioncall.functions.FunctionCallRewriter) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) BuildInDirectMapFunctionCallRewriter(io.hetu.core.plugin.hana.rewrite.functioncall.BuildInDirectMapFunctionCallRewriter) FunctionHandle(io.prestosql.spi.function.FunctionHandle) RowExpressionService(io.prestosql.spi.relation.RowExpressionService) List(java.util.List) BaseJdbcRowExpressionConverter(io.prestosql.plugin.jdbc.optimization.BaseJdbcRowExpressionConverter) Stream(java.util.stream.Stream) ArrayConstructorCallRewriter(io.hetu.core.plugin.hana.rewrite.functioncall.ArrayConstructorCallRewriter) VarbinaryLiteralFunctionCallRewriter(io.hetu.core.plugin.hana.rewrite.functioncall.VarbinaryLiteralFunctionCallRewriter) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression) QualifiedName(io.prestosql.spi.sql.expression.QualifiedName) Optional(java.util.Optional) NOT_SUPPORTED(io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED) DefaultUdfRewriteConfigSupplier(io.prestosql.configmanager.DefaultUdfRewriteConfigSupplier) Joiner(com.google.common.base.Joiner) FunctionWriterManager(io.prestosql.sql.builder.functioncall.FunctionWriterManager) HanaConfig(io.hetu.core.plugin.hana.HanaConfig) Slice(io.airlift.slice.Slice) JdbcConverterContext(io.prestosql.plugin.jdbc.optimization.JdbcConverterContext) HashMap(java.util.HashMap) StandardFunctionResolution(io.prestosql.spi.function.StandardFunctionResolution) SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) HanaConstants(io.hetu.core.plugin.hana.HanaConstants) DateTimeUtils(io.prestosql.spi.util.DateTimeUtils) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) SpecialForm(io.prestosql.spi.relation.SpecialForm) HanaUnsupportedFunctionCallRewriter(io.hetu.core.plugin.hana.rewrite.functioncall.HanaUnsupportedFunctionCallRewriter) DefaultConnectorConfigFunctionRewriter(io.prestosql.sql.builder.functioncall.functions.config.DefaultConnectorConfigFunctionRewriter) VARBINARY(io.prestosql.spi.type.VarbinaryType.VARBINARY) UdfFunctionRewriteConstants(io.hetu.core.plugin.hana.rewrite.UdfFunctionRewriteConstants) FunctionWriterManagerGroup(io.prestosql.sql.builder.functioncall.FunctionWriterManagerGroup) DateAddFunctionCallRewrite(io.hetu.core.plugin.hana.rewrite.functioncall.DateAddFunctionCallRewrite) RowExpression(io.prestosql.spi.relation.RowExpression) Collections(java.util.Collections) VarcharType(io.prestosql.spi.type.VarcharType) PrestoException(io.prestosql.spi.PrestoException) OperatorType(io.prestosql.spi.function.OperatorType)

Aggregations

Slice (io.airlift.slice.Slice)2 PrestoException (io.prestosql.spi.PrestoException)2 INVALID_FUNCTION_ARGUMENT (io.prestosql.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT)2 Type (io.prestosql.spi.type.Type)2 Joiner (com.google.common.base.Joiner)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 HanaConfig (io.hetu.core.plugin.hana.HanaConfig)1 HanaConstants (io.hetu.core.plugin.hana.HanaConstants)1 UdfFunctionRewriteConstants (io.hetu.core.plugin.hana.rewrite.UdfFunctionRewriteConstants)1 ArrayConstructorCallRewriter (io.hetu.core.plugin.hana.rewrite.functioncall.ArrayConstructorCallRewriter)1 BuildInDirectMapFunctionCallRewriter (io.hetu.core.plugin.hana.rewrite.functioncall.BuildInDirectMapFunctionCallRewriter)1 DateAddFunctionCallRewrite (io.hetu.core.plugin.hana.rewrite.functioncall.DateAddFunctionCallRewrite)1 DateTimeFunctionCallRewriter (io.hetu.core.plugin.hana.rewrite.functioncall.DateTimeFunctionCallRewriter)1 HanaUnsupportedFunctionCallRewriter (io.hetu.core.plugin.hana.rewrite.functioncall.HanaUnsupportedFunctionCallRewriter)1 VarbinaryLiteralFunctionCallRewriter (io.hetu.core.plugin.hana.rewrite.functioncall.VarbinaryLiteralFunctionCallRewriter)1 ConfigSupplier (io.prestosql.configmanager.ConfigSupplier)1 DefaultUdfRewriteConfigSupplier (io.prestosql.configmanager.DefaultUdfRewriteConfigSupplier)1 BoundVariables (io.prestosql.metadata.BoundVariables)1