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());
}
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);
}
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);
}
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();
}
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();
}
Aggregations