Search in sources :

Example 1 with ViewColumn

use of com.facebook.presto.metadata.ViewDefinition.ViewColumn in project presto by prestodb.

the class MetadataManager method listTableColumns.

@Override
public Map<QualifiedObjectName, List<ColumnMetadata>> listTableColumns(Session session, QualifiedTablePrefix prefix) {
    requireNonNull(prefix, "prefix is null");
    Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, prefix.getCatalogName());
    Map<QualifiedObjectName, List<ColumnMetadata>> tableColumns = new HashMap<>();
    if (catalog.isPresent()) {
        CatalogMetadata catalogMetadata = catalog.get();
        SchemaTablePrefix tablePrefix = prefix.asSchemaTablePrefix();
        for (ConnectorId connectorId : catalogMetadata.listConnectorIds()) {
            ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId);
            ConnectorSession connectorSession = session.toConnectorSession(connectorId);
            for (Entry<SchemaTableName, List<ColumnMetadata>> entry : metadata.listTableColumns(connectorSession, tablePrefix).entrySet()) {
                QualifiedObjectName tableName = new QualifiedObjectName(prefix.getCatalogName(), entry.getKey().getSchemaName(), entry.getKey().getTableName());
                tableColumns.put(tableName, entry.getValue());
            }
            // if table and view names overlap, the view wins
            for (Entry<SchemaTableName, ConnectorViewDefinition> entry : metadata.getViews(connectorSession, tablePrefix).entrySet()) {
                QualifiedObjectName tableName = new QualifiedObjectName(prefix.getCatalogName(), entry.getKey().getSchemaName(), entry.getKey().getTableName());
                ImmutableList.Builder<ColumnMetadata> columns = ImmutableList.builder();
                for (ViewColumn column : deserializeView(entry.getValue().getViewData()).getColumns()) {
                    columns.add(new ColumnMetadata(column.getName(), column.getType()));
                }
                tableColumns.put(tableName, columns.build());
            }
        }
    }
    return ImmutableMap.copyOf(tableColumns);
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ViewColumn(com.facebook.presto.metadata.ViewDefinition.ViewColumn) SchemaTablePrefix(com.facebook.presto.spi.SchemaTablePrefix) SchemaTableName(com.facebook.presto.spi.SchemaTableName) QualifiedObjectName.convertFromSchemaTableName(com.facebook.presto.metadata.QualifiedObjectName.convertFromSchemaTableName) ConnectorViewDefinition(com.facebook.presto.spi.ConnectorViewDefinition) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ConnectorSession(com.facebook.presto.spi.ConnectorSession) ConnectorMetadata(com.facebook.presto.spi.connector.ConnectorMetadata) ConnectorId(com.facebook.presto.connector.ConnectorId)

Example 2 with ViewColumn

use of com.facebook.presto.metadata.ViewDefinition.ViewColumn 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 3 with ViewColumn

use of com.facebook.presto.metadata.ViewDefinition.ViewColumn in project presto by prestodb.

the class TestAnalyzer method setup.

