Search in sources :

Example 41 with SemanticException

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

the class DropViewTask method execute.

@Override
public ListenableFuture<?> execute(DropView statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName());
    Optional<ViewDefinition> view = metadata.getView(session, name);
    if (!view.isPresent()) {
        if (!statement.isExists()) {
            throw new SemanticException(MISSING_TABLE, statement, "View '%s' does not exist", name);
        }
        return immediateFuture(null);
    }
    accessControl.checkCanDropView(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), name);
    metadata.dropView(session, name);
    return immediateFuture(null);
}
Also used : ViewDefinition(com.facebook.presto.metadata.ViewDefinition) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 42 with SemanticException

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

the class DropTableTask method execute.

@Override
public ListenableFuture<?> execute(DropTable statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getTableName());
    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
    if (!tableHandle.isPresent()) {
        if (!statement.isExists()) {
            throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
        }
        return immediateFuture(null);
    }
    Optional<ConnectorMaterializedViewDefinition> optionalMaterializedView = metadata.getMaterializedView(session, tableName);
    if (optionalMaterializedView.isPresent()) {
        if (!statement.isExists()) {
            throw new SemanticException(NOT_SUPPORTED, statement, "'%s' is a materialized view, not a table. Use DROP MATERIALIZED VIEW to drop.", tableName);
        }
        return immediateFuture(null);
    }
    accessControl.checkCanDropTable(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), tableName);
    metadata.dropTable(session, tableHandle.get());
    return immediateFuture(null);
}
Also used : ConnectorMaterializedViewDefinition(com.facebook.presto.spi.ConnectorMaterializedViewDefinition) TableHandle(com.facebook.presto.spi.TableHandle) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 43 with SemanticException

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

the class ExpressionInterpreter method evaluateConstantExpression.

public static Object evaluateConstantExpression(Expression expression, Type expectedType, Metadata metadata, Session session, List<Expression> parameters) {
    ExpressionAnalyzer analyzer = createConstantAnalyzer(metadata, session, parameters, WarningCollector.NOOP);
    analyzer.analyze(expression, Scope.create());
    Type actualType = analyzer.getExpressionTypes().get(NodeRef.of(expression));
    if (!metadata.getFunctionAndTypeManager().canCoerce(actualType, expectedType)) {
        throw new SemanticException(SemanticErrorCode.TYPE_MISMATCH, expression, format("Cannot cast type %s to %s", actualType.getTypeSignature(), expectedType.getTypeSignature()));
    }
    Map<NodeRef<Expression>, Type> coercions = ImmutableMap.<NodeRef<Expression>, Type>builder().putAll(analyzer.getExpressionCoercions()).put(NodeRef.of(expression), expectedType).build();
    return evaluateConstantExpression(expression, coercions, analyzer.getTypeOnlyCoercions(), metadata, session, ImmutableSet.of(), parameters);
}
Also used : NodeRef(com.facebook.presto.sql.tree.NodeRef) FunctionImplementationType(com.facebook.presto.spi.function.FunctionImplementationType) TypeUtils.isEnumType(com.facebook.presto.common.type.TypeUtils.isEnumType) ArrayType(com.facebook.presto.common.type.ArrayType) OperatorType(com.facebook.presto.common.function.OperatorType) FunctionType(com.facebook.presto.common.type.FunctionType) RowType(com.facebook.presto.common.type.RowType) LiteralEncoder.isSupportedLiteralType(com.facebook.presto.sql.planner.LiteralEncoder.isSupportedLiteralType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) Type(com.facebook.presto.common.type.Type) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) CanonicalizeExpressionRewriter.canonicalizeExpression(com.facebook.presto.sql.planner.iterative.rule.CanonicalizeExpressionRewriter.canonicalizeExpression) IfExpression(com.facebook.presto.sql.tree.IfExpression) BindExpression(com.facebook.presto.sql.tree.BindExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) SqlFunctionUtils.getSqlFunctionExpression(com.facebook.presto.sql.relational.SqlFunctionUtils.getSqlFunctionExpression) ArithmeticUnaryExpression(com.facebook.presto.sql.tree.ArithmeticUnaryExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) ExpressionAnalyzer(com.facebook.presto.sql.analyzer.ExpressionAnalyzer) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Example 44 with SemanticException

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

the class TestStartTransactionTask method testStartTransactionTooManyAccessModes.

@Test
public void testStartTransactionTooManyAccessModes() {
    Session session = sessionBuilder().setClientTransactionSupport().build();
    TransactionManager transactionManager = createTestTransactionManager();
    QueryStateMachine stateMachine = createQueryStateMachine("START TRANSACTION", session, true, transactionManager, executor, metadata);
    assertFalse(stateMachine.getSession().getTransactionId().isPresent());
    try {
        getFutureValue(new StartTransactionTask().execute(new StartTransaction(ImmutableList.of(new TransactionAccessMode(true), new TransactionAccessMode(true))), transactionManager, metadata, new AllowAllAccessControl(), stateMachine, emptyList()));
        fail();
    } catch (SemanticException e) {
        assertEquals(e.getCode(), INVALID_TRANSACTION_MODE);
    }
    assertTrue(transactionManager.getAllTransactionInfos().isEmpty());
    assertFalse(stateMachine.getQueryInfo(Optional.empty()).isClearTransactionId());
    assertFalse(stateMachine.getQueryInfo(Optional.empty()).getStartedTransactionId().isPresent());
}
Also used : TaskTestUtils.createQueryStateMachine(com.facebook.presto.execution.TaskTestUtils.createQueryStateMachine) TransactionManager(com.facebook.presto.transaction.TransactionManager) InMemoryTransactionManager.createTestTransactionManager(com.facebook.presto.transaction.InMemoryTransactionManager.createTestTransactionManager) InMemoryTransactionManager(com.facebook.presto.transaction.InMemoryTransactionManager) TransactionAccessMode(com.facebook.presto.sql.tree.TransactionAccessMode) AllowAllAccessControl(com.facebook.presto.security.AllowAllAccessControl) StartTransaction(com.facebook.presto.sql.tree.StartTransaction) Session(com.facebook.presto.Session) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) Test(org.testng.annotations.Test)

Example 45 with SemanticException

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

the class AbstractTestQueries method testExecuteUsingSubqueryFails.

@Test
public void testExecuteUsingSubqueryFails() {
    try {
        String query = "SELECT ?";
        Session session = Session.builder(getSession()).addPreparedStatement("my_query", query).build();
        computeActual(session, "EXECUTE my_query USING (SELECT 1 from nation)");
        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 table 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