Search in sources :

Example 1 with Type

use of io.trino.spi.type.Type in project TiBigData by tidb-incubator.

the class TiDBPageSink method appendColumn.

private void appendColumn(Page page, int position, int channel) throws SQLException {
    Block block = page.getBlock(channel);
    int parameter = channel + 1;
    if (block.isNull(position)) {
        statement.setObject(parameter, null);
        return;
    }
    Type type = columnTypes.get(channel);
    switch(type.getDisplayName()) {
        case "boolean":
            statement.setBoolean(parameter, type.getBoolean(block, position));
            break;
        case "tinyint":
            statement.setByte(parameter, SignedBytes.checkedCast(type.getLong(block, position)));
            break;
        case "smallint":
            statement.setShort(parameter, Shorts.checkedCast(type.getLong(block, position)));
            break;
        case "integer":
            statement.setInt(parameter, toIntExact(type.getLong(block, position)));
            break;
        case "bigint":
            statement.setLong(parameter, type.getLong(block, position));
            break;
        case "real":
            statement.setFloat(parameter, intBitsToFloat(toIntExact(type.getLong(block, position))));
            break;
        case "double":
            statement.setDouble(parameter, type.getDouble(block, position));
            break;
        case "date":
            // convert to midnight in default time zone
            long utcMillis = DAYS.toMillis(type.getLong(block, position));
            long localMillis = getInstanceUTC().getZone().getMillisKeepLocal(DateTimeZone.getDefault(), utcMillis);
            statement.setDate(parameter, new Date(localMillis));
            break;
        case "varbinary":
            statement.setBytes(parameter, type.getSlice(block, position).getBytes());
            break;
        default:
            if (type instanceof DecimalType) {
                statement.setBigDecimal(parameter, readBigDecimal((DecimalType) type, block, position));
            } else if (type instanceof VarcharType || type instanceof CharType) {
                statement.setString(parameter, type.getSlice(block, position).toStringUtf8());
            } else if (type instanceof TimestampType) {
                statement.setTimestamp(parameter, new Timestamp(type.getLong(block, position) / 1000 - TimeZone.getDefault().getRawOffset()));
            } else if (type instanceof TimeType) {
                statement.setTime(parameter, new Time(type.getLong(block, position)));
            } else {
                throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
            }
    }
}
Also used : VarcharType(io.trino.spi.type.VarcharType) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date) TimeType(io.trino.spi.type.TimeType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) CharType(io.trino.spi.type.CharType) DecimalType(io.trino.spi.type.DecimalType) Block(io.trino.spi.block.Block) DecimalType(io.trino.spi.type.DecimalType) TimestampType(io.trino.spi.type.TimestampType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType)

Example 2 with Type

use of io.trino.spi.type.Type in project trino by trinodb.

the class MetadataManager method resolve.

