Search in sources :

Example 1 with CreateView

use of io.trino.sql.tree.CreateView in project trino by trinodb.

the class TestCreateViewTask method executeCreateView.

private ListenableFuture<Void> executeCreateView(QualifiedName viewName, boolean replace) {
    Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("mock_table")));
    CreateView statement = new CreateView(viewName, query, replace, Optional.empty(), Optional.empty());
    return new CreateViewTask(metadata, new AllowAllAccessControl(), parser, analyzerFactory).execute(statement, queryStateMachine, ImmutableList.of(), WarningCollector.NOOP);
}
Also used : Query(io.trino.sql.tree.Query) QueryUtil.simpleQuery(io.trino.sql.QueryUtil.simpleQuery) AllowAllAccessControl(io.trino.security.AllowAllAccessControl) AllColumns(io.trino.sql.tree.AllColumns) CreateView(io.trino.sql.tree.CreateView)

Example 2 with CreateView

use of io.trino.sql.tree.CreateView in project trino by trinodb.

the class CreateViewTask method execute.

@Override
public ListenableFuture<Void> execute(CreateView statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName());
    accessControl.checkCanCreateView(session.toSecurityContext(), name);
    if (metadata.isMaterializedView(session, name)) {
        throw semanticException(TABLE_ALREADY_EXISTS, statement, "Materialized view already exists: '%s'", name);
    }
    if (metadata.isView(session, name)) {
        if (!statement.isReplace()) {
            throw semanticException(TABLE_ALREADY_EXISTS, statement, "View already exists: '%s'", name);
        }
    } else if (metadata.getTableHandle(session, name).isPresent()) {
        throw semanticException(TABLE_ALREADY_EXISTS, statement, "Table already exists: '%s'", name);
    }
    String sql = getFormattedSql(statement.getQuery(), sqlParser);
    Analysis analysis = analyzerFactory.createAnalyzer(session, parameters, parameterExtractor(statement, parameters), stateMachine.getWarningCollector()).analyze(statement);
    List<ViewColumn> columns = analysis.getOutputDescriptor(statement.getQuery()).getVisibleFields().stream().map(field -> new ViewColumn(field.getName().get(), field.getType().getTypeId())).collect(toImmutableList());
    // use DEFINER security by default
    Optional<Identity> owner = Optional.of(session.getIdentity());
    if (statement.getSecurity().orElse(null) == INVOKER) {
        owner = Optional.empty();
    }
    ViewDefinition definition = new ViewDefinition(sql, session.getCatalog(), session.getSchema(), columns, statement.getComment(), owner);
    metadata.createView(session, name, definition, statement.isReplace());
    stateMachine.setOutput(analysis.getTarget());
    stateMachine.setReferencedTables(analysis.getReferencedTables());
    return immediateVoidFuture();
}
Also used : CreateView(io.trino.sql.tree.CreateView) ViewColumn(io.trino.metadata.ViewColumn) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) INVOKER(io.trino.sql.tree.CreateView.Security.INVOKER) AnalyzerFactory(io.trino.sql.analyzer.AnalyzerFactory) ParameterUtils.parameterExtractor(io.trino.sql.ParameterUtils.parameterExtractor) Inject(javax.inject.Inject) TABLE_ALREADY_EXISTS(io.trino.spi.StandardErrorCode.TABLE_ALREADY_EXISTS) MetadataUtil.createQualifiedObjectName(io.trino.metadata.MetadataUtil.createQualifiedObjectName) Identity(io.trino.spi.security.Identity) Objects.requireNonNull(java.util.Objects.requireNonNull) SqlParser(io.trino.sql.parser.SqlParser) SemanticExceptions.semanticException(io.trino.sql.analyzer.SemanticExceptions.semanticException) Futures.immediateVoidFuture(com.google.common.util.concurrent.Futures.immediateVoidFuture) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ViewDefinition(io.trino.metadata.ViewDefinition) List(java.util.List) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) AccessControl(io.trino.security.AccessControl) SqlFormatterUtil.getFormattedSql(io.trino.sql.SqlFormatterUtil.getFormattedSql) WarningCollector(io.trino.execution.warnings.WarningCollector) Metadata(io.trino.metadata.Metadata) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) Session(io.trino.Session) Analysis(io.trino.sql.analyzer.Analysis) Analysis(io.trino.sql.analyzer.Analysis) ViewColumn(io.trino.metadata.ViewColumn) ViewDefinition(io.trino.metadata.ViewDefinition) Identity(io.trino.spi.security.Identity) MetadataUtil.createQualifiedObjectName(io.trino.metadata.MetadataUtil.createQualifiedObjectName) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Session(io.trino.Session)

