Search in sources :

Example 1 with PropertyMetadata

use of com.facebook.presto.spi.session.PropertyMetadata in project presto by prestodb.

the class AbstractPropertyManager method getProperties.

public final Map<String, Object> getProperties(ConnectorId connectorId, // only use this for error messages
String catalog, Map<String, Expression> sqlPropertyValues, Session session, Metadata metadata, List<Expression> parameters) {
    Map<String, PropertyMetadata<?>> supportedProperties = connectorProperties.get(connectorId);
    if (supportedProperties == null) {
        throw new PrestoException(NOT_FOUND, "Catalog not found: " + catalog);
    }
    ImmutableMap.Builder<String, Object> properties = ImmutableMap.builder();
    // Fill in user-specified properties
    for (Map.Entry<String, Expression> sqlProperty : sqlPropertyValues.entrySet()) {
        String propertyName = sqlProperty.getKey().toLowerCase(ENGLISH);
        PropertyMetadata<?> property = supportedProperties.get(propertyName);
        if (property == null) {
            throw new PrestoException(propertyError, format("Catalog '%s' does not support %s property '%s'", catalog, propertyType, propertyName));
        }
        Object sqlObjectValue;
        try {
            sqlObjectValue = evaluatePropertyValue(sqlProperty.getValue(), property.getSqlType(), session, metadata, parameters);
        } catch (SemanticException e) {
            throw new PrestoException(propertyError, format("Invalid value for %s property '%s': Cannot convert '%s' to %s", propertyType, property.getName(), sqlProperty.getValue(), property.getSqlType()), e);
        }
        Object value;
        try {
            value = property.decode(sqlObjectValue);
        } catch (Exception e) {
            throw new PrestoException(propertyError, format("Unable to set %s property '%s' to '%s': %s", propertyType, property.getName(), sqlProperty.getValue(), e.getMessage()), e);
        }
        properties.put(property.getName(), value);
    }
    Map<String, Object> userSpecifiedProperties = properties.build();
    // Fill in the remaining properties with non-null defaults
    for (PropertyMetadata<?> propertyMetadata : supportedProperties.values()) {
        if (!userSpecifiedProperties.containsKey(propertyMetadata.getName())) {
            Object value = propertyMetadata.getDefaultValue();
            if (value != null) {
                properties.put(propertyMetadata.getName(), value);
            }
        }
    }
    return properties.build();
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) ImmutableMap(com.google.common.collect.ImmutableMap) PrestoException(com.facebook.presto.spi.PrestoException) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) ExpressionInterpreter.evaluateConstantExpression(com.facebook.presto.sql.planner.ExpressionInterpreter.evaluateConstantExpression) Expression(com.facebook.presto.sql.tree.Expression) PropertyMetadata(com.facebook.presto.spi.session.PropertyMetadata) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 2 with PropertyMetadata

use of com.facebook.presto.spi.session.PropertyMetadata in project presto by prestodb.

the class AbstractPropertiesSystemTable method cursor.

@Override
public final RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint) {
    TransactionId transactionId = ((GlobalSystemTransactionHandle) transactionHandle).getTransactionId();
    InMemoryRecordSet.Builder table = InMemoryRecordSet.builder(tableMetadata);
    Map<ConnectorId, Map<String, PropertyMetadata<?>>> connectorProperties = propertySupplier.get();
    for (Entry<String, ConnectorId> entry : new TreeMap<>(transactionManager.getCatalogNames(transactionId)).entrySet()) {
        String catalog = entry.getKey();
        Map<String, PropertyMetadata<?>> properties = new TreeMap<>(connectorProperties.getOrDefault(entry.getValue(), ImmutableMap.of()));
        for (PropertyMetadata<?> propertyMetadata : properties.values()) {
            table.addRow(catalog, propertyMetadata.getName(), firstNonNull(propertyMetadata.getDefaultValue(), "").toString(), propertyMetadata.getSqlType().toString(), propertyMetadata.getDescription());
        }
    }
    return table.build().cursor();
}
Also used : TreeMap(java.util.TreeMap) InMemoryRecordSet(com.facebook.presto.spi.InMemoryRecordSet) TransactionId(com.facebook.presto.transaction.TransactionId) PropertyMetadata(com.facebook.presto.spi.session.PropertyMetadata) ImmutableMap(com.google.common.collect.ImmutableMap) TreeMap(java.util.TreeMap) Map(java.util.Map) ConnectorId(com.facebook.presto.connector.ConnectorId)

Aggregations

PropertyMetadata (com.facebook.presto.spi.session.PropertyMetadata)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Map (java.util.Map)2 ConnectorId (com.facebook.presto.connector.ConnectorId)1 InMemoryRecordSet (com.facebook.presto.spi.InMemoryRecordSet)1 PrestoException (com.facebook.presto.spi.PrestoException)1 SemanticException (com.facebook.presto.sql.analyzer.SemanticException)1 ExpressionInterpreter.evaluateConstantExpression (com.facebook.presto.sql.planner.ExpressionInterpreter.evaluateConstantExpression)1 Expression (com.facebook.presto.sql.tree.Expression)1 TransactionId (com.facebook.presto.transaction.TransactionId)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1