@VisibleForTesting
public ResolvedFunction resolve(Session session, FunctionBinding functionBinding, FunctionMetadata functionMetadata, FunctionDependencyDeclaration declaration) {
    Map<TypeSignature, Type> dependentTypes = declaration.getTypeDependencies().stream().map(typeSignature -> applyBoundVariables(typeSignature, functionBinding)).collect(toImmutableMap(Function.identity(), typeManager::getType, (left, right) -> left));
    ImmutableSet.Builder<ResolvedFunction> functions = ImmutableSet.builder();
    declaration.getFunctionDependencies().stream().map(functionDependency -> {
        try {
            List<TypeSignature> argumentTypes = applyBoundVariables(functionDependency.getArgumentTypes(), functionBinding);
            return resolvedFunctionInternal(session, functionDependency.getName(), fromTypeSignatures(argumentTypes));
        } catch (TrinoException e) {
            if (functionDependency.isOptional()) {
                return null;
            }
            throw e;
        }
    }).filter(Objects::nonNull).forEach(functions::add);
    declaration.getOperatorDependencies().stream().map(operatorDependency -> {
        try {
            List<TypeSignature> argumentTypes = applyBoundVariables(operatorDependency.getArgumentTypes(), functionBinding);
            return resolvedFunctionInternal(session, QualifiedName.of(mangleOperatorName(operatorDependency.getOperatorType())), fromTypeSignatures(argumentTypes));
        } catch (TrinoException e) {
            if (operatorDependency.isOptional()) {
                return null;
            }
            throw e;
        }
    }).filter(Objects::nonNull).forEach(functions::add);
    declaration.getCastDependencies().stream().map(castDependency -> {
        try {
            Type fromType = typeManager.getType(applyBoundVariables(castDependency.getFromType(), functionBinding));
            Type toType = typeManager.getType(applyBoundVariables(castDependency.getToType(), functionBinding));
            return getCoercion(session, fromType, toType);
        } catch (TrinoException e) {
            if (castDependency.isOptional()) {
                return null;
            }
            throw e;
        }
    }).filter(Objects::nonNull).forEach(functions::add);
    return new ResolvedFunction(functionBinding.getBoundSignature(), functionBinding.getFunctionId(), functionMetadata.getKind(), functionMetadata.isDeterministic(), functionMetadata.getFunctionNullability(), dependentTypes, functions.build());
}
Also used : RedirectionAwareTableHandle.noRedirection(io.trino.metadata.RedirectionAwareTableHandle.noRedirection) TransactionManager(io.trino.transaction.TransactionManager) AggregateFunction(io.trino.spi.connector.AggregateFunction) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) TypeOperators(io.trino.spi.type.TypeOperators) ConnectorTableExecuteHandle(io.trino.spi.connector.ConnectorTableExecuteHandle) Collections.singletonList(java.util.Collections.singletonList) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) CatalogName(io.trino.connector.CatalogName) SystemSessionProperties.getRetryPolicy(io.trino.SystemSessionProperties.getRetryPolicy) ConnectorOutputTableHandle(io.trino.spi.connector.ConnectorOutputTableHandle) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) Map(java.util.Map) ProjectionApplicationResult(io.trino.spi.connector.ProjectionApplicationResult) ENGLISH(java.util.Locale.ENGLISH) TABLE_REDIRECTION_ERROR(io.trino.spi.StandardErrorCode.TABLE_REDIRECTION_ERROR) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) MoreFutures.toListenableFuture(io.airlift.concurrent.MoreFutures.toListenableFuture) Set(java.util.Set) ConnectorExpressions(io.trino.sql.planner.ConnectorExpressions) LimitApplicationResult(io.trino.spi.connector.LimitApplicationResult) GuardedBy(javax.annotation.concurrent.GuardedBy) SchemaTableName(io.trino.spi.connector.SchemaTableName) MoreExecutors.directExecutor(com.google.common.util.concurrent.MoreExecutors.directExecutor) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Stream(java.util.stream.Stream) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) ConnectorPartitioningHandle(io.trino.spi.connector.ConnectorPartitioningHandle) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) SchemaTablePrefix(io.trino.spi.connector.SchemaTablePrefix) TableScanRedirectApplicationResult(io.trino.spi.connector.TableScanRedirectApplicationResult) FUNCTION_IMPLEMENTATION_MISSING(io.trino.spi.StandardErrorCode.FUNCTION_IMPLEMENTATION_MISSING) TableColumnsMetadata(io.trino.spi.connector.TableColumnsMetadata) INVALID_VIEW(io.trino.spi.StandardErrorCode.INVALID_VIEW) Session(io.trino.Session) Verify.verifyNotNull(com.google.common.base.Verify.verifyNotNull) ConnectorResolvedIndex(io.trino.spi.connector.ConnectorResolvedIndex) ConnectorInsertTableHandle(io.trino.spi.connector.ConnectorInsertTableHandle) JoinCondition(io.trino.spi.connector.JoinCondition) Slice(io.airlift.slice.Slice) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) QualifiedObjectName.convertFromSchemaTableName(io.trino.metadata.QualifiedObjectName.convertFromSchemaTableName) Variable(io.trino.spi.expression.Variable) TypeSignatureProvider.fromTypeSignatures(io.trino.sql.analyzer.TypeSignatureProvider.fromTypeSignatures) InMemoryTransactionManager.createTestTransactionManager(io.trino.transaction.InMemoryTransactionManager.createTestTransactionManager) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) OptionalLong(java.util.OptionalLong) Signature.mangleOperatorName(io.trino.metadata.Signature.mangleOperatorName) GrantInfo(io.trino.spi.security.GrantInfo) MaterializedViewFreshness(io.trino.spi.connector.MaterializedViewFreshness) Identity(io.trino.spi.security.Identity) ColumnHandle(io.trino.spi.connector.ColumnHandle) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) LinkedHashSet(java.util.LinkedHashSet) SCHEMA_NOT_FOUND(io.trino.spi.StandardErrorCode.SCHEMA_NOT_FOUND) ConstraintApplicationResult(io.trino.spi.connector.ConstraintApplicationResult) BlockTypeOperators(io.trino.type.BlockTypeOperators) ConnectorCapabilities(io.trino.spi.connector.ConnectorCapabilities) TESTING_TYPE_MANAGER(io.trino.type.InternalTypeManager.TESTING_TYPE_MANAGER) ConnectorSession(io.trino.spi.connector.ConnectorSession) RoleGrant(io.trino.spi.security.RoleGrant) OperatorType(io.trino.spi.function.OperatorType) Futures(com.google.common.util.concurrent.Futures) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) TypeSignatureProvider(io.trino.sql.analyzer.TypeSignatureProvider) TableStatisticsMetadata(io.trino.spi.statistics.TableStatisticsMetadata) QueryId(io.trino.spi.QueryId) TypeNotFoundException(io.trino.spi.type.TypeNotFoundException) SortItem(io.trino.spi.connector.SortItem) SecurityManagement(io.trino.metadata.Catalog.SecurityManagement) TopNApplicationResult(io.trino.spi.connector.TopNApplicationResult) ConnectorMaterializedViewDefinition(io.trino.spi.connector.ConnectorMaterializedViewDefinition) RedirectionAwareTableHandle.withRedirectionTo(io.trino.metadata.RedirectionAwareTableHandle.withRedirectionTo) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Locale(java.util.Locale) CatalogSchemaName(io.trino.spi.connector.CatalogSchemaName) ConnectorViewDefinition(io.trino.spi.connector.ConnectorViewDefinition) SignatureBinder.applyBoundVariables(io.trino.metadata.SignatureBinder.applyBoundVariables) SampleType(io.trino.spi.connector.SampleType) SampleApplicationResult(io.trino.spi.connector.SampleApplicationResult) TypeSignature(io.trino.spi.type.TypeSignature) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) FUNCTION_NOT_FOUND(io.trino.spi.StandardErrorCode.FUNCTION_NOT_FOUND) Collections.nCopies(java.util.Collections.nCopies) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ComputedStatistics(io.trino.spi.statistics.ComputedStatistics) PartitioningHandle(io.trino.sql.planner.PartitioningHandle) TrinoException(io.trino.spi.TrinoException) Streams(com.google.common.collect.Streams) AGGREGATE(io.trino.metadata.FunctionKind.AGGREGATE) ConnectorOutputMetadata(io.trino.spi.connector.ConnectorOutputMetadata) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ConnectorTableSchema(io.trino.spi.connector.ConnectorTableSchema) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) SafeCaches.buildNonEvictableCache(io.trino.collect.cache.SafeCaches.buildNonEvictableCache) List(java.util.List) UNKNOWN(io.trino.client.NodeVersion.UNKNOWN) Assignment(io.trino.spi.connector.Assignment) BeginTableExecuteResult(io.trino.spi.connector.BeginTableExecuteResult) Entry(java.util.Map.Entry) Optional(java.util.Optional) CacheBuilder(com.google.common.cache.CacheBuilder) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata) SystemTable(io.trino.spi.connector.SystemTable) SYNTAX_ERROR(io.trino.spi.StandardErrorCode.SYNTAX_ERROR) Constraint(io.trino.spi.connector.Constraint) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Type(io.trino.spi.type.Type) HashMap(java.util.HashMap) Function(java.util.function.Function) ConnectorTableVersion(io.trino.spi.connector.ConnectorTableVersion) ConcurrentMap(java.util.concurrent.ConcurrentMap) Inject(javax.inject.Inject) JoinStatistics(io.trino.spi.connector.JoinStatistics) ImmutableList(com.google.common.collect.ImmutableList) JoinType(io.trino.spi.connector.JoinType) Verify.verify(com.google.common.base.Verify.verify) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Objects.requireNonNull(java.util.Objects.requireNonNull) TableStatistics(io.trino.spi.statistics.TableStatistics) CacheUtils.uncheckedCacheGet(io.trino.collect.cache.CacheUtils.uncheckedCacheGet) NonEvictableCache(io.trino.collect.cache.NonEvictableCache) Privilege(io.trino.spi.security.Privilege) ResolvedFunctionDecoder(io.trino.metadata.ResolvedFunction.ResolvedFunctionDecoder) TupleDomain(io.trino.spi.predicate.TupleDomain) AggregationApplicationResult(io.trino.spi.connector.AggregationApplicationResult) QualifiedName(io.trino.sql.tree.QualifiedName) JoinApplicationResult(io.trino.spi.connector.JoinApplicationResult) FeaturesConfig(io.trino.FeaturesConfig) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TypeManager(io.trino.spi.type.TypeManager) ConnectorTransactionHandle(io.trino.spi.connector.ConnectorTransactionHandle) TypeSignature(io.trino.spi.type.TypeSignature) OperatorType(io.trino.spi.function.OperatorType) SampleType(io.trino.spi.connector.SampleType) Type(io.trino.spi.type.Type) JoinType(io.trino.spi.connector.JoinType) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) TrinoException(io.trino.spi.TrinoException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with Type

use of io.trino.spi.type.Type in project trino by trinodb.

the class FunctionManager method verifyMethodHandleSignature.

private static void verifyMethodHandleSignature(BoundSignature boundSignature, FunctionInvoker functionInvoker, InvocationConvention convention) {
    MethodHandle methodHandle = functionInvoker.getMethodHandle();
    MethodType methodType = methodHandle.type();
    checkArgument(convention.getArgumentConventions().size() == boundSignature.getArgumentTypes().size(), "Expected %s arguments, but got %s", boundSignature.getArgumentTypes().size(), convention.getArgumentConventions().size());
    int expectedParameterCount = convention.getArgumentConventions().stream().mapToInt(InvocationArgumentConvention::getParameterCount).sum();
    expectedParameterCount += methodType.parameterList().stream().filter(ConnectorSession.class::equals).count();
    if (functionInvoker.getInstanceFactory().isPresent()) {
        expectedParameterCount++;
    }
    checkArgument(expectedParameterCount == methodType.parameterCount(), "Expected %s method parameters, but got %s", expectedParameterCount, methodType.parameterCount());
    int parameterIndex = 0;
    if (functionInvoker.getInstanceFactory().isPresent()) {
        verifyFunctionSignature(convention.supportsInstanceFactor(), "Method requires instance factory, but calling convention does not support an instance factory");
        MethodHandle factoryMethod = functionInvoker.getInstanceFactory().orElseThrow();
        verifyFunctionSignature(methodType.parameterType(parameterIndex).equals(factoryMethod.type().returnType()), "Invalid return type");
        parameterIndex++;
    }
    int lambdaArgumentIndex = 0;
    for (int argumentIndex = 0; argumentIndex < boundSignature.getArgumentTypes().size(); argumentIndex++) {
        // skip session parameters
        while (methodType.parameterType(parameterIndex).equals(ConnectorSession.class)) {
            verifyFunctionSignature(convention.supportsSession(), "Method requires session, but calling convention does not support session");
            parameterIndex++;
        }
        Class<?> parameterType = methodType.parameterType(parameterIndex);
        Type argumentType = boundSignature.getArgumentTypes().get(argumentIndex);
        InvocationArgumentConvention argumentConvention = convention.getArgumentConvention(argumentIndex);
        switch(argumentConvention) {
            case NEVER_NULL:
                verifyFunctionSignature(parameterType.isAssignableFrom(argumentType.getJavaType()), "Expected argument type to be %s, but is %s", argumentType, parameterType);
                break;
            case NULL_FLAG:
                verifyFunctionSignature(parameterType.isAssignableFrom(argumentType.getJavaType()), "Expected argument type to be %s, but is %s", argumentType.getJavaType(), parameterType);
                verifyFunctionSignature(methodType.parameterType(parameterIndex + 1).equals(boolean.class), "Expected null flag parameter to be followed by a boolean parameter");
                break;
            case BOXED_NULLABLE:
                verifyFunctionSignature(parameterType.isAssignableFrom(wrap(argumentType.getJavaType())), "Expected argument type to be %s, but is %s", wrap(argumentType.getJavaType()), parameterType);
                break;
            case BLOCK_POSITION:
                verifyFunctionSignature(parameterType.equals(Block.class) && methodType.parameterType(parameterIndex + 1).equals(int.class), "Expected BLOCK_POSITION argument have parameters Block and int");
                break;
            case FUNCTION:
                Class<?> lambdaInterface = functionInvoker.getLambdaInterfaces().get(lambdaArgumentIndex);
                verifyFunctionSignature(parameterType.equals(lambdaInterface), "Expected function interface to be %s, but is %s", lambdaInterface, parameterType);
                lambdaArgumentIndex++;
                break;
            default:
                throw new UnsupportedOperationException("Unknown argument convention: " + argumentConvention);
        }
        parameterIndex += argumentConvention.getParameterCount();
    }
    Type returnType = boundSignature.getReturnType();
    switch(convention.getReturnConvention()) {
        case FAIL_ON_NULL:
            verifyFunctionSignature(methodType.returnType().isAssignableFrom(returnType.getJavaType()), "Expected return type to be %s, but is %s", returnType.getJavaType(), methodType.returnType());
            break;
        case NULLABLE_RETURN:
            verifyFunctionSignature(methodType.returnType().isAssignableFrom(wrap(returnType.getJavaType())), "Expected return type to be %s, but is %s", returnType.getJavaType(), wrap(methodType.returnType()));
            break;
        default:
            throw new UnsupportedOperationException("Unknown return convention: " + convention.getReturnConvention());
    }
}
Also used : MethodType(java.lang.invoke.MethodType) Type(io.trino.spi.type.Type) MethodType(java.lang.invoke.MethodType) InvocationArgumentConvention(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention) ConnectorSession(io.trino.spi.connector.ConnectorSession) MethodHandle(java.lang.invoke.MethodHandle)

Example 4 with Type

use of io.trino.spi.type.Type in project trino by trinodb.

the class GlobalFunctionCatalog method checkNotSpecializedTypeOperator.

/**
 * Type operators are handled automatically by the engine, so custom operator implementations
 * cannot be registered for these.
 */
private static void checkNotSpecializedTypeOperator(Signature signature) {
    String name = signature.getName();
    if (!isOperatorName(name)) {
        return;
    }
    OperatorType operatorType = unmangleOperator(name);
    // The trick here is the Generic*Operator implementations implement these exact signatures,
    // so we only these exact signatures to be registered.  Since, only a single function with
    // a specific signature can be registered, it prevents others from being registered.
    Type expectedReturnType;
    TypeVariableConstraint typeParameter;
    switch(operatorType) {
        case EQUAL:
        case IS_DISTINCT_FROM:
        case INDETERMINATE:
            expectedReturnType = BOOLEAN;
            typeParameter = comparableTypeParameter("T");
            break;
        case HASH_CODE:
        case XX_HASH_64:
            expectedReturnType = BIGINT;
            typeParameter = comparableTypeParameter("T");
            break;
        case COMPARISON_UNORDERED_FIRST:
        case COMPARISON_UNORDERED_LAST:
            expectedReturnType = INTEGER;
            typeParameter = orderableTypeParameter("T");
            break;
        case LESS_THAN:
        case LESS_THAN_OR_EQUAL:
            expectedReturnType = BOOLEAN;
            typeParameter = orderableTypeParameter("T");
            break;
        default:
            return;
    }
    Signature expectedSignature = new Signature(signature.getName(), ImmutableList.of(typeParameter), ImmutableList.of(), expectedReturnType.getTypeSignature(), Collections.nCopies(operatorType.getArgumentCount(), new TypeSignature("T")), false);
    checkArgument(signature.equals(expectedSignature), "Can not register %s functionMetadata: %s", operatorType, signature);
}
Also used : Type(io.trino.spi.type.Type) OperatorType(io.trino.spi.function.OperatorType) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) OperatorType(io.trino.spi.function.OperatorType)