Example 3 with CreateView

use of io.trino.sql.tree.CreateView in project trino by trinodb.

the class TestSqlParser method testCreateView.

@Test
public void testCreateView() {
    Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t")));
    assertStatement("CREATE VIEW a AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), query, false, Optional.empty(), Optional.empty()));
    assertStatement("CREATE OR REPLACE VIEW a AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), query, true, Optional.empty(), Optional.empty()));
    assertStatement("CREATE VIEW a SECURITY DEFINER AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), query, false, Optional.empty(), Optional.of(CreateView.Security.DEFINER)));
    assertStatement("CREATE VIEW a SECURITY INVOKER AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), query, false, Optional.empty(), Optional.of(CreateView.Security.INVOKER)));
    assertStatement("CREATE VIEW a COMMENT 'comment' SECURITY DEFINER AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), query, false, Optional.of("comment"), Optional.of(CreateView.Security.DEFINER)));
    assertStatement("CREATE VIEW a COMMENT '' SECURITY INVOKER AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), query, false, Optional.of(""), Optional.of(CreateView.Security.INVOKER)));
    assertStatement("CREATE VIEW a COMMENT 'comment' AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), query, false, Optional.of("comment"), Optional.empty()));
    assertStatement("CREATE VIEW a COMMENT '' AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), query, false, Optional.of(""), Optional.empty()));
    assertStatement("CREATE VIEW bar.foo AS SELECT * FROM t", new CreateView(QualifiedName.of("bar", "foo"), query, false, Optional.empty(), Optional.empty()));
    assertStatement("CREATE VIEW \"awesome view\" AS SELECT * FROM t", new CreateView(QualifiedName.of("awesome view"), query, false, Optional.empty(), Optional.empty()));
    assertStatement("CREATE VIEW \"awesome schema\".\"awesome view\" AS SELECT * FROM t", new CreateView(QualifiedName.of("awesome schema", "awesome view"), query, false, Optional.empty(), Optional.empty()));
}
Also used : QueryUtil.simpleQuery(io.trino.sql.QueryUtil.simpleQuery) Query(io.trino.sql.tree.Query) WithQuery(io.trino.sql.tree.WithQuery) AllColumns(io.trino.sql.tree.AllColumns) CreateView(io.trino.sql.tree.CreateView) Test(org.junit.jupiter.api.Test)

Aggregations

CreateView (io.trino.sql.tree.CreateView)3 QueryUtil.simpleQuery (io.trino.sql.QueryUtil.simpleQuery)2 AllColumns (io.trino.sql.tree.AllColumns)2 Query (io.trino.sql.tree.Query)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 Futures.immediateVoidFuture (com.google.common.util.concurrent.Futures.immediateVoidFuture)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 Session (io.trino.Session)1 WarningCollector (io.trino.execution.warnings.WarningCollector)1 Metadata (io.trino.metadata.Metadata)1 MetadataUtil.createQualifiedObjectName (io.trino.metadata.MetadataUtil.createQualifiedObjectName)1 QualifiedObjectName (io.trino.metadata.QualifiedObjectName)1 ViewColumn (io.trino.metadata.ViewColumn)1 ViewDefinition (io.trino.metadata.ViewDefinition)1 AccessControl (io.trino.security.AccessControl)1 AllowAllAccessControl (io.trino.security.AllowAllAccessControl)1 TABLE_ALREADY_EXISTS (io.trino.spi.StandardErrorCode.TABLE_ALREADY_EXISTS)1 Identity (io.trino.spi.security.Identity)1 ParameterUtils.parameterExtractor (io.trino.sql.ParameterUtils.parameterExtractor)1 SqlFormatterUtil.getFormattedSql (io.trino.sql.SqlFormatterUtil.getFormattedSql)1