Search in sources :

Example 6 with SemanticException

use of com.facebook.presto.sql.analyzer.SemanticException in project presto by prestodb.

the class DropSchemaTask method execute.

@Override
public ListenableFuture<?> execute(DropSchema statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters) {
    if (statement.isCascade()) {
        throw new PrestoException(NOT_SUPPORTED, "CASCADE is not yet supported for DROP SCHEMA");
    }
    Session session = stateMachine.getSession();
    CatalogSchemaName schema = createCatalogSchemaName(session, statement, Optional.of(statement.getSchemaName()));
    if (!metadata.schemaExists(session, schema)) {
        if (!statement.isExists()) {
            throw new SemanticException(MISSING_SCHEMA, statement, "Schema '%s' does not exist", schema);
        }
        return immediateFuture(null);
    }
    accessControl.checkCanDropSchema(session.getRequiredTransactionId(), session.getIdentity(), schema);
    metadata.dropSchema(session, schema);
    return immediateFuture(null);
}
Also used : CatalogSchemaName(com.facebook.presto.spi.CatalogSchemaName) MetadataUtil.createCatalogSchemaName(com.facebook.presto.metadata.MetadataUtil.createCatalogSchemaName) PrestoException(com.facebook.presto.spi.PrestoException) Session(com.facebook.presto.Session) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 7 with SemanticException

use of com.facebook.presto.sql.analyzer.SemanticException in project presto by prestodb.

the class RenameSchemaTask method execute.

@Override
public ListenableFuture<?> execute(RenameSchema statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters) {
    Session session = stateMachine.getSession();
    CatalogSchemaName source = createCatalogSchemaName(session, statement, Optional.of(statement.getSource()));
    CatalogSchemaName target = new CatalogSchemaName(source.getCatalogName(), statement.getTarget());
    if (!metadata.schemaExists(session, source)) {
        throw new SemanticException(MISSING_SCHEMA, statement, "Schema '%s' does not exist", source);
    }
    if (metadata.schemaExists(session, target)) {
        throw new SemanticException(SCHEMA_ALREADY_EXISTS, statement, "Target schema '%s' already exists", target);
    }
    accessControl.checkCanRenameSchema(session.getRequiredTransactionId(), session.getIdentity(), source, statement.getTarget());
    metadata.renameSchema(session, source, statement.getTarget());
    return immediateFuture(null);
}
Also used : CatalogSchemaName(com.facebook.presto.spi.CatalogSchemaName) MetadataUtil.createCatalogSchemaName(com.facebook.presto.metadata.MetadataUtil.createCatalogSchemaName) Session(com.facebook.presto.Session) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 8 with SemanticException

use of com.facebook.presto.sql.analyzer.SemanticException 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) Expression(com.facebook.presto.sql.tree.Expression) ExpressionInterpreter.evaluateConstantExpression(com.facebook.presto.sql.planner.ExpressionInterpreter.evaluateConstantExpression) PropertyMetadata(com.facebook.presto.spi.session.PropertyMetadata) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 9 with SemanticException

use of com.facebook.presto.sql.analyzer.SemanticException in project presto by prestodb.

the class AbstractTestQueries method testExecuteWithParametersInGroupBy.

@Test
public void testExecuteWithParametersInGroupBy() {
    try {
        String query = "SELECT a + ?, count(1) FROM (VALUES 1, 2, 3, 2) t(a) GROUP BY a + ?";
        Session session = Session.builder(getSession()).addPreparedStatement("my_query", query).build();
        computeActual(session, "EXECUTE my_query USING 1, 1");
        fail("parameters in GROUP BY and SELECT should fail");
    } catch (SemanticException e) {
        assertEquals(e.getCode(), MUST_BE_AGGREGATE_OR_GROUP_BY);
    } catch (RuntimeException e) {
        assertEquals(e.getMessage(), "line 1:10: '(a + ?)' must be an aggregate expression or appear in GROUP BY clause");
    }
}
Also used : Session(com.facebook.presto.Session) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) Test(org.testng.annotations.Test)

Example 10 with SemanticException

use of com.facebook.presto.sql.analyzer.SemanticException in project presto by prestodb.

the class AbstractTestQueries method testExecuteUsingColumnReferenceFails.

@Test
public void testExecuteUsingColumnReferenceFails() {
    try {
        String query = "SELECT ? from nation";
        Session session = Session.builder(getSession()).addPreparedStatement("my_query", query).build();
        computeActual(session, "EXECUTE my_query USING \"nationkey\"");
        fail("nonLiteral parameters should fail");
    } catch (SemanticException e) {
        assertEquals(e.getCode(), EXPRESSION_NOT_CONSTANT);
    } catch (RuntimeException e) {
        assertEquals(e.getMessage(), "line 1:24: Constant expression cannot contain column references");
    }
}
Also used : Session(com.facebook.presto.Session) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) Test(org.testng.annotations.Test)

Aggregations

SemanticException (com.facebook.presto.sql.analyzer.SemanticException)45 Session (com.facebook.presto.Session)29 MetadataUtil.createQualifiedObjectName (com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName)21 PrestoException (com.facebook.presto.spi.PrestoException)14 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)12 QualifiedObjectName (com.facebook.presto.metadata.QualifiedObjectName)9 TableHandle (com.facebook.presto.spi.TableHandle)9 Test (org.testng.annotations.Test)9 Expression (com.facebook.presto.sql.tree.Expression)8 TableHandle (com.facebook.presto.metadata.TableHandle)7 ConnectorId (com.facebook.presto.spi.ConnectorId)7 ConnectorMaterializedViewDefinition (com.facebook.presto.spi.ConnectorMaterializedViewDefinition)7 MetadataUtil.createCatalogSchemaName (com.facebook.presto.metadata.MetadataUtil.createCatalogSchemaName)6 TransactionManager (com.facebook.presto.transaction.TransactionManager)6 ColumnHandle (com.facebook.presto.spi.ColumnHandle)5 Type (com.facebook.presto.common.type.Type)4 Metadata (com.facebook.presto.metadata.Metadata)4 AccessControl (com.facebook.presto.security.AccessControl)4 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)4 Privilege (com.facebook.presto.spi.security.Privilege)4