Example 5 with Type

use of io.trino.spi.type.Type in project trino by trinodb.

the class InternalBlockEncodingSerde method readType.

@Override
public Type readType(SliceInput sliceInput) {
    requireNonNull(sliceInput, "sliceInput is null");
    String id = readLengthPrefixedString(sliceInput);
    Type type = types.apply(TypeId.of(id));
    if (type == null) {
        throw new IllegalArgumentException("Unknown type " + id);
    }
    return type;
}
Also used : Type(io.trino.spi.type.Type)

Aggregations

Type (io.trino.spi.type.Type)688 Test (org.testng.annotations.Test)266 ArrayType (io.trino.spi.type.ArrayType)218 ImmutableList (com.google.common.collect.ImmutableList)191 RowType (io.trino.spi.type.RowType)177 List (java.util.List)155 VarcharType (io.trino.spi.type.VarcharType)134 Page (io.trino.spi.Page)126 ArrayList (java.util.ArrayList)126 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)114 Block (io.trino.spi.block.Block)110 MapType (io.trino.spi.type.MapType)107 DecimalType (io.trino.spi.type.DecimalType)102 TrinoException (io.trino.spi.TrinoException)98 Optional (java.util.Optional)98 Map (java.util.Map)97 ImmutableMap (com.google.common.collect.ImmutableMap)93 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)92 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)86 BlockBuilder (io.trino.spi.block.BlockBuilder)72