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());
}
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);
}
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;
}
}
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));
}
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);
}
Aggregations