Search in sources :

Example 1 with CreateView

use of com.facebook.presto.sql.tree.CreateView in project presto by prestodb.

the class CreateViewTask method execute.

@Override
public ListenableFuture<?> execute(CreateView statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters) {
    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);
    List<ViewColumn> columns = analysis.getOutputDescriptor(statement.getQuery()).getVisibleFields().stream().map(field -> new ViewColumn(field.getName().get(), field.getType())).collect(toImmutableList());
    String data = codec.toJson(new ViewDefinition(sql, session.getCatalog(), session.getSchema(), columns, Optional.of(session.getUser())));
    metadata.createView(session, name, data, statement.isReplace());
    return immediateFuture(null);
}
Also used : QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) Analyzer(com.facebook.presto.sql.analyzer.Analyzer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ViewDefinition(com.facebook.presto.metadata.ViewDefinition) Session(com.facebook.presto.Session) SqlParser(com.facebook.presto.sql.parser.SqlParser) FeaturesConfig(com.facebook.presto.sql.analyzer.FeaturesConfig) Inject(javax.inject.Inject) SqlFormatterUtil.getFormattedSql(com.facebook.presto.sql.SqlFormatterUtil.getFormattedSql) List(java.util.List) Expression(com.facebook.presto.sql.tree.Expression) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) ViewColumn(com.facebook.presto.metadata.ViewDefinition.ViewColumn) Objects.requireNonNull(java.util.Objects.requireNonNull) Analysis(com.facebook.presto.sql.analyzer.Analysis) Optional(java.util.Optional) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) TransactionManager(com.facebook.presto.transaction.TransactionManager) Metadata(com.facebook.presto.metadata.Metadata) AccessControl(com.facebook.presto.security.AccessControl) CreateView(com.facebook.presto.sql.tree.CreateView) Statement(com.facebook.presto.sql.tree.Statement) JsonCodec(io.airlift.json.JsonCodec) Analysis(com.facebook.presto.sql.analyzer.Analysis) ViewColumn(com.facebook.presto.metadata.ViewDefinition.ViewColumn) ViewDefinition(com.facebook.presto.metadata.ViewDefinition) QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) Session(com.facebook.presto.Session)

Example 2 with CreateView

use of com.facebook.presto.sql.tree.CreateView in project presto by prestodb.

the class TestSqlParser method testCreateView.

@Test
public void testCreateView() throws Exception {
    assertStatement("CREATE VIEW a AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), false));
    assertStatement("CREATE OR REPLACE VIEW a AS SELECT * FROM t", new CreateView(QualifiedName.of("a"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t"))), true));
}
Also used : AllColumns(com.facebook.presto.sql.tree.AllColumns) CreateView(com.facebook.presto.sql.tree.CreateView) Test(org.testng.annotations.Test)

Aggregations

CreateView (com.facebook.presto.sql.tree.CreateView)2 Session (com.facebook.presto.Session)1 Metadata (com.facebook.presto.metadata.Metadata)1 MetadataUtil.createQualifiedObjectName (com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName)1 QualifiedObjectName (com.facebook.presto.metadata.QualifiedObjectName)1 ViewDefinition (com.facebook.presto.metadata.ViewDefinition)1 ViewColumn (com.facebook.presto.metadata.ViewDefinition.ViewColumn)1 AccessControl (com.facebook.presto.security.AccessControl)1 SqlFormatterUtil.getFormattedSql (com.facebook.presto.sql.SqlFormatterUtil.getFormattedSql)1 Analysis (com.facebook.presto.sql.analyzer.Analysis)1 Analyzer (com.facebook.presto.sql.analyzer.Analyzer)1 FeaturesConfig (com.facebook.presto.sql.analyzer.FeaturesConfig)1 SqlParser (com.facebook.presto.sql.parser.SqlParser)1 AllColumns (com.facebook.presto.sql.tree.AllColumns)1 Expression (com.facebook.presto.sql.tree.Expression)1 Statement (com.facebook.presto.sql.tree.Statement)1 TransactionManager (com.facebook.presto.transaction.TransactionManager)1 ImmutableCollectors.toImmutableList (com.facebook.presto.util.ImmutableCollectors.toImmutableList)1 Futures.immediateFuture (com.google.common.util.concurrent.Futures.immediateFuture)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1