Search in sources :

Example 1 with CreateView

use of io.prestosql.sql.tree.CreateView in project hetu-core by openlookeng.

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()));
    assertStatement("CREATE OR REPLACE VIEW a AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), query, true, Optional.empty()));
    assertStatement("CREATE VIEW a SECURITY DEFINER AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), query, false, Optional.of(CreateView.Security.DEFINER)));
    assertStatement("CREATE VIEW a SECURITY INVOKER AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), query, false, Optional.of(CreateView.Security.INVOKER)));
    assertStatement("CREATE VIEW bar.foo AS SELECT * FROM t", new CreateView(QualifiedName.of("bar", "foo"), query, false, Optional.empty()));
    assertStatement("CREATE VIEW \"awesome view\" AS SELECT * FROM t", new CreateView(QualifiedName.of("awesome view"), query, false, 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()));
}
Also used : QueryUtil.simpleQuery(io.prestosql.sql.QueryUtil.simpleQuery) Query(io.prestosql.sql.tree.Query) WithQuery(io.prestosql.sql.tree.WithQuery) AllColumns(io.prestosql.sql.tree.AllColumns) CreateView(io.prestosql.sql.tree.CreateView) Test(org.testng.annotations.Test)

Example 2 with CreateView

use of io.prestosql.sql.tree.CreateView in project hetu-core by openlookeng.

the class CreateViewTask method execute.

@Override
public ListenableFuture<?> execute(CreateView statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters, HeuristicIndexerManager heuristicIndexerManager) {
    Session session = stateMachine.getSession();
    QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName());
    accessControl.checkCanCreateView(session.getRequiredTransactionId(), session.getIdentity(), name);
    String sql = getFormattedSql(statement.getQuery(), sqlParser, Optional.of(parameters));
    Analysis analysis = analyzeStatement(statement, session, metadata, accessControl, parameters, stateMachine.getWarningCollector());
    List<ViewColumn> columns = analysis.getOutputDescriptor(statement.getQuery()).getVisibleFields().stream().map(field -> new ViewColumn(field.getName().get(), field.getType().getTypeSignature())).collect(toImmutableList());
    // use DEFINER security by default
    Optional<String> owner = Optional.of(session.getUser());
    if (statement.getSecurity().orElse(null) == INVOKER) {
        owner = Optional.empty();
    }
    ConnectorViewDefinition definition = new ConnectorViewDefinition(sql, session.getCatalog(), session.getSchema(), columns, owner, !owner.isPresent());
    metadata.createView(session, name, definition, statement.isReplace());
    return immediateFuture(null);
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ConnectorViewDefinition(io.prestosql.spi.connector.ConnectorViewDefinition) TransactionManager(io.prestosql.transaction.TransactionManager) SqlParser(io.prestosql.sql.parser.SqlParser) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) Inject(javax.inject.Inject) INVOKER(io.prestosql.sql.tree.CreateView.Security.INVOKER) Statement(io.prestosql.sql.tree.Statement) WarningCollector(io.prestosql.execution.warnings.WarningCollector) ViewColumn(io.prestosql.spi.connector.ConnectorViewDefinition.ViewColumn) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) HeuristicIndexerManager(io.prestosql.heuristicindex.HeuristicIndexerManager) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) AccessControl(io.prestosql.security.AccessControl) SqlFormatterUtil.getFormattedSql(io.prestosql.sql.SqlFormatterUtil.getFormattedSql) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Metadata(io.prestosql.metadata.Metadata) Analyzer(io.prestosql.sql.analyzer.Analyzer) CubeManager(io.prestosql.cube.CubeManager) List(java.util.List) CreateView(io.prestosql.sql.tree.CreateView) FeaturesConfig(io.prestosql.sql.analyzer.FeaturesConfig) Optional(java.util.Optional) Analysis(io.prestosql.sql.analyzer.Analysis) MetadataUtil.createQualifiedObjectName(io.prestosql.metadata.MetadataUtil.createQualifiedObjectName) Expression(io.prestosql.sql.tree.Expression) Analysis(io.prestosql.sql.analyzer.Analysis) ViewColumn(io.prestosql.spi.connector.ConnectorViewDefinition.ViewColumn) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(io.prestosql.metadata.MetadataUtil.createQualifiedObjectName) Session(io.prestosql.Session) ConnectorViewDefinition(io.prestosql.spi.connector.ConnectorViewDefinition)

Aggregations

CreateView (io.prestosql.sql.tree.CreateView)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 Futures.immediateFuture (com.google.common.util.concurrent.Futures.immediateFuture)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 Session (io.prestosql.Session)1 CubeManager (io.prestosql.cube.CubeManager)1 WarningCollector (io.prestosql.execution.warnings.WarningCollector)1 HeuristicIndexerManager (io.prestosql.heuristicindex.HeuristicIndexerManager)1 Metadata (io.prestosql.metadata.Metadata)1 MetadataUtil.createQualifiedObjectName (io.prestosql.metadata.MetadataUtil.createQualifiedObjectName)1 AccessControl (io.prestosql.security.AccessControl)1 ConnectorViewDefinition (io.prestosql.spi.connector.ConnectorViewDefinition)1 ViewColumn (io.prestosql.spi.connector.ConnectorViewDefinition.ViewColumn)1 QualifiedObjectName (io.prestosql.spi.connector.QualifiedObjectName)1 QueryUtil.simpleQuery (io.prestosql.sql.QueryUtil.simpleQuery)1 SqlFormatterUtil.getFormattedSql (io.prestosql.sql.SqlFormatterUtil.getFormattedSql)1 Analysis (io.prestosql.sql.analyzer.Analysis)1 Analyzer (io.prestosql.sql.analyzer.Analyzer)1 FeaturesConfig (io.prestosql.sql.analyzer.FeaturesConfig)1 SqlParser (io.prestosql.sql.parser.SqlParser)1