@BeforeMethod(alwaysRun = true)
public void setup() throws Exception {
    TypeManager typeManager = new TypeRegistry();
    CatalogManager catalogManager = new CatalogManager();
    transactionManager = createTestTransactionManager(catalogManager);
    accessControl = new AccessControlManager(transactionManager);
    metadata = new MetadataManager(new FeaturesConfig(), typeManager, new BlockEncodingManager(typeManager), new SessionPropertyManager(), new SchemaPropertyManager(), new TablePropertyManager(), transactionManager);
    metadata.getFunctionRegistry().addFunctions(ImmutableList.of(APPLY_FUNCTION));
    catalogManager.registerCatalog(createTestingCatalog(TPCH_CATALOG, TPCH_CONNECTOR_ID));
    catalogManager.registerCatalog(createTestingCatalog(SECOND_CATALOG, SECOND_CONNECTOR_ID));
    catalogManager.registerCatalog(createTestingCatalog(THIRD_CATALOG, THIRD_CONNECTOR_ID));
    SchemaTableName table1 = new SchemaTableName("s1", "t1");
    inSetupTransaction(session -> metadata.createTable(session, TPCH_CATALOG, new ConnectorTableMetadata(table1, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", BIGINT), new ColumnMetadata("c", BIGINT), new ColumnMetadata("d", BIGINT)))));
    SchemaTableName table2 = new SchemaTableName("s1", "t2");
    inSetupTransaction(session -> metadata.createTable(session, TPCH_CATALOG, new ConnectorTableMetadata(table2, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", BIGINT)))));
    SchemaTableName table3 = new SchemaTableName("s1", "t3");
    inSetupTransaction(session -> metadata.createTable(session, TPCH_CATALOG, new ConnectorTableMetadata(table3, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", BIGINT), new ColumnMetadata("x", BIGINT, null, true)))));
    // table in different catalog
    SchemaTableName table4 = new SchemaTableName("s2", "t4");
    inSetupTransaction(session -> metadata.createTable(session, SECOND_CATALOG, new ConnectorTableMetadata(table4, ImmutableList.of(new ColumnMetadata("a", BIGINT)))));
    // table with a hidden column
    SchemaTableName table5 = new SchemaTableName("s1", "t5");
    inSetupTransaction(session -> metadata.createTable(session, TPCH_CATALOG, new ConnectorTableMetadata(table5, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", BIGINT, null, true)))));
    // table with a varchar column
    SchemaTableName table6 = new SchemaTableName("s1", "t6");
    inSetupTransaction(session -> metadata.createTable(session, TPCH_CATALOG, new ConnectorTableMetadata(table6, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", VARCHAR), new ColumnMetadata("c", BIGINT), new ColumnMetadata("d", BIGINT)))));
    // table with bigint, double, array of bigints and array of doubles column
    SchemaTableName table7 = new SchemaTableName("s1", "t7");
    inSetupTransaction(session -> metadata.createTable(session, TPCH_CATALOG, new ConnectorTableMetadata(table7, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", DOUBLE), new ColumnMetadata("c", new ArrayType(BIGINT)), new ColumnMetadata("d", new ArrayType(DOUBLE))))));
    // valid view referencing table in same schema
    String viewData1 = JsonCodec.jsonCodec(ViewDefinition.class).toJson(new ViewDefinition("select a from t1", Optional.of(TPCH_CATALOG), Optional.of("s1"), ImmutableList.of(new ViewColumn("a", BIGINT)), Optional.of("user")));
    inSetupTransaction(session -> metadata.createView(session, new QualifiedObjectName(TPCH_CATALOG, "s1", "v1"), viewData1, false));
    // stale view (different column type)
    String viewData2 = JsonCodec.jsonCodec(ViewDefinition.class).toJson(new ViewDefinition("select a from t1", Optional.of(TPCH_CATALOG), Optional.of("s1"), ImmutableList.of(new ViewColumn("a", VARCHAR)), Optional.of("user")));
    inSetupTransaction(session -> metadata.createView(session, new QualifiedObjectName(TPCH_CATALOG, "s1", "v2"), viewData2, false));
    // view referencing table in different schema from itself and session
    String viewData3 = JsonCodec.jsonCodec(ViewDefinition.class).toJson(new ViewDefinition("select a from t4", Optional.of(SECOND_CATALOG), Optional.of("s2"), ImmutableList.of(new ViewColumn("a", BIGINT)), Optional.of("owner")));
    inSetupTransaction(session -> metadata.createView(session, new QualifiedObjectName(THIRD_CATALOG, "s3", "v3"), viewData3, false));
    // valid view with uppercase column name
    String viewData4 = JsonCodec.jsonCodec(ViewDefinition.class).toJson(new ViewDefinition("select A from t1", Optional.of("tpch"), Optional.of("s1"), ImmutableList.of(new ViewColumn("a", BIGINT)), Optional.of("user")));
    inSetupTransaction(session -> metadata.createView(session, new QualifiedObjectName("tpch", "s1", "v4"), viewData4, false));
    // recursive view referencing to itself
    String viewData5 = JsonCodec.jsonCodec(ViewDefinition.class).toJson(new ViewDefinition("select * from v5", Optional.of(TPCH_CATALOG), Optional.of("s1"), ImmutableList.of(new ViewColumn("a", BIGINT)), Optional.of("user")));
    inSetupTransaction(session -> metadata.createView(session, new QualifiedObjectName(TPCH_CATALOG, "s1", "v5"), viewData5, false));
    this.metadata = metadata;
}
Also used : AccessControlManager(com.facebook.presto.security.AccessControlManager) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ViewColumn(com.facebook.presto.metadata.ViewDefinition.ViewColumn) ViewDefinition(com.facebook.presto.metadata.ViewDefinition) TypeRegistry(com.facebook.presto.type.TypeRegistry) SchemaTableName(com.facebook.presto.spi.SchemaTableName) CatalogManager(com.facebook.presto.metadata.CatalogManager) QualifiedObjectName(com.facebook.presto.metadata.QualifiedObjectName) ArrayType(com.facebook.presto.type.ArrayType) MetadataManager(com.facebook.presto.metadata.MetadataManager) BlockEncodingManager(com.facebook.presto.block.BlockEncodingManager) SessionPropertyManager(com.facebook.presto.metadata.SessionPropertyManager) TypeManager(com.facebook.presto.spi.type.TypeManager) TablePropertyManager(com.facebook.presto.metadata.TablePropertyManager) SchemaPropertyManager(com.facebook.presto.metadata.SchemaPropertyManager) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

ViewColumn (com.facebook.presto.metadata.ViewDefinition.ViewColumn)3 QualifiedObjectName (com.facebook.presto.metadata.QualifiedObjectName)2 ViewDefinition (com.facebook.presto.metadata.ViewDefinition)2 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)2 SchemaTableName (com.facebook.presto.spi.SchemaTableName)2 ImmutableCollectors.toImmutableList (com.facebook.presto.util.ImmutableCollectors.toImmutableList)2 List (java.util.List)2 Session (com.facebook.presto.Session)1 BlockEncodingManager (com.facebook.presto.block.BlockEncodingManager)1 ConnectorId (com.facebook.presto.connector.ConnectorId)1 CatalogManager (com.facebook.presto.metadata.CatalogManager)1 Metadata (com.facebook.presto.metadata.Metadata)1 MetadataManager (com.facebook.presto.metadata.MetadataManager)1 MetadataUtil.createQualifiedObjectName (com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName)1 QualifiedObjectName.convertFromSchemaTableName (com.facebook.presto.metadata.QualifiedObjectName.convertFromSchemaTableName)1 SchemaPropertyManager (com.facebook.presto.metadata.SchemaPropertyManager)1 SessionPropertyManager (com.facebook.presto.metadata.SessionPropertyManager)1 TablePropertyManager (com.facebook.presto.metadata.TablePropertyManager)1 AccessControl (com.facebook.presto.security.AccessControl)1 AccessControlManager (com.facebook.presto.security.AccessControlManager)1