Search in sources :

Example 1 with UnknownType

use of io.trino.type.UnknownType in project trino by trinodb.

the class LogicalPlanner method noTruncationCast.

/*
    According to the standard, for the purpose of store assignment (INSERT),
    no non-space characters of a character string, and no non-zero octets
    of a binary string must be lost when the inserted value is truncated to
    fit in the target column type.
    The following method returns a cast from source type to target type
    with a guarantee of no illegal truncation.
    TODO Once BINARY and parametric VARBINARY types are supported, they should be handled here.
    TODO This workaround is insufficient to handle structural types
     */
private Expression noTruncationCast(Expression expression, Type fromType, Type toType) {
    if (fromType instanceof UnknownType || (!(toType instanceof VarcharType) && !(toType instanceof CharType))) {
        return new Cast(expression, toSqlType(toType));
    }
    int targetLength;
    if (toType instanceof VarcharType) {
        if (((VarcharType) toType).isUnbounded()) {
            return new Cast(expression, toSqlType(toType));
        }
        targetLength = ((VarcharType) toType).getBoundedLength();
    } else {
        targetLength = ((CharType) toType).getLength();
    }
    checkState(fromType instanceof VarcharType || fromType instanceof CharType, "inserting non-character value to column of character type");
    ResolvedFunction spaceTrimmedLength = metadata.resolveFunction(session, QualifiedName.of("$space_trimmed_length"), fromTypes(VARCHAR));
    ResolvedFunction fail = metadata.resolveFunction(session, QualifiedName.of("fail"), fromTypes(VARCHAR));
    return new IfExpression(// check if the trimmed value fits in the target type
    new ComparisonExpression(GREATER_THAN_OR_EQUAL, new GenericLiteral("BIGINT", Integer.toString(targetLength)), new CoalesceExpression(new FunctionCall(spaceTrimmedLength.toQualifiedName(), ImmutableList.of(new Cast(expression, toSqlType(VARCHAR)))), new GenericLiteral("BIGINT", "0"))), new Cast(expression, toSqlType(toType)), new Cast(new FunctionCall(fail.toQualifiedName(), ImmutableList.of(new Cast(new StringLiteral(format("Cannot truncate non-space characters when casting from %s to %s on INSERT", fromType.getDisplayName(), toType.getDisplayName())), toSqlType(VARCHAR)))), toSqlType(toType)));
}
Also used : UnknownType(io.trino.type.UnknownType) Cast(io.trino.sql.tree.Cast) IfExpression(io.trino.sql.tree.IfExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) StringLiteral(io.trino.sql.tree.StringLiteral) VarcharType(io.trino.spi.type.VarcharType) ResolvedFunction(io.trino.metadata.ResolvedFunction) CharType(io.trino.spi.type.CharType) FunctionCall(io.trino.sql.tree.FunctionCall) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) GenericLiteral(io.trino.sql.tree.GenericLiteral)

Example 2 with UnknownType

use of io.trino.type.UnknownType in project trino by trinodb.

the class ArrayJoin method specializeArrayJoin.

private static ChoicesScalarFunctionImplementation specializeArrayJoin(BoundSignature boundSignature, FunctionDependencies functionDependencies, MethodHandle methodHandle) {
    List<InvocationArgumentConvention> argumentConventions = Collections.nCopies(boundSignature.getArity(), NEVER_NULL);
    Type type = ((ArrayType) boundSignature.getArgumentTypes().get(0)).getElementType();
    if (type instanceof UnknownType) {
        return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, argumentConventions, methodHandle.bindTo(null), Optional.of(STATE_FACTORY));
    } else {
        try {
            InvocationConvention convention = new InvocationConvention(ImmutableList.of(BLOCK_POSITION), NULLABLE_RETURN, true, false);
            MethodHandle cast = functionDependencies.getCastInvoker(type, VARCHAR, convention).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);
            }
            MethodHandle target = MethodHandles.insertArguments(methodHandle, 0, cast);
            return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, argumentConventions, target, Optional.of(STATE_FACTORY));
        } catch (TrinoException e) {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Input type %s not supported", type), e);
        }
    }
}
Also used : ArrayType(io.trino.spi.type.ArrayType) UnknownType(io.trino.type.UnknownType) Type(io.trino.spi.type.Type) TypeSignature.arrayType(io.trino.spi.type.TypeSignature.arrayType) ArrayType(io.trino.spi.type.ArrayType) UnknownType(io.trino.type.UnknownType) InvocationArgumentConvention(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention) InvocationConvention(io.trino.spi.function.InvocationConvention) TrinoException(io.trino.spi.TrinoException) ConnectorSession(io.trino.spi.connector.ConnectorSession) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

UnknownType (io.trino.type.UnknownType)2 ResolvedFunction (io.trino.metadata.ResolvedFunction)1 TrinoException (io.trino.spi.TrinoException)1 ConnectorSession (io.trino.spi.connector.ConnectorSession)1 InvocationConvention (io.trino.spi.function.InvocationConvention)1 InvocationArgumentConvention (io.trino.spi.function.InvocationConvention.InvocationArgumentConvention)1 ArrayType (io.trino.spi.type.ArrayType)1 CharType (io.trino.spi.type.CharType)1 Type (io.trino.spi.type.Type)1 TypeSignature.arrayType (io.trino.spi.type.TypeSignature.arrayType)1 VarcharType (io.trino.spi.type.VarcharType)1 Cast (io.trino.sql.tree.Cast)1 CoalesceExpression (io.trino.sql.tree.CoalesceExpression)1 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)1 FunctionCall (io.trino.sql.tree.FunctionCall)1 GenericLiteral (io.trino.sql.tree.GenericLiteral)1 IfExpression (io.trino.sql.tree.IfExpression)1 StringLiteral (io.trino.sql.tree.StringLiteral)1 MethodHandle (java.lang.invoke.MethodHandle)1