Search in sources :

Example 1 with ConnectorViewDefinition

use of com.facebook.presto.spi.ConnectorViewDefinition in project presto by prestodb.

the class TestRaptorMetadata method testViews.

@Test
public void testViews() {
    SchemaTableName test1 = new SchemaTableName("test", "test_view1");
    SchemaTableName test2 = new SchemaTableName("test", "test_view2");
    // create views
    metadata.createView(SESSION, test1, "test1", false);
    metadata.createView(SESSION, test2, "test2", false);
    // verify listing
    List<SchemaTableName> list = metadata.listViews(SESSION, "test");
    assertEqualsIgnoreOrder(list, ImmutableList.of(test1, test2));
    // verify getting data
    Map<SchemaTableName, ConnectorViewDefinition> views = metadata.getViews(SESSION, new SchemaTablePrefix("test"));
    assertEquals(views.keySet(), ImmutableSet.of(test1, test2));
    assertEquals(views.get(test1).getViewData(), "test1");
    assertEquals(views.get(test2).getViewData(), "test2");
    // drop first view
    metadata.dropView(SESSION, test1);
    views = metadata.getViews(SESSION, new SchemaTablePrefix("test"));
    assertEquals(views.keySet(), ImmutableSet.of(test2));
    // drop second view
    metadata.dropView(SESSION, test2);
    views = metadata.getViews(SESSION, new SchemaTablePrefix("test"));
    assertTrue(views.isEmpty());
    // verify listing everything
    views = metadata.getViews(SESSION, new SchemaTablePrefix());
    assertTrue(views.isEmpty());
}
Also used : SchemaTablePrefix(com.facebook.presto.spi.SchemaTablePrefix) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ConnectorViewDefinition(com.facebook.presto.spi.ConnectorViewDefinition) Test(org.testng.annotations.Test)

Example 2 with ConnectorViewDefinition

use of com.facebook.presto.spi.ConnectorViewDefinition in project presto by prestodb.

the class MetadataManager method getViews.

@Override
public Map<QualifiedObjectName, ViewDefinition> getViews(Session session, QualifiedTablePrefix prefix) {
    requireNonNull(prefix, "prefix is null");
    Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, prefix.getCatalogName());
    Map<QualifiedObjectName, ViewDefinition> views = new LinkedHashMap<>();
    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, ConnectorViewDefinition> entry : metadata.getViews(connectorSession, tablePrefix).entrySet()) {
                QualifiedObjectName viewName = new QualifiedObjectName(prefix.getCatalogName(), entry.getKey().getSchemaName(), entry.getKey().getTableName());
                views.put(viewName, deserializeView(entry.getValue().getViewData()));
            }
        }
    }
    return ImmutableMap.copyOf(views);
}
Also used : ConnectorViewDefinition(com.facebook.presto.spi.ConnectorViewDefinition) SchemaTablePrefix(com.facebook.presto.spi.SchemaTablePrefix) SchemaTableName(com.facebook.presto.spi.SchemaTableName) QualifiedObjectName.convertFromSchemaTableName(com.facebook.presto.metadata.QualifiedObjectName.convertFromSchemaTableName) LinkedHashMap(java.util.LinkedHashMap) ConnectorViewDefinition(com.facebook.presto.spi.ConnectorViewDefinition) ConnectorSession(com.facebook.presto.spi.ConnectorSession) ConnectorMetadata(com.facebook.presto.spi.connector.ConnectorMetadata) ConnectorId(com.facebook.presto.connector.ConnectorId)

Example 3 with ConnectorViewDefinition

use of com.facebook.presto.spi.ConnectorViewDefinition 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 4 with ConnectorViewDefinition

use of com.facebook.presto.spi.ConnectorViewDefinition in project presto by prestodb.

the class MetadataManager method getView.

@Override
public Optional<ViewDefinition> getView(Session session, QualifiedObjectName viewName) {
    Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, viewName.getCatalogName());
    if (catalog.isPresent()) {
        CatalogMetadata catalogMetadata = catalog.get();
        ConnectorId connectorId = catalogMetadata.getConnectorId(viewName);
        ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId);
        Map<SchemaTableName, ConnectorViewDefinition> views = metadata.getViews(session.toConnectorSession(connectorId), viewName.asSchemaTableName().toSchemaTablePrefix());
        ConnectorViewDefinition view = views.get(viewName.asSchemaTableName());
        if (view != null) {
            return Optional.of(deserializeView(view.getViewData()));
        }
    }
    return Optional.empty();
}
Also used : ConnectorMetadata(com.facebook.presto.spi.connector.ConnectorMetadata) SchemaTableName(com.facebook.presto.spi.SchemaTableName) QualifiedObjectName.convertFromSchemaTableName(com.facebook.presto.metadata.QualifiedObjectName.convertFromSchemaTableName) ConnectorId(com.facebook.presto.connector.ConnectorId) ConnectorViewDefinition(com.facebook.presto.spi.ConnectorViewDefinition)

Example 5 with ConnectorViewDefinition

use of com.facebook.presto.spi.ConnectorViewDefinition in project presto by prestodb.

the class HiveMetadata method getViews.

@Override
public Map<SchemaTableName, ConnectorViewDefinition> getViews(ConnectorSession session, SchemaTablePrefix prefix) {
    ImmutableMap.Builder<SchemaTableName, ConnectorViewDefinition> views = ImmutableMap.builder();
    List<SchemaTableName> tableNames;
    if (prefix.getTableName() != null) {
        tableNames = ImmutableList.of(new SchemaTableName(prefix.getSchemaName(), prefix.getTableName()));
    } else {
        tableNames = listViews(session, prefix.getSchemaName());
    }
    for (SchemaTableName schemaTableName : tableNames) {
        Optional<Table> table = metastore.getTable(schemaTableName.getSchemaName(), schemaTableName.getTableName());
        if (table.isPresent() && HiveUtil.isPrestoView(table.get())) {
            views.put(schemaTableName, new ConnectorViewDefinition(schemaTableName, Optional.ofNullable(table.get().getOwner()), decodeViewData(table.get().getViewOriginalText().get())));
        }
    }
    return views.build();
}
Also used : Table(com.facebook.presto.hive.metastore.Table) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ImmutableMap(com.google.common.collect.ImmutableMap) ConnectorViewDefinition(com.facebook.presto.spi.ConnectorViewDefinition)

Aggregations

ConnectorViewDefinition (com.facebook.presto.spi.ConnectorViewDefinition)6 SchemaTableName (com.facebook.presto.spi.SchemaTableName)6 ConnectorMetadata (com.facebook.presto.spi.connector.ConnectorMetadata)4 ConnectorId (com.facebook.presto.connector.ConnectorId)3 QualifiedObjectName.convertFromSchemaTableName (com.facebook.presto.metadata.QualifiedObjectName.convertFromSchemaTableName)3 SchemaTablePrefix (com.facebook.presto.spi.SchemaTablePrefix)3 ConnectorSession (com.facebook.presto.spi.ConnectorSession)2 LinkedHashMap (java.util.LinkedHashMap)2 Table (com.facebook.presto.hive.metastore.Table)1 ViewColumn (com.facebook.presto.metadata.ViewDefinition.ViewColumn)1 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)1 ImmutableCollectors.toImmutableList (com.facebook.presto.util.ImmutableCollectors.toImmutableList)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Test (org.testng.annotations.Test)1