use of io.trino.spi.TrinoException in project trino by trinodb.
the class SessionPropertyManager method decodeCatalogPropertyValue.
public <T> T decodeCatalogPropertyValue(CatalogName catalog, String catalogName, String propertyName, @Nullable String propertyValue, Class<T> type) {
String fullPropertyName = catalogName + "." + propertyName;
PropertyMetadata<?> property = getConnectorSessionPropertyMetadata(catalog, propertyName).orElseThrow(() -> new TrinoException(INVALID_SESSION_PROPERTY, "Unknown session property " + fullPropertyName));
return decodePropertyValue(fullPropertyName, propertyValue, type, property);
}
use of io.trino.spi.TrinoException in project trino by trinodb.
the class PropertyUtil method evaluateProperties.
public static Map<String, Optional<Object>> evaluateProperties(Iterable<Property> setProperties, Session session, PlannerContext plannerContext, AccessControl accessControl, Map<NodeRef<Parameter>, Expression> parameters, boolean includeAllProperties, Map<String, PropertyMetadata<?>> metadata, ErrorCodeSupplier errorCode, String propertyTypeDescription) {
Map<String, Optional<Object>> propertyValues = new LinkedHashMap<>();
// Fill in user-specified properties
for (Property property : setProperties) {
// property names are case-insensitive and normalized to lower case
String propertyName = property.getName().getValue().toLowerCase(ENGLISH);
PropertyMetadata<?> propertyMetadata = metadata.get(propertyName);
if (propertyMetadata == null) {
throw new TrinoException(errorCode, format("%s '%s' does not exist", capitalize(propertyTypeDescription), propertyName));
}
Optional<Object> value;
if (property.isSetToDefault()) {
value = Optional.ofNullable(propertyMetadata.getDefaultValue());
} else {
value = Optional.of(evaluateProperty(property.getNonDefaultValue(), propertyMetadata, session, plannerContext, accessControl, parameters, errorCode, propertyTypeDescription));
}
propertyValues.put(propertyMetadata.getName(), value);
}
if (includeAllProperties) {
for (PropertyMetadata<?> propertyMetadata : metadata.values()) {
if (!propertyValues.containsKey(propertyMetadata.getName())) {
propertyValues.put(propertyMetadata.getName(), Optional.ofNullable(propertyMetadata.getDefaultValue()));
}
}
}
return ImmutableMap.copyOf(propertyValues);
}
use of io.trino.spi.TrinoException in project trino by trinodb.
the class PropertyUtil method evaluateProperty.
private static Object evaluateProperty(Expression expression, PropertyMetadata<?> property, Session session, PlannerContext plannerContext, AccessControl accessControl, Map<NodeRef<Parameter>, Expression> parameters, ErrorCodeSupplier errorCode, String propertyTypeDescription) {
Object sqlObjectValue;
try {
Type expectedType = property.getSqlType();
Expression rewritten = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(parameters), expression);
Object value = evaluateConstantExpression(rewritten, expectedType, plannerContext, session, accessControl, parameters);
// convert to object value type of SQL type
BlockBuilder blockBuilder = expectedType.createBlockBuilder(null, 1);
writeNativeValue(expectedType, blockBuilder, value);
sqlObjectValue = expectedType.getObjectValue(session.toConnectorSession(), blockBuilder, 0);
} catch (TrinoException e) {
throw new TrinoException(errorCode, format("Invalid value for %s '%s': Cannot convert [%s] to %s", propertyTypeDescription, property.getName(), expression, property.getSqlType()), e);
}
if (sqlObjectValue == null) {
throw new TrinoException(errorCode, format("Invalid null value for %s '%s' from [%s]", propertyTypeDescription, property.getName(), expression));
}
try {
return property.decode(sqlObjectValue);
} catch (Exception e) {
throw new TrinoException(errorCode, format("Unable to set %s '%s' to [%s]: %s", propertyTypeDescription, property.getName(), expression, e.getMessage()), e);
}
}
use of io.trino.spi.TrinoException in project trino by trinodb.
the class TableProceduresRegistry method resolve.
public TableProcedureMetadata resolve(CatalogName catalogName, String name) {
Map<String, TableProcedureMetadata> procedures = tableProcedures.get(catalogName);
if (procedures == null) {
throw new TrinoException(GENERIC_INTERNAL_ERROR, format("Catalog %s not registered", catalogName));
}
TableProcedureMetadata procedure = procedures.get(name);
if (procedure == null) {
throw new TrinoException(PROCEDURE_NOT_FOUND, format("Procedure %s not registered for catalog %s", name, catalogName));
}
return procedure;
}
use of io.trino.spi.TrinoException in project trino by trinodb.
the class ParametricAggregation method findMatchingImplementation.
private AggregationImplementation findMatchingImplementation(BoundSignature boundSignature) {
Signature signature = boundSignature.toSignature();
Optional<AggregationImplementation> foundImplementation = Optional.empty();
if (implementations.getExactImplementations().containsKey(signature)) {
foundImplementation = Optional.of(implementations.getExactImplementations().get(signature));
} else {
for (AggregationImplementation candidate : implementations.getGenericImplementations()) {
if (candidate.areTypesAssignable(boundSignature)) {
if (foundImplementation.isPresent()) {
throw new TrinoException(AMBIGUOUS_FUNCTION_CALL, format("Ambiguous function call (%s) for %s", boundSignature, getFunctionMetadata().getSignature()));
}
foundImplementation = Optional.of(candidate);
}
}
}
if (foundImplementation.isEmpty()) {
throw new TrinoException(FUNCTION_IMPLEMENTATION_MISSING, format("Unsupported type parameters (%s) for %s", boundSignature, getFunctionMetadata().getSignature()));
}
return foundImplementation.get();
}
Aggregations