Search in sources :

Example 6 with TrinoException

use of io.trino.spi.TrinoException 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 7 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class MetadataManager method beginRefreshMaterializedView.

@Override
public InsertTableHandle beginRefreshMaterializedView(Session session, TableHandle tableHandle, List<TableHandle> sourceTableHandles) {
    CatalogName catalogName = tableHandle.getCatalogName();
    CatalogMetadata catalogMetadata = getCatalogMetadataForWrite(session, catalogName);
    ConnectorMetadata metadata = catalogMetadata.getMetadata(session);
    ConnectorTransactionHandle transactionHandle = catalogMetadata.getTransactionHandleFor(catalogName);
    List<ConnectorTableHandle> sourceConnectorHandles = sourceTableHandles.stream().map(TableHandle::getConnectorHandle).collect(Collectors.toList());
    sourceConnectorHandles.add(tableHandle.getConnectorHandle());
    if (sourceConnectorHandles.stream().map(Object::getClass).distinct().count() > 1) {
        throw new TrinoException(NOT_SUPPORTED, "Cross connector materialized views are not supported");
    }
    ConnectorInsertTableHandle handle = metadata.beginRefreshMaterializedView(session.toConnectorSession(catalogName), tableHandle.getConnectorHandle(), sourceConnectorHandles, getRetryPolicy(session).getRetryMode());
    return new InsertTableHandle(tableHandle.getCatalogName(), transactionHandle, handle);
}
Also used : ConnectorInsertTableHandle(io.trino.spi.connector.ConnectorInsertTableHandle) ConnectorTransactionHandle(io.trino.spi.connector.ConnectorTransactionHandle) TrinoException(io.trino.spi.TrinoException) CatalogName(io.trino.connector.CatalogName) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata) ConnectorInsertTableHandle(io.trino.spi.connector.ConnectorInsertTableHandle) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle)

Example 8 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class MetadataManager method getCoercion.

@Override
public ResolvedFunction getCoercion(Session session, OperatorType operatorType, Type fromType, Type toType) {
    checkArgument(operatorType == OperatorType.CAST || operatorType == OperatorType.SATURATED_FLOOR_CAST);
    try {
        // todo we should not be caching functions across session
        return uncheckedCacheGet(coercionCache, new CoercionCacheKey(operatorType, fromType, toType), () -> {
            String name = mangleOperatorName(operatorType);
            FunctionBinding functionBinding = functionResolver.resolveCoercion(session, functions.getFunctions(QualifiedName.of(name)), new Signature(name, toType.getTypeSignature(), ImmutableList.of(fromType.getTypeSignature())));
            return resolve(session, functionBinding);
        });
    } catch (UncheckedExecutionException e) {
        if (e.getCause() instanceof TrinoException) {
            TrinoException cause = (TrinoException) e.getCause();
            if (cause.getErrorCode().getCode() == FUNCTION_IMPLEMENTATION_MISSING.toErrorCode().getCode()) {
                throw new OperatorNotFoundException(operatorType, ImmutableList.of(fromType), toType.getTypeSignature(), cause);
            }
            throw cause;
        }
        throw e;
    }
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) TypeSignature(io.trino.spi.type.TypeSignature) TrinoException(io.trino.spi.TrinoException)

Example 9 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class MetadataManager method getMaterializedView.

@Override
public Optional<MaterializedViewDefinition> getMaterializedView(Session session, QualifiedObjectName viewName) {
    Optional<ConnectorMaterializedViewDefinition> connectorView = getMaterializedViewInternal(session, viewName);
    if (connectorView.isEmpty() || isCatalogManagedSecurity(session, viewName.getCatalogName())) {
        return connectorView.map(view -> {
            String runAsUser = view.getOwner().orElseThrow(() -> new TrinoException(INVALID_VIEW, "Owner not set for a run-as invoker view: " + viewName));
            return new MaterializedViewDefinition(view, Identity.ofUser(runAsUser));
        });
    }
    Identity runAsIdentity = systemSecurityMetadata.getViewRunAsIdentity(session, viewName.asCatalogSchemaTableName()).or(() -> connectorView.get().getOwner().map(Identity::ofUser)).orElseThrow(() -> new TrinoException(NOT_SUPPORTED, "Materialized view does not have an owner: " + viewName));
    return Optional.of(new MaterializedViewDefinition(connectorView.get(), runAsIdentity));
}
Also used : ConnectorMaterializedViewDefinition(io.trino.spi.connector.ConnectorMaterializedViewDefinition) ConnectorMaterializedViewDefinition(io.trino.spi.connector.ConnectorMaterializedViewDefinition) TrinoException(io.trino.spi.TrinoException) Identity(io.trino.spi.security.Identity)

Example 10 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class MetadataManager method getSchemaProperties.

@Override
public Map<String, Object> getSchemaProperties(Session session, CatalogSchemaName schemaName) {
    if (!schemaExists(session, schemaName)) {
        throw new TrinoException(SCHEMA_NOT_FOUND, format("Schema '%s' does not exist", schemaName));
    }
    CatalogMetadata catalogMetadata = getCatalogMetadata(session, new CatalogName(schemaName.getCatalogName()));
    CatalogName catalogName = catalogMetadata.getConnectorIdForSchema(schemaName);
    ConnectorMetadata metadata = catalogMetadata.getMetadataFor(session, catalogName);
    ConnectorSession connectorSession = session.toConnectorSession(catalogName);
    return metadata.getSchemaProperties(connectorSession, schemaName);
}
Also used : TrinoException(io.trino.spi.TrinoException) CatalogName(io.trino.connector.CatalogName) ConnectorSession(io.trino.spi.connector.ConnectorSession) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata)

Aggregations

TrinoException (io.trino.spi.TrinoException)623 IOException (java.io.IOException)151 ImmutableList (com.google.common.collect.ImmutableList)105 List (java.util.List)100 Type (io.trino.spi.type.Type)93 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)90 SchemaTableName (io.trino.spi.connector.SchemaTableName)83 Path (org.apache.hadoop.fs.Path)83 Optional (java.util.Optional)79 ArrayList (java.util.ArrayList)77 Map (java.util.Map)76 ImmutableMap (com.google.common.collect.ImmutableMap)70 Objects.requireNonNull (java.util.Objects.requireNonNull)69 ConnectorSession (io.trino.spi.connector.ConnectorSession)63 TableNotFoundException (io.trino.spi.connector.TableNotFoundException)62 ImmutableSet (com.google.common.collect.ImmutableSet)56 VarcharType (io.trino.spi.type.VarcharType)54 Set (java.util.Set)54 Slice (io.airlift.slice.Slice)53 Table (io.trino.plugin.hive.metastore.Table)52